Files
mtusi2026lab2/student-tests/test_without_db.py
T

42 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Level 4: with PostgreSQL stopped the API degrades to a bounded error, never to trust."""
import uuid
from common import arguments, call, expect, run
def readiness_reports_the_outage(base):
ready = call(base, "GET", "/health/ready")
expect(ready["status"] == 503, "/health/ready без БД дал %s" % ready["status"])
expect(ready["json"]["error"]["code"] == "unavailable", "код ошибки %s" % ready["json"]["error"]["code"])
live = call(base, "GET", "/health/live")
expect(live["status"] == 200, "/health/live без БД дал %s" % live["status"])
def writes_fail_with_503(base):
body = {"username": "u_nodb_" + uuid.uuid4().hex[:8], "display_name": "Без БД", "password": "T3st-" + uuid.uuid4().hex}
created = call(base, "POST", "/api/v1/users", body=body)
expect(created["status"] == 503, "регистрация без БД дала %s" % created["status"])
def unknown_token_never_becomes_trusted(base):
for token in ("definitely-not-a-valid-session-token", uuid.uuid4().hex):
result = call(base, "GET", "/api/v1/users/me", token=token)
expect(result["status"] in (401, 503), "неизвестный токен без БД дал %s" % result["status"])
anonymous = call(base, "GET", "/api/v1/chats", token=None)
expect(anonymous["status"] == 401, "анонимный запрос без БД дал %s" % anonymous["status"])
def login_fails_closed(base):
import base64
header = "Basic " + base64.b64encode(b"u_any_user:any-password-123").decode()
result = call(base, "POST", "/api/v1/auth/sessions", headers={"Authorization": header})
expect(result["status"] in (401, 503), "вход без БД дал %s" % result["status"])
TESTS = [readiness_reports_the_outage, writes_fail_with_503, unknown_token_never_becomes_trusted, login_fails_closed]
if __name__ == "__main__":
run(TESTS, arguments(__doc__).base_url)