Skip to content

Commit

Permalink
Fix delete user rout (#206)
Browse files Browse the repository at this point in the history
## Изменения
При удалении юзера юзер ... удаляется. 

## Детали реализации
Помимо удаления юзера добавлено удаление соединений юзера с его группами
(soft-delete)

<!-- Resolve #204 -->

## Check-List
<!-- После сохранения у следующих полей появятся галочки, которые нужно
проставить мышкой -->
- [ ] Вы проверили свой код перед отправкой запроса?
- [ ] Вы написали тесты к реализованным функциям?
- [ ] Вы не забыли применить форматирование `black` и `isort` для
_Back-End_ или `Prettier` для _Front-End_?
  • Loading branch information
DaymasS authored Sep 21, 2024
1 parent e6154d8 commit 4a1570d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion auth_backend/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async def delete_user(
# Удаляем AuthMethod
AuthMethod.delete(method.id, session=db.session)
logger.info(f'{method=} for {user.id=} deleted')

User.delete(user_id, session=db.session)
db.session.commit()
await AuthPluginMeta.user_updated(None, old_user)
logger.info(f'{user=} deleted')
22 changes: 21 additions & 1 deletion tests/test_routes/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.orm import Session

from auth_backend.models import AuthMethod, User
from auth_backend.models.db import Group
from auth_backend.models.db import Group, GroupScope, UserGroup


def test_user_email(client: TestClient, dbsession: Session, user_factory):
Expand All @@ -23,3 +23,23 @@ def test_user_email(client: TestClient, dbsession: Session, user_factory):
gr = Group.get(group, session=dbsession)
dbsession.delete(gr)
dbsession.commit()


def test_delete_user(client: TestClient, dbsession: Session, user_factory):
user1 = user_factory(client)
time1 = datetime.utcnow()
email_user = AuthMethod(user_id=user1, param="email", auth_method="email", value="testemailx@x.xy")
dbsession.add(email_user)
dbsession.commit()
body = {"name": f"group{time1}", "parent_id": None, "scopes": []}
group = client.post(url="/group", json=body).json()["id"]
client.patch(f"/user/{user1}", json={"groups": [group]})
resp = client.delete(f"user/{user1}")
assert resp.status_code == 200
user = dbsession.query(User).filter(User.id == user1).one_or_none()
assert user.is_deleted
dbsession.delete(email_user)
dbsession.query(GroupScope).filter(GroupScope.group_id == group).delete()
dbsession.query(UserGroup).filter(UserGroup.group_id == group).delete()
dbsession.query(Group).filter(Group.id == group).delete()
dbsession.commit()

0 comments on commit 4a1570d

Please sign in to comment.