forked from mtusi2026/mtusi2026lab6
96 lines
3.6 KiB
Python
96 lines
3.6 KiB
Python
from base64 import b64encode
|
|
from datetime import UTC, datetime, timedelta
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, Mock
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from starlette.types import Scope
|
|
|
|
from messenger.identity.application.queries import VerifyCredentials
|
|
from messenger.identity.domain.errors import InvalidUserData
|
|
from messenger.identity.domain.values import DisplayName
|
|
from messenger.identity.entrypoints.authentication import (
|
|
BASIC_CHALLENGE,
|
|
LOGIN_PATH,
|
|
basic_credentials,
|
|
guard,
|
|
)
|
|
from messenger.messaging.application.commands import PostMessage
|
|
from messenger.messaging.application.handlers import PostMessageHandler
|
|
from messenger.messaging.application.ports import ChatDirectory, MessageLog, MessagingUnitOfWork
|
|
from messenger.messaging.domain.errors import ChatNotFound, InvalidChatData, InvalidMessageText
|
|
from messenger.messaging.domain.values import ChatTitle, MessageText
|
|
from messenger.shared.application.ports import Clock, IdGenerator
|
|
from messenger.shared.domain.errors import DomainError
|
|
from messenger.shared.entrypoints.pipeline import Rejection
|
|
|
|
NUL_TEXTS = ["\x00", "\x00abc", "abc\x00def", "abc\x00"]
|
|
|
|
|
|
@pytest.mark.parametrize("username", NUL_TEXTS)
|
|
def test_basic_rejects_nul_username(username: str) -> None:
|
|
raw = b64encode(f"{username}:synthetic-password".encode()).decode()
|
|
assert basic_credentials(f"Basic {raw}") is None
|
|
|
|
|
|
async def test_nul_username_is_rejected_before_credential_lookup() -> None:
|
|
verify = AsyncMock(return_value=None)
|
|
container = SimpleNamespace(get=AsyncMock(return_value=verify))
|
|
scope: Scope = {
|
|
"method": "POST",
|
|
"path": LOGIN_PATH,
|
|
"headers": [(b"authorization", b"Basic " + b64encode(b"abc\x00def:synthetic-password"))],
|
|
"app": SimpleNamespace(state=SimpleNamespace(dishka_container=container)),
|
|
}
|
|
assert await guard(scope) == Rejection(
|
|
401, "unauthorized", (("www-authenticate", BASIC_CHALLENGE),)
|
|
)
|
|
container.get.assert_awaited_once_with(VerifyCredentials)
|
|
verify.assert_not_awaited()
|
|
assert "user" not in scope["state"]
|
|
|
|
|
|
@pytest.mark.parametrize("raw", NUL_TEXTS)
|
|
@pytest.mark.parametrize(
|
|
("kind", "error"),
|
|
[
|
|
(DisplayName, InvalidUserData),
|
|
(ChatTitle, InvalidChatData),
|
|
(MessageText, InvalidMessageText),
|
|
],
|
|
)
|
|
def test_text_values_reject_nul(
|
|
kind: type[DisplayName | ChatTitle | MessageText], error: type[DomainError], raw: str
|
|
) -> None:
|
|
with pytest.raises(error):
|
|
kind(raw)
|
|
|
|
|
|
@pytest.mark.parametrize("kind", [DisplayName, ChatTitle, MessageText])
|
|
def test_text_values_preserve_unicode_and_whitespace(
|
|
kind: type[DisplayName | ChatTitle | MessageText],
|
|
) -> None:
|
|
raw = " Привет\tмир\n👋 "
|
|
assert kind(raw).value == raw
|
|
|
|
|
|
@pytest.mark.parametrize("member", [False, True])
|
|
async def test_nul_message_checks_access_without_writing(member: bool) -> None:
|
|
chats = AsyncMock(spec=ChatDirectory)
|
|
chats.chat_for_member.return_value = Mock() if member else None
|
|
log = AsyncMock(spec=MessageLog)
|
|
log.post.return_value = None
|
|
uow = Mock(spec=MessagingUnitOfWork)
|
|
clock = Mock(spec=Clock)
|
|
clock.now.return_value = datetime(2026, 1, 1, tzinfo=UTC)
|
|
handler = PostMessageHandler(
|
|
uow, log, chats, clock, Mock(spec=IdGenerator), ttl=timedelta(days=1)
|
|
)
|
|
command = PostMessage(uuid4(), uuid4(), "abc\x00def", "same-key", b"fingerprint")
|
|
with pytest.raises(InvalidMessageText if member else ChatNotFound):
|
|
await handler(command)
|
|
chats.chat_for_member.assert_awaited_once_with(command.chat_id, command.sender_id)
|
|
log.post.assert_not_awaited()
|
|
uow.record.assert_not_called()
|