Files
sales-trainer/backend/tests/conftest.py

60 lines
1.7 KiB
Python

"""Shared isolated fixtures for backend tests."""
from __future__ import annotations
from pathlib import Path
import pytest
@pytest.fixture()
def app(monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
"""Create a Flask app against a fresh temporary data directory."""
monkeypatch.setenv("DATA_DIR", str(tmp_path))
monkeypatch.setenv("JWT_SECRET", "pytest-only-secret-0123456789abcdef")
monkeypatch.setenv("APP_ENV", "test")
monkeypatch.setenv("BOOTSTRAP_ADMIN_PASSWORD", "pytest-bootstrap-password")
monkeypatch.setenv("FLASK_DEBUG", "false")
from app.config import Config
Config.DATA_DIR = tmp_path
Config.SECRET_KEY = "pytest-only-secret-0123456789abcdef"
Config.APP_ENV = "test"
Config.BOOTSTRAP_ADMIN_PASSWORD = "pytest-bootstrap-password"
Config.FLASK_DEBUG = False
from app.factory import create_app
from app.services import rate_limit
rate_limit._mem.clear()
application = create_app()
application.config.update(TESTING=True)
return application
@pytest.fixture()
def client(app):
return app.test_client()
@pytest.fixture()
def user_store(app):
return app.extensions["user_store"]
def auth_headers(token: str) -> dict[str, str]:
"""Build an Authorization header without exposing the token in assertions."""
return {"Authorization": f"Bearer {token}"}
@pytest.fixture()
def login(client):
def _login(username: str, password: str) -> dict:
response = client.post(
"/api/auth/login",
json={"username": username, "password": password},
)
assert response.status_code == 200, response.get_json()
return response.get_json()
return _login