test callbacks for reactions and test caching

This commit is contained in:
HgO 2022-07-17 02:35:15 +02:00
parent 8dd72a6416
commit f1691fc3a6
2 changed files with 402 additions and 26 deletions

View file

@ -1,9 +1,10 @@
import unittest
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch
import nio
from diskcache import Cache
import matrix_alertbot.command
import matrix_alertbot.callback
from matrix_alertbot.alertmanager import AlertmanagerClient
from matrix_alertbot.callback import Callbacks
@ -18,7 +19,7 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_client = Mock(spec=nio.AsyncClient)
self.fake_client.user = "@fake_user:example.com"
self.fake_cache = Mock(spec=Cache)
self.fake_cache = MagicMock(spec=Cache)
self.fake_alertmanager = Mock(spec=AlertmanagerClient)
# Create a fake room to play with
@ -32,7 +33,7 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_config.command_prefix = "!alert "
self.callbacks = Callbacks(
self.fake_client, self.fake_cache, self.fake_alertmanager, self.fake_config
self.fake_client, self.fake_alertmanager, self.fake_cache, self.fake_config
)
async def test_invite(self) -> None:
@ -64,15 +65,12 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
# Check that the command was not executed
fake_command_create.assert_not_called()
@patch.object(matrix_alertbot.callback.CommandFactory, "create", autospec=True)
async def test_message_not_in_reply_with_prefix(
self, fake_command_create: Mock
@patch.object(matrix_alertbot.command, "HelpCommand", autospec=True)
async def test_message_help_not_in_reply_with_prefix(
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_command = Mock(spec=BaseCommand)
fake_command_create.return_value = fake_command
fake_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
@ -83,27 +81,22 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command_create.assert_called_with(
"help",
fake_command.assert_called_with(
self.fake_client,
self.fake_alertmanager,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"help",
self.fake_room,
fake_message_event.sender,
fake_message_event.event_id,
None,
)
fake_command.process.assert_called_once()
fake_command.return_value.process.assert_called_once()
@patch.object(matrix_alertbot.callback.CommandFactory, "create", autospec=True)
async def test_message_in_reply_with_prefix(
self, fake_command_create: Mock
) -> None:
@patch.object(matrix_alertbot.command, "HelpCommand", autospec=True)
async def test_message_help_in_reply_with_prefix(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_command = Mock(spec=BaseCommand)
fake_command_create.return_value = fake_command
fake_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
@ -119,18 +112,341 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that we attempted to execute the command
fake_command_create.assert_called_once_with(
"help",
fake_command.assert_called_once_with(
self.fake_client,
self.fake_alertmanager,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"help",
self.fake_room,
fake_message_event.sender,
fake_message_event.event_id,
)
fake_command.return_value.process.assert_called_once()
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_message_ack_not_in_reply_with_prefix(
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_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "!alert ack"
fake_message_event.source = {"content": {}}
# Pretend that we received a text message event
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_not_called()
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_message_ack_in_reply_with_prefix(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_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "!alert ack"
fake_message_event.source = {
"content": {
"m.relates_to": {"m.in_reply_to": {"event_id": "some alert event id"}}
}
}
# Pretend that we received a text message event
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_called_once_with(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"ack",
self.fake_room,
fake_message_event.sender,
fake_message_event.event_id,
"some alert event id",
)
fake_command.process.assert_called_once()
fake_command.return_value.process.assert_called_once()
@patch.object(matrix_alertbot.command, "UnackAlertCommand", autospec=True)
async def test_message_unack_not_in_reply_with_prefix(
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_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "!alert unack"
fake_message_event.source = {"content": {}}
# Pretend that we received a text message event
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_not_called()
@patch.object(matrix_alertbot.command, "UnackAlertCommand", autospec=True)
async def test_message_unack_in_reply_with_prefix(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_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "!alert unack"
fake_message_event.source = {
"content": {
"m.relates_to": {"m.in_reply_to": {"event_id": "some alert event id"}}
}
}
# Pretend that we received a text message event
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_called_once_with(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"unack",
self.fake_room,
fake_message_event.sender,
fake_message_event.event_id,
"some alert event id",
)
fake_command.return_value.process.assert_called_once()
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_reaction_to_existing_alert(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 = Mock(spec=nio.RoomMessageText)
fake_alert_event.event_id = "some alert event id"
fake_alert_event.sender = self.fake_config.user_id
fake_reaction_event = Mock(spec=nio.UnknownEvent)
fake_reaction_event.type = "m.reaction"
fake_reaction_event.event_id = "some event id"
fake_reaction_event.sender = "@some_other_fake_user:example.com"
fake_reaction_event.source = {
"content": {
"m.relates_to": {
"event_id": fake_alert_event.event_id,
"key": "🤫",
"rel_type": "m.annotation",
}
}
}
fake_event_response = Mock(spec=nio.RoomGetEventResponse)
fake_event_response.event = fake_alert_event
self.fake_client.room_get_event.return_value = make_awaitable(
fake_event_response
)
# Pretend that we received a text message event
await self.callbacks.unknown(self.fake_room, fake_reaction_event)
# Check that we attempted to execute the command
fake_command.assert_called_once_with(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"ack 12h",
self.fake_room,
fake_reaction_event.sender,
fake_reaction_event.event_id,
"some alert event id",
)
fake_command.return_value.process.assert_called_once()
self.fake_cache.set.assert_called_once_with(
fake_reaction_event.event_id,
fake_alert_event.event_id,
expire=self.fake_config.cache_expire_time,
)
self.fake_client.room_get_event.assert_called_once_with(
self.fake_room.room_id, fake_alert_event.event_id
)
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_reaction_to_unknown_event(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 = Mock(spec=nio.RoomMessageText)
fake_alert_event.event_id = "some alert event id"
fake_alert_event.sender = self.fake_config.user_id
fake_reaction_event = Mock(spec=nio.UnknownEvent)
fake_reaction_event.type = "m.reaction"
fake_reaction_event.event_id = "some event id"
fake_reaction_event.sender = "@some_other_fake_user:example.com"
fake_reaction_event.source = {
"content": {
"m.relates_to": {
"event_id": fake_alert_event.event_id,
"key": "🤫",
"rel_type": "m.annotation",
}
}
}
fake_event_response = Mock(spec=nio.RoomGetEventError)
self.fake_client.room_get_event.return_value = make_awaitable(
fake_event_response
)
# Pretend that we received a text message event
await self.callbacks.unknown(self.fake_room, fake_reaction_event)
# Check that we attempted to execute the command
fake_command.assert_not_called()
self.fake_cache.set.assert_not_called()
self.fake_client.room_get_event.assert_called_once_with(
self.fake_room.room_id, fake_alert_event.event_id
)
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_reaction_to_event_with_incorrect_sender(
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 = Mock(spec=nio.RoomMessageText)
fake_alert_event.event_id = "some alert event id"
fake_alert_event.sender = "@some_other_fake_user.example.com"
fake_reaction_event = Mock(spec=nio.UnknownEvent)
fake_reaction_event.type = "m.reaction"
fake_reaction_event.event_id = "some event id"
fake_reaction_event.sender = "@some_other_fake_user:example.com"
fake_reaction_event.source = {
"content": {
"m.relates_to": {
"event_id": fake_alert_event.event_id,
"key": "🤫",
"rel_type": "m.annotation",
}
}
}
fake_event_response = Mock(spec=nio.RoomGetEventResponse)
fake_event_response.event = fake_alert_event
self.fake_client.room_get_event.return_value = make_awaitable(
fake_event_response
)
# Pretend that we received a text message event
await self.callbacks.unknown(self.fake_room, fake_reaction_event)
# Check that we attempted to execute the command
fake_command.assert_not_called()
self.fake_cache.set.assert_not_called()
self.fake_client.room_get_event.assert_called_once_with(
self.fake_room.room_id, fake_alert_event.event_id
)
@patch.object(matrix_alertbot.command, "AckAlertCommand", autospec=True)
async def test_reaction_unknown(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 = Mock(spec=nio.RoomMessageText)
fake_alert_event.event_id = "some alert event id"
fake_reaction_event = Mock(spec=nio.UnknownEvent)
fake_reaction_event.type = "m.reaction"
fake_reaction_event.event_id = "some event id"
fake_reaction_event.sender = "@some_other_fake_user:example.com"
fake_reaction_event.source = {
"content": {
"m.relates_to": {
"event_id": fake_alert_event.event_id,
"key": "unknown",
"rel_type": "m.annotation",
}
}
}
# Pretend that we received a text message event
await self.callbacks.unknown(self.fake_room, fake_reaction_event)
# Check that we attempted to execute the command
fake_command.assert_not_called()
self.fake_client.room_get_event.assert_not_called()
@patch.object(matrix_alertbot.command, "UnackAlertCommand", autospec=True)
async def test_redaction_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"
self.fake_cache.__getitem__.return_value = "some alert event id"
self.fake_cache.__contains__.return_value = True
# 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_called_once_with(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"unack",
self.fake_room,
fake_redaction_event.sender,
fake_redaction_event.redacts,
"some 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.command, "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"
self.fake_cache.__contains__.return_value = False
# 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()
@patch.object(matrix_alertbot.callback.CommandFactory, "create", autospec=True)
async def test_unknown(self, fake_command_create: 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_command = Mock(spec=BaseCommand)
fake_command_create.return_value = fake_command
fake_reaction_event = Mock(spec=nio.UnknownEvent)
fake_reaction_event.type = "m.reaction"
fake_reaction_event.event_id = "some event id"
fake_reaction_event.sender = "@some_other_fake_user:example.com"
fake_reaction_event.source = {}
# Pretend that we received a text message event
await self.callbacks.unknown(self.fake_room, fake_reaction_event)
# Check that we attempted to execute the command
fake_command_create.assert_not_called()
if __name__ == "__main__":

View file

@ -25,8 +25,18 @@ from matrix_alertbot.matcher import AlertMatcher, AlertRegexMatcher
from tests.utils import make_awaitable
async def create_silence(
fingerprint: str, seconds: int, user: str, matchers: List[AlertMatcher]
) -> str:
if fingerprint == "fingerprint1":
return "silence1"
elif fingerprint == "fingerprint2":
return "silence2"
raise AlertmanagerError
async def create_silence_raise_alertmanager_error(
fingerprint: str, duration: str, user: str, matchers: List[AlertMatcher]
fingerprint: str, seconds: int, user: str, matchers: List[AlertMatcher]
) -> str:
if fingerprint == "fingerprint1":
raise AlertmanagerError
@ -34,7 +44,7 @@ async def create_silence_raise_alertmanager_error(
async def create_silence_raise_alert_not_found_error(
fingerprint: str, duration: str, user: str, matchers: List[AlertMatcher]
fingerprint: str, seconds: int, user: str, matchers: List[AlertMatcher]
) -> str:
if fingerprint == "fingerprint1":
raise AlertNotFoundError
@ -74,6 +84,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_alertmanager = Mock(spec=AlertmanagerClient)
self.fake_alertmanager.delete_silences.return_value = self.fake_silences
self.fake_alertmanager.create_silence.side_effect = create_silence
# Create a fake room to play with
self.fake_room = Mock(spec=nio.MatrixRoom)
@ -205,6 +216,12 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room.room_id,
"Created 2 silences with a duration of 1d.",
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_called_once_with(
"".join(self.fake_fingerprints) + "86400",
tuple(self.fake_silences),
expire=86400,
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_without_duration_and_with_matchers(
@ -247,6 +264,14 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room.room_id,
"Created 2 silences with a duration of 1d.",
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_called_once_with(
"".join(self.fake_fingerprints)
+ "86400"
+ "".join(str(matcher) for matcher in matchers),
tuple(self.fake_silences),
expire=86400,
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_duration_and_without_matchers(
@ -280,6 +305,12 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room.room_id,
"Created 2 silences with a duration of 1w 3d.",
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_called_once_with(
"".join(self.fake_fingerprints) + "864000",
tuple(self.fake_silences),
expire=864000,
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_duration_and_matchers(
@ -322,6 +353,14 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room.room_id,
"Created 2 silences with a duration of 1w 3d.",
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_called_once_with(
"".join(self.fake_fingerprints)
+ "864000"
+ "".join(str(matcher) for matcher in matchers),
tuple(self.fake_silences),
expire=864000,
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_raise_alertmanager_error(
@ -359,6 +398,12 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room.room_id,
"Created 1 silences with a duration of 1d.",
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_called_once_with(
"".join(self.fake_fingerprints) + "86400",
("silence1",),
expire=86400,
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_raise_alert_not_found_error(
@ -405,6 +450,12 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
),
]
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_called_once_with(
"".join(self.fake_fingerprints) + "86400",
("silence1",),
expire=86400,
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_invalid_duration(
@ -434,6 +485,8 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room.room_id,
"I tried really hard, but I can't convert the duration 'invalid duration' to a number of seconds.",
)
self.fake_cache.__getitem__.assert_not_called()
self.fake_cache.set.assert_not_called()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_event_not_found_in_cache(
@ -461,6 +514,8 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_not_called()
fake_send_text_to_room.assert_not_called()
self.fake_cache.__getitem__.assert_not_called()
self.fake_cache.set.assert_not_called()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_without_matchers(self, fake_send_text_to_room: Mock) -> None:
@ -487,6 +542,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
fake_send_text_to_room.assert_called_with(
self.fake_client, self.fake_room.room_id, "Removed 4 silences."
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_with_matchers(self, fake_send_text_to_room: Mock) -> None:
@ -518,6 +574,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
fake_send_text_to_room.assert_called_with(
self.fake_client, self.fake_room.room_id, "Removed 4 silences."
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_silence_raise_alertmanager_error(
@ -550,6 +607,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
fake_send_text_to_room.assert_called_with(
self.fake_client, self.fake_room.room_id, "Removed 1 silences."
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_raise_silence_not_found_error(
@ -593,6 +651,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
),
]
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_with_event_not_found_in_cache(
@ -620,6 +679,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_not_called()
fake_send_text_to_room.assert_not_called()
self.fake_cache.__getitem__.assert_not_called()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_without_topic(self, fake_send_text_to_room: Mock) -> None: