|
| 1 | +import sys |
| 2 | +import pytest |
| 3 | +from unittest.mock import Mock, MagicMock |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def mock_logger(): |
| 8 | + return MagicMock() |
| 9 | + |
| 10 | + |
| 11 | +def test_process_slack_message_split_citation(): |
| 12 | + # set up mocks |
| 13 | + mock_client = Mock() |
| 14 | + mock_client.chat_postMessage.return_value = {"ts": "1234567890.124"} |
| 15 | + mock_client.chat_update.return_value = {"ok": True} |
| 16 | + |
| 17 | + |
| 18 | +def test_process_citation_events_update_chat(): |
| 19 | + # set up mocks |
| 20 | + mock_client = Mock() |
| 21 | + mock_client.chat_postMessage.return_value = {"ts": "1234567890.124"} |
| 22 | + mock_client.chat_update.return_value = {"ok": True} |
| 23 | + |
| 24 | + # delete and import module to test |
| 25 | + if "app.slack.slack_events" in sys.modules: |
| 26 | + del sys.modules["app.slack.slack_events"] |
| 27 | + from app.slack.slack_events import process_async_slack_action |
| 28 | + |
| 29 | + body = { |
| 30 | + "type": "block_actions", |
| 31 | + "message": { |
| 32 | + "ts": "123", |
| 33 | + "text": "", |
| 34 | + "blocks": [ |
| 35 | + { |
| 36 | + "type": "section", |
| 37 | + "block_id": "OvNCm", |
| 38 | + "text": { |
| 39 | + "type": "mrkdwn", |
| 40 | + "text": "", |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + "type": "actions", |
| 45 | + "block_id": "citation_actions", |
| 46 | + "elements": [ |
| 47 | + { |
| 48 | + "type": "button", |
| 49 | + "action_id": "cite_1", |
| 50 | + "text": { |
| 51 | + "type": "plain_text", |
| 52 | + "text": "[1] Downloading a single prescription using the prescription's ID, or ...", |
| 53 | + "emoji": "true", |
| 54 | + }, |
| 55 | + "value": '{"ck":"123","ch":"123","mt":"123","tt":"123","source_number":"1","title":"title"', |
| 56 | + } |
| 57 | + ], |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + "channel": { |
| 62 | + "id": "ABC123", |
| 63 | + }, |
| 64 | + "actions": [ |
| 65 | + { |
| 66 | + "action_id": "cite_1", |
| 67 | + "block_id": "citation_actions", |
| 68 | + "text": { |
| 69 | + "type": "plain_text", |
| 70 | + "text": "[1] Downloading a single prescription using the prescription's ID, or ...", |
| 71 | + "emoji": "true", |
| 72 | + }, |
| 73 | + "value": '{"ck":"123","ch":"C095D4SRX6W","mt":"123","tt":"123","source_number":"1","title":""}', |
| 74 | + "type": "button", |
| 75 | + "action_ts": "1765807735.805872", |
| 76 | + } |
| 77 | + ], |
| 78 | + } |
| 79 | + |
| 80 | + # perform operation |
| 81 | + process_async_slack_action(body, mock_client) |
| 82 | + |
| 83 | + # assertions |
| 84 | + mock_client.chat_update.assert_called() |
| 85 | + |
| 86 | + |
| 87 | +def test_process_citation_events_update_chat_message_open_citation(): |
| 88 | + # set up mocks |
| 89 | + mock_client = Mock() |
| 90 | + mock_client.chat_postMessage.return_value = {"ts": "1234567890.124"} |
| 91 | + mock_client.chat_update.return_value = {"ok": True} |
| 92 | + |
| 93 | + # delete and import module to test |
| 94 | + if "app.slack.slack_events" in sys.modules: |
| 95 | + del sys.modules["app.slack.slack_events"] |
| 96 | + from app.slack.slack_events import open_citation |
| 97 | + |
| 98 | + params = { |
| 99 | + "ck": "123123", |
| 100 | + "ch": "123123", |
| 101 | + "mt": "123123.123123", |
| 102 | + "tt": "123123.123123", |
| 103 | + "source_number": "1", |
| 104 | + "title": "Citation Title", |
| 105 | + "body": "Citation Body", |
| 106 | + "link": "https://example.com", |
| 107 | + } |
| 108 | + |
| 109 | + citations = { |
| 110 | + "type": "actions", |
| 111 | + "block_id": "citation_actions", |
| 112 | + "elements": [ |
| 113 | + { |
| 114 | + "type": "button", |
| 115 | + "action_id": "cite_1", |
| 116 | + "text": { |
| 117 | + "type": "plain_text", |
| 118 | + "text": "[1] The body of the citation", |
| 119 | + "emoji": "true", |
| 120 | + }, |
| 121 | + "style": None, # Set citation as de-active |
| 122 | + "value": str(params), |
| 123 | + }, |
| 124 | + ], |
| 125 | + } |
| 126 | + |
| 127 | + message = { |
| 128 | + "blocks": [citations], |
| 129 | + } |
| 130 | + |
| 131 | + # perform operation |
| 132 | + open_citation("ABC", "123", message, params, mock_client) |
| 133 | + |
| 134 | + # assertions |
| 135 | + expected_blocks = [ |
| 136 | + citations, |
| 137 | + { |
| 138 | + "type": "section", |
| 139 | + "text": {"type": "mrkdwn", "text": "*Citation Title*\n\n> Citation Body"}, |
| 140 | + "block_id": "citation_block", |
| 141 | + }, |
| 142 | + ] |
| 143 | + mock_client.chat_update.assert_called() |
| 144 | + mock_client.chat_update.assert_called_with(channel="ABC", ts="123", blocks=expected_blocks) |
| 145 | + |
| 146 | + |
| 147 | +def test_process_citation_events_update_chat_message_close_citation(): |
| 148 | + # set up mocks |
| 149 | + mock_client = Mock() |
| 150 | + mock_client.chat_postMessage.return_value = {"ts": "1234567890.124"} |
| 151 | + mock_client.chat_update.return_value = {"ok": True} |
| 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 open_citation |
| 157 | + |
| 158 | + params = { |
| 159 | + "ck": "123123", |
| 160 | + "ch": "123123", |
| 161 | + "mt": "123123.123123", |
| 162 | + "tt": "123123.123123", |
| 163 | + "source_number": "1", |
| 164 | + "title": "Citation Title", |
| 165 | + "body": "Citation Body", |
| 166 | + "link": "https://example.com", |
| 167 | + } |
| 168 | + |
| 169 | + citations = { |
| 170 | + "type": "actions", |
| 171 | + "block_id": "citation_actions", |
| 172 | + "elements": [ |
| 173 | + { |
| 174 | + "type": "button", |
| 175 | + "action_id": "cite_1", |
| 176 | + "text": { |
| 177 | + "type": "plain_text", |
| 178 | + "text": "[1] The body of the citation", |
| 179 | + "emoji": "true", |
| 180 | + }, |
| 181 | + "style": "primary", # Set citation as active |
| 182 | + "value": str(params), |
| 183 | + }, |
| 184 | + ], |
| 185 | + } |
| 186 | + |
| 187 | + citation_body = { |
| 188 | + "type": "section", |
| 189 | + "text": {"type": "mrkdwn", "text": "*Citation Title*\n\n> Citation Body"}, |
| 190 | + "block_id": "citation_block", |
| 191 | + } |
| 192 | + |
| 193 | + message = { |
| 194 | + "blocks": [citations, citation_body], |
| 195 | + } |
| 196 | + |
| 197 | + # perform operation |
| 198 | + open_citation("ABC", "123", message, params, mock_client) |
| 199 | + |
| 200 | + # assertions |
| 201 | + expected_blocks = [ |
| 202 | + citations, |
| 203 | + ] |
| 204 | + mock_client.chat_update.assert_called() |
| 205 | + mock_client.chat_update.assert_called_with(channel="ABC", ts="123", blocks=expected_blocks) |
| 206 | + |
| 207 | + |
| 208 | +def test_process_citation_events_update_chat_message_change_close_citation(): |
| 209 | + # set up mocks |
| 210 | + mock_client = Mock() |
| 211 | + mock_client.chat_postMessage.return_value = {"ts": "1234567890.124"} |
| 212 | + mock_client.chat_update.return_value = {"ok": True} |
| 213 | + |
| 214 | + # delete and import module to test |
| 215 | + if "app.slack.slack_events" in sys.modules: |
| 216 | + del sys.modules["app.slack.slack_events"] |
| 217 | + from app.slack.slack_events import open_citation |
| 218 | + |
| 219 | + params = { |
| 220 | + "ck": "123123", |
| 221 | + "ch": "123123", |
| 222 | + "mt": "123123.123123", |
| 223 | + "tt": "123123.123123", |
| 224 | + "source_number": "2", |
| 225 | + "title": "Second Citation Title", |
| 226 | + "body": "Second Citation Body", |
| 227 | + "link": "https://example.com", |
| 228 | + } |
| 229 | + |
| 230 | + citations = { |
| 231 | + "type": "actions", |
| 232 | + "block_id": "citation_actions", |
| 233 | + "elements": [ |
| 234 | + { |
| 235 | + "type": "button", |
| 236 | + "action_id": "cite_1", |
| 237 | + "text": { |
| 238 | + "type": "plain_text", |
| 239 | + "text": "[1] The body of the citation", |
| 240 | + "emoji": "true", |
| 241 | + }, |
| 242 | + "style": "primary", # Set citation as active |
| 243 | + "value": str(params), |
| 244 | + }, |
| 245 | + { |
| 246 | + "type": "button", |
| 247 | + "action_id": "cite_2", |
| 248 | + "text": { |
| 249 | + "type": "plain_text", |
| 250 | + "text": "[2] The body of the citation", |
| 251 | + "emoji": "true", |
| 252 | + }, |
| 253 | + "style": None, # Set citation as active |
| 254 | + "value": str(params), |
| 255 | + }, |
| 256 | + ], |
| 257 | + } |
| 258 | + |
| 259 | + first_citation_body = { |
| 260 | + "type": "section", |
| 261 | + "text": {"type": "mrkdwn", "text": "*First Citation Title*\n\n> First Citation Body"}, |
| 262 | + "block_id": "citation_block", |
| 263 | + } |
| 264 | + |
| 265 | + second_citation_body = { |
| 266 | + "type": "section", |
| 267 | + "text": {"type": "mrkdwn", "text": "*Second Citation Title*\n\n> Second Citation Body"}, |
| 268 | + "block_id": "citation_block", |
| 269 | + } |
| 270 | + |
| 271 | + message = { |
| 272 | + "blocks": [citations, first_citation_body], |
| 273 | + } |
| 274 | + |
| 275 | + # perform operation |
| 276 | + open_citation("ABC", "123", message, params, mock_client) |
| 277 | + |
| 278 | + # assertions |
| 279 | + expected_blocks = [citations, second_citation_body] |
| 280 | + mock_client.chat_update.assert_called() |
| 281 | + mock_client.chat_update.assert_called_with(channel="ABC", ts="123", blocks=expected_blocks) |
0 commit comments