From 74a4f3152c6a8ac9b9930694eb45d1e52f7e650c Mon Sep 17 00:00:00 2001 From: HgO Date: Wed, 26 Oct 2022 17:15:13 +0200 Subject: [PATCH 1/2] display text next to emojis --- matrix_alertbot/callback.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/matrix_alertbot/callback.py b/matrix_alertbot/callback.py index e04456a..2f57cb4 100644 --- a/matrix_alertbot/callback.py +++ b/matrix_alertbot/callback.py @@ -305,11 +305,12 @@ class Callbacks: async def key_verification_confirm(self, event: KeyVerificationKey): sas = self.matrix_client.key_verifications[event.transaction_id] - emoji_list = sas.get_emoji() - emoji_str = " ".join(emoji for emoji, alt_text in emoji_list) + emoji_list, alt_text_list = zip(*sas.get_emoji()) + emoji_str = " ".join(emoji_list) + alt_text_str = " ".join(alt_text_list) logger.info( - f"Received request to verify emojis from {event.sender}: {emoji_str}" + f"Received request to verify emojis from {event.sender}: {emoji_str} ({alt_text_str})" ) event_response = await self.matrix_client.confirm_short_auth_string( @@ -338,7 +339,14 @@ class Callbacks: # ) async def key_verification_end(self, event: KeyVerificationMac): - sas = self.matrix_client.key_verifications[event.transaction_id] + try: + sas = self.matrix_client.key_verifications[event.transaction_id] + except KeyError: + logger.error( + f"Unable to find transaction ID {event.transaction_id} sent by {event.sender}" + ) + return + try: todevice_msg = sas.get_mac() except LocalProtocolError as e: From 6bd000d4bfbf3bcb55801d2f3fef8ebc6deb2e97 Mon Sep 17 00:00:00 2001 From: HgO Date: Wed, 26 Oct 2022 17:17:31 +0200 Subject: [PATCH 2/2] add test for key verification end callback --- tests/test_callback.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_callback.py b/tests/test_callback.py index 702767f..1e46016 100644 --- a/tests/test_callback.py +++ b/tests/test_callback.py @@ -752,6 +752,28 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase): fake_sas.get_mac.assert_called_once_with() self.fake_matrix_client.to_device.assert_called_once_with(fake_sas.get_mac()) + async def test_key_verification_end_with_missing_transaction_id(self) -> None: + """Tests the callback for RoomMessageText with the command prefix""" + # Tests that the bot process messages in the room that contain a command + fake_transaction_id = "fake transaction id" + + fake_key_verification_event = Mock(spec=nio.KeyVerificationStart) + fake_key_verification_event.sender = "@some_other_fake_user:example.com" + fake_key_verification_event.transaction_id = fake_transaction_id + + self.fake_matrix_client.to_device.return_value = make_awaitable() + + fake_sas = Mock() + fake_transactions_dict = {} + self.fake_matrix_client.key_verifications = fake_transactions_dict + + # Pretend that we received a text message event + await self.callbacks.key_verification_end(fake_key_verification_event) + + # Check that we attempted to execute the command + fake_sas.get_mac.assert_not_called() + self.fake_matrix_client.to_device.assert_not_called() + async def test_key_verification_end_with_mac_error(self) -> None: """Tests the callback for RoomMessageText with the command prefix""" # Tests that the bot process messages in the room that contain a command