-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_dev_test.go
110 lines (92 loc) · 3.45 KB
/
store_dev_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package leanauthz_test
import (
"context"
"testing"
authz "github.com/lazharichir/leanauthz"
)
func TestDevStoreImpl_SaveStatements(t *testing.T) {
store := authz.NewDevStore()
stmts := []authz.Statement{
authz.NewAllowStatement(authz.NewPattern("user123"), authz.NewPattern("SaveDocuments"), "documents/*"),
authz.NewDenyStatement(authz.NewPattern("user456"), authz.NewPattern("DeleteDocuments"), "documents/doc_123"),
}
err := store.SaveStatements(context.Background(), stmts)
if err != nil {
t.Errorf("SaveStatements() error = %v, want nil", err)
}
// Test that statements are saved
got, err := store.GetStatementsByPrincipal(context.Background(), authz.NewPattern("user123"))
if err != nil {
t.Errorf("GetStatementsByPrincipal() error = %v, want nil", err)
}
if len(got) != 1 {
t.Errorf("GetStatementsByPrincipal() len = %v, want 1", len(got))
}
}
func TestDevStoreImpl_GetStatementsByPrincipal(t *testing.T) {
store := authz.NewDevStore()
stmts := []authz.Statement{
authz.NewAllowStatement(authz.NewPattern("user123"), authz.NewPattern("SaveDocuments"), "documents/*"),
authz.NewDenyStatement(authz.NewPattern("user456"), authz.NewPattern("DeleteDocuments"), "documents/doc_123"),
}
err := store.SaveStatements(context.Background(), stmts)
if err != nil {
t.Errorf("SaveStatements() error = %v, want nil", err)
}
// Test getting statements by principal
got, err := store.GetStatementsByPrincipal(context.Background(), authz.NewPattern("user123"))
if err != nil {
t.Errorf("GetStatementsByPrincipal() error = %v, want nil", err)
}
if len(got) != 1 {
t.Errorf("GetStatementsByPrincipal() len = %v, want 1", len(got))
}
}
func TestDevStoreImpl_FindCandidates(t *testing.T) {
store := authz.NewDevStore()
stmts := []authz.Statement{
authz.NewAllowStatement(authz.NewPattern("users/123"), authz.NewPattern("SaveDocuments"), "documents/*"),
authz.NewDenyStatement(authz.NewPattern("users/456"), authz.NewPattern("DeleteDocuments"), "documents/doc_123"),
}
err := store.SaveStatements(context.Background(), stmts)
if err != nil {
t.Errorf("SaveStatements() error = %v, want nil", err)
}
// Test finding candidates
e := authz.Evaluation{
Principal: authz.NewPrincipal("users/123"),
Action: authz.Action("SaveDocuments"),
Resource: authz.NewResource("documents/doc_123"),
}
got, err := store.FindCandidates(context.Background(), e)
if err != nil {
t.Errorf("FindCandidates() error = %v, want nil", err)
}
if len(got) != 1 {
t.Errorf("FindCandidates() len = %v, want 1", len(got))
}
}
func TestDevStoreImpl_DeleteStatements(t *testing.T) {
store := authz.NewDevStore()
stmts := []authz.Statement{
authz.NewAllowStatement(authz.NewPattern("user123"), authz.NewPattern("SaveDocuments"), "documents/*"),
authz.NewDenyStatement(authz.NewPattern("user456"), authz.NewPattern("DeleteDocuments"), "documents/doc_123"),
}
err := store.SaveStatements(context.Background(), stmts)
if err != nil {
t.Errorf("SaveStatements() error = %v, want nil", err)
}
// Test deleting statements
err = store.DeleteStatements(context.Background(), stmts)
if err != nil {
t.Errorf("DeleteStatements() error = %v, want nil", err)
}
// Test that statements are deleted
got, err := store.GetStatementsByPrincipal(context.Background(), authz.NewPattern("user123"))
if err != nil {
t.Errorf("GetStatementsByPrincipal() error = %v, want nil", err)
}
if len(got) != 0 {
t.Errorf("GetStatementsByPrincipal() len = %v, want 0", len(got))
}
}