"""Tests for composing outgoing encrypted frames.""" import base64 import json from neotalk.core.entities.constants import FrameType, MessageKind from neotalk.core.entities.responses import ResponseTypes from neotalk.core.use_cases.conversation.compose_message import ( compose_message_use_case, compose_typing_use_case, ) def test_compose_message_seals_the_text(crypto, shared_key): # When composing a message response = compose_message_use_case(" hola bob ", crypto, shared_key) # Then the frame is secure and its body decrypts to the trimmed text assert response["type"] == ResponseTypes.SUCCESS frame = response["data"]["frame"] assert frame["type"] == FrameType.SECURE ciphertext = base64.b64decode(frame["body"]) payload = json.loads(crypto.decrypt(shared_key, ciphertext)) assert payload == {"kind": MessageKind.MESSAGE, "text": "hola bob"} def test_compose_message_rejects_empty_text(crypto, shared_key): # When composing whitespace response = compose_message_use_case(" ", crypto, shared_key) # Then it is a parameters error assert response["type"] == ResponseTypes.PARAMETERS_ERROR def test_compose_message_rejects_overly_long_text(crypto, shared_key): # When composing a huge message response = compose_message_use_case("x" * 5000, crypto, shared_key) # Then it is a parameters error assert response["type"] == ResponseTypes.PARAMETERS_ERROR def test_compose_typing_has_no_text(crypto, shared_key): # When composing a typing signal response = compose_typing_use_case(crypto, shared_key) # Then the sealed payload is just the typing kind frame = response["data"]["frame"] ciphertext = base64.b64decode(frame["body"]) payload = json.loads(crypto.decrypt(shared_key, ciphertext)) assert payload == {"kind": MessageKind.TYPING}