app.bettersight.io/backend/tests/test_app.py

51 lines
1.7 KiB
Python

"""
app.py's Sentry initialisation is conditional on SENTRY_DSN at module
import time — Python caches `sys.modules['app']`, so re-importing it
inside the same pytest process with a different env var wouldn't
actually re-run the conditional. Each case is verified in a fresh
subprocess instead.
"""
import os
import subprocess
import sys
def _run_import_check(env_overrides, assertion_snippet):
env = {
**os.environ,
'USE_MOCK_REPOSITORIES': 'true',
'API_SECRET_KEY': 'test-api-key',
'POCKETBASE_URL': 'http://pocketbase.invalid',
'REDIS_URL': 'redis://localhost:6379/0',
'RATELIMIT_STORAGE_URI': 'memory://',
'FIRECRAWL_URL': 'http://firecrawl.invalid',
**env_overrides,
}
backend_dir = os.path.join(os.path.dirname(__file__), '..')
result = subprocess.run(
[sys.executable, '-c', assertion_snippet],
cwd=backend_dir, env=env, capture_output=True, text=True, timeout=30,
)
assert result.returncode == 0, result.stderr
return result.stdout
def test_app_boots_without_sentry_dsn():
env = {k: v for k, v in os.environ.items() if k != 'SENTRY_DSN'}
env.pop('SENTRY_DSN', None)
out = _run_import_check(
{'SENTRY_DSN': ''},
"from app import app; import sentry_sdk; "
"print('client_none' if sentry_sdk.Hub.current.client is None else 'client_set')"
)
assert 'client_none' in out
def test_app_boots_with_sentry_dsn_set():
out = _run_import_check(
{'SENTRY_DSN': 'https://abc123@o0.ingest.sentry.io/0', 'FLASK_ENV': 'staging'},
"from app import app; import sentry_sdk; "
"print('client_none' if sentry_sdk.Hub.current.client is None else 'client_set')"
)
assert 'client_set' in out