51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
import pytest
|
|
from httpx import AsyncClient
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.main import app
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
|
|
@pytest.fixture
|
|
async def client():
|
|
async with AsyncClient(app=app, base_url="http://test") as ac:
|
|
yield ac
|
|
|
|
@pytest.fixture
|
|
async def db_session():
|
|
async with AsyncSession() as session:
|
|
yield session
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register(client: AsyncClient, db_session: AsyncSession):
|
|
response = await client.post("/register", json={
|
|
"email": "test@example.com",
|
|
"password": "password123",
|
|
"full_name": "Test User"
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["email"] == "test@example.com"
|
|
assert "id" in data
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login(client: AsyncClient, db_session: AsyncSession):
|
|
# Create a user first
|
|
user = User(
|
|
email="test@example.com",
|
|
password_hash=get_password_hash("password123"),
|
|
full_name="Test User"
|
|
)
|
|
db_session.add(user)
|
|
await db_session.commit()
|
|
await db_session.refresh(user)
|
|
|
|
response = await client.post("/login", data={
|
|
"username": "test@example.com",
|
|
"password": "password123"
|
|
})
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "access_token" in data
|
|
assert data["token_type"] == "bearer"
|