Spaces:
Sleeping
Sleeping
File size: 3,332 Bytes
78a5fab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | """
Unit tests for settings validation.
These exist because of a production incident: CLERK_JWKS_URL was set without
its scheme, the application booted normally and reported healthy, and then
every authenticated request failed with a generic 401. The only clue was a log
line buried among the request logs:
Failed to fetch JWKS: Request URL is missing an 'http://' or 'https://'
protocol.
A service that cannot authenticate anyone is not healthy. It should refuse to
start instead of accepting traffic it can only reject.
"""
import pytest
from pydantic import ValidationError
from app.config import Settings
pytestmark = pytest.mark.unit
BASE = dict(
DATABASE_URL="sqlite://",
GEMINI_API_KEY="x",
GROQ_API_KEY="x",
MISTRAL_API_KEY="x",
)
def build(**overrides) -> Settings:
"""Construct Settings in isolation, ignoring any .env on disk."""
return Settings(_env_file=None, **{**BASE, **overrides})
# ── The incident ───────────────────────────────────────────────────────────────
def test_jwks_url_without_scheme_is_rejected():
"""The exact value that caused the outage."""
with pytest.raises(ValidationError, match="absolute http"):
build(
CLERK_JWKS_URL="elegant-wahoo-58.clerk.accounts.dev/.well-known/jwks.json"
)
def test_valid_jwks_url_is_accepted():
s = build(
CLERK_JWKS_URL="https://elegant-wahoo-58.clerk.accounts.dev/.well-known/jwks.json"
)
assert s.CLERK_JWKS_URL.startswith("https://")
# ── Other malformed values ─────────────────────────────────────────────────────
@pytest.mark.parametrize(
"bad",
[
"", # empty
" ", # whitespace only
"ftp://host/jwks.json", # wrong scheme
"//host/jwks.json", # protocol-relative
"https://", # scheme but no host
"not a url at all",
],
)
def test_malformed_jwks_urls_are_rejected(bad):
with pytest.raises(ValidationError):
build(CLERK_JWKS_URL=bad)
def test_surrounding_whitespace_is_stripped():
"""Copy-paste from a dashboard often drags whitespace along."""
s = build(CLERK_JWKS_URL=" https://x.clerk.accounts.dev/.well-known/jwks.json ")
assert s.CLERK_JWKS_URL == "https://x.clerk.accounts.dev/.well-known/jwks.json"
def test_plain_http_is_allowed_for_local_development():
s = build(CLERK_JWKS_URL="http://localhost:9999/.well-known/jwks.json")
assert s.CLERK_JWKS_URL.startswith("http://")
# ── The failure is legible ─────────────────────────────────────────────────────
def test_error_message_names_the_variable_and_shows_an_example():
"""
An operator reading a crashed container's last line should be able to fix
this without going to the source.
"""
with pytest.raises(ValidationError) as exc:
build(CLERK_JWKS_URL="clerk.accounts.dev/jwks.json")
message = str(exc.value)
assert "CLERK_JWKS_URL" in message
assert "https://" in message # shows the expected shape
|