-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpolicy_test.go
66 lines (58 loc) · 1.18 KB
/
policy_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
package cedar
import (
"bytes"
"encoding/json"
"testing"
"github.com/cedar-policy/cedar-go/internal/testutil"
)
func prettifyJson(in []byte) []byte {
var buf bytes.Buffer
_ = json.Indent(&buf, in, "", " ")
return buf.Bytes()
}
func TestPolicyJSON(t *testing.T) {
t.Parallel()
// Taken from https://docs.cedarpolicy.com/policies/json-format.html
jsonEncodedPolicy := prettifyJson([]byte(`
{
"effect": "permit",
"principal": {
"op": "==",
"entity": { "type": "User", "id": "12UA45" }
},
"action": {
"op": "==",
"entity": { "type": "Action", "id": "view" }
},
"resource": {
"op": "in",
"entity": { "type": "Folder", "id": "abc" }
},
"conditions": [
{
"kind": "when",
"body": {
"==": {
"left": {
".": {
"left": {
"Var": "context"
},
"attr": "tls_version"
}
},
"right": {
"Value": "1.3"
}
}
}
}
]
}`,
))
var policy Policy
testutil.OK(t, policy.UnmarshalJSON(jsonEncodedPolicy))
output, err := policy.MarshalJSON()
testutil.OK(t, err)
testutil.Equals(t, string(prettifyJson(output)), string(jsonEncodedPolicy))
}