Complete Flask → FastAPI migration with: - FastAPI app with session auth, Azure AD SSO, rate limiting - SQLite-backed session store (survives restarts) - Bulk AI metadata generation with SSE progress - Admin panel (user management, audit log, AI usage) - Subpath deployment support (ROOT_PATH config) - Docker + deploy.sh for production deployment - Test suite (auth, upload, templates, imports, admin, sessions) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
95 lines
2.5 KiB
Python
95 lines
2.5 KiB
Python
"""Test fixtures for Oliver Metadata Tool."""
|
|
|
|
import os
|
|
import tempfile
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
# Set test environment BEFORE importing app
|
|
os.environ["SECRET_KEY"] = "test-secret-key-for-testing-only"
|
|
os.environ["ENABLE_TEST_USER"] = "true"
|
|
os.environ["DOCKER_MODE"] = "false"
|
|
os.environ["OPENAI_API_KEY"] = "" # No AI in tests
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def temp_dir():
|
|
"""Create a temporary directory for test artifacts."""
|
|
d = tempfile.mkdtemp(prefix="oliver_test_")
|
|
yield d
|
|
shutil.rmtree(d, ignore_errors=True)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def app(temp_dir):
|
|
"""Create test FastAPI application."""
|
|
os.environ["UPLOAD_FOLDER"] = str(Path(temp_dir) / "uploads")
|
|
os.environ["DB_PATH"] = str(Path(temp_dir) / "test.db")
|
|
os.environ["SESSION_DB_PATH"] = str(Path(temp_dir) / "test_sessions.db")
|
|
os.environ["TEMPLATES_DIR"] = str(Path(__file__).parent.parent / "templates")
|
|
|
|
# Force settings reload
|
|
from app.config import get_settings
|
|
import app.config as config_module
|
|
config_module._settings = None
|
|
|
|
from app.main import create_app
|
|
return create_app()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def client(app):
|
|
"""Create test HTTP client."""
|
|
return TestClient(app)
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_client(client):
|
|
"""Authenticated test client (logged in as tester)."""
|
|
# Login as test user
|
|
response = client.post(
|
|
"/login",
|
|
data={"username": "tester", "password": "oliveradmin"},
|
|
follow_redirects=False,
|
|
)
|
|
assert response.status_code == 302
|
|
return client
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_pdf(temp_dir):
|
|
"""Create a minimal PDF for testing."""
|
|
pdf_path = Path(temp_dir) / "test.pdf"
|
|
# Minimal valid PDF
|
|
pdf_content = b"""%PDF-1.4
|
|
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
|
|
2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj
|
|
3 0 obj<</Type/Page/MediaBox[0 0 612 792]/Parent 2 0 R>>endobj
|
|
xref
|
|
0 4
|
|
0000000000 65535 f
|
|
0000000009 00000 n
|
|
0000000058 00000 n
|
|
0000000115 00000 n
|
|
trailer<</Size 4/Root 1 0 R>>
|
|
startxref
|
|
190
|
|
%%EOF"""
|
|
pdf_path.write_bytes(pdf_content)
|
|
return str(pdf_path)
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_csv(temp_dir):
|
|
"""Create a sample CSV for import testing."""
|
|
csv_path = Path(temp_dir) / "metadata.csv"
|
|
csv_path.write_text(
|
|
"filename,title,subject,keywords\n"
|
|
"test.pdf,Test Title,Test Subject,keyword1 keyword2\n"
|
|
"image.jpg,Image Title,Image Subject,photo landscape\n",
|
|
encoding="utf-8",
|
|
)
|
|
return str(csv_path)
|