2022-07-08 21:11:25 +02:00
|
|
|
import unittest
|
2022-07-26 19:33:04 +02:00
|
|
|
from typing import Dict, Optional
|
2022-07-08 21:11:25 +02:00
|
|
|
from unittest.mock import MagicMock, Mock, call, patch
|
|
|
|
|
|
|
|
import nio
|
|
|
|
from diskcache import Cache
|
|
|
|
|
|
|
|
import matrix_alertbot.callback
|
2022-07-16 23:08:12 +02:00
|
|
|
import matrix_alertbot.command
|
2022-07-08 21:11:25 +02:00
|
|
|
from matrix_alertbot.alertmanager import AlertmanagerClient
|
2022-07-16 23:08:12 +02:00
|
|
|
from matrix_alertbot.command import (
|
|
|
|
AckAlertCommand,
|
|
|
|
CommandFactory,
|
|
|
|
HelpCommand,
|
|
|
|
UnackAlertCommand,
|
|
|
|
UnknownCommand,
|
|
|
|
)
|
2022-07-12 00:27:17 +02:00
|
|
|
from matrix_alertbot.errors import (
|
|
|
|
AlertmanagerError,
|
|
|
|
AlertNotFoundError,
|
|
|
|
SilenceNotFoundError,
|
|
|
|
)
|
2022-07-08 21:11:25 +02:00
|
|
|
|
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
def cache_get_item(key: str) -> str:
|
|
|
|
return {
|
|
|
|
"some alert event id": "fingerprint1",
|
|
|
|
"fingerprint1": "silence1",
|
|
|
|
}[key]
|
|
|
|
|
|
|
|
|
2022-07-17 02:35:15 +02:00
|
|
|
async def create_silence(
|
2022-08-08 11:26:08 +02:00
|
|
|
fingerprint: str,
|
|
|
|
user: str,
|
|
|
|
duration_seconds: Optional[int] = None,
|
|
|
|
*,
|
|
|
|
force: bool = True,
|
2022-07-17 02:35:15 +02:00
|
|
|
) -> 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
|
|
|
|
|
|
|
|
2022-07-12 00:27:17 +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,
|
2022-07-12 00:27:17 +02:00
|
|
|
) -> str:
|
|
|
|
if fingerprint == "fingerprint1":
|
|
|
|
raise AlertNotFoundError
|
|
|
|
return "silence1"
|
|
|
|
|
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
async def delete_silence_raise_silence_not_found_error(silence_id: str) -> None:
|
|
|
|
if silence_id == "silence1":
|
2022-07-12 00:27:17 +02:00
|
|
|
raise SilenceNotFoundError
|
|
|
|
|
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
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"
|
2022-07-08 21:11:25 +02:00
|
|
|
|
|
|
|
self.fake_cache = MagicMock(spec=Cache)
|
2022-07-26 19:33:04 +02:00
|
|
|
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-07-08 21:11:25 +02:00
|
|
|
|
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
|
|
|
|
)
|
2022-07-08 21:11:25 +02:00
|
|
|
|
|
|
|
# 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"
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id = "some alert event id"
|
2022-07-09 09:56:28 +02:00
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
# We don't spec config, as it doesn't currently have well defined attributes
|
|
|
|
self.fake_config = Mock()
|
2022-07-28 14:37:23 +02:00
|
|
|
self.fake_config.allowed_rooms = [self.fake_room.room_id]
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_config.command_prefix = "!alert "
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
@patch.object(matrix_alertbot.command.AckAlertCommand, "process")
|
2022-07-08 21:11:25 +02:00
|
|
|
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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = CommandFactory.create(
|
|
|
|
"ack",
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
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,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-08 21:11:25 +02:00
|
|
|
)
|
|
|
|
await command.process()
|
|
|
|
|
2022-07-09 09:56:28 +02:00
|
|
|
# Check that we attempted to process the command
|
|
|
|
fake_ack.assert_called_once()
|
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
@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,
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-26 19:33:04 +02:00
|
|
|
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()
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
@patch.object(matrix_alertbot.command.UnackAlertCommand, "process")
|
2022-07-08 21:11:25 +02:00
|
|
|
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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
for unack_cmd in ("unack", "nack"):
|
|
|
|
command = CommandFactory.create(
|
|
|
|
unack_cmd,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
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,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-08 21:11:25 +02:00
|
|
|
)
|
|
|
|
await command.process()
|
|
|
|
|
|
|
|
# Check that we attempted to process the command
|
|
|
|
fake_unack.assert_has_calls([call(), call()])
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
@patch.object(matrix_alertbot.command.HelpCommand, "process")
|
2022-07-08 21:11:25 +02:00
|
|
|
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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = CommandFactory.create(
|
|
|
|
"help",
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
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,
|
2022-07-08 21:11:25 +02:00
|
|
|
)
|
|
|
|
await command.process()
|
|
|
|
|
|
|
|
# Check that we attempted to process the command
|
|
|
|
fake_help.assert_called_once()
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
@patch.object(matrix_alertbot.command.UnknownCommand, "process")
|
2022-07-08 21:11:25 +02:00
|
|
|
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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = CommandFactory.create(
|
|
|
|
"",
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
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,
|
2022-07-08 21:11:25 +02:00
|
|
|
)
|
|
|
|
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")
|
2022-07-26 19:33:04 +02:00
|
|
|
async def test_ack_without_duration(self, fake_send_text_to_room: Mock) -> None:
|
2022-07-08 21:11:25 +02:00
|
|
|
"""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"}
|
2022-07-26 19:33:04 +02:00
|
|
|
|
|
|
|
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
|
2022-07-08 21:11:25 +02:00
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = AckAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
2022-07-10 14:06:36 +02:00
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-08 21:11:25 +02:00
|
|
|
)
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-08 21:11:25 +02:00
|
|
|
|
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,
|
2022-07-26 19:33:04 +02:00
|
|
|
"Created silence with ID silence1.",
|
2022-07-10 02:40:04 +02:00
|
|
|
)
|
2022-07-17 02:35:15 +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
|
2022-07-08 21:11:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
2022-07-26 19:33:04 +02:00
|
|
|
async def test_ack_with_duration(self, fake_send_text_to_room: Mock) -> None:
|
2022-07-08 21:11:25 +02:00
|
|
|
"""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"}
|
2022-07-26 19:33:04 +02:00
|
|
|
|
|
|
|
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
|
2022-07-08 21:11:25 +02:00
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = AckAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-08 21:11:25 +02:00
|
|
|
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,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-26 19:33:04 +02:00
|
|
|
("1w", "3d"),
|
2022-07-10 02:40:04 +02:00
|
|
|
)
|
2022-07-16 23:08:12 +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,
|
2022-07-26 19:33:04 +02:00
|
|
|
"Created silence with ID silence1.",
|
2022-07-10 02:40:04 +02:00
|
|
|
)
|
2022-07-17 02:35:15 +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-08 21:11:25 +02:00
|
|
|
)
|
|
|
|
|
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
|
2022-07-26 19:33:04 +02:00
|
|
|
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
|
|
|
|
2022-07-16 23:08:12 +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,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-09 09:56:28 +02:00
|
|
|
)
|
2022-07-16 23:08:12 +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
|
|
|
)
|
2022-07-17 02:35:15 +02:00
|
|
|
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.set.assert_not_called()
|
2022-07-09 09:56:28 +02:00
|
|
|
|
2022-07-12 00:27:17 +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
|
2022-07-26 19:33:04 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-12 00:27:17 +02:00
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = AckAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-12 00:27:17 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-12 00:27:17 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-12 00:27:17 +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-12 00:27:17 +02:00
|
|
|
)
|
2022-07-26 19:33: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-26 19:33:04 +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: ",
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
2022-07-17 02:35:15 +02:00
|
|
|
self.fake_cache.__getitem__.assert_called_once_with(self.fake_alert_event_id)
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.set.assert_not_called()
|
2022-07-12 00:27:17 +02:00
|
|
|
|
|
|
|
@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
|
2022-07-16 23:08:12 +02:00
|
|
|
command = AckAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-12 00:27:17 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-12 00:27:17 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-26 19:33:04 +02:00
|
|
|
("invalid duration",),
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-12 00:27:17 +02:00
|
|
|
|
|
|
|
# Check that we attempted to create silences
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client.create_silence.assert_not_called()
|
2022-07-12 00:27:17 +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-12 00:27:17 +02:00
|
|
|
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.",
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
2022-07-17 02:35:15 +02:00
|
|
|
self.fake_cache.__getitem__.assert_not_called()
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.get.assert_not_called()
|
2022-07-17 02:35:15 +02:00
|
|
|
self.fake_cache.set.assert_not_called()
|
2022-07-12 00:27:17 +02:00
|
|
|
|
2022-07-28 14:37:23 +02:00
|
|
|
@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,
|
2022-07-28 14:37:23 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-28 14:37:23 +02:00
|
|
|
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()
|
2022-07-28 14:37:23 +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-28 14:37:23 +02:00
|
|
|
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()
|
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
2022-07-26 19:33:04 +02:00
|
|
|
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:
|
2022-07-08 21:11:25 +02:00
|
|
|
"""Tests the callback for InviteMemberEvents"""
|
|
|
|
# Tests that the bot attempts to join a room after being invited to it
|
2022-07-26 19:33:04 +02:00
|
|
|
fake_cache_dict: Dict = {}
|
2022-07-08 21:11:25 +02:00
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
|
|
|
|
self.fake_cache.get.side_effect = fake_cache_dict.get
|
2022-07-11 23:18:57 +02:00
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = AckAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-11 23:18:57 +02:00
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# Check that we attempted to create silences
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client.create_silence.assert_not_called()
|
2022-07-11 23:18:57 +02:00
|
|
|
fake_send_text_to_room.assert_not_called()
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.__getitem__.assert_called_once_with("some alert event id")
|
|
|
|
self.fake_cache.get.assert_not_called()
|
2022-07-17 02:35:15 +02:00
|
|
|
self.fake_cache.set.assert_not_called()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
2022-07-10 12:51:49 +02:00
|
|
|
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
2022-07-26 19:33:04 +02:00
|
|
|
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
|
2022-07-26 19:33:04 +02:00
|
|
|
fake_cache_dict = {
|
|
|
|
self.fake_alert_event_id: "fingerprint1",
|
|
|
|
"fingerprint1": "silence1",
|
|
|
|
}
|
2022-07-10 12:51:49 +02:00
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
|
2022-07-10 12:51:49 +02:00
|
|
|
|
2022-07-16 23:08:12 +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,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-10 12:51:49 +02:00
|
|
|
)
|
2022-07-16 23:08:12 +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")
|
2022-07-26 19:33: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-26 19:33:04 +02:00
|
|
|
self.fake_room.room_id,
|
|
|
|
"Removed silence with ID silence1.",
|
2022-07-09 09:56:28 +02:00
|
|
|
)
|
2022-07-26 19:33:04 +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
|
2022-07-26 19:33:04 +02:00
|
|
|
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
|
|
|
|
2022-07-16 23:08:12 +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,
|
2022-07-16 23:08:12 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-16 23:08:12 +02:00
|
|
|
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")
|
2022-07-26 19:33: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-26 19:33:04 +02:00
|
|
|
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
|
|
|
)
|
2022-07-26 19:33:04 +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
|
|
|
)
|
2022-07-08 21:11:25 +02:00
|
|
|
|
2022-07-12 00:27:17 +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
|
2022-07-26 19:33:04 +02:00
|
|
|
fake_cache_dict = {
|
|
|
|
self.fake_alert_event_id: "fingerprint1",
|
|
|
|
"fingerprint1": "silence1",
|
|
|
|
}
|
|
|
|
|
|
|
|
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
|
2022-07-12 00:27:17 +02:00
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = UnackAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-12 00:27:17 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-12 00:27:17 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
|
|
|
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client.delete_silence.side_effect = (
|
2022-07-12 00:27:17 +02:00
|
|
|
delete_silence_raise_silence_not_found_error
|
|
|
|
)
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-12 00:27:17 +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")
|
2022-07-26 19:33: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-26 19:33:04 +02:00
|
|
|
self.fake_room.room_id,
|
2022-07-28 10:35:11 +02:00
|
|
|
"Sorry, I couldn't remove silence for alert with fingerprint fingerprint1: ",
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.__getitem__.assert_has_calls(
|
|
|
|
[call(self.fake_alert_event_id), call("fingerprint1")]
|
2022-07-12 00:27:17 +02:00
|
|
|
)
|
|
|
|
|
2022-07-11 23:18:57 +02:00
|
|
|
@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
|
2022-07-26 19:33:04 +02:00
|
|
|
fake_cache_dict: Dict = {}
|
2022-07-11 23:18:57 +02:00
|
|
|
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache.__getitem__.side_effect = fake_cache_dict.__getitem__
|
2022-07-11 23:18:57 +02:00
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = UnackAlertCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-16 23:08:12 +02:00
|
|
|
self.fake_alert_event_id,
|
2022-07-11 23:18:57 +02:00
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# Check that we attempted to create silences
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client.delete_silence.assert_not_called()
|
2022-07-11 23:18:57 +02:00
|
|
|
fake_send_text_to_room.assert_not_called()
|
2022-07-26 19:33:04 +02:00
|
|
|
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,
|
2022-07-26 19:33:04 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-26 19:33:04 +02:00
|
|
|
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()
|
2022-07-26 19:33:04 +02:00
|
|
|
fake_send_text_to_room.assert_not_called()
|
|
|
|
self.fake_cache.__getitem__.assert_has_calls(
|
|
|
|
[call(self.fake_alert_event_id), call("fingerprint1")]
|
|
|
|
)
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = HelpCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = HelpCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-26 19:33:04 +02:00
|
|
|
("rules",),
|
2022-07-11 23:18:57 +02:00
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = HelpCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-26 19:33:04 +02:00
|
|
|
("commands",),
|
2022-07-11 23:18:57 +02:00
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = HelpCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
2022-07-26 19:33:04 +02:00
|
|
|
("unknown",),
|
2022-07-11 23:18:57 +02:00
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
command = UnknownCommand(
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_matrix_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_cache,
|
2022-08-08 00:28:36 +02:00
|
|
|
self.fake_alertmanager_client,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_config,
|
|
|
|
self.fake_room,
|
|
|
|
self.fake_sender,
|
|
|
|
self.fake_event_id,
|
|
|
|
)
|
|
|
|
|
2022-07-16 23:08:12 +02:00
|
|
|
await command.process()
|
2022-07-11 23:18:57 +02:00
|
|
|
|
|
|
|
# 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,
|
2022-07-11 23:18:57 +02:00
|
|
|
self.fake_room.room_id,
|
2022-07-26 19:33:04 +02:00
|
|
|
"Unknown command. Try the 'help' command for more information.",
|
2022-07-11 23:18:57 +02:00
|
|
|
)
|
|
|
|
|
2022-07-08 21:11:25 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|