You've already forked RekomenciBackend
refactor tests
This commit is contained in:
@@ -1,107 +1,112 @@
|
||||
from http import HTTPStatus as status
|
||||
from uuid import uuid4
|
||||
from typing import Final
|
||||
|
||||
from dirty_equals import IsDict, IsPartialDict, IsStr
|
||||
from dishka import FromDishka
|
||||
from httpx import AsyncClient, Response
|
||||
|
||||
from tests.web_api.helpers import (
|
||||
is_conflict_response,
|
||||
is_not_found_response,
|
||||
is_success_response,
|
||||
is_unauthorized_response,
|
||||
)
|
||||
from tests.web_api.ioc import DatabaseClearer, inject
|
||||
from tests.web_api.test_api_gateway import TestApiGateway
|
||||
|
||||
DEFAULT_PASSWORD = "Sup3rSecret" # noqa: S105
|
||||
|
||||
|
||||
async def _sign_up_email(client: AsyncClient, email: str, password: str = DEFAULT_PASSWORD) -> None:
|
||||
response = await client.post(
|
||||
"/auth/sign_up/email",
|
||||
json={"email": email, "password": password},
|
||||
)
|
||||
assert response.status_code == status.OK, response.text
|
||||
|
||||
|
||||
async def _sign_in_email(client: AsyncClient, email: str, password: str = DEFAULT_PASSWORD) -> Response:
|
||||
return await client.post(
|
||||
"/auth/sign_in/email",
|
||||
json={"email": email, "password": password},
|
||||
)
|
||||
|
||||
|
||||
def _unique_email() -> str:
|
||||
return f"user-{uuid4().hex}@example.com"
|
||||
DEFAULT_PASSWORD: Final = "Sup3rPuperS3cret" # noqa: S105
|
||||
|
||||
|
||||
@inject
|
||||
async def test_email_sign_up_creates_user(
|
||||
http_client: FromDishka[AsyncClient],
|
||||
unique_email: str,
|
||||
test_api_gateway: FromDishka[TestApiGateway],
|
||||
database_clearer: FromDishka[DatabaseClearer],
|
||||
) -> None:
|
||||
await database_clearer.clear()
|
||||
email = _unique_email()
|
||||
|
||||
response = await http_client.post(
|
||||
"/auth/sign_up/email",
|
||||
json={"email": email, "password": DEFAULT_PASSWORD},
|
||||
response = await test_api_gateway.sign_up_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
assert is_success_response(response)
|
||||
assert response.json() == IsPartialDict(
|
||||
access_token=IsStr()
|
||||
)
|
||||
body = response.json()
|
||||
|
||||
assert response.status_code == status.OK
|
||||
assert isinstance(body["access_token"], str)
|
||||
assert body["access_token"]
|
||||
|
||||
|
||||
@inject
|
||||
async def test_email_sign_up_existing_user_conflict(
|
||||
http_client: FromDishka[AsyncClient],
|
||||
unique_email: str,
|
||||
test_api_gateway: FromDishka[TestApiGateway],
|
||||
database_clearer: FromDishka[DatabaseClearer],
|
||||
) -> None:
|
||||
await database_clearer.clear()
|
||||
email = _unique_email()
|
||||
await _sign_up_email(http_client, email)
|
||||
|
||||
response = await http_client.post(
|
||||
"/auth/sign_up/email",
|
||||
json={"email": email, "password": DEFAULT_PASSWORD},
|
||||
await test_api_gateway.sign_up_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
|
||||
assert response.status_code == status.CONFLICT
|
||||
response = await test_api_gateway.sign_up_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
assert is_conflict_response(response)
|
||||
|
||||
|
||||
@inject
|
||||
async def test_email_sign_in_returns_token(
|
||||
http_client: FromDishka[AsyncClient],
|
||||
unique_email: str,
|
||||
test_api_gateway: FromDishka[TestApiGateway],
|
||||
database_clearer: FromDishka[DatabaseClearer],
|
||||
) -> None:
|
||||
await database_clearer.clear()
|
||||
email = _unique_email()
|
||||
await _sign_up_email(http_client, email)
|
||||
|
||||
response = await _sign_in_email(http_client, email)
|
||||
body = response.json()
|
||||
await test_api_gateway.sign_up_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
|
||||
assert response.status_code == status.OK
|
||||
assert isinstance(body["access_token"], str)
|
||||
assert body["access_token"]
|
||||
response = await test_api_gateway.sign_in_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
assert is_success_response(response)
|
||||
assert response.json() == IsDict(
|
||||
access_token=IsStr,
|
||||
)
|
||||
|
||||
|
||||
@inject
|
||||
async def test_email_sign_in_invalid_password(
|
||||
http_client: FromDishka[AsyncClient],
|
||||
unique_email: str,
|
||||
test_api_gateway: FromDishka[TestApiGateway],
|
||||
database_clearer: FromDishka[DatabaseClearer],
|
||||
) -> None:
|
||||
await database_clearer.clear()
|
||||
email = _unique_email()
|
||||
await _sign_up_email(http_client, email)
|
||||
|
||||
response = await _sign_in_email(http_client, email, password="wrong-password")
|
||||
await test_api_gateway.sign_up_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
|
||||
assert response.status_code == status.UNAUTHORIZED
|
||||
response = await test_api_gateway.sign_in_email(
|
||||
email=unique_email,
|
||||
password="wrong-password",
|
||||
)
|
||||
assert is_unauthorized_response(response)
|
||||
|
||||
|
||||
@inject
|
||||
async def test_email_sign_in_user_not_found(
|
||||
http_client: FromDishka[AsyncClient],
|
||||
unique_email: str,
|
||||
test_api_gateway: FromDishka[TestApiGateway],
|
||||
database_clearer: FromDishka[DatabaseClearer],
|
||||
) -> None:
|
||||
await database_clearer.clear()
|
||||
|
||||
response = await _sign_in_email(http_client, email=_unique_email())
|
||||
response = await test_api_gateway.sign_in_email(
|
||||
email=unique_email,
|
||||
password=DEFAULT_PASSWORD,
|
||||
)
|
||||
|
||||
assert response.status_code == status.NOT_FOUND
|
||||
assert is_not_found_response(response)
|
||||
|
||||
Reference in New Issue
Block a user