|
3 | 3 | from unittest import mock |
4 | 4 | from unittest.mock import MagicMock |
5 | 5 |
|
| 6 | +from testfixtures import LogCapture |
| 7 | +from slack.web.slack_response import SlackResponse |
| 8 | +from slack import WebClient |
| 9 | + |
6 | 10 | import gokart.slack |
7 | 11 |
|
8 | 12 | logger = getLogger(__name__) |
9 | 13 |
|
10 | 14 |
|
| 15 | +def _slack_response(token, data): |
| 16 | + return SlackResponse(client=WebClient(token=token), |
| 17 | + http_verb="POST", |
| 18 | + api_url="http://localhost:3000/api.test", |
| 19 | + req_args={}, |
| 20 | + data=data, |
| 21 | + headers={}, |
| 22 | + status_code=200) |
| 23 | + |
| 24 | + |
11 | 25 | class TestSlackAPI(unittest.TestCase): |
12 | 26 | @mock.patch('gokart.slack.slack_api.slack.WebClient') |
13 | 27 | def test_initialization_with_invalid_token(self, patch): |
14 | | - def _channels_list(method, http_verb="POST", params={}): |
15 | | - assert method == 'channels.list' |
16 | | - return {'ok': False, 'error': 'error_reason'} |
| 28 | + def _conversations_list(params={}): |
| 29 | + return _slack_response(token='invalid', data={'ok': False, 'error': 'error_reason'}) |
17 | 30 |
|
18 | 31 | mock_client = MagicMock() |
19 | | - mock_client.api_call = MagicMock(side_effect=_channels_list) |
| 32 | + mock_client.conversations_list = MagicMock(side_effect=_conversations_list) |
20 | 33 | patch.return_value = mock_client |
21 | 34 |
|
22 | | - with self.assertRaises(gokart.slack.slack_api.ChannelListNotLoadedError): |
| 35 | + with LogCapture() as l: |
23 | 36 | gokart.slack.SlackAPI(token='invalid', channel='test', to_user='test user') |
| 37 | + l.check(('gokart.slack.slack_api', 'WARNING', |
| 38 | + 'The job will start without slack notification: Channel test is not found in public channels.')) |
24 | 39 |
|
25 | 40 | @mock.patch('gokart.slack.slack_api.slack.WebClient') |
26 | 41 | def test_invalid_channel(self, patch): |
27 | | - def _channels_list(method, http_verb="POST", params={}): |
28 | | - assert method == 'channels.list' |
29 | | - return {'ok': True, 'channels': [{'name': 'valid', 'id': 'valid_id'}], 'response_metadata': {'next_cursor': ''}} |
| 42 | + def _conversations_list(params={}): |
| 43 | + return _slack_response(token='valid', |
| 44 | + data={ |
| 45 | + 'ok': True, |
| 46 | + 'channels': [{ |
| 47 | + 'name': 'valid', |
| 48 | + 'id': 'valid_id' |
| 49 | + }], |
| 50 | + 'response_metadata': { |
| 51 | + 'next_cursor': '' |
| 52 | + } |
| 53 | + }) |
30 | 54 |
|
31 | 55 | mock_client = MagicMock() |
32 | | - mock_client.api_call = MagicMock(side_effect=_channels_list) |
| 56 | + mock_client.conversations_list = MagicMock(side_effect=_conversations_list) |
33 | 57 | patch.return_value = mock_client |
34 | 58 |
|
35 | | - with self.assertRaises(gokart.slack.slack_api.ChannelNotFoundError): |
| 59 | + with LogCapture() as l: |
36 | 60 | gokart.slack.SlackAPI(token='valid', channel='invalid_channel', to_user='test user') |
| 61 | + l.check(( |
| 62 | + 'gokart.slack.slack_api', 'WARNING', |
| 63 | + 'The job will start without slack notification: Channel invalid_channel is not found in public channels.' |
| 64 | + )) |
37 | 65 |
|
38 | 66 | @mock.patch('gokart.slack.slack_api.slack.WebClient') |
39 | 67 | def test_send_snippet_with_invalid_token(self, patch): |
40 | | - def _api_call(*args, **kwargs): |
41 | | - if args[0] == 'channels.list': |
42 | | - return {'ok': True, 'channels': [{'name': 'valid', 'id': 'valid_id'}], 'response_metadata': {'next_cursor': ''}} |
43 | | - if args[0] == 'files.upload': |
44 | | - return {'ok': False, 'error': 'error_reason'} |
45 | | - assert False |
| 68 | + def _conversations_list(params={}): |
| 69 | + return _slack_response(token='valid', |
| 70 | + data={ |
| 71 | + 'ok': True, |
| 72 | + 'channels': [{ |
| 73 | + 'name': 'valid', |
| 74 | + 'id': 'valid_id' |
| 75 | + }], |
| 76 | + 'response_metadata': { |
| 77 | + 'next_cursor': '' |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + def _api_call(method, data={}): |
| 82 | + assert method == 'files.upload' |
| 83 | + return {'ok': False, 'error': 'error_reason'} |
46 | 84 |
|
47 | 85 | mock_client = MagicMock() |
| 86 | + mock_client.conversations_list = MagicMock(side_effect=_conversations_list) |
48 | 87 | mock_client.api_call = MagicMock(side_effect=_api_call) |
49 | 88 | patch.return_value = mock_client |
50 | 89 |
|
51 | | - with self.assertRaises(gokart.slack.slack_api.FileNotUploadedError): |
| 90 | + with LogCapture() as l: |
52 | 91 | api = gokart.slack.SlackAPI(token='valid', channel='valid', to_user='test user') |
53 | 92 | api.send_snippet(comment='test', title='title', content='content') |
| 93 | + l.check( |
| 94 | + ('gokart.slack.slack_api', 'WARNING', |
| 95 | + 'Failed to send slack notification: Error while uploading file. The error reason is "error_reason".')) |
54 | 96 |
|
55 | 97 | @mock.patch('gokart.slack.slack_api.slack.WebClient') |
56 | 98 | def test_send(self, patch): |
57 | | - def _api_call(*args, **kwargs): |
58 | | - if args[0] == 'channels.list': |
59 | | - return {'ok': True, 'channels': [{'name': 'valid', 'id': 'valid_id'}], 'response_metadata': {'next_cursor': ''}} |
60 | | - if args[0] == 'files.upload': |
61 | | - return {'ok': True} |
62 | | - assert False |
| 99 | + def _conversations_list(params={}): |
| 100 | + return _slack_response(token='valid', |
| 101 | + data={ |
| 102 | + 'ok': True, |
| 103 | + 'channels': [{ |
| 104 | + 'name': 'valid', |
| 105 | + 'id': 'valid_id' |
| 106 | + }], |
| 107 | + 'response_metadata': { |
| 108 | + 'next_cursor': '' |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + def _api_call(method, data={}): |
| 113 | + assert method == 'files.upload' |
| 114 | + return {'ok': False, 'error': 'error_reason'} |
63 | 115 |
|
64 | 116 | mock_client = MagicMock() |
| 117 | + mock_client.conversations_list = MagicMock(side_effect=_conversations_list) |
65 | 118 | mock_client.api_call = MagicMock(side_effect=_api_call) |
66 | 119 | patch.return_value = mock_client |
67 | 120 |
|
|
0 commit comments