@@ -52,6 +52,48 @@ func TestDeriveClaims_CoreAndForeign(t *testing.T) {
5252 }
5353}
5454
55+ func TestMergeClaims (t * testing.T ) {
56+ gr := func (g , r string ) apisv1alpha2.GroupResource {
57+ return apisv1alpha2.GroupResource {Group : g , Resource : r }
58+ }
59+ claim := func (g , r string , verbs ... string ) apisv1alpha2.PermissionClaim {
60+ return apisv1alpha2.PermissionClaim {GroupResource : gr (g , r ), Verbs : verbs }
61+ }
62+
63+ writable := []apisv1alpha2.PermissionClaim {
64+ claim ("b.example" , "widgets" , "get" , "create" ),
65+ claim ("access.opendefense.cloud" , "scopes" , "get" , "create" ),
66+ }
67+ external := []apisv1alpha2.PermissionClaim {
68+ claim ("access.opendefense.cloud" , "scopes" , "get" ), // overlaps writable
69+ claim ("" , "configmaps" , "get" ),
70+ }
71+
72+ got := mergeClaims (writable , external )
73+
74+ wantGRs := []apisv1alpha2.GroupResource {
75+ gr ("" , "configmaps" ),
76+ gr ("access.opendefense.cloud" , "scopes" ),
77+ gr ("b.example" , "widgets" ),
78+ }
79+ gotGRs := make ([]apisv1alpha2.GroupResource , len (got ))
80+ for i , c := range got {
81+ gotGRs [i ] = c .GroupResource
82+ }
83+ if ! reflect .DeepEqual (gotGRs , wantGRs ) {
84+ t .Fatalf ("order = %+v, want %+v" , gotGRs , wantGRs )
85+ }
86+
87+ // Overlapping GR keeps the writable (superset) verbs, not the read-only ones.
88+ for _ , c := range got {
89+ if c .Group == "access.opendefense.cloud" && c .Resource == "scopes" {
90+ if ! reflect .DeepEqual (c .Verbs , []string {"get" , "create" }) {
91+ t .Fatalf ("scopes verbs = %v, want writable superset [get create]" , c .Verbs )
92+ }
93+ }
94+ }
95+ }
96+
5597func TestValidateClaims (t * testing.T ) {
5698 tests := []struct {
5799 name string
0 commit comments