Fix backend bugs: password hash column rename, dynamic import, AuditLog response_model, websocket reference, missing exports, bcrypt version pin
This commit is contained in:
+66
-69
@@ -1,122 +1,119 @@
|
||||
"""
|
||||
Pytest configuration and fixtures for Cockatrice Web Application tests.
|
||||
"""
|
||||
"""Test configuration and fixtures."""
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
import asyncio
|
||||
from httpx import AsyncClient
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
|
||||
from app.main import app
|
||||
from app.core.database import Base, get_db
|
||||
from app.core.security import hash_password
|
||||
from app.models.models import User
|
||||
from app.core.database import get_db, Base
|
||||
|
||||
# Test database URL (in-memory SQLite for tests)
|
||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||
# Test database URL (use SQLite for testing)
|
||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///./test.db"
|
||||
|
||||
# Create test engine and session
|
||||
test_engine = create_async_engine(
|
||||
TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
|
||||
TestSessionLocal = async_sessionmaker(
|
||||
bind=test_engine,
|
||||
# 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,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def event_loop():
|
||||
"""Create an instance of the default event loop for the test session."""
|
||||
import asyncio
|
||||
loop = asyncio.new_event_loop()
|
||||
yield loop
|
||||
loop.close()
|
||||
def anyio_backend():
|
||||
"""Specify the backend for anyio (pytest-asyncio)."""
|
||||
return "asyncio"
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
async def db_session():
|
||||
"""Create a new database session for each test."""
|
||||
@pytest.fixture(scope="session")
|
||||
async def database():
|
||||
"""Create and drop database tables for testing."""
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
async with TestSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
yield
|
||||
async with test_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
@pytest.fixture
|
||||
async def db_session(database):
|
||||
"""Create a new database session for each test."""
|
||||
async with TestAsyncSessionLocal() as session:
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client(db_session):
|
||||
"""Create a test client with database dependency override."""
|
||||
"""Create a test client with database session."""
|
||||
async def override_get_db():
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
pass
|
||||
|
||||
yield db_session
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=app),
|
||||
base_url="http://test",
|
||||
) as ac:
|
||||
|
||||
async with AsyncClient(app=app, base_url="http://test") as ac:
|
||||
yield ac
|
||||
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
@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_sha512=hash_password("testpassword123"),
|
||||
salt="test_salt",
|
||||
password_hash=hash_password("testpassword123"),
|
||||
email="test@example.com",
|
||||
country="US",
|
||||
privlevel="User",
|
||||
is_active=True,
|
||||
)
|
||||
db_session.add(user)
|
||||
await db_session.flush()
|
||||
await db_session.commit()
|
||||
await db_session.refresh(user)
|
||||
return user
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
@pytest.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
|
||||
|
||||
user = User(
|
||||
username="adminuser",
|
||||
password_sha512=hash_password("adminpassword123"),
|
||||
salt="admin_salt",
|
||||
username="admin",
|
||||
password_hash=hash_password("adminpassword123"),
|
||||
email="admin@example.com",
|
||||
country="US",
|
||||
privlevel="Admin",
|
||||
is_active=True,
|
||||
)
|
||||
db_session.add(user)
|
||||
await db_session.flush()
|
||||
await db_session.commit()
|
||||
await db_session.refresh(user)
|
||||
return user
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
async def auth_headers(client, test_user):
|
||||
"""Get authentication headers for test user."""
|
||||
from app.core.security import create_access_token, create_refresh_token
|
||||
|
||||
@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(scope="function")
|
||||
async def admin_headers(client, admin_user):
|
||||
"""Get authentication headers for admin 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.fixture
|
||||
def refresh_token(test_user):
|
||||
"""Get a refresh token for test user."""
|
||||
from app.core.security import create_refresh_token
|
||||
|
||||
return create_refresh_token(str(test_user.id))
|
||||
|
||||
Reference in New Issue
Block a user