-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpolicy_test.go
58 lines (48 loc) · 1.21 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
package ast_test
import (
"testing"
"github.com/cedar-policy/cedar-go/ast"
"github.com/cedar-policy/cedar-go/internal/testutil"
"github.com/cedar-policy/cedar-go/types"
internalast "github.com/cedar-policy/cedar-go/x/exp/ast"
)
func TestPolicy_MarshalJSON(t *testing.T) {
t.Parallel()
p := ast.Permit().PrincipalEq(types.NewEntityUID("Foo::Bar", "Baz"))
expected := `{
"effect": "permit",
"principal": {
"op": "==",
"entity": {
"type": "Foo::Bar",
"id": "Baz"
}
},
"action": {
"op": "All"
},
"resource": {
"op": "All"
}
}`
testutil.JSONMarshalsTo(t, p, expected)
var unmarshaled ast.Policy
err := unmarshaled.UnmarshalJSON([]byte(expected))
testutil.OK(t, err)
testutil.Equals(t, &unmarshaled, p)
}
func TestPolicy_MarshalCedar(t *testing.T) {
t.Parallel()
p := ast.Permit().PrincipalEq(types.NewEntityUID("Foo::Bar", "Baz"))
expected := `permit (
principal == Foo::Bar::"Baz",
action,
resource
);`
testutil.Equals(t, string(p.MarshalCedar()), expected)
var unmarshaled ast.Policy
err := unmarshaled.UnmarshalCedar([]byte(expected))
p.Position = internalast.Position{Offset: 0, Line: 1, Column: 1}
testutil.OK(t, err)
testutil.Equals(t, &unmarshaled, p)
}