fix remove reaction

This commit is contained in:
HgO 2022-07-28 10:35:11 +02:00
parent ffd1c898d6
commit 810f9e2ceb
4 changed files with 15 additions and 48 deletions

View file

@ -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()

View file

@ -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

View file

@ -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:

View file

@ -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")]