From 810f9e2ceb00c686662130f975bf5ec5231b96a4 Mon Sep 17 00:00:00 2001 From: HgO Date: Thu, 28 Jul 2022 10:35:11 +0200 Subject: [PATCH] fix remove reaction --- matrix_alertbot/callback.py | 12 ++---------- matrix_alertbot/command.py | 15 +++++++-------- tests/test_callback.py | 28 ++-------------------------- tests/test_command.py | 8 ++++---- 4 files changed, 15 insertions(+), 48 deletions(-) diff --git a/matrix_alertbot/callback.py b/matrix_alertbot/callback.py index fe689b0..e3be031 100644 --- a/matrix_alertbot/callback.py +++ b/matrix_alertbot/callback.py @@ -216,15 +216,7 @@ class Callbacks: if event.sender == self.client.user: return - logger.debug( - f"Read alert event ID for redacted event {event.redacts} from cache" - ) - - try: - reacted_to_event_id: str = self.cache[event.redacts] - except KeyError: - logger.warning(f"Unable to find silence from event {event.redacts}") - return + logger.debug(f"Received event to remove event ID {event.redacts}") command = UnackAlertCommand( self.client, @@ -233,8 +225,8 @@ class Callbacks: self.config, room, event.sender, + event.event_id, event.redacts, - reacted_to_event_id, ) await command.process() diff --git a/matrix_alertbot/command.py b/matrix_alertbot/command.py index 3c3b88f..2fd3f98 100644 --- a/matrix_alertbot/command.py +++ b/matrix_alertbot/command.py @@ -143,17 +143,17 @@ class AckAlertCommand(BaseAlertCommand): await send_text_to_room( self.client, self.room.room_id, - f"Sorry, I couldn't find alert with fingerprint {alert_fingerprint}, therefore " - "I couldn't create the silence.", + f"Sorry, I couldn't create silence for alert with fingerprint {alert_fingerprint}: {e}", ) return + return except AlertmanagerError as e: logger.exception(f"Unable to create silence: {e}", exc_info=e) await send_text_to_room( self.client, self.room.room_id, - "Something went wrong with Alertmanager, therefore " - f"I couldn't create silence for alert fingerprint {alert_fingerprint}.", + f"Sorry, I couldn't create silence for alert with fingerprint {alert_fingerprint} " + f"because something went wrong with Alertmanager: {e}", ) return @@ -207,8 +207,7 @@ class UnackAlertCommand(BaseAlertCommand): await send_text_to_room( self.client, self.room.room_id, - f"Sorry, I couldn't find alert with fingerprint {alert_fingerprint}, therefore " - "I couldn't remove its silence.", + f"Sorry, I couldn't remove silence for alert with fingerprint {alert_fingerprint}: {e}", ) return except AlertmanagerError as e: @@ -216,8 +215,8 @@ class UnackAlertCommand(BaseAlertCommand): await send_text_to_room( self.client, self.room.room_id, - "Something went wrong with Alertmanager, therefore " - f"I couldn't delete silence for alert fingerprint {alert_fingerprint}.", + f"Sorry, I couldn't remove silence for alert with fingerprint {alert_fingerprint} " + f"because something went wrong with Alertmanager: {e}", ) return diff --git a/tests/test_callback.py b/tests/test_callback.py index 4311ef3..ae29250 100644 --- a/tests/test_callback.py +++ b/tests/test_callback.py @@ -471,7 +471,7 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase): self.fake_client.room_get_event.assert_not_called() @patch.object(matrix_alertbot.callback, "UnackAlertCommand", autospec=True) - async def test_redaction_in_cache(self, fake_command: Mock) -> None: + async def test_redaction(self, fake_command: Mock) -> None: """Tests the callback for RoomMessageText with the command prefix""" # Tests that the bot process messages in the room that contain a command fake_alert_event_id = "some alert event id" @@ -495,34 +495,10 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase): self.fake_config, self.fake_room, fake_redaction_event.sender, + fake_redaction_event.event_id, fake_redaction_event.redacts, - fake_alert_event_id, ) fake_command.return_value.process.assert_called_once() - self.fake_cache.__getitem__.assert_called_once_with( - fake_redaction_event.redacts - ) - - @patch.object(matrix_alertbot.callback, "UnackAlertCommand", autospec=True) - async def test_redaction_not_in_cache(self, fake_command: Mock) -> None: - """Tests the callback for RoomMessageText with the command prefix""" - # Tests that the bot process messages in the room that contain a command - fake_redaction_event = Mock(spec=nio.RedactionEvent) - fake_redaction_event.redacts = "some other event id" - fake_redaction_event.event_id = "some event id" - fake_redaction_event.sender = "@some_other_fake_user:example.com" - - fake_cache_dict: Dict = {} - self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__ - - # Pretend that we received a text message event - await self.callbacks.redaction(self.fake_room, fake_redaction_event) - - # Check that we attempted to execute the command - fake_command.assert_not_called() - self.fake_cache.__getitem__.assert_called_once_with( - fake_redaction_event.redacts - ) @patch.object(matrix_alertbot.callback, "UnackAlertCommand", autospec=True) async def test_ignore_redaction_sent_by_bot_user(self, fake_command: Mock) -> None: diff --git a/tests/test_command.py b/tests/test_command.py index f7bc8fa..003bd83 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -329,7 +329,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase): fake_send_text_to_room.assert_called_once_with( self.fake_client, self.fake_room.room_id, - "Something went wrong with Alertmanager, therefore I couldn't create silence for alert fingerprint fingerprint1.", + "Sorry, I couldn't create silence for alert with fingerprint fingerprint1 because something went wrong with Alertmanager: ", ) self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id) self.fake_cache.get.assert_called_once_with("fingerprint1") @@ -371,7 +371,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase): fake_send_text_to_room.assert_called_once_with( self.fake_client, self.fake_room.room_id, - "Sorry, I couldn't find alert with fingerprint fingerprint1, therefore I couldn't create the silence.", + "Sorry, I couldn't create silence for alert with fingerprint fingerprint1: ", ) self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id) self.fake_cache.get.assert_called_once_with("fingerprint1") @@ -552,7 +552,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase): fake_send_text_to_room.assert_called_once_with( self.fake_client, self.fake_room.room_id, - "Something went wrong with Alertmanager, therefore I couldn't delete silence for alert fingerprint fingerprint1.", + "Sorry, I couldn't remove silence for alert with fingerprint fingerprint1 because something went wrong with Alertmanager: ", ) self.fake_cache.__getitem__.assert_has_calls( [call(self.fake_alert_event_id), call("fingerprint1")] @@ -592,7 +592,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase): fake_send_text_to_room.assert_called_once_with( self.fake_client, self.fake_room.room_id, - "Sorry, I couldn't find alert with fingerprint fingerprint1, therefore I couldn't remove its silence.", + "Sorry, I couldn't remove silence for alert with fingerprint fingerprint1: ", ) self.fake_cache.__getitem__.assert_has_calls( [call(self.fake_alert_event_id), call("fingerprint1")]