Skip to content

Commit 3cc9015

Browse files
authored
orgs: remove member removes teams (#17957)
* add failing test to validate issue * ensure that TeamRole objects associated to a user's organization membership are removed along with their membership.
1 parent 6544272 commit 3cc9015

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tests/unit/organizations/test_services.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,35 @@ def test_delete_organization_role(self, organization_service, user_service):
438438
is None
439439
)
440440

441+
def test_delete_organization_role_deletes_team_roles(
442+
self, organization_service, user_service
443+
):
444+
user = UserFactory.create()
445+
organization = OrganizationFactory.create()
446+
organization_role = OrganizationRoleFactory.create(
447+
organization=organization, user=user
448+
)
449+
team = TeamFactory.create(organization=organization)
450+
TeamRoleFactory.create(team=team, user=user)
451+
452+
organization_service.delete_organization_role(organization_role.id)
453+
454+
assert (
455+
organization_service.get_organization_role_by_user(
456+
organization_role.organization_id,
457+
user.id,
458+
)
459+
is None
460+
)
461+
462+
assert (
463+
organization_service.get_organization_team_roles_by_user(
464+
organization.id,
465+
user.id,
466+
)
467+
== []
468+
)
469+
441470
def test_get_organization_invite(self, organization_service):
442471
organization_invite = OrganizationInvitationFactory.create()
443472

warehouse/organizations/services.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ def delete_organization_role(self, organization_role_id):
391391
"""
392392
role = self.get_organization_role(organization_role_id)
393393

394+
for team_role in self.get_organization_team_roles_by_user(
395+
role.organization.id, role.user_id
396+
):
397+
self.db.delete(team_role)
398+
394399
self.db.delete(role)
395400

396401
def get_organization_invite(self, organization_invite_id):
@@ -709,6 +714,16 @@ def get_teams_by_user(self, user_id):
709714
.all()
710715
)
711716

717+
def get_organization_team_roles_by_user(self, organization_id, user_id):
718+
return (
719+
self.db.query(TeamRole)
720+
.join(Team, Team.id == TeamRole.team_id)
721+
.filter(
722+
TeamRole.user_id == user_id, Team.organization_id == organization_id
723+
)
724+
.all()
725+
)
726+
712727
def add_team(self, organization_id, name):
713728
"""
714729
Attempts to create a team with the specified name in an organization

0 commit comments

Comments
 (0)