Files
mtusi2026lab2/student-tests/unit/test_cursor.py
T
ArtyomandClaude Opus 5.5 416ce4f9db Carry over the lab 2 solution
Bring the application, own tests, Dockerfile, toolchain, compose
stack, migrations, secret generator, restart hook, evidence scripts and
CI job from the lab 2 solution commit 8cddb09 unchanged, merge the lab
2 Makefile targets into the lab 3 skeleton Makefile without touching
its targets, and append the lab 2 solution variables to the skeleton
.env.example. The package version follows the contract version 1.3.0.

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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-28 11:26:15 +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)