69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
from httpx import AsyncClient
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.security import hash_password
|
|
from app.models.mariadb.user import User, UserProfile
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register(client: AsyncClient) -> None:
|
|
response = await client.post(
|
|
"/api/v1/auth/register",
|
|
json={"email": "test@example.com", "password": "password123", "full_name": "Test User"},
|
|
)
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert "refresh_token" in data
|
|
assert data["token_type"] == "bearer"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_duplicate_email(client: AsyncClient) -> None:
|
|
await client.post(
|
|
"/api/v1/auth/register",
|
|
json={"email": "dup@example.com", "password": "pass123"},
|
|
)
|
|
response = await client.post(
|
|
"/api/v1/auth/register",
|
|
json={"email": "dup@example.com", "password": "pass456"},
|
|
)
|
|
assert response.status_code == 409
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login(client: AsyncClient, db_session: AsyncSession) -> None:
|
|
user = User(email="login@example.com", hashed_password=hash_password("pass123"))
|
|
db_session.add(user)
|
|
await db_session.flush()
|
|
profile = UserProfile(user_id=user.id, full_name="Login User")
|
|
db_session.add(profile)
|
|
await db_session.commit()
|
|
|
|
response = await client.post(
|
|
"/api/v1/auth/login",
|
|
json={"email": "login@example.com", "password": "pass123"},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_wrong_password(client: AsyncClient, db_session: AsyncSession) -> None:
|
|
user = User(email="wrong@example.com", hashed_password=hash_password("correct"))
|
|
db_session.add(user)
|
|
await db_session.flush()
|
|
profile = UserProfile(user_id=user.id)
|
|
db_session.add(profile)
|
|
await db_session.commit()
|
|
|
|
response = await client.post(
|
|
"/api/v1/auth/login",
|
|
json={"email": "wrong@example.com", "password": "incorrect"},
|
|
)
|
|
assert response.status_code == 401
|