Skip to content

Commit 9abb4f9

Browse files
Implement Request.Equal() method
Implement Request.Equal(), include a basic check that it's working correctly, and check that cmp.Diff can consume two requests and compare them. Signed-off-by: Greg NISBET <gregory.nisbet@gmail.com>
1 parent 8955696 commit 9abb4f9

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

types/authorize.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ type Request struct {
1111
Context Record `json:"context"`
1212
}
1313

14+
// Equal comapres two requests for sameness
15+
func (r Request) Equal(other Request) bool {
16+
return (r.Principal == other.Principal &&
17+
r.Action == other.Action &&
18+
r.Resource == other.Resource &&
19+
r.Context.Equal(other.Context))
20+
}
21+
1422
// A Decision is the result of the authorization.
1523
type Decision bool
1624

types/authorize_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/cedar-policy/cedar-go/internal/testutil"
8+
"github.com/google/go-cmp/cmp"
89
)
910

1011
func TestJSONDecision(t *testing.T) {
@@ -39,6 +40,60 @@ func TestJSONDecision(t *testing.T) {
3940
})
4041
}
4142

43+
func TestRequestEqual(t *testing.T) {
44+
t.Parallel()
45+
46+
testcases := []Request{
47+
{},
48+
{
49+
Principal: EntityUID{
50+
Type: "a",
51+
ID: "b",
52+
},
53+
},
54+
{
55+
Action: EntityUID{
56+
Type: "a",
57+
ID: "b",
58+
},
59+
},
60+
{
61+
Resource: EntityUID{
62+
Type: "a",
63+
ID: "b",
64+
},
65+
},
66+
{
67+
Context: NewRecord(RecordMap{
68+
"a": Long(42),
69+
}),
70+
},
71+
}
72+
73+
for i, r := range testcases {
74+
for j, s := range testcases[:i] {
75+
expected := i == j
76+
actual := r.Equal(s)
77+
if actual != s.Equal(r) {
78+
t.Errorf("Request.Equal not symmetric for examples #%d and #%d, actual=%v, expected=%v", i, j, actual, expected)
79+
}
80+
if actual != expected {
81+
t.Errorf("Request.Equal returned unexpected result for #%d and #%d, actual=%v, expected=%v", i, j, actual, expected)
82+
}
83+
}
84+
}
85+
}
86+
87+
func TestRequestEqualSupportsCmpDiff(t *testing.T) {
88+
t.Parallel()
89+
90+
// This test is not very interesting, we just want to make
91+
// sure that it compiles.
92+
if diff := cmp.Diff(Record{}, Record{}); diff != "" {
93+
t.Errorf("unexpected diff (-want +got): %s", diff)
94+
}
95+
}
96+
4297
func TestError(t *testing.T) {
4398
t.Parallel()
4499
e := DiagnosticError{PolicyID: "policy42", Message: "bad error"}

0 commit comments

Comments
 (0)