import os import tempfile import pytest import importlib import sys from pathlib import Path # Ensure the repository root is on sys.path so tests can import the server package. ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) server_app_module = importlib.import_module("server.app") # Expose app and init_db from the imported module app = server_app_module.app init_db = server_app_module.init_db @pytest.fixture(autouse=True, scope="function") def setup_tmp_db(tmp_path, monkeypatch): """Set up the database for each test function.""" tmp_db = tmp_path / "forms.db" # Patch the module attribute directly to avoid package name collisions monkeypatch.setattr(server_app_module, "DB_PATH", tmp_db, raising=False) monkeypatch.setattr("server.settings.ADMIN_USERNAME", "admin") monkeypatch.setattr("server.settings.ADMIN_PASSWORD", "admin") init_db() yield @pytest.fixture(autouse=True, scope="function") def stub_smtp(monkeypatch): """Replace smtplib SMTP clients with fast stubs to avoid real network calls.""" class _DummySMTP: def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs def __enter__(self): return self def __exit__(self, exc_type, exc, tb): return False def starttls(self): return None def login(self, *args, **kwargs): return None def send_message(self, *args, **kwargs): return {} def sendmail(self, *args, **kwargs): return {} monkeypatch.setattr("smtplib.SMTP", _DummySMTP) monkeypatch.setattr("smtplib.SMTP_SSL", _DummySMTP) yield