|
| 1 | +import sys |
| 2 | +import pytest |
| 3 | +from unittest.mock import Mock, patch, MagicMock |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def mock_logger(): |
| 8 | + return MagicMock() |
| 9 | + |
| 10 | + |
| 11 | +@patch("app.utils.handler_utils.forward_event_to_pull_request_lambda") |
| 12 | +def test_process_async_slack_event_feedback( |
| 13 | + mock_forward_event_to_pull_request_lambda: Mock, |
| 14 | + mock_get_parameter: Mock, |
| 15 | + mock_env: Mock, |
| 16 | +): |
| 17 | + """Test successful async event processing""" |
| 18 | + # set up mocks |
| 19 | + mock_client = Mock() |
| 20 | + |
| 21 | + # delete and import module to test |
| 22 | + if "app.slack.slack_events" in sys.modules: |
| 23 | + del sys.modules["app.slack.slack_events"] |
| 24 | + from app.slack.slack_events import process_async_slack_event |
| 25 | + |
| 26 | + # perform operation |
| 27 | + slack_event_data = { |
| 28 | + "text": "feedback: this is some feedback", |
| 29 | + "user": "U456", |
| 30 | + "channel": "C789", |
| 31 | + "ts": "1234567890.123", |
| 32 | + } |
| 33 | + with patch("app.slack.slack_events.process_feedback_event") as mock_process_feedback_event, patch( |
| 34 | + "app.slack.slack_events.process_slack_message" |
| 35 | + ) as mock_process_slack_message: |
| 36 | + process_async_slack_event(event=slack_event_data, event_id="evt123", client=mock_client) |
| 37 | + mock_forward_event_to_pull_request_lambda.assert_not_called() |
| 38 | + mock_process_feedback_event.assert_called_once_with( |
| 39 | + message_text="feedback: this is some feedback", |
| 40 | + conversation_key="thread#C789#1234567890.123", |
| 41 | + user_id="U456", |
| 42 | + channel_id="C789", |
| 43 | + thread_root="1234567890.123", |
| 44 | + client=mock_client, |
| 45 | + event=slack_event_data, |
| 46 | + ) |
| 47 | + mock_process_slack_message.assert_not_called() |
| 48 | + |
| 49 | + |
| 50 | +@patch("app.utils.handler_utils.is_latest_message") |
| 51 | +def test_process_async_slack_action_positive( |
| 52 | + mock_is_latest_message: Mock, |
| 53 | + mock_get_parameter: Mock, |
| 54 | + mock_env: Mock, |
| 55 | +): |
| 56 | + """Test successful async action processing""" |
| 57 | + # set up mocks |
| 58 | + mock_client = Mock() |
| 59 | + mock_is_latest_message.return_value = True |
| 60 | + |
| 61 | + # delete and import module to test |
| 62 | + if "app.slack.slack_events" in sys.modules: |
| 63 | + del sys.modules["app.slack.slack_events"] |
| 64 | + from app.slack.slack_events import process_async_slack_action |
| 65 | + |
| 66 | + feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}' |
| 67 | + |
| 68 | + # perform operation |
| 69 | + slack_action_data = { |
| 70 | + "type": "block_actions", |
| 71 | + "user": {"id": "U123"}, |
| 72 | + "channel": {"id": "C123"}, |
| 73 | + "message": {"ts": "1759845126.972219"}, |
| 74 | + "actions": [{"action_id": "feedback_yes", "value": feedback_value}], |
| 75 | + } |
| 76 | + with patch("app.slack.slack_events.store_feedback") as mock_store_feedback: |
| 77 | + process_async_slack_action(body=slack_action_data, client=mock_client) |
| 78 | + |
| 79 | + # assertions |
| 80 | + mock_store_feedback.assert_called_once_with( |
| 81 | + conversation_key="thread#C123#123", |
| 82 | + feedback_type="positive", |
| 83 | + user_id="U123", |
| 84 | + channel_id="C123", |
| 85 | + thread_ts="1759845114.407989", |
| 86 | + message_ts="1759845126.972219", |
| 87 | + client=mock_client, |
| 88 | + ) |
| 89 | + mock_client.chat_postMessage.assert_called_once_with( |
| 90 | + channel="C123", |
| 91 | + text="Thank you for your feedback.", |
| 92 | + thread_ts="1759845114.407989", |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +@patch("app.utils.handler_utils.is_latest_message") |
| 97 | +def test_process_async_slack_action_negative( |
| 98 | + mock_is_latest_message: Mock, |
| 99 | + mock_get_parameter: Mock, |
| 100 | + mock_env: Mock, |
| 101 | +): |
| 102 | + """Test successful async action processing""" |
| 103 | + # set up mocks |
| 104 | + mock_client = Mock() |
| 105 | + mock_is_latest_message.return_value = True |
| 106 | + |
| 107 | + # delete and import module to test |
| 108 | + if "app.slack.slack_events" in sys.modules: |
| 109 | + del sys.modules["app.slack.slack_events"] |
| 110 | + from app.slack.slack_events import process_async_slack_action |
| 111 | + |
| 112 | + feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}' |
| 113 | + |
| 114 | + # perform operation |
| 115 | + slack_action_data = { |
| 116 | + "type": "block_actions", |
| 117 | + "user": {"id": "U123"}, |
| 118 | + "channel": {"id": "C123"}, |
| 119 | + "message": {"ts": "1759845126.972219"}, |
| 120 | + "actions": [{"action_id": "feedback_no", "value": feedback_value}], |
| 121 | + } |
| 122 | + with patch("app.slack.slack_events.store_feedback") as mock_store_feedback: |
| 123 | + process_async_slack_action(body=slack_action_data, client=mock_client) |
| 124 | + |
| 125 | + # assertions |
| 126 | + mock_store_feedback.assert_called_once_with( |
| 127 | + conversation_key="thread#C123#123", |
| 128 | + feedback_type="negative", |
| 129 | + user_id="U123", |
| 130 | + channel_id="C123", |
| 131 | + thread_ts="1759845114.407989", |
| 132 | + message_ts="1759845126.972219", |
| 133 | + client=mock_client, |
| 134 | + ) |
| 135 | + mock_client.chat_postMessage.assert_called_once_with( |
| 136 | + channel="C123", |
| 137 | + text='Please let us know how the answer could be improved. Start your message with "feedback:"', |
| 138 | + thread_ts="1759845114.407989", |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +@patch("app.utils.handler_utils.is_latest_message") |
| 143 | +def test_process_async_slack_action_not_latest( |
| 144 | + mock_is_latest_message: Mock, |
| 145 | + mock_get_parameter: Mock, |
| 146 | + mock_env: Mock, |
| 147 | +): |
| 148 | + """Test successful async action processing""" |
| 149 | + # set up mocks |
| 150 | + mock_client = Mock() |
| 151 | + mock_is_latest_message.return_value = False |
| 152 | + |
| 153 | + # delete and import module to test |
| 154 | + if "app.slack.slack_events" in sys.modules: |
| 155 | + del sys.modules["app.slack.slack_events"] |
| 156 | + from app.slack.slack_events import process_async_slack_action |
| 157 | + |
| 158 | + feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}' |
| 159 | + |
| 160 | + # perform operation |
| 161 | + slack_action_data = { |
| 162 | + "type": "block_actions", |
| 163 | + "user": {"id": "U123"}, |
| 164 | + "channel": {"id": "C123"}, |
| 165 | + "actions": [{"action_id": "feedback_no", "value": feedback_value}], |
| 166 | + } |
| 167 | + with patch("app.slack.slack_events.store_feedback") as mock_store_feedback: |
| 168 | + process_async_slack_action(body=slack_action_data, client=mock_client) |
| 169 | + |
| 170 | + # assertions |
| 171 | + mock_store_feedback.assert_not_called() |
| 172 | + mock_client.chat_postMessage.assert_not_called() |
| 173 | + |
| 174 | + |
| 175 | +@patch("app.utils.handler_utils.is_latest_message") |
| 176 | +def test_process_async_slack_action_unknown_action( |
| 177 | + mock_is_latest_message: Mock, |
| 178 | + mock_get_parameter: Mock, |
| 179 | + mock_env: Mock, |
| 180 | +): |
| 181 | + """Test successful async action processing""" |
| 182 | + # set up mocks |
| 183 | + mock_client = Mock() |
| 184 | + mock_is_latest_message.return_value = True |
| 185 | + |
| 186 | + # delete and import module to test |
| 187 | + if "app.slack.slack_events" in sys.modules: |
| 188 | + del sys.modules["app.slack.slack_events"] |
| 189 | + from app.slack.slack_events import process_async_slack_action |
| 190 | + |
| 191 | + feedback_value = '{"ck":"thread#C123#123","ch":"C123","mt":"1759845126.972219","tt":"1759845114.407989"}' |
| 192 | + |
| 193 | + # perform operation |
| 194 | + slack_action_data = { |
| 195 | + "type": "block_actions", |
| 196 | + "user": {"id": "U123"}, |
| 197 | + "channel": {"id": "C123"}, |
| 198 | + "actions": [{"action_id": "I_Do_Not_Know_This_Action", "value": feedback_value}], |
| 199 | + } |
| 200 | + with patch("app.slack.slack_events.store_feedback") as mock_store_feedback: |
| 201 | + process_async_slack_action(body=slack_action_data, client=mock_client) |
| 202 | + |
| 203 | + # assertions |
| 204 | + mock_store_feedback.assert_not_called() |
| 205 | + mock_client.chat_postMessage.assert_not_called() |
| 206 | + |
| 207 | + |
| 208 | +def test_process_feedback_event(): |
| 209 | + # set up mocks |
| 210 | + mock_client = Mock() |
| 211 | + |
| 212 | + # delete and import module to test |
| 213 | + if "app.slack.slack_events" in sys.modules: |
| 214 | + del sys.modules["app.slack.slack_events"] |
| 215 | + from app.slack.slack_events import process_feedback_event |
| 216 | + |
| 217 | + # perform operation |
| 218 | + mock_event = {} |
| 219 | + with patch("app.slack.slack_events.store_feedback") as mock_store_feedback: |
| 220 | + process_feedback_event( |
| 221 | + message_text="feedback: this is some feedback", |
| 222 | + conversation_key="thread#C123#123", |
| 223 | + user_id="U123", |
| 224 | + channel_id="C123", |
| 225 | + thread_root="1759845114.407989", |
| 226 | + event=mock_event, |
| 227 | + client=mock_client, |
| 228 | + ) |
| 229 | + |
| 230 | + # assertions |
| 231 | + mock_store_feedback.assert_called_once_with( |
| 232 | + conversation_key="thread#C123#123", |
| 233 | + feedback_type="additional", |
| 234 | + user_id="U123", |
| 235 | + channel_id="C123", |
| 236 | + thread_ts="1759845114.407989", |
| 237 | + message_ts=None, |
| 238 | + feedback_text="this is some feedback", |
| 239 | + client=mock_client, |
| 240 | + ) |
| 241 | + mock_client.chat_postMessage.assert_called_once_with( |
| 242 | + channel="C123", text="Thank you for your feedback.", thread_ts="1759845114.407989" |
| 243 | + ) |
| 244 | + |
| 245 | + |
| 246 | +@patch("app.services.slack.post_error_message") |
| 247 | +def test_process_feedback_event_error( |
| 248 | + mock_post_error_message: Mock, |
| 249 | +): |
| 250 | + # set up mocks |
| 251 | + mock_client = Mock() |
| 252 | + |
| 253 | + # delete and import module to test |
| 254 | + if "app.slack.slack_events" in sys.modules: |
| 255 | + del sys.modules["app.slack.slack_events"] |
| 256 | + from app.slack.slack_events import process_feedback_event |
| 257 | + |
| 258 | + # perform operation |
| 259 | + mock_event = { |
| 260 | + "channel": "C123", |
| 261 | + "thread_ts": "123", |
| 262 | + } |
| 263 | + with patch("app.slack.slack_events.store_feedback") as mock_store_feedback: |
| 264 | + mock_store_feedback.side_effect = Exception("There was an error") |
| 265 | + process_feedback_event( |
| 266 | + message_text="feedback: this is some feedback", |
| 267 | + conversation_key="thread#C123#123", |
| 268 | + user_id="U123", |
| 269 | + channel_id="C123", |
| 270 | + thread_root="1759845114.407989", |
| 271 | + event=mock_event, |
| 272 | + client=mock_client, |
| 273 | + ) |
| 274 | + |
| 275 | + # assertions |
| 276 | + mock_post_error_message.assert_called_once_with(channel="C123", thread_ts="123", client=mock_client) |
0 commit comments