matrix-alertbot/tests/test_command.py

744 lines
26 KiB
Python
Raw Normal View History

import unittest
from typing import Dict, Optional
from unittest.mock import MagicMock, Mock, call, patch
import nio
from diskcache import Cache
import matrix_alertbot.callback
import matrix_alertbot.command
from matrix_alertbot.alertmanager import AlertmanagerClient
from matrix_alertbot.command import (
AckAlertCommand,
CommandFactory,
HelpCommand,
UnackAlertCommand,
UnknownCommand,
)
from matrix_alertbot.errors import (
AlertmanagerError,
AlertNotFoundError,
SilenceNotFoundError,
)
def cache_get_item(key: str) -> str:
return {
"some alert event id": "fingerprint1",
"fingerprint1": "silence1",
}[key]
async def create_silence(
2022-08-08 11:26:08 +02:00
fingerprint: str,
user: str,
duration_seconds: Optional[int] = None,
*,
force: bool = True,
) -> str:
if fingerprint == "fingerprint1":
return "silence1"
elif fingerprint == "fingerprint2":
return "silence2"
raise AlertmanagerError
2022-07-09 09:56:28 +02:00
async def create_silence_raise_alertmanager_error(
2022-08-08 11:26:08 +02:00
fingerprint: str,
user: str,
duration_seconds: Optional[int] = None,
*,
force: bool = True,
2022-07-09 09:56:28 +02:00
) -> str:
if fingerprint == "fingerprint1":
raise AlertmanagerError
2022-07-10 02:40:04 +02:00
return "silence1"
2022-07-09 09:56:28 +02:00
async def create_silence_raise_alert_not_found_error(
2022-08-08 11:26:08 +02:00
fingerprint: str,
user: str,
duration_seconds: Optional[int] = None,
*,
force: bool = True,
) -> str:
if fingerprint == "fingerprint1":
raise AlertNotFoundError
return "silence1"
async def delete_silence_raise_alertmanager_error(silence_id: str) -> None:
if silence_id == "silence1":
2022-07-09 09:56:28 +02:00
raise AlertmanagerError
async def delete_silence_raise_silence_not_found_error(silence_id: str) -> None:
if silence_id == "silence1":
raise SilenceNotFoundError
class CommandTestCase(unittest.IsolatedAsyncioTestCase):
def setUp(self) -> None:
# Create a Command object and give it some Mock'd objects to use
2022-08-08 00:28:36 +02:00
self.fake_matrix_client = Mock(spec=nio.AsyncClient)
self.fake_matrix_client.user = "@fake_user:example.com"
self.fake_cache = MagicMock(spec=Cache)
self.fake_cache.__getitem__.side_effect = cache_get_item
2022-07-10 15:11:25 +02:00
self.fake_cache.__contains__.return_value = True
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client = Mock(spec=AlertmanagerClient)
2022-08-08 11:26:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.side_effect = (
create_silence
)
# Create a fake room to play with
self.fake_room = Mock(spec=nio.MatrixRoom)
self.fake_room.room_id = "!abcdefg:example.com"
self.fake_room.display_name = "Fake Room"
self.fake_room.user_name.side_effect = lambda x: x
2022-07-10 14:06:36 +02:00
self.fake_event_id = "some event id"
self.fake_sender = "@some_other_fake_user:example.com"
self.fake_alert_event_id = "some alert event id"
2022-07-09 09:56:28 +02:00
# We don't spec config, as it doesn't currently have well defined attributes
self.fake_config = Mock()
self.fake_config.allowed_rooms = [self.fake_room.room_id]
self.fake_config.command_prefix = "!alert "
@patch.object(matrix_alertbot.command.AckAlertCommand, "process")
async def test_process_ack_command(self, fake_ack: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = CommandFactory.create(
"ack",
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
2022-07-10 14:09:00 +02:00
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
2022-07-09 09:56:28 +02:00
# Check that we attempted to process the command
fake_ack.assert_called_once()
@patch.object(matrix_alertbot.command.AckAlertCommand, "process")
async def test_process_ack_with_duration_command(self, fake_ack: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = CommandFactory.create(
"ack 1w 3d",
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to process the command
fake_ack.assert_called_once()
@patch.object(matrix_alertbot.command.UnackAlertCommand, "process")
async def test_process_unack_command(self, fake_unack: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
for unack_cmd in ("unack", "nack"):
command = CommandFactory.create(
unack_cmd,
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
2022-07-10 14:09:00 +02:00
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to process the command
fake_unack.assert_has_calls([call(), call()])
@patch.object(matrix_alertbot.command.HelpCommand, "process")
async def test_process_help_command(self, fake_help: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = CommandFactory.create(
"help",
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
2022-07-10 14:09:00 +02:00
self.fake_event_id,
)
await command.process()
# Check that we attempted to process the command
fake_help.assert_called_once()
@patch.object(matrix_alertbot.command.UnknownCommand, "process")
async def test_process_unknown_command(self, fake_unknown: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = CommandFactory.create(
"",
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
2022-07-10 14:09:00 +02:00
self.fake_event_id,
)
await command.process()
# Check that we attempted to process the command
fake_unknown.assert_called_once()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_without_duration(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
2022-08-08 01:44:08 +02:00
fake_cache_dict = {self.fake_alert_event_id: "fingerprint1"}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
2022-07-10 02:40:04 +02:00
# Check that we attempted to create silences
2022-08-08 01:44:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.assert_called_once_with(
2022-08-08 11:26:08 +02:00
"fingerprint1", self.fake_sender, None, force=True
2022-07-10 02:40:04 +02:00
)
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
2022-07-10 02:40:04 +02:00
self.fake_room.room_id,
"Created silence with ID silence1.",
2022-07-10 02:40:04 +02:00
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
2022-08-08 00:28:36 +02:00
self.fake_cache.set.assert_called_once_with(
"some event id", "fingerprint1", expire=None
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_duration(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
2022-08-08 01:44:08 +02:00
fake_cache_dict = {self.fake_alert_event_id: "fingerprint1"}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
2022-07-10 02:40:04 +02:00
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
("1w", "3d"),
2022-07-10 02:40:04 +02:00
)
await command.process()
2022-07-10 02:40:04 +02:00
# Check that we attempted to create silences
2022-08-08 01:44:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.assert_called_once_with(
2022-08-08 11:26:08 +02:00
"fingerprint1", self.fake_sender, 864000, force=True
2022-07-10 02:40:04 +02:00
)
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
2022-07-10 02:40:04 +02:00
self.fake_room.room_id,
"Created silence with ID silence1.",
2022-07-10 02:40:04 +02:00
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
2022-08-08 00:28:36 +02:00
self.fake_cache.set.assert_called_once_with(
"some event id", "fingerprint1", expire=864000
)
2022-07-09 09:56:28 +02:00
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_raise_alertmanager_error(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict = {
self.fake_alert_event_id: "fingerprint1",
}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
2022-08-08 01:44:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.side_effect = (
create_silence_raise_alertmanager_error
)
2022-07-09 09:56:28 +02:00
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
2022-07-09 09:56:28 +02:00
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
2022-07-09 09:56:28 +02:00
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
2022-07-09 09:56:28 +02:00
)
await command.process()
2022-07-09 09:56:28 +02:00
# Check that we attempted to create silences
2022-08-08 01:44:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.assert_called_once_with(
2022-08-08 11:26:08 +02:00
"fingerprint1", self.fake_sender, None, force=True
2022-07-09 09:56:28 +02:00
)
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
2022-07-09 09:56:28 +02:00
self.fake_room.room_id,
2022-07-28 10:35:11 +02:00
"Sorry, I couldn't create silence for alert with fingerprint fingerprint1 because something went wrong with Alertmanager: ",
2022-07-09 09:56:28 +02:00
)
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
self.fake_cache.set.assert_not_called()
2022-07-09 09:56:28 +02:00
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_raise_alert_not_found_error(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict = {
self.fake_alert_event_id: "fingerprint1",
}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
2022-08-08 01:44:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.side_effect = (
create_silence_raise_alert_not_found_error
)
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to create silences
2022-08-08 01:44:08 +02:00
self.fake_alertmanager_client.create_or_update_silence.assert_called_once_with(
2022-08-08 11:26:08 +02:00
"fingerprint1", self.fake_sender, None, force=True
)
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
2022-07-28 10:35:11 +02:00
"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.set.assert_not_called()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_invalid_duration(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
("invalid duration",),
)
await command.process()
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.create_silence.assert_not_called()
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
2022-07-12 00:29:46 +02:00
"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.get.assert_not_called()
self.fake_cache.set.assert_not_called()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_negative_duration(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
("-1d",),
)
await command.process()
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.create_silence.assert_not_called()
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
"I can't create a silence with a negative duration!",
)
self.fake_cache.__getitem__.assert_not_called()
self.fake_cache.get.assert_not_called()
self.fake_cache.set.assert_not_called()
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_ack_with_alert_event_not_found_in_cache(
2022-07-10 12:51:49 +02:00
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict: Dict = {}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
self.fake_cache.get.side_effect = fake_cache_dict.get
command = AckAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.create_silence.assert_not_called()
fake_send_text_to_room.assert_not_called()
self.fake_cache.__getitem__.assert_called_once_with("some alert event id")
self.fake_cache.get.assert_not_called()
self.fake_cache.set.assert_not_called()
2022-07-10 12:51:49 +02:00
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack(self, fake_send_text_to_room: Mock) -> None:
2022-07-10 12:51:49 +02:00
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict = {
self.fake_alert_event_id: "fingerprint1",
"fingerprint1": "silence1",
}
2022-07-10 12:51:49 +02:00
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
2022-07-10 12:51:49 +02:00
command = UnackAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
2022-07-10 12:51:49 +02:00
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
2022-07-10 12:51:49 +02:00
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
2022-07-10 12:51:49 +02:00
)
await command.process()
2022-07-10 12:51:49 +02:00
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.assert_called_once_with("silence1")
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
"Removed silence with ID silence1.",
2022-07-09 09:56:28 +02:00
)
self.fake_cache.__getitem__.assert_has_calls(
[call(self.fake_alert_event_id), call("fingerprint1")]
2022-07-09 09:56:28 +02:00
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_silence_raise_alertmanager_error(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict = {
self.fake_alert_event_id: "fingerprint1",
"fingerprint1": "silence1",
}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
2022-07-09 09:56:28 +02:00
command = UnackAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
2022-07-09 09:56:28 +02:00
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
2022-07-09 09:56:28 +02:00
self.fake_config,
self.fake_room,
2022-07-10 14:06:36 +02:00
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
2022-07-09 09:56:28 +02:00
)
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.side_effect = (
2022-07-09 09:56:28 +02:00
delete_silence_raise_alertmanager_error
)
await command.process()
2022-07-09 09:56:28 +02:00
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.assert_called_once_with("silence1")
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
2022-07-28 10:35:11 +02:00
"Sorry, I couldn't remove silence for alert with fingerprint fingerprint1 because something went wrong with Alertmanager: ",
2022-07-09 09:56:28 +02:00
)
self.fake_cache.__getitem__.assert_has_calls(
[call(self.fake_alert_event_id), call("fingerprint1")]
2022-07-09 09:56:28 +02:00
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_raise_silence_not_found_error(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict = {
self.fake_alert_event_id: "fingerprint1",
"fingerprint1": "silence1",
}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
command = UnackAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.side_effect = (
delete_silence_raise_silence_not_found_error
)
await command.process()
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.assert_called_once_with("silence1")
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
2022-07-28 10:35:11 +02:00
"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")]
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unack_with_event_not_found_in_cache(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict: Dict = {}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
command = UnackAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.assert_not_called()
fake_send_text_to_room.assert_not_called()
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_silence_not_found_in_cache(
self, fake_send_text_to_room: Mock
) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
fake_cache_dict = {self.fake_alert_event_id: "fingerprint1"}
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
command = UnackAlertCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to create silences
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client.delete_silence.assert_not_called()
fake_send_text_to_room.assert_not_called()
self.fake_cache.__getitem__.assert_has_calls(
[call(self.fake_alert_event_id), call("fingerprint1")]
)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_without_topic(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = HelpCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
)
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args
self.assertIn("help commands", text)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_with_rules_topic(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = HelpCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
("rules",),
)
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args
self.assertIn("rules!", text)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_with_commands_topic(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = HelpCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
("commands",),
)
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args
self.assertIn("Available commands", text)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_help_with_unknown_topic(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = HelpCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
("unknown",),
)
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
_, _, text = fake_send_text_to_room.call_args.args
self.assertEqual("Unknown help topic!", text)
@patch.object(matrix_alertbot.command, "send_text_to_room")
async def test_unknown_command(self, fake_send_text_to_room: Mock) -> None:
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = UnknownCommand(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_cache,
2022-08-08 00:28:36 +02:00
self.fake_alertmanager_client,
self.fake_config,
self.fake_room,
self.fake_sender,
self.fake_event_id,
)
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once_with(
2022-08-08 00:28:36 +02:00
self.fake_matrix_client,
self.fake_room.room_id,
"Unknown command. Try the 'help' command for more information.",
)
if __name__ == "__main__":
unittest.main()