Skip to content

Commit 5d8a7eb

Browse files
patrick91tiangolo
andauthored
✨Prevent deletion of personal team (#83)
Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent c398dbf commit 5d8a7eb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

backend/app/api/routes/teams.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,14 @@ def delete_team(
141141
raise HTTPException(
142142
status_code=400, detail="Not enough permissions to execute this action"
143143
)
144+
144145
team = link.team
145146

147+
if team.id == current_user.personal_team_id:
148+
raise HTTPException(
149+
status_code=400, detail="You cannot delete your personal team"
150+
)
151+
146152
for link in team.user_links: # remove all links to this team
147153
session.delete(link)
148154
session.delete(team)

backend/app/tests/api/routes/test_teams.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,38 @@ def test_delete_team_not_enough_permissions(client: TestClient, db: Session) ->
363363
assert data["detail"] == "Not enough permissions to execute this action"
364364

365365

366+
def test_delete_personal_team_forbidden(client: TestClient, db: Session) -> None:
367+
team = create_random_team(db)
368+
user = create_user(
369+
session=db,
370+
371+
password="test123",
372+
full_name="default-org",
373+
is_verified=True,
374+
)
375+
add_user_to_team(session=db, user=user, team=team, role=Role.admin)
376+
user.personal_team_id = team.id
377+
db.add(user)
378+
db.commit()
379+
380+
user_auth_headers = user_authentication_headers(
381+
client=client, email="[email protected]", password="test123"
382+
)
383+
384+
response = client.delete(
385+
f"{settings.API_V1_STR}/teams/{team.slug}",
386+
headers=user_auth_headers,
387+
)
388+
389+
assert response.status_code == 400
390+
data = response.json()
391+
assert data["detail"] == "You cannot delete your personal team"
392+
393+
team_query = select(Team).where(Team.id == team.id)
394+
team_db = db.exec(team_query).first()
395+
assert team_db is not None
396+
397+
366398
def test_update_member_in_team(client: TestClient, db: Session) -> None:
367399
team = create_random_team(db)
368400
user = create_user(

0 commit comments

Comments
 (0)