import pytest from django.core import signing from apps.notifications.api.views import _format_sse_event, _issue_stream_token_for_user, _safe_int, _validate_stream_token pytestmark = pytest.mark.unit def test_stream_token_round_trip_and_invalid_token(): token = _issue_stream_token_for_user("42") assert _validate_stream_token(token) == "42" with pytest.raises(signing.BadSignature): _validate_stream_token("wrong") with pytest.raises(signing.BadSignature): _validate_stream_token(None) def test_sse_formatter_and_safe_integer_limits(): assert _safe_int("-5", 10) == 0 assert _safe_int("oops", 10) == 10 assert 'event: connected\ndata: {"ready": true}\n\n' == _format_sse_event("connected", {"ready": True})