Files
2026-10-23 18:55:20 +03:00

204 lines
7.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""Grade 4 checks that the public smoke does not cover.
Covers: concurrent duplicate registration, atomic chat rollback, short TTL
expiry, logout invalidation, wrong password, and Redis/PostgreSQL outage
behaviour. Failure drills use `docker compose stop/start` and never remove
volumes. Tokens, passwords and hook output are not printed.
"""
import argparse
import concurrent.futures
import os
from pathlib import Path
import subprocess
import sys
import time
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check # noqa: E402
def say(message):
print("PASS: " + message, flush=True)
def compose(args, *command):
env_file = ROOT / ".env"
check(env_file.is_file(), ".env is missing; copy .env.example to .env")
full = [
"docker", "compose",
"--env-file", str(env_file),
"-p", args.compose_project,
"-f", str((ROOT / args.compose_file).resolve()),
*command,
]
result = subprocess.run(full, cwd=ROOT, stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE, text=True, timeout=60)
if result.returncode != 0:
# stderr may contain paths, never credentials in normal compose output.
raise Failure("docker compose " + command[0] + " failed: " + result.stderr.strip()[:300])
def readiness(client):
try:
client.call("GET", "/health/ready", 200, schema="Health")
return 200
except Failure:
try:
client.call("GET", "/health/ready", 503)
return 503
except Failure:
return None
def wait_for(client, expected, seconds=45):
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
if readiness(client) == expected:
return
time.sleep(0.5)
raise Failure("/health/ready did not become " + str(expected))
def check_wrong_password(args):
client = Client(args.base_url)
actor = client.user("wrongpw")
registration = actor["registration"]
credential = _basic(registration["username"], "definitely-wrong-password")
response = client.call("POST", "/api/v1/auth/sessions", 401,
headers={"Authorization": "Basic " + credential})
check(response["headers"].get("WWW-Authenticate", "").lower().startswith("basic"),
"wrong password did not return a Basic challenge")
say("wrong password -> 401 with Basic challenge")
def check_logout(args):
client = Client(args.base_url)
actor = client.user("logout")
client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=actor)
client.call("GET", "/api/v1/users/me", 401, actor=actor)
say("logout revoked the current token (repeat -> 401)")
def check_x_user_id(args):
client = Client(args.base_url)
alice = client.user("xalice")
bob = client.user("xbob")
me = client.call("GET", "/api/v1/users/me", actor=alice,
headers={"X-User-Id": bob["id"]}, schema="User")["json"]
check(me["id"] == alice["id"], "X-User-Id overrode bearer identity")
client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
say("X-User-Id is ignored and grants no access")
def check_concurrent_registration(args):
username = "race_" + uuid.uuid4().hex[:12]
body = {"username": username, "display_name": "Race", "password": "concurrent-password-123"}
def register(_):
client = Client(args.base_url)
response = client.call("POST", "/api/v1/users", (201, 409), body=body)
return response["status"]
with concurrent.futures.ThreadPoolExecutor(max_workers=12) as pool:
statuses = list(pool.map(register, range(12)))
check(statuses.count(201) == 1, "expected exactly one 201, got " + str(statuses.count(201)))
check(statuses.count(409) == 11, "expected eleven 409, got " + str(statuses.count(409)))
say("12 concurrent registrations of one username -> one 201, eleven 409")
def check_chat_rollback(args):
client = Client(args.base_url)
actor = client.user("rollback")
title = "rollback-" + uuid.uuid4().hex
unknown = str(uuid.uuid4())
client.call("POST", "/api/v1/chats", 400, actor=actor,
body={"title": title, "member_ids": [actor["id"], unknown]})
page = client.call("GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage")["json"]
check(all(chat["title"] != title for chat in page["items"]),
"failed chat creation left a partial chat behind")
say("failed composite chat creation rolled back completely")
def check_short_ttl(args):
check(args.short_ttl_url, "--short-ttl-url is required for the expiry check")
short = Client(args.short_ttl_url)
main = Client(args.base_url)
actor = short.user("ttl")
main.call("GET", "/api/v1/users/me", actor=actor, schema="User")
time.sleep(3.5)
main.call("GET", "/api/v1/users/me", 401, actor=actor)
say("SESSION_TTL_SECONDS=2 session expired and was rejected by the main API")
def drill(args, client, actor, chat, message, service, expected_message):
started = time.monotonic()
compose(args, "stop", service)
wait_for(client, 503, 45)
detection = time.monotonic() - started
client.call("GET", "/health/live", 200, schema="Health")
client.call("GET", "/health/ready", 503)
client.call("GET", "/api/v1/users/me", 503, actor=actor)
say("%s down: live=200, ready=503, protected=503 (%s; detected in %.1fs)"
% (service, expected_message, detection))
started = time.monotonic()
compose(args, "start", service)
wait_for(client, 200, 60)
recovery = time.monotonic() - started
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
page = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=100",
actor=actor, schema="MessagePage")["json"]
check(any(item == message for item in page["items"]),
service + " recovery lost a confirmed message")
say("%s recovered in %.1fs; same token and confirmed message intact" % (service, recovery))
def check_failure_drills(args):
client = Client(args.base_url)
actor = client.user("drill")
chat = client.call("POST", "/api/v1/chats", 201, actor=actor,
body={"title": "Failure drill", "member_ids": [actor["id"]]},
schema="Chat")["json"]
message = client.message(actor, chat, "drill-persist-" + uuid.uuid4().hex)["json"]
drill(args, client, actor, chat, message, "redis", "bounded unavailable error, no anonymous access")
drill(args, client, actor, chat, message, "postgres", "bounded unavailable error, session retained")
def _basic(username, password):
import base64
return base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
def main():
parser = argparse.ArgumentParser(description="Lab 2 grade 4 evidence checks")
parser.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080"))
parser.add_argument("--short-ttl-url", default=os.environ.get("SHORT_TTL_URL", "http://localhost:8081"))
parser.add_argument("--compose-project", default=os.environ.get("COMPOSE_PROJECT_NAME", "mtusi2026lab2"))
parser.add_argument("--compose-file", default="compose.yaml")
parser.add_argument("--skip-failure", action="store_true", help="skip Redis/PostgreSQL stop drills")
args = parser.parse_args()
check_wrong_password(args)
check_logout(args)
check_x_user_id(args)
check_concurrent_registration(args)
check_chat_rollback(args)
check_short_ttl(args)
if not args.skip_failure:
check_failure_drills(args)
print("PASS: grade 4 checks completed")
if __name__ == "__main__":
try:
main()
except Failure as error:
print("FAIL:", str(error))
raise SystemExit(1)