|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from apps.slack.blocks import DIVIDER, SECTION_BREAK |
| 7 | +from apps.slack.commands.command import CommandBase |
| 8 | + |
| 9 | + |
| 10 | +class Command(CommandBase): |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +class TestCommandBase: |
| 15 | + @pytest.fixture |
| 16 | + def command_instance(self): |
| 17 | + return Command() |
| 18 | + |
| 19 | + @pytest.fixture |
| 20 | + def mock_command_payload(self): |
| 21 | + return {"user_id": "U123ABC"} |
| 22 | + |
| 23 | + @patch("apps.slack.commands.command.logger") |
| 24 | + @patch("apps.slack.commands.command.SlackConfig") |
| 25 | + def test_configure_commands_when_app_is_none(self, mock_slack_config, mock_logger): |
| 26 | + """Tests that a warning is logged if the Slack app is not configured.""" |
| 27 | + mock_slack_config.app = None |
| 28 | + CommandBase.configure_commands() |
| 29 | + mock_logger.warning.assert_called_once() |
| 30 | + |
| 31 | + def test_command_name_property(self, command_instance): |
| 32 | + """Tests that the command_name is derived correctly from the class name.""" |
| 33 | + assert command_instance.command_name == "/command" |
| 34 | + |
| 35 | + def test_template_path_property(self, command_instance): |
| 36 | + """Tests that the template_path is derived correctly.""" |
| 37 | + assert command_instance.template_path == Path("commands/command.jinja") |
| 38 | + |
| 39 | + @patch("apps.slack.commands.command.env") |
| 40 | + def test_template_property(self, mock_jinja_env, command_instance): |
| 41 | + """Tests that the correct template is requested from the jinja environment.""" |
| 42 | + _ = command_instance.template |
| 43 | + |
| 44 | + mock_jinja_env.get_template.assert_called_once_with("commands/command.jinja") |
| 45 | + |
| 46 | + def test_render_blocks(self, command_instance): |
| 47 | + """Tests that the render_blocks method correctly parses rendered text into blocks.""" |
| 48 | + test_string = f"Hello World{SECTION_BREAK}{DIVIDER}{SECTION_BREAK}Welcome to Nest" |
| 49 | + |
| 50 | + with patch.object(command_instance, "render_text", return_value=test_string): |
| 51 | + blocks = command_instance.render_blocks(command={}) |
| 52 | + |
| 53 | + assert len(blocks) == 3 |
| 54 | + assert blocks[0]["text"]["text"] == "Hello World" |
| 55 | + assert blocks[1]["type"] == "divider" |
| 56 | + assert blocks[2]["text"]["text"] == "Welcome to Nest" |
| 57 | + |
| 58 | + def test_handler_success(self, settings, command_instance, mock_command_payload): |
| 59 | + """Tests the successful path of the command handler.""" |
| 60 | + settings.SLACK_COMMANDS_ENABLED = True |
| 61 | + ack = MagicMock() |
| 62 | + mock_client = MagicMock() |
| 63 | + mock_client.conversations_open.return_value = {"channel": {"id": "D123XYZ"}} |
| 64 | + |
| 65 | + with patch.object(command_instance, "render_blocks", return_value=[{"type": "section"}]): |
| 66 | + command_instance.handler(ack=ack, command=mock_command_payload, client=mock_client) |
| 67 | + |
| 68 | + ack.assert_called_once() |
| 69 | + mock_client.conversations_open.assert_called_once_with(users="U123ABC") |
| 70 | + mock_client.chat_postMessage.assert_called_once() |
| 71 | + assert mock_client.chat_postMessage.call_args[1]["channel"] == "D123XYZ" |
| 72 | + |
| 73 | + def test_handler_api_error(self, mocker, settings, command_instance, mock_command_payload): |
| 74 | + """Tests that an exception during API calls is caught and logged.""" |
| 75 | + settings.SLACK_COMMANDS_ENABLED = True |
| 76 | + mock_logger = mocker.patch("apps.slack.commands.command.logger") |
| 77 | + ack = MagicMock() |
| 78 | + mock_client = MagicMock() |
| 79 | + mock_client.chat_postMessage.side_effect = [Exception("API Error"), {"ok": True}] |
| 80 | + mocker.patch.object(command_instance, "render_blocks", return_value=[{"type": "section"}]) |
| 81 | + command_instance.handler(ack=ack, command=mock_command_payload, client=mock_client) |
| 82 | + ack.assert_called_once() |
| 83 | + mock_logger.exception.assert_called_once() |
| 84 | + # Verify retry occurred and eventually succeeded |
| 85 | + assert mock_client.chat_postMessage.call_count == 2 |
| 86 | + mock_logger.exception.assert_called_with( |
| 87 | + "Failed to handle command '%s'", command_instance.command_name |
| 88 | + ) |
| 89 | + |
| 90 | + def test_handler_when_commands_disabled( |
| 91 | + self, settings, command_instance, mock_command_payload |
| 92 | + ): |
| 93 | + """Tests that no message is sent when commands are disabled.""" |
| 94 | + settings.SLACK_COMMANDS_ENABLED = False |
| 95 | + ack = MagicMock() |
| 96 | + mock_client = MagicMock() |
| 97 | + command_instance.handler(ack=ack, command=mock_command_payload, client=mock_client) |
| 98 | + ack.assert_called_once() |
| 99 | + mock_client.chat_postMessage.assert_not_called() |
0 commit comments