|
| 1 | +from datetime import datetime |
| 2 | +from typing import Dict, List |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import BaseModel |
| 7 | + |
| 8 | +from elementary.messages.blocks import HeaderBlock |
| 9 | +from elementary.messages.message_body import MessageBody |
| 10 | +from elementary.messages.messaging_integrations.base_messaging_integration import ( |
| 11 | + BaseMessagingIntegration, |
| 12 | + MessageSendResult, |
| 13 | +) |
| 14 | +from elementary.messages.messaging_integrations.exceptions import ( |
| 15 | + MessagingIntegrationError, |
| 16 | +) |
| 17 | +from elementary.messages.messaging_integrations.mapped import MappedMessagingIntegration |
| 18 | + |
| 19 | + |
| 20 | +class MockMessageContext(BaseModel): |
| 21 | + id: str |
| 22 | + |
| 23 | + |
| 24 | +class MockMessagingIntegration(BaseMessagingIntegration[None, MockMessageContext]): |
| 25 | + def __init__(self, supports_reply: bool = True, supports_actions: bool = False): |
| 26 | + self.supports_reply_value = supports_reply |
| 27 | + self.supports_actions_value = supports_actions |
| 28 | + self.send_message_mock = MagicMock() |
| 29 | + self.send_message_mock.return_value = MessageSendResult( |
| 30 | + timestamp=datetime.now(), |
| 31 | + message_format="test_format", |
| 32 | + message_context=MockMessageContext(id="test_id"), |
| 33 | + ) |
| 34 | + self.reply_to_message_mock = MagicMock() |
| 35 | + self.reply_to_message_mock.return_value = MessageSendResult( |
| 36 | + timestamp=datetime.now(), |
| 37 | + message_format="test_format", |
| 38 | + message_context=MockMessageContext(id="test_id"), |
| 39 | + ) |
| 40 | + |
| 41 | + def send_message( |
| 42 | + self, destination: None, body: MessageBody |
| 43 | + ) -> MessageSendResult[MockMessageContext]: |
| 44 | + return self.send_message_mock(destination, body) |
| 45 | + |
| 46 | + def supports_reply(self) -> bool: |
| 47 | + return self.supports_reply_value |
| 48 | + |
| 49 | + def supports_actions(self) -> bool: |
| 50 | + return self.supports_actions_value |
| 51 | + |
| 52 | + def reply_to_message( |
| 53 | + self, |
| 54 | + destination: None, |
| 55 | + message_context: MockMessageContext, |
| 56 | + body: MessageBody, |
| 57 | + ) -> MessageSendResult[MockMessageContext]: |
| 58 | + return self.reply_to_message_mock(destination, message_context, body) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def mock_integration() -> MockMessagingIntegration: |
| 63 | + return MockMessagingIntegration() |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def mapped_integration( |
| 68 | + mock_integration: MockMessagingIntegration, |
| 69 | +) -> MappedMessagingIntegration: |
| 70 | + return MappedMessagingIntegration({"test_destination": mock_integration}) |
| 71 | + |
| 72 | + |
| 73 | +def test_send_message_success( |
| 74 | + mapped_integration: MappedMessagingIntegration, |
| 75 | + mock_integration: MockMessagingIntegration, |
| 76 | +) -> None: |
| 77 | + destination = "test_destination" |
| 78 | + body = MessageBody(blocks=[HeaderBlock(text="test message")]) |
| 79 | + expected_result: MessageSendResult[MockMessageContext] = MessageSendResult( |
| 80 | + timestamp=datetime.now(), |
| 81 | + message_format="test_format", |
| 82 | + message_context=None, |
| 83 | + ) |
| 84 | + mock_integration.send_message_mock.return_value = expected_result |
| 85 | + |
| 86 | + result = mapped_integration.send_message(destination, body) |
| 87 | + |
| 88 | + assert result == expected_result |
| 89 | + mock_integration.send_message_mock.assert_called_once_with(None, body) |
| 90 | + |
| 91 | + |
| 92 | +def test_send_message_invalid_destination( |
| 93 | + mapped_integration: MappedMessagingIntegration, |
| 94 | +) -> None: |
| 95 | + destination = "invalid_destination" |
| 96 | + body = MessageBody(blocks=[HeaderBlock(text="test message")]) |
| 97 | + |
| 98 | + with pytest.raises(MessagingIntegrationError) as exc_info: |
| 99 | + mapped_integration.send_message(destination, body) |
| 100 | + assert str(exc_info.value) == "Invalid destination: invalid_destination" |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize( |
| 104 | + "integrations_support_reply,expected_support", |
| 105 | + [ |
| 106 | + ([True, True], True), |
| 107 | + ([True, False], False), |
| 108 | + ([False, True], False), |
| 109 | + ([False, False], False), |
| 110 | + ], |
| 111 | +) |
| 112 | +def test_supports_reply( |
| 113 | + integrations_support_reply: List[bool], expected_support: bool |
| 114 | +) -> None: |
| 115 | + integrations: Dict[str, BaseMessagingIntegration[None, MockMessageContext]] = { |
| 116 | + f"dest_{i}": MockMessagingIntegration(supports_reply=supports_reply) |
| 117 | + for i, supports_reply in enumerate(integrations_support_reply) |
| 118 | + } |
| 119 | + mapped_integration = MappedMessagingIntegration(integrations) |
| 120 | + |
| 121 | + result = mapped_integration.supports_reply() |
| 122 | + |
| 123 | + assert result == expected_support |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.parametrize( |
| 127 | + "integrations_support_actions,expected_support", |
| 128 | + [ |
| 129 | + ([True, True], True), |
| 130 | + ([True, False], False), |
| 131 | + ([False, True], False), |
| 132 | + ([False, False], False), |
| 133 | + ], |
| 134 | +) |
| 135 | +def test_supports_actions( |
| 136 | + integrations_support_actions: List[bool], expected_support: bool |
| 137 | +) -> None: |
| 138 | + integrations: Dict[str, BaseMessagingIntegration[None, MockMessageContext]] = { |
| 139 | + f"dest_{i}": MockMessagingIntegration(supports_actions=supports_actions) |
| 140 | + for i, supports_actions in enumerate(integrations_support_actions) |
| 141 | + } |
| 142 | + mapped_integration = MappedMessagingIntegration(integrations) |
| 143 | + |
| 144 | + result = mapped_integration.supports_actions() |
| 145 | + |
| 146 | + assert result == expected_support |
| 147 | + |
| 148 | + |
| 149 | +def test_reply_to_message_success( |
| 150 | + mapped_integration: MappedMessagingIntegration, |
| 151 | + mock_integration: MockMessagingIntegration, |
| 152 | +) -> None: |
| 153 | + destination = "test_destination" |
| 154 | + message_context = MagicMock() |
| 155 | + body = MessageBody(blocks=[HeaderBlock(text="test reply")]) |
| 156 | + expected_result: MessageSendResult[MockMessageContext] = MessageSendResult( |
| 157 | + timestamp=datetime.now(), |
| 158 | + message_format="test_format", |
| 159 | + message_context=message_context, |
| 160 | + ) |
| 161 | + mock_integration.reply_to_message_mock.return_value = expected_result |
| 162 | + |
| 163 | + result = mapped_integration.reply_to_message(destination, message_context, body) |
| 164 | + |
| 165 | + assert result == expected_result |
| 166 | + mock_integration.reply_to_message_mock.assert_called_once_with( |
| 167 | + None, message_context, body |
| 168 | + ) |
| 169 | + |
| 170 | + |
| 171 | +def test_reply_to_message_invalid_destination( |
| 172 | + mapped_integration: MappedMessagingIntegration, |
| 173 | +) -> None: |
| 174 | + destination = "invalid_destination" |
| 175 | + message_context = MagicMock() |
| 176 | + body = MessageBody(blocks=[HeaderBlock(text="test reply")]) |
| 177 | + |
| 178 | + with pytest.raises(MessagingIntegrationError) as exc_info: |
| 179 | + mapped_integration.reply_to_message(destination, message_context, body) |
| 180 | + assert str(exc_info.value) == "Invalid destination: invalid_destination" |
0 commit comments