-
Notifications
You must be signed in to change notification settings - Fork 10
/
policy_set_test.go
158 lines (129 loc) · 4.11 KB
/
policy_set_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cedar_test
import (
"fmt"
"testing"
"github.com/cedar-policy/cedar-go"
"github.com/cedar-policy/cedar-go/ast"
"github.com/cedar-policy/cedar-go/internal/testutil"
)
func TestNewPolicySetFromFile(t *testing.T) {
t.Parallel()
t.Run("err-in-tokenize", func(t *testing.T) {
t.Parallel()
_, err := cedar.NewPolicySetFromBytes("policy.cedar", []byte(`"`))
testutil.Error(t, err)
})
t.Run("err-in-parse", func(t *testing.T) {
t.Parallel()
_, err := cedar.NewPolicySetFromBytes("policy.cedar", []byte(`err`))
testutil.Error(t, err)
})
t.Run("annotations", func(t *testing.T) {
t.Parallel()
ps, err := cedar.NewPolicySetFromBytes("policy.cedar", []byte(`@key("value") permit (principal, action, resource);`))
testutil.OK(t, err)
testutil.Equals(t, ps.Get("policy0").Annotations(), cedar.Annotations{"key": "value"})
})
}
func TestUpsertPolicy(t *testing.T) {
t.Parallel()
t.Run("insert", func(t *testing.T) {
t.Parallel()
policy0 := cedar.NewPolicyFromAST(ast.Forbid())
var policy1 cedar.Policy
testutil.OK(t, policy1.UnmarshalJSON(
[]byte(`{"effect":"permit","principal":{"op":"All"},"action":{"op":"All"},"resource":{"op":"All"}}`),
))
ps := cedar.NewPolicySet()
added := ps.Add("policy0", policy0)
testutil.Equals(t, added, true)
added = ps.Add("policy1", &policy1)
testutil.Equals(t, added, true)
testutil.Equals(t, ps.Get("policy0"), policy0)
testutil.Equals(t, ps.Get("policy1"), &policy1)
testutil.Equals(t, ps.Get("policy2"), nil)
})
t.Run("upsert", func(t *testing.T) {
t.Parallel()
ps := cedar.NewPolicySet()
p1 := cedar.NewPolicyFromAST(ast.Forbid())
ps.Add("a wavering policy", p1)
p2 := cedar.NewPolicyFromAST(ast.Permit())
added := ps.Add("a wavering policy", p2)
testutil.Equals(t, added, false)
testutil.Equals(t, ps.Get("a wavering policy"), p2)
})
}
func TestDeletePolicy(t *testing.T) {
t.Parallel()
t.Run("delete non-existent", func(t *testing.T) {
t.Parallel()
ps := cedar.NewPolicySet()
existed := ps.Remove("not a policy")
testutil.Equals(t, existed, false)
})
t.Run("delete existing", func(t *testing.T) {
t.Parallel()
ps := cedar.NewPolicySet()
p1 := cedar.NewPolicyFromAST(ast.Forbid())
ps.Add("a policy", p1)
existed := ps.Remove("a policy")
testutil.Equals(t, existed, true)
testutil.Equals(t, ps.Get("a policy"), nil)
})
}
func TestNewPolicySetFromSlice(t *testing.T) {
t.Parallel()
policiesStr := `permit (
principal,
action == Action::"editPhoto",
resource
)
when { resource.owner == principal };
forbid (
principal in Groups::"bannedUsers",
action,
resource
);`
policies, err := cedar.NewPolicyListFromBytes("", []byte(policiesStr))
testutil.OK(t, err)
ps := cedar.NewPolicySet()
for i, p := range policies {
p.SetFilename("example.cedar")
ps.Add(cedar.PolicyID(fmt.Sprintf("policy%d", i)), p)
}
testutil.Equals(t, ps.Get("policy0").Effect(), cedar.Permit)
testutil.Equals(t, ps.Get("policy1").Effect(), cedar.Forbid)
testutil.Equals(t, string(ps.MarshalCedar()), policiesStr)
}
func TestPolicyMap(t *testing.T) {
t.Parallel()
ps, err := cedar.NewPolicySetFromBytes("", []byte(`permit (principal, action, resource);`))
testutil.OK(t, err)
m := ps.Map()
testutil.Equals(t, len(m), 1)
}
func TestPolicySetJSON(t *testing.T) {
t.Parallel()
t.Run("UnmarshalError", func(t *testing.T) {
t.Parallel()
var ps cedar.PolicySet
err := ps.UnmarshalJSON([]byte(`!@#$`))
testutil.Error(t, err)
})
t.Run("UnmarshalOK", func(t *testing.T) {
t.Parallel()
var ps cedar.PolicySet
err := ps.UnmarshalJSON([]byte(`{"staticPolicies":{"policy0":{"effect":"permit","principal":{"op":"All"},"action":{"op":"All"},"resource":{"op":"All"}}}}`))
testutil.OK(t, err)
testutil.Equals(t, len(ps.Map()), 1)
})
t.Run("MarshalOK", func(t *testing.T) {
t.Parallel()
ps, err := cedar.NewPolicySetFromBytes("", []byte(`permit (principal, action, resource);`))
testutil.OK(t, err)
out, err := ps.MarshalJSON()
testutil.OK(t, err)
testutil.Equals(t, string(out), `{"staticPolicies":{"policy0":{"effect":"permit","principal":{"op":"All"},"action":{"op":"All"},"resource":{"op":"All"}}}}`)
})
}