This changed a bunch of code but no Reviewed-on: #17 Co-authored-by: ahtlon <git@ahtlon.de> Co-committed-by: ahtlon <git@ahtlon.de>
135 lines
3.4 KiB
Python
135 lines
3.4 KiB
Python
from datetime import time
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy.pool import StaticPool
|
|
from sqlmodel import Session, SQLModel, create_engine
|
|
|
|
from app.main import app
|
|
from app.model.models import (
|
|
AccessAuthorizationDB,
|
|
Card,
|
|
GroupDB,
|
|
Timetable,
|
|
UserDB,
|
|
)
|
|
from app.services.database import get_session
|
|
|
|
# Use in-memory SQLite for testing
|
|
TEST_SQLALCHEMY_DATABASE_URL = "sqlite://"
|
|
|
|
engine = create_engine(
|
|
TEST_SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def db_session():
|
|
"""Create a fresh database session for each test."""
|
|
SQLModel.metadata.create_all(engine)
|
|
with Session(engine) as session:
|
|
yield session
|
|
SQLModel.metadata.drop_all(engine)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def client(db_session):
|
|
"""Create a test client with a database session override."""
|
|
|
|
def override_get_session():
|
|
yield db_session
|
|
|
|
app.dependency_overrides[get_session] = override_get_session
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_user(db_session):
|
|
"""Create an admin user for testing."""
|
|
from app.services.auth import get_password_hash
|
|
|
|
admin = UserDB(
|
|
name="admin", passwordhash=get_password_hash("admin123"), is_admin=True
|
|
)
|
|
db_session.add(admin)
|
|
db_session.commit()
|
|
db_session.refresh(admin)
|
|
return admin
|
|
|
|
|
|
@pytest.fixture
|
|
def regular_user(db_session):
|
|
"""Create a regular user for testing."""
|
|
from app.services.auth import get_password_hash
|
|
|
|
user = UserDB(
|
|
name="user", passwordhash=get_password_hash("user123"), is_admin=False
|
|
)
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
db_session.refresh(user)
|
|
return user
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_headers(client, admin_user):
|
|
"""Get authentication headers for admin user."""
|
|
response = client.post(
|
|
"/api/v1/token", data={"username": admin_user.name, "password": "admin123"}
|
|
)
|
|
token = response.json()["access_token"]
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def user_auth_headers(client, regular_user):
|
|
"""Get authentication headers for regular user."""
|
|
response = client.post(
|
|
"/api/v1/token", data={"username": regular_user.name, "password": "user123"}
|
|
)
|
|
token = response.json()["access_token"]
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def test_group(db_session):
|
|
"""Create a test group."""
|
|
group = GroupDB(name="Test Group")
|
|
db_session.add(group)
|
|
db_session.commit()
|
|
db_session.refresh(group)
|
|
return group
|
|
|
|
|
|
@pytest.fixture
|
|
def test_card(db_session, test_group):
|
|
"""Create a test card."""
|
|
card = Card(
|
|
key="test-key-123",
|
|
group_id=test_group.id,
|
|
enabled=True,
|
|
name="test_card",
|
|
card_serial="00:00:00:00:00:00:00",
|
|
)
|
|
db_session.add(card)
|
|
db_session.commit()
|
|
db_session.refresh(card)
|
|
return card
|
|
|
|
|
|
@pytest.fixture
|
|
def test_aa_tt(db_session):
|
|
"""Create a test access authorization with timetable."""
|
|
tt = Timetable(weekday=1, starttime=time(1, 0, 0, 0), duration=50)
|
|
aa = AccessAuthorizationDB(
|
|
name="Test AA", is_active=True, type="timetable", timetables=[tt]
|
|
)
|
|
db_session.add(aa)
|
|
db_session.commit()
|
|
db_session.refresh(aa)
|
|
return aa
|