|
| 1 | +import httpx |
| 2 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 3 | + |
| 4 | +from app.core.common.services.user import UserService |
| 5 | +from app.core.common.value_objects.raw_password import RawPassword |
| 6 | +from app.core.common.value_objects.username import Username |
| 7 | +from tests.integration.with_infra.account.constants import AUTH_COOKIE_NAME, LOG_IN_ENDPOINT |
| 8 | +from tests.integration.with_infra.authentication import authenticate |
| 9 | +from tests.integration.with_infra.factories import ( |
| 10 | + create_raw_password, |
| 11 | + create_raw_username, |
| 12 | + create_user_with_password, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +async def test_returns_204_and_sets_cookie( |
| 17 | + it_client: httpx.AsyncClient, |
| 18 | + it_session: AsyncSession, |
| 19 | + it_user_service: UserService, |
| 20 | +) -> None: |
| 21 | + password = create_raw_password() |
| 22 | + user = await create_user_with_password(it_user_service, raw_password=password) |
| 23 | + it_session.add(user) |
| 24 | + await it_session.commit() |
| 25 | + payload = {"username": user.username.value, "password": password} |
| 26 | + |
| 27 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 28 | + |
| 29 | + assert r.status_code == 204 |
| 30 | + assert AUTH_COOKIE_NAME in r.cookies |
| 31 | + |
| 32 | + |
| 33 | +async def test_returns_400_when_username_is_too_short( |
| 34 | + it_client: httpx.AsyncClient, |
| 35 | +) -> None: |
| 36 | + payload = {"username": "x" * (Username.MIN_LEN - 1), "password": create_raw_password()} |
| 37 | + |
| 38 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 39 | + |
| 40 | + assert r.status_code == 400 |
| 41 | + |
| 42 | + |
| 43 | +async def test_returns_400_when_password_is_too_short( |
| 44 | + it_client: httpx.AsyncClient, |
| 45 | +) -> None: |
| 46 | + payload = {"username": create_raw_username(), "password": "x" * (RawPassword.MIN_LEN - 1)} |
| 47 | + |
| 48 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 49 | + |
| 50 | + assert r.status_code == 400 |
| 51 | + |
| 52 | + |
| 53 | +async def test_returns_401_when_user_does_not_exist( |
| 54 | + it_client: httpx.AsyncClient, |
| 55 | +) -> None: |
| 56 | + payload = {"username": create_raw_username(), "password": create_raw_password()} |
| 57 | + |
| 58 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 59 | + |
| 60 | + assert r.status_code == 401 |
| 61 | + |
| 62 | + |
| 63 | +async def test_returns_401_when_password_is_wrong( |
| 64 | + it_client: httpx.AsyncClient, |
| 65 | + it_session: AsyncSession, |
| 66 | + it_user_service: UserService, |
| 67 | +) -> None: |
| 68 | + user = await create_user_with_password(it_user_service) |
| 69 | + it_session.add(user) |
| 70 | + await it_session.commit() |
| 71 | + payload = {"username": user.username.value, "password": create_raw_password()} |
| 72 | + |
| 73 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 74 | + |
| 75 | + assert r.status_code == 401 |
| 76 | + |
| 77 | + |
| 78 | +async def test_returns_401_when_user_is_inactive( |
| 79 | + it_client: httpx.AsyncClient, |
| 80 | + it_session: AsyncSession, |
| 81 | + it_user_service: UserService, |
| 82 | +) -> None: |
| 83 | + password = create_raw_password() |
| 84 | + user = await create_user_with_password(it_user_service, raw_password=password, is_active=False) |
| 85 | + it_session.add(user) |
| 86 | + await it_session.commit() |
| 87 | + payload = {"username": user.username.value, "password": password} |
| 88 | + |
| 89 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 90 | + |
| 91 | + assert r.status_code == 401 |
| 92 | + |
| 93 | + |
| 94 | +async def test_returns_403_when_already_authenticated( |
| 95 | + it_client: httpx.AsyncClient, |
| 96 | + it_session: AsyncSession, |
| 97 | + it_user_service: UserService, |
| 98 | +) -> None: |
| 99 | + password = create_raw_password() |
| 100 | + user = await create_user_with_password(it_user_service, raw_password=password) |
| 101 | + it_session.add(user) |
| 102 | + await it_session.commit() |
| 103 | + await authenticate(it_client, user.username.value, password) |
| 104 | + payload = {"username": user.username.value, "password": password} |
| 105 | + |
| 106 | + r = await it_client.post(LOG_IN_ENDPOINT, json=payload) |
| 107 | + |
| 108 | + assert r.status_code == 403 |
0 commit comments