Files
mtusi2026labs/student-tests/unit/test_cursor.py
T
ArtyomandClaude Opus 5.5 540fcb35ba Carry over the lab 4 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6 and RustFS configuration, migrations, hooks, evidence and
scenario scripts and the CI job from the lab 4 solution commit d2744bc,
merge the lab 4 Makefile targets into the lab 5 skeleton Makefile
without touching its targets, and append the lab 4 solution variables
to the skeleton .env.example. The package and the application follow
the contract version 1.5.0, and the image and the throwaway test
containers are tagged lab5 so they do not collide with a lab 4 stand on
the same host.

This is the starting point of the lab 5 cycle: lint, own tests and the
offline skeleton check pass before any lab 5 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 00:34:03 +03:00

57 lines
1.8 KiB
Python

from datetime import UTC, datetime
from uuid import uuid4
import pytest
from messenger.messaging.domain.errors import InvalidPageRequest
from messenger.messaging.infrastructure.cursor import SignedCursorCodec
KEY = (datetime(2026, 5, 17, 12, 30, 45, 123456, tzinfo=UTC), uuid4().int)
def codec(secret: bytes = b"secret-for-tests") -> SignedCursorCodec:
return SignedCursorCodec(secret)
def test_roundtrip_preserves_key() -> None:
subject = codec()
assert subject.decode("messages:a", subject.encode("messages:a", KEY)) == KEY
def test_cursor_is_opaque() -> None:
encoded = codec().encode("messages:a", KEY)
assert "2026" not in encoded
assert str(KEY[1]) not in encoded
def test_cursor_from_another_scope_is_rejected() -> None:
subject = codec()
encoded = subject.encode("messages:chat-one", KEY)
with pytest.raises(InvalidPageRequest):
subject.decode("messages:chat-two", encoded)
def test_forged_cursor_is_rejected() -> None:
encoded = codec(b"one-secret").encode("messages:a", KEY)
with pytest.raises(InvalidPageRequest):
codec(b"another-secret").decode("messages:a", encoded)
@pytest.mark.parametrize(
"raw",
["", "garbage", "1.2.3", "2.1.2.3", "1.notanumber.ff.AAAA", "1.1.zz.AAAA"],
)
def test_broken_cursor_is_rejected(raw: str) -> None:
with pytest.raises(InvalidPageRequest):
codec().decode("messages:a", raw)
def test_tampered_payload_is_rejected() -> None:
subject = codec()
encoded = subject.encode("messages:a", KEY)
head, _, signature = encoded.rpartition(".")
version, micros, identifier = head.split(".")
tampered = f"{version}.{int(micros) + 1}.{identifier}.{signature}"
with pytest.raises(InvalidPageRequest):
subject.decode("messages:a", tampered)