Skip to content

Commit

Permalink
Merge "Ensure that deleting a team also removes the team from users"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul CI authored and Gerrit Code Review committed Sep 3, 2024
2 parents 631345c + 046c819 commit 0ffa7da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
11 changes: 7 additions & 4 deletions dci/api/v1/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,29 @@ def put_team(user, t_id):
def delete_team_by_id(user, t_id):
# get If-Match header
if_match_etag = utils.check_and_get_etag(flask.request.headers)
base.get_resource_orm(models2.Team, t_id)
team = base.get_resource_orm(models2.Team, t_id)

if user.is_not_super_admin():
raise dci_exc.Unauthorized()

deleted_team = (
updated_rows = (
flask.g.session.query(models2.Team)
.filter(models2.Team.id == t_id)
.filter(models2.Team.etag == if_match_etag)
.update({"state": "archived"})
)
flask.g.session.commit()

if not deleted_team:
if not updated_rows:
flask.g.session.rollback()
raise dci_exc.DCIException(
message="delete failed, either team already deleted or etag not matched",
status_code=409,
)

[team.users.remove(user) for user in team.users]
flask.g.session.add(team)
flask.g.session.commit()

try:
for model in [models2.File, models2.Remoteci, models2.Job]:
flask.g.session.query(model).filter(model.team_id == t_id).update(
Expand Down
32 changes: 32 additions & 0 deletions tests/api/v1/test_teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,38 @@ def test_delete_team_archive_dependencies(
assert deleted_job.status_code == 404


def test_deleted_team_has_no_users(admin, user_id, team_user_id):
pt = admin.post("/api/v1/teams", data={"name": "pname"})
pt_etag = pt.headers.get("ETag")
pt_id = pt.data["team"]["id"]
assert pt.status_code == 201

created_t = admin.get("/api/v1/teams/%s" % pt_id)
assert created_t.status_code == 200

pu = admin.post("/api/v1/teams/%s/users/%s" % (pt_id, user_id), data={})
assert pu.status_code == 201

uteams = admin.get("/api/v1/users/%s/teams" % user_id)
assert uteams.status_code == 200
assert len(uteams.data["teams"]) == 2
team_ids = {t["id"] for t in uteams.data["teams"]}
assert team_ids == set([pt_id, team_user_id])

deleted_t = admin.delete("/api/v1/teams/%s" % pt_id, headers={"If-match": pt_etag})
assert deleted_t.status_code == 204

uteams = admin.get("/api/v1/users/%s/teams" % user_id)
assert uteams.status_code == 200
assert len(uteams.data["teams"]) == 1
team_ids = {t["id"] for t in uteams.data["teams"]}
assert team_ids == set(
[
team_user_id,
]
)


# Tests for the isolation


Expand Down

0 comments on commit 0ffa7da

Please sign in to comment.