-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathtest_sign_up.py
More file actions
92 lines (71 loc) · 3.01 KB
/
Copy pathtest_sign_up.py
File metadata and controls
92 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import httpx
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.common.entities.types_ import UserRole
from app.core.common.entities.user import User
from app.core.common.services.user import UserService
from app.core.common.value_objects.raw_password import RawPassword
from app.core.common.value_objects.username import Username
from app.infrastructure.persistence_sqla.mappings.user import users_table
from tests.integration.with_infra.account.constants import SIGN_UP_ENDPOINT
from tests.integration.with_infra.authentication import authenticate
from tests.integration.with_infra.factories import (
create_raw_password,
create_raw_username,
create_user,
create_user_with_password,
)
async def test_returns_204_and_creates_user(
it_client: httpx.AsyncClient,
it_session: AsyncSession,
) -> None:
username = create_raw_username()
password = create_raw_password()
payload = {"username": username, "password": password}
r = await it_client.post(SIGN_UP_ENDPOINT, json=payload)
assert r.status_code == 204
stmt = select(User).where(users_table.c.username == username)
user = await it_session.scalar(stmt)
assert isinstance(user, User)
assert user.role == UserRole.USER
assert user.is_active is True
async def test_returns_400_when_username_is_too_short(
it_client: httpx.AsyncClient,
) -> None:
payload = {"username": "x" * (Username.MIN_LEN - 1), "password": create_raw_password()}
r = await it_client.post(SIGN_UP_ENDPOINT, json=payload)
assert r.status_code == 400
async def test_returns_400_when_password_is_too_short(
it_client: httpx.AsyncClient,
) -> None:
payload = {"username": create_raw_username(), "password": "x" * (RawPassword.MIN_LEN - 1)}
r = await it_client.post(SIGN_UP_ENDPOINT, json=payload)
assert r.status_code == 400
async def test_returns_409_when_username_already_exists(
it_client: httpx.AsyncClient,
it_session: AsyncSession,
it_user_service: UserService,
) -> None:
username = create_raw_username()
user = create_user(it_user_service, raw_username=username)
it_session.add(user)
await it_session.commit()
payload = {"username": username, "password": create_raw_password()}
r = await it_client.post(SIGN_UP_ENDPOINT, json=payload)
assert r.status_code == 409
stmt = select(func.count()).select_from(User)
count = await it_session.scalar(stmt)
assert count == 1
async def test_returns_403_when_already_authenticated(
it_client: httpx.AsyncClient,
it_session: AsyncSession,
it_user_service: UserService,
) -> None:
password = create_raw_password()
user = await create_user_with_password(it_user_service, raw_password=password)
it_session.add(user)
await it_session.commit()
await authenticate(it_client, user.username.value, password)
payload = {"username": create_raw_username(), "password": create_raw_password()}
r = await it_client.post(SIGN_UP_ENDPOINT, json=payload)
assert r.status_code == 403