@@ -99,6 +99,22 @@ func Test_NamespaceProjectOrganizationMutator_Handle(t *testing.T) {
9999 orgPatch : "some-org" ,
100100 },
101101
102+ {
103+ name : "Namespace: user with default org, no org set on object but other labels exist" ,
104+
105+ object : newNamespace ("project" , map [string ]string {"foo" : "bar" }, nil ),
106+ additionalObjects : func (* testing.T ) []client.Object {
107+ return []client.Object {
108+ newUser ("user" , "some-org" ),
109+ newGroup ("some-org" , "user" ),
110+ }
111+ },
112+
113+ user : "user" ,
114+ allowed : true ,
115+ orgPatch : "some-org" ,
116+ },
117+
102118 {
103119 name : "Project: request with org label set, user not in org" ,
104120
@@ -396,17 +412,23 @@ func Test_NamespaceProjectOrganizationMutator_Handle(t *testing.T) {
396412 require .Equal (t , tc .allowed , resp .Allowed )
397413
398414 if tc .orgPatch != "" {
399- requireOrgPatch (t , tc .orgPatch , resp .Patches )
415+ requireOrgPatch (t , tc .object , tc . orgPatch , resp .Patches )
400416 } else {
401417 require .Empty (t , resp .Patches )
402418 }
403419 })
404420 }
405421}
406422
407- func requireOrgPatch (t * testing.T , org string , ps []jsonpatch.Operation ) {
423+ func requireOrgPatch (t * testing.T , origObject client.Object , org string , ps []jsonpatch.Operation ) {
424+ t .Helper ()
425+
408426 require .Len (t , ps , 1 )
409- require .Equal (t , jsonpatch .NewOperation ("add" , "/metadata/labels/example.com~1organization" , org ), ps [0 ])
427+ if origObject .GetLabels () != nil {
428+ require .Equal (t , jsonpatch .NewOperation ("add" , "/metadata/labels/example.com~1organization" , org ), ps [0 ])
429+ return
430+ }
431+ require .Equal (t , jsonpatch .NewOperation ("add" , "/metadata/labels" , map [string ]string {"example.com/organization" : org }), ps [0 ])
410432}
411433
412434func newGroup (name string , users ... string ) * userv1.Group {
0 commit comments