Files
mtusi2026lab3/student-tests/test_lab3.py
T

146 lines
4.3 KiB
Python

import base64
import json
import os
import uuid
from concurrent.futures import ThreadPoolExecutor
import pytest
from client import Client
@pytest.fixture(scope="module")
def shared():
c = Client(os.environ["BASE_URL"])
return c, *c.bootstrap()
def test_concurrent_idempotency_and_semantic_empty_attachments(shared):
c, alice, _, _, chat = shared
path = f"/api/v1/chats/{chat['id']}/messages"
key = uuid.uuid4().hex
def post(i):
return Client(os.environ["BASE_URL"]).call(
"POST",
path,
201,
schema="Message",
actor=alice,
body={"text": "same", **({"attachment_ids": []} if i % 2 else {})},
headers={"Idempotency-Key": key},
)
with ThreadPoolExecutor(max_workers=10) as pool:
results = list(pool.map(post, range(20)))
assert len({r["json"]["id"] for r in results}) == 1
assert len({r["headers"]["X-Instance-Id"] for r in results}) >= 2
assert all(r["json"] == results[0]["json"] for r in results)
c.call(
"POST", path, 409, actor=alice, body={"text": "different"}, headers={"Idempotency-Key": key}
)
def test_key_scope_and_acl(shared):
c, alice, bob, eve, chat = shared
path = f"/api/v1/chats/{chat['id']}/messages"
key = uuid.uuid4().hex
first = c.call(
"POST",
path,
201,
schema="Message",
actor=alice,
body={"text": "a"},
headers={"Idempotency-Key": key},
)["json"]
other = c.call(
"POST",
path,
201,
schema="Message",
actor=bob,
body={"text": "b"},
headers={"Idempotency-Key": key},
)["json"]
assert first["id"] != other["id"]
for _ in range(2):
c.call("POST", path, 404, actor=eve, body={"text": "a"}, headers={"Idempotency-Key": key})
@pytest.mark.parametrize("key", [None, "", "x" * 129, "bad key", "bad/key", "é"])
def test_invalid_idempotency_key(shared, key):
c, alice, _, _, chat = shared
c.call(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
actor=alice,
body={"text": "x"},
headers={} if key is None else {"Idempotency-Key": key},
)
def test_lossless_nul_message_replay_and_history(shared):
c, alice, _, _, chat = shared
key = uuid.uuid4().hex
path = f"/api/v1/chats/{chat['id']}/messages"
body = {"text": "abc\u0000def"}
first = c.call(
"POST",
path,
201,
schema="Message",
actor=alice,
body=body,
headers={"Idempotency-Key": key},
)["json"]
repeat = c.call(
"POST",
path,
201,
schema="Message",
actor=alice,
body=body,
headers={"Idempotency-Key": key},
)["json"]
assert first == repeat
assert first["text"] == body["text"]
history = c.call("GET", path, 200, schema="MessagePage", actor=alice)["json"]["items"]
assert next(m for m in history if m["id"] == first["id"]) == first
@pytest.mark.parametrize("uid", [[], {}, 123, False, None])
def test_typed_invalid_cursor(shared, uid):
c, alice, _, _, chat = shared
payload = [1, "messages:" + chat["id"], "2026-10-30T12:00:00Z", uid]
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
c.call("GET", f"/api/v1/chats/{chat['id']}/messages?cursor={cursor}", 400, actor=alice)
def test_empty_cursor_and_bad_utf8(shared):
c, alice, _, _, chat = shared
for path in ["/api/v1/chats", f"/api/v1/chats/{chat['id']}/messages"]:
c.call("GET", path + "?cursor=", 400, actor=alice)
response = c.call(
"POST", "/api/v1/users", 400, raw=b"\xff", headers={"Content-Type": "application/json"}
)
assert response["json"]["error"]["code"] == "invalid_request"
assert response["headers"]["X-Instance-Id"]
c.call("GET", "/api/v1/users/me", 401, headers={"Authorization": "Bearer é"})
def test_login_body_is_rejected(shared):
c, alice, *_ = shared
registration = alice["registration"]
basic = base64.b64encode(
(registration["username"] + ":" + registration["password"]).encode()
).decode()
for body in [b"{}", b"{bad", b"x"]:
c.call(
"POST",
"/api/v1/auth/sessions",
400,
raw=body,
headers={"Authorization": "Basic " + basic},
)