feat: add user model and registration endpoint
This commit is contained in:
@@ -1,8 +1,31 @@
|
||||
import os
|
||||
import pytest_asyncio
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||
|
||||
from app.db import Base, engine, get_db
|
||||
from app.main import app
|
||||
|
||||
TestSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
"""Set up minimal environment variables for tests before any imports."""
|
||||
os.environ.setdefault("DATABASE_URL", "postgresql+asyncpg://test:test@localhost/test")
|
||||
os.environ.setdefault("OLLAMA_BASE_URL", "http://localhost:11434")
|
||||
os.environ.setdefault("SESSION_SECRET", "test-secret-key")
|
||||
@pytest_asyncio.fixture(autouse=True)
|
||||
async def _reset_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
yield
|
||||
|
||||
|
||||
async def _override_get_db():
|
||||
async with TestSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
|
||||
app.dependency_overrides[get_db] = _override_get_db
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||
yield ac
|
||||
|
||||
21
backend/tests/test_auth.py
Normal file
21
backend/tests/test_auth.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_register_creates_user(client):
|
||||
response = await client.post(
|
||||
"/auth/register",
|
||||
json={"username": "medium1", "password": "spookyspooky"},
|
||||
)
|
||||
assert response.status_code == 201
|
||||
body = response.json()
|
||||
assert body["username"] == "medium1"
|
||||
assert "id" in body
|
||||
assert "password" not in body
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_register_duplicate_username_rejected(client):
|
||||
await client.post("/auth/register", json={"username": "medium1", "password": "spookyspooky"})
|
||||
response = await client.post("/auth/register", json={"username": "medium1", "password": "anotherpass"})
|
||||
assert response.status_code == 409
|
||||
Reference in New Issue
Block a user