Bug
tests/test_api/test_authorization.py::TestSelectApiKey::test_no_platform_key_configured_raises_500 fails locally for any developer who has a .env file (i.e. essentially every dev with a working setup).
CI is green because GitHub Actions runners have no .env on disk.
Reproduction
# With .env present at repo root containing OPENROUTER_API_KEY=...
uv run pytest tests/test_api/test_authorization.py::TestSelectApiKey::test_no_platform_key_configured_raises_500 -v
Result:
FAILED ... - Failed: DID NOT RAISE <class 'fastapi.exceptions.HTTPException'>
Root cause
The test relies on monkeypatch.delenv("OPENROUTER_API_KEY") to simulate "no platform key configured". However, src/api/config.py defines:
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
...
)
openrouter_api_key: str | None = Field(default=None, ...)
get_settings() re-reads from .env on each call (or via cached settings populated from .env), so deleting the env var does not actually remove the key. The test's intent ("no platform key configured") is never realized.
Fix options
- Monkeypatch
Settings.openrouter_api_key directly (e.g. via monkeypatch.setattr(settings, "openrouter_api_key", None) after invalidating any lru_cache on get_settings).
- Use
monkeypatch.setattr on the Settings model's _env_file to point to a temp empty file.
- Override
get_settings dependency for the duration of the test.
Same pattern likely affects other tests that try to clear API key env vars while a .env exists.
Related
Discovered while reviewing #269 (OpenNeuroPET community PR) on branch feature/issue-269-openneuropet-community.
Bug
tests/test_api/test_authorization.py::TestSelectApiKey::test_no_platform_key_configured_raises_500fails locally for any developer who has a.envfile (i.e. essentially every dev with a working setup).CI is green because GitHub Actions runners have no
.envon disk.Reproduction
# With .env present at repo root containing OPENROUTER_API_KEY=... uv run pytest tests/test_api/test_authorization.py::TestSelectApiKey::test_no_platform_key_configured_raises_500 -vResult:
Root cause
The test relies on
monkeypatch.delenv("OPENROUTER_API_KEY")to simulate "no platform key configured". However,src/api/config.pydefines:get_settings()re-reads from.envon each call (or via cached settings populated from.env), so deleting the env var does not actually remove the key. The test's intent ("no platform key configured") is never realized.Fix options
Settings.openrouter_api_keydirectly (e.g. viamonkeypatch.setattr(settings, "openrouter_api_key", None)after invalidating any lru_cache onget_settings).monkeypatch.setattron theSettingsmodel's_env_fileto point to a temp empty file.get_settingsdependency for the duration of the test.Same pattern likely affects other tests that try to clear API key env vars while a
.envexists.Related
Discovered while reviewing #269 (OpenNeuroPET community PR) on branch
feature/issue-269-openneuropet-community.