@@ -262,40 +262,13 @@ func TestRemoveUsersFromOrg(t *testing.T) {
262262 const orgID = "org-1"
263263 const userID = "user-1"
264264
265- t .Run ("delegates org removal to membership force-remove" , func (t * testing.T ) {
266- orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc := newMocks (t )
267-
268- projSvc .EXPECT ().List (mock .Anything , project.Filter {OrgID : orgID }).Return ([]project.Project {}, nil )
269- polSvc .EXPECT ().List (mock .Anything , policy.Filter {PrincipalID : userID , PrincipalType : schema .UserPrincipal }).
270- Return ([]policy.Policy {
271- {ID : "org-p1" , ResourceType : schema .OrganizationNamespace , ResourceID : orgID },
272- {ID : "proj-p1" , ResourceType : schema .ProjectNamespace , ResourceID : "proj-1" },
273- }, nil )
274- // org/project policies are left to the membership cascade
275- mbrSvc .EXPECT ().ForceRemoveOrganizationMember (mock .Anything , orgID , userID , schema .UserPrincipal ).Return (nil )
276-
277- svc := deleter .NewCascadeDeleter (orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc )
278- err := svc .RemoveUsersFromOrg (context .Background (), orgID , []string {userID })
279- assert .NoError (t , err )
280- })
281-
282- t .Run ("cleans custom-resource policies owned by org projects" , func (t * testing.T ) {
265+ t .Run ("cleans resource access via resource service then delegates org removal to membership" , func (t * testing.T ) {
283266 orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc := newMocks (t )
284267
285268 projSvc .EXPECT ().List (mock .Anything , project.Filter {OrgID : orgID }).Return ([]project.Project {{ID : "proj-1" }}, nil )
286- polSvc .EXPECT ().List (mock .Anything , policy.Filter {PrincipalID : userID , PrincipalType : schema .UserPrincipal }).
287- Return ([]policy.Policy {
288- {ID : "res-p1" , ResourceType : "compute/instance" , ResourceID : "res-1" },
289- {ID : "res-p2" , ResourceType : "compute/instance" , ResourceID : "res-2" },
290- {ID : "res-p3" , ResourceType : "compute/instance" , ResourceID : "res-3" },
291- }, nil )
292- // res-1 belongs to an org project: policy deleted
293- resSvc .EXPECT ().Get (mock .Anything , "res-1" ).Return (resource.Resource {ID : "res-1" , ProjectID : "proj-1" }, nil )
294- polSvc .EXPECT ().Delete (mock .Anything , "res-p1" ).Return (nil )
295- // res-2 belongs to a project outside the org: untouched
296- resSvc .EXPECT ().Get (mock .Anything , "res-2" ).Return (resource.Resource {ID : "res-2" , ProjectID : "other-proj" }, nil )
297- // res-3 no longer exists: skipped
298- resSvc .EXPECT ().Get (mock .Anything , "res-3" ).Return (resource.Resource {}, resource .ErrNotExist )
269+ // custom-resource policy cleanup is owned by the resource service
270+ resSvc .EXPECT ().RemovePrincipalAccess (mock .Anything , userID , schema .UserPrincipal , []string {"proj-1" }).Return (nil )
271+ // org/project/group policies and relations are left to the membership cascade
299272 mbrSvc .EXPECT ().ForceRemoveOrganizationMember (mock .Anything , orgID , userID , schema .UserPrincipal ).Return (nil )
300273
301274 svc := deleter .NewCascadeDeleter (orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc )
@@ -307,8 +280,7 @@ func TestRemoveUsersFromOrg(t *testing.T) {
307280 orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc := newMocks (t )
308281
309282 projSvc .EXPECT ().List (mock .Anything , project.Filter {OrgID : orgID }).Return ([]project.Project {}, nil )
310- polSvc .EXPECT ().List (mock .Anything , policy.Filter {PrincipalID : userID , PrincipalType : schema .UserPrincipal }).
311- Return ([]policy.Policy {}, nil )
283+ resSvc .EXPECT ().RemovePrincipalAccess (mock .Anything , userID , schema .UserPrincipal , []string {}).Return (nil )
312284 mbrSvc .EXPECT ().ForceRemoveOrganizationMember (mock .Anything , orgID , userID , schema .UserPrincipal ).
313285 Return (errors .New ("cascade boom" ))
314286
@@ -317,17 +289,17 @@ func TestRemoveUsersFromOrg(t *testing.T) {
317289 assert .ErrorContains (t , err , "cascade boom" )
318290 })
319291
320- t .Run ("surfaces policy-list error and skips membership removal for that user" , func (t * testing.T ) {
292+ t .Run ("surfaces resource cleanup error and skips membership removal for that user" , func (t * testing.T ) {
321293 orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc := newMocks (t )
322294
323295 projSvc .EXPECT ().List (mock .Anything , project.Filter {OrgID : orgID }).Return ([]project.Project {}, nil )
324- polSvc .EXPECT ().List (mock .Anything , policy. Filter { PrincipalID : userID , PrincipalType : schema .UserPrincipal }).
325- Return (nil , errors .New ("policy list boom" ))
296+ resSvc .EXPECT ().RemovePrincipalAccess (mock .Anything , userID , schema .UserPrincipal , [] string { }).
297+ Return (errors .New ("resource cleanup boom" ))
326298 // ForceRemoveOrganizationMember must NOT be called — strict mock fails on unexpected call
327299
328300 svc := deleter .NewCascadeDeleter (orgSvc , projSvc , resSvc , grpSvc , mbrSvc , polSvc , roleSvc , invSvc , usrSvc , patSvc , suSvc , custSvc , subSvc , invocSvc )
329301 err := svc .RemoveUsersFromOrg (context .Background (), orgID , []string {userID })
330- assert .ErrorContains (t , err , "policy list boom" )
302+ assert .ErrorContains (t , err , "resource cleanup boom" )
331303 })
332304}
333305
0 commit comments