182 lines
5.6 KiB
Python
182 lines
5.6 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},
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"date",
|
|
[
|
|
"2026-W01-1T00:00:00Z",
|
|
"20260101T000000Z",
|
|
"2026-01-01 00:00:00Z",
|
|
"2026-01-01X00:00:00Z",
|
|
"2026-01-01T00:00:00+00:00",
|
|
"2026-02-30T00:00:00Z",
|
|
"2026-01-01T24:00:00Z",
|
|
"2026-01-01T00:00:00.1234567Z",
|
|
],
|
|
)
|
|
def test_cursor_rejects_noncanonical_dates_over_http(shared, date):
|
|
c, alice, _, _, chat = shared
|
|
for path, scope in [
|
|
("/api/v1/chats", "chats:" + alice["id"]),
|
|
(f"/api/v1/chats/{chat['id']}/messages", "messages:" + chat["id"]),
|
|
]:
|
|
payload = [1, scope, date, str(uuid.uuid4())]
|
|
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
|
c.call("GET", path + "?cursor=" + cursor, 400, actor=alice)
|
|
|
|
|
|
@pytest.mark.parametrize("version", [True, 1.0])
|
|
def test_cursor_rejects_noninteger_version_over_http(shared, version):
|
|
c, alice, _, _, chat = shared
|
|
for path, scope in [
|
|
("/api/v1/chats", "chats:" + alice["id"]),
|
|
(f"/api/v1/chats/{chat['id']}/messages", "messages:" + chat["id"]),
|
|
]:
|
|
payload = [version, scope, "2026-01-01T00:00:00Z", str(uuid.uuid4())]
|
|
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
|
c.call("GET", path + "?cursor=" + cursor, 400, actor=alice)
|