2021-01-11 04:00:33 +01:00
|
|
|
import unittest
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
import nio
|
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
from matrix_alertbot.alertmanager import AlertmanagerClient
|
2022-06-13 20:55:01 +02:00
|
|
|
from matrix_alertbot.callbacks import Callbacks
|
2021-01-11 04:00:33 +01:00
|
|
|
|
|
|
|
from tests.utils import make_awaitable, run_coroutine
|
|
|
|
|
|
|
|
|
|
|
|
class CallbacksTestCase(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
# Create a Callbacks object and give it some Mock'd objects to use
|
|
|
|
self.fake_client = Mock(spec=nio.AsyncClient)
|
|
|
|
self.fake_client.user = "@fake_user:example.com"
|
|
|
|
|
2022-07-04 01:03:24 +02:00
|
|
|
self.fake_alertmanager = Mock(spec=AlertmanagerClient)
|
2021-01-11 04:00:33 +01:00
|
|
|
|
|
|
|
# We don't spec config, as it doesn't currently have well defined attributes
|
|
|
|
self.fake_config = Mock()
|
|
|
|
|
|
|
|
self.callbacks = Callbacks(
|
2022-07-06 01:19:44 +02:00
|
|
|
self.fake_client, self.fake_alertmanager, self.fake_config
|
2021-01-11 04:00:33 +01:00
|
|
|
)
|
|
|
|
|
2022-06-14 23:37:54 +02:00
|
|
|
def test_invite(self) -> None:
|
2021-01-11 04:00:33 +01:00
|
|
|
"""Tests the callback for InviteMemberEvents"""
|
|
|
|
# Tests that the bot attempts to join a room after being invited to it
|
|
|
|
|
|
|
|
# Create a fake room and invite event to call the 'invite' callback with
|
|
|
|
fake_room = Mock(spec=nio.MatrixRoom)
|
|
|
|
fake_room_id = "!abcdefg:example.com"
|
|
|
|
fake_room.room_id = fake_room_id
|
|
|
|
|
|
|
|
fake_invite_event = Mock(spec=nio.InviteMemberEvent)
|
|
|
|
fake_invite_event.sender = "@some_other_fake_user:example.com"
|
|
|
|
|
|
|
|
# Pretend that attempting to join a room is always successful
|
|
|
|
self.fake_client.join.return_value = make_awaitable(None)
|
|
|
|
|
|
|
|
# Pretend that we received an invite event
|
|
|
|
run_coroutine(self.callbacks.invite(fake_room, fake_invite_event))
|
|
|
|
|
|
|
|
# Check that we attempted to join the room
|
|
|
|
self.fake_client.join.assert_called_once_with(fake_room_id)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|