fix: reject noncanonical cursors and verify storage failover

This commit is contained in:
2026-11-06 18:20:32 +03:00
parent 619155f1e0
commit ff17362bcd
8 changed files with 246 additions and 16 deletions
+36
View File
@@ -1,6 +1,8 @@
"""Black-box regressions derived from the contract and teacher review comments."""
import base64
import hashlib
import json
import os
import struct
import uuid
@@ -243,3 +245,37 @@ def test_parallel_same_key_across_replicas(scenario):
assert len({r["headers"]["X-Instance-Id"] for r in responses}) >= 2
history = c.call("GET", path, actor=alice, schema="MessagePage")["json"]["items"]
assert sum(m["id"] == responses[0]["json"]["id"] for m in history) == 1
def test_exact_pixel_boundary_is_accepted(scenario):
uploaded = upload(scenario, data=image_bytes(size=(8000, 5000)))["json"]
assert uploaded["status"] == "ready"
@pytest.mark.parametrize(
"version,date",
[
(True, "2026-01-01T00:00:00Z"),
(1.0, "2026-01-01T00:00:00Z"),
(1, "2026-W01-1T00:00:00Z"),
(1, "20260101T000000Z"),
(1, "2026-01-01 00:00:00Z"),
(1, "2026-01-01X00:00:00Z"),
(1, "2026-02-30T00:00:00Z"),
(1, "2026-01-01T00:00:00.1234567Z"),
(1, "0000-01-01T00:00:00Z"),
(1, "2026-01-01T24:00:00Z"),
],
)
def test_noncanonical_cursor_over_http(scenario, version, date):
c, alice, _, _, chat = scenario
for path, scope in [
("/api/v1/chats", "chats:" + alice["id"]),
(f"/api/v1/chats/{chat['id']}/messages", "messages:" + chat["id"]),
]:
cursor = (
base64.urlsafe_b64encode(json.dumps([version, scope, date, str(uuid.uuid4())]).encode())
.decode()
.rstrip("=")
)
c.call("GET", path + "?cursor=" + cursor, 400, actor=alice)
+39
View File
@@ -0,0 +1,39 @@
import base64
import json
from uuid import uuid4
import pytest
from messenger.domain import Failure, boundary
@pytest.mark.parametrize(
"version,date",
[
(True, "2026-01-01T00:00:00Z"),
(1.0, "2026-01-01T00:00:00Z"),
(1, "2026-W01-1T00:00:00Z"),
(1, "20260101T000000Z"),
(1, "2026-01-01 00:00:00Z"),
(1, "2026-01-01X00:00:00Z"),
(1, "2026-02-30T00:00:00Z"),
(1, "2026-01-01T00:00:00.1234567Z"),
],
)
def test_boundary_rejects_python_iso_extensions(version, date):
payload = [version, "scope", date, str(uuid4())]
cursor = base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
with pytest.raises(Failure) as error:
boundary("scope", cursor)
assert error.value.status == 400
@pytest.mark.parametrize(
"date", ["2026-01-01T00:00:00Z", "2026-01-01T00:00:00.1Z", "2026-01-01T00:00:00.123456Z"]
)
def test_boundary_preserves_generated_format(date):
uid = str(uuid4())
cursor = (
base64.urlsafe_b64encode(json.dumps([1, "scope", date, uid]).encode()).decode().rstrip("=")
)
assert boundary("scope", cursor) == (date, uid)