|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +from slackbot.management.commands.run_bot import Command |
| 4 | +from slack_sdk.socket_mode.request import SocketModeRequest |
| 5 | + |
| 6 | + |
| 7 | +class TestSlackBotCommand(unittest.TestCase): |
| 8 | + def setUp(self): |
| 9 | + self.command = Command() |
| 10 | + self.command.web = MagicMock() |
| 11 | + self.command.client = MagicMock() |
| 12 | + self.command.my_id = "U123456" |
| 13 | + self.command.my_id_match = "<@U123456>" |
| 14 | + self.command.processors = [] |
| 15 | + |
| 16 | + @patch("slackbot.management.commands.run_bot.close_old_connections") |
| 17 | + def test_handle_message(self, mock_close_old_connections): |
| 18 | + payload = { |
| 19 | + "event": { |
| 20 | + "channel": "C12345", |
| 21 | + "user": "U67890", |
| 22 | + "text": "Hello, bot!", |
| 23 | + "ts": "123456.789", |
| 24 | + } |
| 25 | + } |
| 26 | + result = self.command.handle_message(**payload) |
| 27 | + self.assertFalse(result) # No processors, so should return False |
| 28 | + |
| 29 | + @patch("threading.Event.wait", return_value=None) |
| 30 | + @patch("slackbot.management.commands.run_bot.SocketModeClient") |
| 31 | + @patch("slackbot.management.commands.run_bot.settings") |
| 32 | + def test_handle(self, mock_settings, mock_socket_client, mock_event_wait): |
| 33 | + mock_settings.SLACKBOT_BOT_TOKEN = "xoxb-123" |
| 34 | + mock_settings.SLACKBOT_APP_TOKEN = "xapp-123" |
| 35 | + |
| 36 | + mock_client_instance = mock_socket_client.return_value |
| 37 | + self.command.set_up = MagicMock() |
| 38 | + self.command.handle() |
| 39 | + |
| 40 | + self.command.set_up.assert_called_once() |
| 41 | + mock_client_instance.connect.assert_called_once() |
| 42 | + |
| 43 | + def test_handle_reaction(self): |
| 44 | + payload = { |
| 45 | + "event": { |
| 46 | + "type": "reaction_added", |
| 47 | + "item": {"channel": "C12345"}, |
| 48 | + "user": "U67890", |
| 49 | + "event_ts": "123456.789", |
| 50 | + } |
| 51 | + } |
| 52 | + result = self.command.handle_reaction(**payload) |
| 53 | + self.assertFalse(result) # No processors, so should return False |
| 54 | + |
| 55 | + def test_process_event_message(self): |
| 56 | + request = SocketModeRequest( |
| 57 | + type="events_api", |
| 58 | + envelope_id="envelope123", |
| 59 | + payload={"event": {"type": "message", "subtype": None, "text": "Hello!"}}, |
| 60 | + ) |
| 61 | + |
| 62 | + self.command.handle_message = MagicMock(return_value=True) |
| 63 | + result = self.command.process(self.command.client, request) |
| 64 | + |
| 65 | + self.command.handle_message.assert_called_once() |
| 66 | + self.assertTrue(result) |
| 67 | + |
| 68 | + def test_post_message(self): |
| 69 | + self.command.post_message(channel="C12345", text="Hello!") |
| 70 | + self.command.web.chat_postMessage.assert_called_once_with(channel="C12345", text="Hello!", as_user=1) |
| 71 | + |
| 72 | + def test_post_ephemeral(self): |
| 73 | + self.command.post_ephemeral(channel="C12345", text="Hello!", user="U67890") |
| 74 | + self.command.web.chat_postEphemeral.assert_called_once_with( |
| 75 | + channel="C12345", text="Hello!", user="U67890", as_user=True |
| 76 | + ) |
| 77 | + |
| 78 | + @patch( |
| 79 | + "slackbot.management.commands.run_bot.unicodedata.normalize", |
| 80 | + return_value="Hello bot!", |
| 81 | + ) |
| 82 | + def test_handle_message_really_reacts_when_no_processors(self, mock_normalize): |
| 83 | + self.command.web.reactions_add = MagicMock() |
| 84 | + self.command.post_ephemeral = MagicMock() |
| 85 | + |
| 86 | + payload = { |
| 87 | + "event": { |
| 88 | + "channel": "D12345", |
| 89 | + "user": "U67890", |
| 90 | + "text": "Hello!", |
| 91 | + "ts": "123456.789", |
| 92 | + "team": "T123", |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + self.command.handle_message_really(**payload) |
| 97 | + |
| 98 | + self.command.web.reactions_add.assert_called_once_with( |
| 99 | + name="surface_not_found", channel="D12345", timestamp="123456.789" |
| 100 | + ) |
| 101 | + self.command.post_ephemeral.assert_called_once() |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments