TT-006: stabilize architecture and add quality gates
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
"""Test configuration and fixtures."""
|
||||
|
||||
import os
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.main import app
|
||||
from app.core.database import get_db, Base
|
||||
from app.core.security import get_password_hash, create_access_token
|
||||
from app.models.user import User
|
||||
from app.models.role import Role
|
||||
from app.models.associations import UserRole
|
||||
|
||||
# Use file-based SQLite for tests (in-memory doesn't work with FastAPI TestClient)
|
||||
TEST_DB = os.path.join(os.path.dirname(__file__), "test.db")
|
||||
engine = create_engine(f"sqlite:///{TEST_DB}", connect_args={"check_same_thread": False})
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
# Enable foreign key support for SQLite
|
||||
@event.listens_for(engine, "connect")
|
||||
def set_sqlite_pragma(dbapi_connection, connection_record):
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
|
||||
def override_get_db():
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def setup_database():
|
||||
"""Create tables once for all tests."""
|
||||
# Remove old database
|
||||
if os.path.exists(TEST_DB):
|
||||
try:
|
||||
os.remove(TEST_DB)
|
||||
except PermissionError:
|
||||
pass
|
||||
Base.metadata.create_all(bind=engine)
|
||||
yield
|
||||
# Cleanup - try to remove, ignore if locked
|
||||
engine.dispose()
|
||||
if os.path.exists(TEST_DB):
|
||||
try:
|
||||
os.remove(TEST_DB)
|
||||
except PermissionError:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clean_db():
|
||||
"""Clean database between tests."""
|
||||
yield
|
||||
# Clean up after each test
|
||||
with engine.connect() as conn:
|
||||
for table in reversed(Base.metadata.sorted_tables):
|
||||
conn.execute(table.delete())
|
||||
conn.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session():
|
||||
"""Provide a database session for each test."""
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""Test client."""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_user(db_session):
|
||||
"""Create an admin user with platform_admin role."""
|
||||
user = User(
|
||||
email="admin@test.com",
|
||||
password_hash=get_password_hash("admin123"),
|
||||
first_name="Admin",
|
||||
last_name="User",
|
||||
is_active=True,
|
||||
is_superadmin=True,
|
||||
)
|
||||
db_session.add(user)
|
||||
db_session.flush()
|
||||
|
||||
role = db_session.query(Role).filter(Role.name == "platform_admin").first()
|
||||
if not role:
|
||||
role = Role(name="platform_admin", description="Platform admin", is_system=True)
|
||||
db_session.add(role)
|
||||
db_session.flush()
|
||||
|
||||
user_role = UserRole(user_id=user.id, role_id=role.id)
|
||||
db_session.add(user_role)
|
||||
db_session.commit()
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def regular_user(db_session):
|
||||
"""Create a regular user without admin role."""
|
||||
user = User(
|
||||
email="user@test.com",
|
||||
password_hash=get_password_hash("user123"),
|
||||
first_name="Regular",
|
||||
last_name="User",
|
||||
is_active=True,
|
||||
is_superadmin=False,
|
||||
)
|
||||
db_session.add(user)
|
||||
db_session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_token(admin_user):
|
||||
"""JWT token for admin user."""
|
||||
return create_access_token(data={
|
||||
"sub": str(admin_user.id),
|
||||
"email": admin_user.email,
|
||||
})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_token(regular_user):
|
||||
"""JWT token for regular user."""
|
||||
return create_access_token(data={
|
||||
"sub": str(regular_user.id),
|
||||
"email": regular_user.email,
|
||||
})
|
||||
Reference in New Issue
Block a user