Files
mtusi2026lab7/tests/custom_tests.py
T
2026-11-23 21:13:23 +03:00

47 lines
1.6 KiB
Python

import base64
import uuid
from client import Client, Failure, arguments, check, run
def main():
args = arguments("Lab7 grade 3 negative checks")
c = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = c.bootstrap()
c.call("GET", f"/api/v1/chats/{chat['id']}", 404, actor=eve)
c.call("GET", f"/api/v1/chats/{chat['id']}/messages", 404, actor=eve)
oversized = b"\x89PNG\r\n\x1a\n" + (b"0" * (10 * 1024 * 1024 + 100))
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments?filename=big.png",
413,
actor=alice,
raw=oversized,
headers={"Content-Type": "image/png"},
)
bad_cred = base64.b64encode(f"{alice['registration']['username']}:wrong-pass-12345".encode()).decode()
got_429 = False
for _ in range(10):
res = c.call(
"POST",
"/api/v1/auth/sessions",
[401, 429],
headers={"Authorization": f"Basic {bad_cred}"},
)
if res["status"] == 429:
retry_after = res["headers"].get("Retry-After")
check(retry_after is not None and int(retry_after) >= 1, "429 must include positive Retry-After")
got_429 = True
break
check(got_429, "expected 429 after repeated failed login attempts")
temp_user = c.user("logout_check")
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=temp_user)
c.call("GET", "/api/v1/users/me", 401, actor=temp_user)
print("PASS: grade 3 negative checks (ACL 404, upload 413, login 429+Retry-After, logout 401)")
if __name__ == "__main__":
run(main)