#!/usr/bin/env python3 """Dependency-failure probe (grade 4). Proves the documented behaviour of the running compose stand: * Valkey down -> API keeps authenticating from PostgreSQL (cache bypass), /health/ready stays 200; * PostgreSQL down -> /health/ready is 503 and authenticated calls return a bounded 503 (an anonymous caller is never trusted); * both recover when the service is started again. Run from the repository root where ``docker compose`` is available. """ from __future__ import annotations import base64 import json import os import subprocess import time import urllib.error import urllib.request BASE = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/") def call(method, path, token=None, body=None): headers = {"Accept": "application/json"} data = None if body is not None: data = json.dumps(body).encode("utf-8") headers["Content-Type"] = "application/json" if token: headers["Authorization"] = "Bearer " + token request = urllib.request.Request(BASE + path, data=data, headers=headers, method=method) try: with urllib.request.urlopen(request, timeout=15) as response: return response.status except urllib.error.HTTPError as error: return error.code except (urllib.error.URLError, TimeoutError, OSError): return 0 def compose(*args): subprocess.run(["docker", "compose", *args], check=True) def wait_ready(expected, timeout=120.0): deadline = time.time() + timeout while time.time() < deadline: if call("GET", "/health/ready") == expected: return time.sleep(2) raise SystemExit(f"readiness did not reach {expected}") def login(): username = "degrade_" + os.urandom(4).hex() password = "T3st-" + os.urandom(8).hex() assert call("POST", "/api/v1/users", body={ "username": username, "display_name": "Degrade", "password": password}) == 201 credential = base64.b64encode(f"{username}:{password}".encode()).decode() request = urllib.request.Request( BASE + "/api/v1/auth/sessions", headers={"Authorization": "Basic " + credential, "Accept": "application/json"}, method="POST", ) with urllib.request.urlopen(request, timeout=15) as response: return json.loads(response.read())["token"] def main() -> None: wait_ready(200) token = login() assert call("GET", "/api/v1/users/me", token=token) == 200 print("baseline: /users/me = 200") print("stopping valkey ...") compose("stop", "valkey") ready = call("GET", "/health/ready") me = call("GET", "/api/v1/users/me", token=token) assert ready == 200, f"readiness must stay 200 without cache, got {ready}" assert me == 200, f"auth must work from PostgreSQL without cache, got {me}" print("valkey down: /health/ready = 200, /users/me = 200 (cache bypassed)") compose("start", "valkey") wait_ready(200) assert call("GET", "/api/v1/users/me", token=token) == 200 print("valkey restored: /users/me = 200") print("stopping postgres ...") compose("stop", "postgres") time.sleep(2) ready = call("GET", "/health/ready") me = call("GET", "/api/v1/users/me", token=token) anon = call("GET", "/api/v1/users/me") assert ready == 503, f"readiness must be 503 without PostgreSQL, got {ready}" assert me == 503, f"authenticated call must be a bounded 503, got {me}" assert anon == 503, f"anonymous call must not be trusted, got {anon}" print("postgres down: /health/ready = 503, /users/me = 503, anonymous = 503") compose("start", "postgres") wait_ready(200, timeout=180) assert call("GET", "/api/v1/users/me", token=token) == 200 print("postgres restored: /users/me = 200 (session survived)") print("PASS: graceful degradation and recovery without trusting the cache/anonymous user") if __name__ == "__main__": main()