|
| 1 | +""" |
| 2 | +Regression tests for #174: |
| 3 | +- validate_config() fails loudly at startup, naming every missing required key. |
| 4 | +- the unsigned in-memory OAuth-state fallback is unreachable outside local dev. |
| 5 | +
|
| 6 | +The fail-closed test fails on pre-fix code, where _encode_oauth_cookie silently |
| 7 | +fell back to the unsigned in-memory store whenever SESSION_SECRET was empty, |
| 8 | +regardless of environment. |
| 9 | +""" |
| 10 | +import pytest |
| 11 | + |
| 12 | +import config |
| 13 | +from routes import auth as auth_mod |
| 14 | + |
| 15 | + |
| 16 | +def _set_required(monkeypatch, **overrides): |
| 17 | + base = { |
| 18 | + "SUPABASE_URL": "https://x.supabase.co", |
| 19 | + "SUPABASE_SERVICE_KEY": "service-key", |
| 20 | + "GEMINI_API_KEY": "gemini-key", |
| 21 | + "SESSION_SECRET": "session-secret", |
| 22 | + "IS_LOCAL": False, |
| 23 | + } |
| 24 | + base.update(overrides) |
| 25 | + for k, v in base.items(): |
| 26 | + monkeypatch.setattr(config, k, v) |
| 27 | + |
| 28 | + |
| 29 | +class TestValidateConfig: |
| 30 | + def test_passes_when_all_present(self, monkeypatch): |
| 31 | + _set_required(monkeypatch) |
| 32 | + config.validate_config() # no raise |
| 33 | + |
| 34 | + def test_raises_naming_every_missing_key(self, monkeypatch): |
| 35 | + _set_required( |
| 36 | + monkeypatch, |
| 37 | + SUPABASE_URL="", |
| 38 | + SUPABASE_SERVICE_KEY="", |
| 39 | + GEMINI_API_KEY="", |
| 40 | + SESSION_SECRET="", |
| 41 | + IS_LOCAL=False, |
| 42 | + ) |
| 43 | + with pytest.raises(RuntimeError) as exc: |
| 44 | + config.validate_config() |
| 45 | + msg = str(exc.value) |
| 46 | + for key in ("SUPABASE_URL", "SUPABASE_SERVICE_KEY", "GEMINI_API_KEY", "SESSION_SECRET"): |
| 47 | + assert key in msg |
| 48 | + |
| 49 | + def test_session_secret_required_outside_local(self, monkeypatch): |
| 50 | + _set_required(monkeypatch, SESSION_SECRET="", IS_LOCAL=False) |
| 51 | + with pytest.raises(RuntimeError) as exc: |
| 52 | + config.validate_config() |
| 53 | + assert "SESSION_SECRET" in str(exc.value) |
| 54 | + |
| 55 | + def test_session_secret_relaxed_in_local(self, monkeypatch): |
| 56 | + _set_required(monkeypatch, SESSION_SECRET="", IS_LOCAL=True) |
| 57 | + config.validate_config() # no raise — relaxed for local dev |
| 58 | + |
| 59 | + |
| 60 | +class TestUnsignedOAuthFallbackFailsClosed: |
| 61 | + def test_fails_closed_in_production(self, monkeypatch): |
| 62 | + # No SESSION_SECRET + not local → must refuse (pre-fix: silently used |
| 63 | + # the unsigned in-memory store). |
| 64 | + monkeypatch.setattr(auth_mod, "SESSION_SECRET", "") |
| 65 | + monkeypatch.setattr(auth_mod, "IS_LOCAL", False) |
| 66 | + with pytest.raises(RuntimeError): |
| 67 | + auth_mod._encode_oauth_cookie({"n": "nonce-123", "v": "verifier"}) |
| 68 | + |
| 69 | + def test_allowed_in_local(self, monkeypatch): |
| 70 | + monkeypatch.setattr(auth_mod, "SESSION_SECRET", "") |
| 71 | + monkeypatch.setattr(auth_mod, "IS_LOCAL", True) |
| 72 | + out = auth_mod._encode_oauth_cookie({"n": "nonce-123", "v": "verifier"}) |
| 73 | + # Unsigned payload (no "." separator) is permitted only in local dev. |
| 74 | + assert isinstance(out, str) and "." not in out |
| 75 | + |
| 76 | + def test_signed_when_secret_present(self, monkeypatch): |
| 77 | + monkeypatch.setattr(auth_mod, "SESSION_SECRET", "supersecret-value") |
| 78 | + out = auth_mod._encode_oauth_cookie({"n": "nonce-123", "v": "verifier"}) |
| 79 | + assert "." in out # payload_b64.sig_b64 |
| 80 | + |
| 81 | + def test_decode_refuses_unsigned_cookie_in_production(self, monkeypatch): |
| 82 | + # Symmetric decode-side guard: even a hand-built unsigned cookie must |
| 83 | + # not be honored outside local dev. |
| 84 | + import base64 |
| 85 | + import json |
| 86 | + |
| 87 | + unsigned = base64.urlsafe_b64encode( |
| 88 | + json.dumps({"n": "x", "v": "y"}).encode() |
| 89 | + ).decode().rstrip("=") |
| 90 | + # Force the unsigned path (empty secret) in production mode. |
| 91 | + monkeypatch.setattr(auth_mod, "SESSION_SECRET", "") |
| 92 | + monkeypatch.setattr(auth_mod, "IS_LOCAL", False) |
| 93 | + assert auth_mod._decode_oauth_cookie(unsigned) is None |
0 commit comments