feat: restructure test infrastructure with unit/integration/system separation

Test Organization:
- Create tests/unit/, tests/integration/, tests/system/ directories
- Move existing tests into appropriate categories
- Add pytest markers (@pytest.mark.unit, @pytest.mark.integration)

Shared Fixtures:
- Create conftest.py with SQLite engine (for unit tests)
- Add PostgreSQL session fixture with transaction rollback
- Add TestClient fixture for API tests

Configuration:
- Update pyproject.toml with asyncio_mode=auto
- Add test markers and default addopts
- Add aiosqlite dependency for SQLite support

E2E Testing:
- Initialize Playwright in e2e/ directory
- Add playwright.config.ts
- Create login flow E2E test

Build:
- Add test-unit, test-integration, test-system, test-e2e to Makefile
- Update test target to run all categories
- Add testing documentation to README

Note: Some tests have import issues due to missing python-jose
package in dev environment. This needs to be addressed separately.
This commit is contained in:
Fusion
2026-05-18 15:00:33 +02:00
parent a441ea2fac
commit 3ccd94f661
26 changed files with 627 additions and 11 deletions
+8
View File
@@ -25,7 +25,15 @@ dev = [
"mypy>=1.7.0",
"ruff>=0.1.0",
"httpx>=0.25.0",
"aiosqlite>=0.19.0",
]
[tool.pytest.ini_options]
pythonpath = ["."]
asyncio_mode = "auto"
markers = [
"unit: Fast tests with no external dependencies",
"integration: Tests with database and external services",
"system: End-to-end tests of the full stack",
]
addopts = "-m 'not system'"
+92
View File
@@ -0,0 +1,92 @@
"""Shared test fixtures for all test categories."""
import asyncio
import os
from typing import AsyncGenerator, Generator
import pytest
import pytest_asyncio
from fastapi.testclient import TestClient
from sqlalchemy import create_engine, text
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import sessionmaker
from src.config import Settings, build_database_url
from src.models.base import Base
from src.main import app
# Unit test fixtures (SQLite in-memory)
@pytest.fixture(scope="session")
def sqlite_engine():
"""Create a SQLite in-memory engine for unit tests."""
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
Base.metadata.create_all(engine)
yield engine
engine.dispose()
@pytest.fixture
def sqlite_session(sqlite_engine) -> Generator:
"""Provide a SQLite session for unit tests."""
connection = sqlite_engine.connect()
transaction = connection.begin()
session = sessionmaker(bind=connection)()
yield session
session.close()
transaction.rollback()
connection.close()
# Integration test fixtures (PostgreSQL)
TEST_DATABASE_URL = build_database_url(
user="headquarter",
password="headquarter",
host="localhost",
port=5432,
database="headquarter",
)
@pytest_asyncio.fixture(scope="session")
async def postgres_engine():
"""Create a PostgreSQL engine for integration tests."""
engine = create_async_engine(TEST_DATABASE_URL)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield engine
await engine.dispose()
@pytest_asyncio.fixture
async def db_session(postgres_engine) -> AsyncGenerator[AsyncSession, None]:
"""Provide a database session with transaction rollback."""
async with postgres_engine.connect() as connection:
transaction = await connection.begin_nested()
session_factory = async_sessionmaker(
connection, expire_on_commit=False, class_=AsyncSession
)
session = session_factory()
yield session
await session.close()
await transaction.rollback()
@pytest.fixture
def test_client() -> Generator[TestClient, None, None]:
"""Provide a FastAPI test client."""
with TestClient(app) as client:
yield client
@pytest.fixture(autouse=True)
def configure_test_env(monkeypatch):
"""Configure environment for testing."""
monkeypatch.setenv("DATABASE_URL", TEST_DATABASE_URL)
monkeypatch.setenv("APP_ENV", "testing")
@@ -87,6 +87,8 @@ def _insert_user_for_refresh(user_id: str) -> None:
asyncio.run(_run())
n@pytest.mark.integration
def test_login_redirects_to_authentik_authorize_endpoint() -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -98,6 +100,8 @@ def test_login_redirects_to_authentik_authorize_endpoint() -> None:
assert "response_type=code" in response.headers["location"]
n@pytest.mark.integration
def test_me_returns_401_without_access_cookie() -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -108,6 +112,8 @@ def test_me_returns_401_without_access_cookie() -> None:
assert response.status_code == 401
n@pytest.mark.integration
def test_me_returns_user_payload_with_valid_access_cookie() -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -129,6 +135,8 @@ def test_me_returns_user_payload_with_valid_access_cookie() -> None:
assert response.json()["email"] == "dev@headquarter.local"
n@pytest.mark.integration
def test_logout_clears_auth_cookies() -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -141,6 +149,8 @@ def test_logout_clears_auth_cookies() -> None:
assert "access_token=" in response.headers.get("set-cookie", "")
n@pytest.mark.integration
def test_callback_rejects_mismatched_state() -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -152,6 +162,8 @@ def test_callback_rejects_mismatched_state() -> None:
assert response.status_code == 401
n@pytest.mark.integration
def test_callback_sets_auth_cookies_after_success(monkeypatch) -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -180,6 +192,8 @@ def test_callback_sets_auth_cookies_after_success(monkeypatch) -> None:
assert "refresh_token=" in set_cookie_header
n@pytest.mark.integration
def test_refresh_rotates_cookie_and_returns_user_payload(monkeypatch) -> None:
_prepare_auth_test_db()
_insert_user_for_refresh("7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb")
@@ -202,6 +216,8 @@ def test_refresh_rotates_cookie_and_returns_user_payload(monkeypatch) -> None:
assert "refresh_token=" in response.headers.get("set-cookie", "")
n@pytest.mark.integration
def test_refresh_returns_401_for_invalid_refresh_token(monkeypatch) -> None:
_prepare_auth_test_db()
app = _load_app()
@@ -16,6 +16,8 @@ from src.models import Base
from src.models.user import User
n@pytest.mark.integration
def test_cookie_options_follow_environment_defaults(monkeypatch) -> None:
monkeypatch.setenv("APP_ENV", "development")
dev_settings = Settings()
@@ -32,6 +34,8 @@ def test_cookie_options_follow_environment_defaults(monkeypatch) -> None:
assert prod_options["samesite"] == "strict"
n@pytest.mark.integration
def test_login_redirect_url_contains_required_oidc_params() -> None:
settings = Settings()
@@ -49,6 +53,8 @@ def test_login_redirect_url_contains_required_oidc_params() -> None:
assert "nonce=nonce-123" in url
n@pytest.mark.integration
def test_mint_and_decode_internal_access_token_round_trip() -> None:
settings = Settings()
expires_at = datetime.now(UTC) + timedelta(minutes=15)
@@ -69,6 +75,8 @@ def test_mint_and_decode_internal_access_token_round_trip() -> None:
assert "exp" in claims
n@pytest.mark.integration
def test_refresh_token_hash_is_deterministic_and_non_reversible() -> None:
raw_token = "refresh-token-abc"
@@ -80,6 +88,8 @@ def test_refresh_token_hash_is_deterministic_and_non_reversible() -> None:
assert len(first_hash) == 64
n@pytest.mark.integration
def test_decode_access_token_rejects_invalid_signature() -> None:
settings = Settings()
other_settings = Settings(jwt_secret="different-secret")
@@ -98,6 +108,8 @@ def test_decode_access_token_rejects_invalid_signature() -> None:
@pytest.mark.asyncio
n@pytest.mark.integration
async def test_exchange_code_for_tokens_posts_expected_payload() -> None:
settings = Settings()
@@ -121,6 +133,8 @@ async def test_exchange_code_for_tokens_posts_expected_payload() -> None:
assert token_payload["access_token"] == "provider-token"
n@pytest.mark.integration
def test_verify_provider_access_token_with_jwks_oct_key() -> None:
settings = Settings(authentik_audience="headquarter-web", authentik_issuer="https://authentik.local/")
shared_secret = b"shared-secret-123"
@@ -181,6 +195,8 @@ async def db_session() -> AsyncSession:
@pytest.mark.asyncio
n@pytest.mark.integration
async def test_refresh_store_create_rotate_and_revoke(db_session: AsyncSession) -> None:
user = User(email="dev-auth@headquarter.local", name="Dev Auth", authentik_id="auth-dev", avatar_url=None)
db_session.add(user)
@@ -58,16 +58,22 @@ async def db_session() -> AsyncIterator[AsyncSession]:
await engine.dispose()
n@pytest.mark.integration
def test_base_metadata_collects_declared_tables() -> None:
assert isinstance(Base.metadata.tables, dict)
n@pytest.mark.integration
def test_shared_mixins_define_expected_columns() -> None:
assert "id" in UUIDPrimaryKeyMixin.__dict__
assert "created_at" in TimestampMixin.__dict__
assert "updated_at" in TimestampMixin.__dict__
n@pytest.mark.integration
def test_expected_tables_are_registered() -> None:
assert set(Base.metadata.tables) == {
"refresh_tokens",
@@ -79,6 +85,8 @@ def test_expected_tables_are_registered() -> None:
}
n@pytest.mark.integration
def test_user_table_has_required_columns() -> None:
columns = User.__table__.columns
@@ -96,6 +104,8 @@ def test_user_table_has_required_columns() -> None:
assert columns["avatar_url"].nullable is True
n@pytest.mark.integration
def test_project_relationships_point_to_owner_and_default_ssh_key() -> None:
owner_fk = next(iter(Project.__table__.c.owner_id.foreign_keys))
ssh_fk = next(iter(Project.__table__.c.default_ssh_key_id.foreign_keys))
@@ -106,6 +116,8 @@ def test_project_relationships_point_to_owner_and_default_ssh_key() -> None:
assert Project.default_ssh_key.property.mapper.class_ is SSHKey
n@pytest.mark.integration
def test_repository_and_user_config_relationships_are_registered() -> None:
project_fk = next(iter(GitRepository.__table__.c.project_id.foreign_keys))
owner_fk = next(iter(GitRepository.__table__.c.owner_id.foreign_keys))
@@ -119,6 +131,8 @@ def test_repository_and_user_config_relationships_are_registered() -> None:
assert UserConfig.user.property.mapper.class_ is User
n@pytest.mark.integration
def test_refresh_token_table_has_required_columns_and_relationships() -> None:
columns = RefreshToken.__table__.columns
user_fk = next(iter(RefreshToken.__table__.c.user_id.foreign_keys))
@@ -140,6 +154,8 @@ def test_refresh_token_table_has_required_columns_and_relationships() -> None:
@pytest.mark.asyncio
n@pytest.mark.integration
async def test_async_session_can_insert_and_load_user(db_session: AsyncSession) -> None:
user = User(email="dev@headquarter.local", name="Dev User", authentik_id="dev-user", avatar_url=None)
@@ -132,6 +132,8 @@ def _insert_project(project_id: str, owner_id: str, name: str = "Test Project")
asyncio.run(_run())
n@pytest.mark.integration
def test_create_project_requires_authentication() -> None:
_prepare_test_db()
app = _load_app()
@@ -142,6 +144,8 @@ def test_create_project_requires_authentication() -> None:
assert response.status_code == 401
n@pytest.mark.integration
def test_create_project_successfully() -> None:
_prepare_test_db()
user_id = "11111111-1111-1111-1111-111111111111"
@@ -161,6 +165,8 @@ def test_create_project_successfully() -> None:
assert "id" in data
n@pytest.mark.integration
def test_list_projects_returns_only_owned_projects() -> None:
_prepare_test_db()
user1_id = "11111111-1111-1111-1111-111111111111"
@@ -182,6 +188,8 @@ def test_list_projects_returns_only_owned_projects() -> None:
assert data[0]["name"] == "User1 Project"
n@pytest.mark.integration
def test_update_project_requires_ownership() -> None:
_prepare_test_db()
owner_id = "11111111-1111-1111-1111-111111111111"
@@ -200,6 +208,8 @@ def test_update_project_requires_ownership() -> None:
assert response.status_code == 403
n@pytest.mark.integration
def test_update_project_successfully() -> None:
_prepare_test_db()
owner_id = "11111111-1111-1111-1111-111111111111"
@@ -218,6 +228,8 @@ def test_update_project_successfully() -> None:
assert data["name"] == "Updated Name"
n@pytest.mark.integration
def test_delete_project_requires_ownership() -> None:
_prepare_test_db()
owner_id = "11111111-1111-1111-1111-111111111111"
@@ -236,6 +248,8 @@ def test_delete_project_requires_ownership() -> None:
assert response.status_code == 403
n@pytest.mark.integration
def test_delete_project_successfully() -> None:
_prepare_test_db()
owner_id = "11111111-1111-1111-1111-111111111111"
@@ -252,6 +266,8 @@ def test_delete_project_successfully() -> None:
assert response.status_code == 204
n@pytest.mark.integration
def test_set_default_ssh_key_requires_ownership() -> None:
_prepare_test_db()
owner_id = "11111111-1111-1111-1111-111111111111"
@@ -34,6 +34,8 @@ async def db_session() -> AsyncIterator[AsyncSession]:
await engine.dispose()
n@pytest.mark.integration
def test_build_seed_user_returns_deterministic_payload() -> None:
payload = build_seed_user()
@@ -46,6 +48,8 @@ def test_build_seed_user_returns_deterministic_payload() -> None:
@pytest.mark.asyncio
n@pytest.mark.integration
async def test_seed_database_creates_development_user(db_session: AsyncSession) -> None:
await seed_database(db_session)
@@ -11,12 +11,16 @@ async def async_client():
@pytest.mark.asyncio
n@pytest.mark.integration
async def test_create_ssh_key_requires_authentication(async_client: AsyncClient) -> None:
response = await async_client.post("/ssh-keys", json={"name": "test-key"})
assert response.status_code == 401
@pytest.mark.asyncio
n@pytest.mark.integration
async def test_list_ssh_keys_requires_authentication(async_client: AsyncClient) -> None:
response = await async_client.get("/ssh-keys")
assert response.status_code == 401
@@ -99,6 +99,8 @@ def _create_auth_cookie(user_id: str) -> str:
)
n@pytest.mark.integration
def test_get_profile_returns_401_without_cookie() -> None:
_prepare_users_test_db()
app = _load_app()
@@ -109,6 +111,8 @@ def test_get_profile_returns_401_without_cookie() -> None:
assert response.status_code == 401
n@pytest.mark.integration
def test_get_profile_returns_user_data() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
@@ -126,6 +130,8 @@ def test_get_profile_returns_user_data() -> None:
assert data["avatar_url"] is None
n@pytest.mark.integration
def test_update_profile_changes_name_and_email() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
@@ -142,6 +148,8 @@ def test_update_profile_changes_name_and_email() -> None:
assert data["email"] == "updated@headquarter.local"
n@pytest.mark.integration
def test_update_profile_rejects_empty_name() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
@@ -155,6 +163,8 @@ def test_update_profile_rejects_empty_name() -> None:
assert response.status_code == 400
n@pytest.mark.integration
def test_update_profile_rejects_invalid_email() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
@@ -168,6 +178,8 @@ def test_update_profile_rejects_invalid_email() -> None:
assert response.status_code == 400
n@pytest.mark.integration
def test_upload_avatar_updates_avatar_url() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
@@ -190,6 +202,8 @@ def test_upload_avatar_updates_avatar_url() -> None:
assert data["avatar_url"].startswith("/uploads/avatars/")
n@pytest.mark.integration
def test_upload_avatar_rejects_invalid_file_type() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
@@ -207,6 +221,8 @@ def test_upload_avatar_rejects_invalid_file_type() -> None:
assert response.status_code == 400
n@pytest.mark.integration
def test_upload_avatar_rejects_oversized_file() -> None:
_prepare_users_test_db()
user_id = "7f4b7ad8-c4ce-4d1b-8c83-7ce0f4f66dfb"
View File
View File
@@ -1,13 +1,19 @@
import pytest
from src.config import Settings
from src.database import build_database_url
n@pytest.mark.unit
def test_settings_default_database_url_uses_asyncpg() -> None:
settings = Settings()
assert settings.database_url == "postgresql+asyncpg://headquarter:headquarter@postgres:5432/headquarter"
n@pytest.mark.unit
def test_build_database_url_uses_explicit_values() -> None:
url = build_database_url(
user="user",
@@ -20,6 +26,8 @@ def test_build_database_url_uses_explicit_values() -> None:
assert url == "postgresql+asyncpg://user:pass@db:5433/app"
n@pytest.mark.unit
def test_settings_prefers_explicit_database_url_env(monkeypatch) -> None:
monkeypatch.setenv("DATABASE_URL", "postgresql+asyncpg://local:local@localhost:5432/localdb")
@@ -28,6 +36,8 @@ def test_settings_prefers_explicit_database_url_env(monkeypatch) -> None:
assert settings.database_url == "postgresql+asyncpg://local:local@localhost:5432/localdb"
n@pytest.mark.unit
def test_auth_settings_have_secure_defaults() -> None:
settings = Settings()
@@ -41,6 +51,8 @@ def test_auth_settings_have_secure_defaults() -> None:
assert settings.refresh_token_ttl_days == 7
n@pytest.mark.unit
def test_cookie_policy_is_strict_in_production(monkeypatch) -> None:
monkeypatch.setenv("APP_ENV", "production")
@@ -50,6 +62,8 @@ def test_cookie_policy_is_strict_in_production(monkeypatch) -> None:
assert settings.cookie_samesite == "strict"
n@pytest.mark.unit
def test_cookie_policy_is_relaxed_for_local_dev(monkeypatch) -> None:
monkeypatch.setenv("APP_ENV", "development")
@@ -1,7 +1,11 @@
import pytest
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
n@pytest.mark.unit
def test_initial_migration_defines_all_core_tables() -> None:
migration_path = Path(__file__).resolve().parents[1] / "alembic" / "versions" / "0001_initial_schema.py"
spec = spec_from_file_location("initial_schema", migration_path)
@@ -21,6 +25,8 @@ def test_initial_migration_defines_all_core_tables() -> None:
]
n@pytest.mark.unit
def test_refresh_tokens_migration_has_expected_revision_chain() -> None:
migration_path = Path(__file__).resolve().parents[1] / "alembic" / "versions" / "0002_refresh_tokens.py"
spec = spec_from_file_location("refresh_tokens", migration_path)