|
| 1 | +import httpx |
| 2 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 3 | + |
| 4 | +from app.core.common.entities.types_ import UserRole |
| 5 | +from app.core.common.entities.user import User |
| 6 | +from app.core.common.services.user import UserService |
| 7 | +from tests.integration.with_infra.authentication import authenticate |
| 8 | +from tests.integration.with_infra.factories import ( |
| 9 | + create_raw_password, |
| 10 | + create_raw_user_id, |
| 11 | + create_user, |
| 12 | + create_user_with_password, |
| 13 | +) |
| 14 | +from tests.integration.with_infra.users.constants import USERS_ENDPOINT |
| 15 | + |
| 16 | + |
| 17 | +async def test_returns_204_and_deactivates_user( |
| 18 | + it_client: httpx.AsyncClient, |
| 19 | + it_session: AsyncSession, |
| 20 | + it_admin: User, |
| 21 | + it_user_service: UserService, |
| 22 | +) -> None: |
| 23 | + target = create_user(it_user_service, is_active=True) |
| 24 | + it_session.add(target) |
| 25 | + await it_session.commit() |
| 26 | + |
| 27 | + r = await it_client.delete(f"{USERS_ENDPOINT}{target.id_}/activation/") |
| 28 | + |
| 29 | + assert r.status_code == 204 |
| 30 | + await it_session.refresh(target) |
| 31 | + assert target.is_active is False |
| 32 | + |
| 33 | + |
| 34 | +async def test_returns_204_when_user_already_inactive( |
| 35 | + it_client: httpx.AsyncClient, |
| 36 | + it_session: AsyncSession, |
| 37 | + it_admin: User, |
| 38 | + it_user_service: UserService, |
| 39 | +) -> None: |
| 40 | + target = create_user(it_user_service, is_active=False) |
| 41 | + it_session.add(target) |
| 42 | + await it_session.commit() |
| 43 | + |
| 44 | + r = await it_client.delete(f"{USERS_ENDPOINT}{target.id_}/activation/") |
| 45 | + |
| 46 | + assert r.status_code == 204 |
| 47 | + await it_session.refresh(target) |
| 48 | + assert target.is_active is False |
| 49 | + |
| 50 | + |
| 51 | +async def test_returns_401_when_not_authenticated( |
| 52 | + it_client: httpx.AsyncClient, |
| 53 | +) -> None: |
| 54 | + r = await it_client.delete(f"{USERS_ENDPOINT}{create_raw_user_id()}/activation/") |
| 55 | + |
| 56 | + assert r.status_code == 401 |
| 57 | + |
| 58 | + |
| 59 | +async def test_returns_403_when_user_role( |
| 60 | + it_client: httpx.AsyncClient, |
| 61 | + it_session: AsyncSession, |
| 62 | + it_user_service: UserService, |
| 63 | +) -> None: |
| 64 | + password = create_raw_password() |
| 65 | + user = await create_user_with_password(it_user_service, raw_password=password) |
| 66 | + target = create_user(it_user_service, is_active=True) |
| 67 | + it_session.add_all([user, target]) |
| 68 | + await it_session.commit() |
| 69 | + await authenticate(it_client, user.username.value, password) |
| 70 | + |
| 71 | + r = await it_client.delete(f"{USERS_ENDPOINT}{target.id_}/activation/") |
| 72 | + |
| 73 | + assert r.status_code == 403 |
| 74 | + |
| 75 | + |
| 76 | +async def test_returns_403_when_admin_targets_admin( |
| 77 | + it_client: httpx.AsyncClient, |
| 78 | + it_session: AsyncSession, |
| 79 | + it_admin: User, |
| 80 | + it_user_service: UserService, |
| 81 | +) -> None: |
| 82 | + other_admin = create_user(it_user_service, role=UserRole.ADMIN) |
| 83 | + it_session.add(other_admin) |
| 84 | + await it_session.commit() |
| 85 | + |
| 86 | + r = await it_client.delete(f"{USERS_ENDPOINT}{other_admin.id_}/activation/") |
| 87 | + |
| 88 | + assert r.status_code == 403 |
| 89 | + |
| 90 | + |
| 91 | +async def test_returns_404_when_user_not_found( |
| 92 | + it_client: httpx.AsyncClient, |
| 93 | + it_admin: User, |
| 94 | +) -> None: |
| 95 | + r = await it_client.delete(f"{USERS_ENDPOINT}{create_raw_user_id()}/activation/") |
| 96 | + |
| 97 | + assert r.status_code == 404 |
0 commit comments