@@ -744,12 +744,14 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
744744 projSvc * mocks.ProjectService
745745 grpSvc * mocks.GroupService
746746 resSvc * mocks.ResourceService
747+ patSvc * mocks.UserPATService
747748 auditRepo * mocks.AuditRecordRepository
748749 }
749750
750751 tests := []struct {
751752 name string
752753 principalType string
754+ noPATService bool
753755 setup func (d testDeps )
754756 wantErr error
755757 wantErrContain string
@@ -775,13 +777,23 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
775777 wantErr : organization .ErrDisabled ,
776778 },
777779 {
778- name : "should return ErrNotMember if principal has no org policies" ,
780+ name : "should revoke leftover PATs and return ErrNotMember if principal has no org policies" ,
779781 setup : func (d testDeps ) {
780782 d .orgSvc .EXPECT ().Get (ctx , orgID ).Return (enabledOrg , nil )
781783 d .policySvc .EXPECT ().List (ctx , policy.Filter {OrgID : orgID , PrincipalID : userID , PrincipalType : schema .UserPrincipal }).Return ([]policy.Policy {}, nil )
784+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (nil )
782785 },
783786 wantErr : membership .ErrNotMember ,
784787 },
788+ {
789+ name : "should return error if revoking leftover PATs fails for a principal with no org policies" ,
790+ setup : func (d testDeps ) {
791+ d .orgSvc .EXPECT ().Get (ctx , orgID ).Return (enabledOrg , nil )
792+ d .policySvc .EXPECT ().List (ctx , policy.Filter {OrgID : orgID , PrincipalID : userID , PrincipalType : schema .UserPrincipal }).Return ([]policy.Policy {}, nil )
793+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (errors .New ("pat boom" ))
794+ },
795+ wantErrContain : "revoke member PATs" ,
796+ },
785797 {
786798 name : "should return ErrLastOwnerRole when removing the last owner" ,
787799 setup : func (d testDeps ) {
@@ -827,6 +839,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
827839 d .policySvc .EXPECT ().Delete (ctx , "org-p1" ).Return (nil )
828840 d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .UserPrincipal , orgID , []string {}).Return (nil )
829841 d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
842+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (nil )
830843 },
831844 wantErr : nil ,
832845 },
@@ -851,6 +864,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
851864 d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .UserPrincipal , orgID , []string {projectID }).Return (nil )
852865 d .relSvc .EXPECT ().Delete (ctx , relation.Relation {Object : grpObj , Subject : userSub , RelationName : schema .MemberRelationName }).Return (relation .ErrNotExist )
853866 d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
867+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (nil )
854868 },
855869 wantErr : nil ,
856870 },
@@ -872,6 +886,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
872886 d .policySvc .EXPECT ().Delete (ctx , "org-p1" ).Return (nil )
873887 d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .UserPrincipal , orgID , []string {}).Return (nil )
874888 d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
889+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (nil )
875890 },
876891 wantErr : nil ,
877892 },
@@ -925,6 +940,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
925940 d .policySvc .EXPECT ().Delete (ctx , "org-p1" ).Return (nil )
926941 d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .UserPrincipal , orgID , []string {projectID , secondProjectID }).Return (nil )
927942 d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
943+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (nil )
928944 },
929945 wantErr : nil ,
930946 },
@@ -962,6 +978,59 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
962978 },
963979 wantErr : membership .ErrLastOwnerRole ,
964980 },
981+ {
982+ name : "should return error if PAT revocation fails after the member is removed" ,
983+ setup : func (d testDeps ) {
984+ d .orgSvc .EXPECT ().Get (ctx , orgID ).Return (enabledOrg , nil )
985+ d .policySvc .EXPECT ().List (ctx , policy.Filter {OrgID : orgID , PrincipalID : userID , PrincipalType : schema .UserPrincipal }).Return ([]policy.Policy {{ID : "org-p1" , RoleID : viewerRoleID }}, nil )
986+ d .roleSvc .EXPECT ().Get (ctx , schema .RoleOrganizationOwner ).Return (role.Role {ID : ownerRoleID , Name : schema .RoleOrganizationOwner }, nil )
987+ d .projSvc .EXPECT ().List (ctx , project.Filter {OrgID : orgID }).Return ([]project.Project {}, nil )
988+ d .grpSvc .EXPECT ().List (ctx , group.Filter {OrganizationID : orgID }).Return ([]group.Group {}, nil )
989+ d .policySvc .EXPECT ().List (ctx , policy.Filter {PrincipalID : userID , PrincipalType : schema .UserPrincipal }).Return ([]policy.Policy {
990+ {ID : "org-p1" , ResourceType : schema .OrganizationNamespace , ResourceID : orgID },
991+ }, nil )
992+ d .policySvc .EXPECT ().Delete (ctx , "org-p1" ).Return (nil )
993+ d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .UserPrincipal , orgID , []string {}).Return (nil )
994+ d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
995+ d .patSvc .EXPECT ().OnOrgMemberRemoved (ctx , orgID , userID ).Return (errors .New ("pat boom" ))
996+ },
997+ wantErrContain : "revoke member PATs" ,
998+ },
999+ {
1000+ name : "should not revoke PATs when the removed principal is not a user" ,
1001+ principalType : schema .PATPrincipal ,
1002+ setup : func (d testDeps ) {
1003+ d .orgSvc .EXPECT ().Get (ctx , orgID ).Return (enabledOrg , nil )
1004+ d .policySvc .EXPECT ().List (ctx , policy.Filter {OrgID : orgID , PrincipalID : userID , PrincipalType : schema .PATPrincipal }).Return ([]policy.Policy {{ID : "org-p1" , RoleID : viewerRoleID }}, nil )
1005+ d .projSvc .EXPECT ().List (ctx , project.Filter {OrgID : orgID }).Return ([]project.Project {}, nil )
1006+ d .grpSvc .EXPECT ().List (ctx , group.Filter {OrganizationID : orgID }).Return ([]group.Group {}, nil )
1007+ d .policySvc .EXPECT ().List (ctx , policy.Filter {PrincipalID : userID , PrincipalType : schema .PATPrincipal }).Return ([]policy.Policy {
1008+ {ID : "org-p1" , ResourceType : schema .OrganizationNamespace , ResourceID : orgID },
1009+ }, nil )
1010+ d .policySvc .EXPECT ().Delete (ctx , "org-p1" ).Return (nil )
1011+ d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .PATPrincipal , orgID , []string {}).Return (nil )
1012+ d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
1013+ },
1014+ wantErr : nil ,
1015+ },
1016+ {
1017+ name : "should still remove the member when the PAT service is not set" ,
1018+ noPATService : true ,
1019+ setup : func (d testDeps ) {
1020+ d .orgSvc .EXPECT ().Get (ctx , orgID ).Return (enabledOrg , nil )
1021+ d .policySvc .EXPECT ().List (ctx , policy.Filter {OrgID : orgID , PrincipalID : userID , PrincipalType : schema .UserPrincipal }).Return ([]policy.Policy {{ID : "org-p1" , RoleID : viewerRoleID }}, nil )
1022+ d .roleSvc .EXPECT ().Get (ctx , schema .RoleOrganizationOwner ).Return (role.Role {ID : ownerRoleID , Name : schema .RoleOrganizationOwner }, nil )
1023+ d .projSvc .EXPECT ().List (ctx , project.Filter {OrgID : orgID }).Return ([]project.Project {}, nil )
1024+ d .grpSvc .EXPECT ().List (ctx , group.Filter {OrganizationID : orgID }).Return ([]group.Group {}, nil )
1025+ d .policySvc .EXPECT ().List (ctx , policy.Filter {PrincipalID : userID , PrincipalType : schema .UserPrincipal }).Return ([]policy.Policy {
1026+ {ID : "org-p1" , ResourceType : schema .OrganizationNamespace , ResourceID : orgID },
1027+ }, nil )
1028+ d .policySvc .EXPECT ().Delete (ctx , "org-p1" ).Return (nil )
1029+ d .resSvc .EXPECT ().RemovePrincipalAccess (ctx , userID , schema .UserPrincipal , orgID , []string {}).Return (nil )
1030+ d .auditRepo .EXPECT ().Create (ctx , mock .Anything ).Return (auditrecord.AuditRecord {}, nil )
1031+ },
1032+ wantErr : nil ,
1033+ },
9651034 }
9661035
9671036 for _ , tt := range tests {
@@ -974,6 +1043,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
9741043 projSvc : mocks .NewProjectService (t ),
9751044 grpSvc : mocks .NewGroupService (t ),
9761045 resSvc : mocks .NewResourceService (t ),
1046+ patSvc : mocks .NewUserPATService (t ),
9771047 auditRepo : mocks .NewAuditRecordRepository (t ),
9781048 }
9791049 if tt .setup != nil {
@@ -982,6 +1052,9 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
9821052
9831053 svc := membership .NewService (slog .New (slog .NewTextHandler (io .Discard , nil )), d .policySvc , d .relSvc , d .roleSvc , d .orgSvc , mocks .NewUserService (t ), d .projSvc , d .grpSvc , mocks .NewServiceuserService (t ), d .auditRepo )
9841054 svc .SetResourceService (d .resSvc )
1055+ if ! tt .noPATService {
1056+ svc .SetUserPATService (d .patSvc )
1057+ }
9851058
9861059 principalType := tt .principalType
9871060 if principalType == "" {
@@ -1096,6 +1169,7 @@ func TestService_RemoveOrganizationMemberOnDelete(t *testing.T) {
10961169
10971170 svc := membership .NewService (slog .New (slog .NewTextHandler (io .Discard , nil )), d .policySvc , mocks .NewRelationService (t ), d .roleSvc , d .orgSvc , mocks .NewUserService (t ), d .projSvc , d .grpSvc , mocks .NewServiceuserService (t ), d .auditRepo )
10981171 svc .SetResourceService (d .resSvc )
1172+ svc .SetUserPATService (mocks .NewUserPATService (t ))
10991173
11001174 err := svc .RemoveOrganizationMemberOnDelete (ctx , orgID , userID , schema .UserPrincipal )
11011175 if tt .wantErr != nil {
0 commit comments