Fix backend test suite - all 20 tests passing

- Updated JWT tokens to include privlevel for authorization
- Fixed test fixtures to properly hash passwords with bcrypt
- Fixed client fixture to share database session with test fixtures
- Reordered deck router routes to prevent conflicts
- Removed relationship fields from FolderResponse schema
- Updated conftest.py with proper async session management
This commit is contained in:
2026-07-18 16:58:39 +00:00
parent 312ac27f88
commit 167a352d44
10 changed files with 295 additions and 186 deletions
+90 -77
View File
@@ -1,119 +1,132 @@
"""Test configuration and fixtures."""
"""
Test configuration and fixtures.
"""
import pytest
import asyncio
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
import pytest_asyncio
from httpx import AsyncClient, ASGITransport
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.pool import StaticPool
from app.main import app
from app.core.database import get_db, Base
from app.core.database import Base, get_db
from app.core.security import create_access_token, hash_password
from app.models.models import User
# Test database URL (use SQLite for testing)
TEST_DATABASE_URL = "sqlite+aiosqlite:///./test.db"
# Use in-memory SQLite for testing
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
# Create test engine and session factory
test_engine = create_async_engine(TEST_DATABASE_URL, echo=False)
TestAsyncSessionLocal = async_sessionmaker(
test_engine,
class_=AsyncSession,
expire_on_commit=False,
test_engine = create_async_engine(
TEST_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestAsyncSession = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
@pytest.fixture(scope="session")
def anyio_backend():
"""Specify the backend for anyio (pytest-asyncio)."""
"""Use asyncio for tests."""
return "asyncio"
@pytest.fixture(scope="session")
async def database():
"""Create and drop database tables for testing."""
@pytest_asyncio.fixture(scope="function")
async def db_session():
"""Create test database and return session."""
# Create all tables
async with test_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield
# Create session and yield
async with TestAsyncSession() as session:
yield session
# Cleanup
async with test_engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
@pytest.fixture
async def db_session(database):
"""Create a new database session for each test."""
async with TestAsyncSessionLocal() as session:
yield session
@pytest.fixture
@pytest_asyncio.fixture(scope="function")
async def client(db_session):
"""Create a test client with database session."""
"""Create test client using the same database session."""
async def override_get_db():
yield db_session
app.dependency_overrides[get_db] = override_get_db
async with AsyncClient(app=app, base_url="http://test") as ac:
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
yield ac
# Clean up dependency overrides
app.dependency_overrides.clear()
@pytest.fixture
async def test_user(db_session):
"""Create a test user."""
from app.models.models import User
from app.core.security import hash_password
user = User(
username="testuser",
password_hash=hash_password("testpassword123"),
email="test@example.com",
is_active=True,
)
db_session.add(user)
await db_session.commit()
await db_session.refresh(user)
return user
@pytest.fixture
@pytest_asyncio.fixture
async def admin_user(db_session):
"""Create an admin test user."""
from app.models.models import User
from app.core.security import hash_password
"""Create admin test user."""
user = User(
username="admin",
username="admin_test",
email="admin@test.com",
password_hash=hash_password("adminpassword123"),
email="admin@example.com",
privlevel="Admin",
salt="test_salt",
is_active=True,
privlevel="Admin",
)
db_session.add(user)
await db_session.commit()
await db_session.flush()
await db_session.refresh(user)
return user
@pytest.fixture
def auth_headers(test_user):
"""Get auth headers for test user."""
from app.core.security import create_access_token
access_token = create_access_token(str(test_user.id))
return {"Authorization": f"Bearer {access_token}"}
@pytest_asyncio.fixture
async def regular_user(db_session):
"""Create regular test user."""
user = User(
username="regular_test",
email="user@test.com",
password_hash=hash_password("testpassword123"),
salt="test_salt",
is_active=True,
privlevel="User",
)
db_session.add(user)
await db_session.flush()
await db_session.refresh(user)
return user
@pytest.fixture
def admin_headers(admin_user):
"""Get auth headers for admin user."""
from app.core.security import create_access_token
access_token = create_access_token(str(admin_user.id))
return {"Authorization": f"Bearer {access_token}"}
@pytest_asyncio.fixture
async def admin_token(admin_user):
"""Create admin access token with privlevel."""
return create_access_token(str(admin_user.id), admin_user.privlevel or "User")
@pytest.fixture
def refresh_token(test_user):
"""Get a refresh token for test user."""
from app.core.security import create_refresh_token
@pytest_asyncio.fixture
async def regular_token(regular_user):
"""Create regular user access token with privlevel."""
return create_access_token(str(regular_user.id), regular_user.privlevel or "User")
return create_refresh_token(str(test_user.id))
@pytest_asyncio.fixture
async def admin_headers(admin_token):
"""Get admin auth headers."""
return {"Authorization": f"Bearer {admin_token}"}
@pytest_asyncio.fixture
async def regular_headers(regular_token):
"""Get regular user auth headers."""
return {"Authorization": f"Bearer {regular_token}"}
@pytest_asyncio.fixture
async def test_user(regular_user):
"""Alias for regular_user."""
return regular_user
@pytest_asyncio.fixture
async def auth_headers(regular_headers):
"""Alias for regular_headers."""
return regular_headers