-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_retro_bot_to_airtable_test.py
346 lines (289 loc) · 14.6 KB
/
slack_retro_bot_to_airtable_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
"""Test /retro commands."""
import textwrap
import unittest
from os import environ
import airtablemock
import mock
import slack_retro_bot_to_airtable
@mock.patch(slack_retro_bot_to_airtable.__name__ + '._STEPS_TO_FINISH_SETUP', None)
@mock.patch(slack_retro_bot_to_airtable.__name__ + '._SLACK_RETRO_TOKEN', 'meowser_token')
class TestBot(unittest.TestCase):
"""Test /retro commands."""
def setUp(self):
environ['DATABASE_URL'] = 'postgres:///retrospective-bot-test'
environ['SLACK_TOKEN'] = 'meowser_token'
environ['SLACK_WEBHOOK_URL'] = 'http://hooks.example.com/services/HELLO/LOVELY/WORLD'
self.app = slack_retro_bot_to_airtable.app.test_client()
airtablemock.clear()
self.airtable_client = airtablemock.Airtable('retro-base-id')
patcher = mock.patch(
slack_retro_bot_to_airtable.__name__ + '._AIRTABLE_CLIENT',
self.airtable_client)
patcher.start()
self.airtable_client.create('Items', {'sprint': 'old'})
self.airtable_client.create_view('Items', 'Current View', 'sprint != "old"')
self.airtable_client.create('Moods', {'sprint': 'old'})
self.airtable_client.create_view('Moods', 'Current View', 'sprint != "old"')
def _post_command(self, text, slash_command='/retro'):
return self.app.post('/handle_slack_command', data={
'token': 'meowser_token',
'text': text,
'user_name': 'retroman',
'channel_id': '123456',
'command': slash_command,
'response_url': 'https://lambda-to-slack.com',
})
# As the name of the tests are self-explanatory, we don't need docstrings for them
# pylint: disable=missing-docstring
def test_app_exists(self):
"""The app exists."""
self.assertTrue(self.app)
# def test_unauthorized_access(self):
# """The app rejects unauthorized access."""
# robo_response = self.client.post('/', data={'token': 'woofer_token'})
# self.assertEqual(robo_response.status_code, 401)
# def test_authorized_access(self):
# """The app accepts authorized access."""
# robo_response = self.post_command(text='')
# self.assertEqual(robo_response.status_code, 200)
def test_set_retrospective_item_with_good_command(self):
return self._test_set_retrospective_item(
'good', 'good', 'The coffee was great', is_using_direct_command=True)
def test_set_retrospective_item_with_bad_command(self):
return self._test_set_retrospective_item(
'bad', 'danger', 'The coffee was bad', is_using_direct_command=True)
def test_set_retrospective_item_with_weird_case(self):
return self._test_set_retrospective_item(
'bad', 'danger', 'the coffee was bad, because of Cyrille',
is_using_direct_command=True, expected_text='The coffee was bad, because of Cyrille')
# def test_set_retrospective_item_with_try_command(self):
# return self._test_set_retrospective_item(
# 'try', 'warning', 'Make more coffee', is_using_direct_command=True)
def test_set_retrospective_item_with_retrospective_good_command(self):
return self._test_set_retrospective_item(
'good', 'good', 'The tea was great', is_using_direct_command=False)
def test_set_retrospective_item_with_quotes(self):
return self._test_set_retrospective_item(
'good', 'good', 'It\'s possible to use "quotes"')
def test_set_retrospective_item_with_retrospective_bad_command(self):
return self._test_set_retrospective_item(
'bad', 'danger', 'The tea was bad', is_using_direct_command=False)
# def test_set_retrospective_item_with_retrospective_try_command(self):
# return self._test_set_retrospective_item(
# 'try', 'warning', 'Make more tea', is_using_direct_command=False)
def _test_set_retrospective_item(
self, category, color, text,
is_using_direct_command=False, expected_text=None):
""" A retrospective item set via a POST is recorded in the database."""
if is_using_direct_command:
# '/good Bla bla'
robo_response = self._post_command(text=text, slash_command=category)
else:
# '/retrospective good Bla bla'
robo_response = self._post_command(
text='{} {}'.format(category, text), slash_command='retro')
if expected_text is None:
expected_text = text
self.assertEqual(
{
'text': 'New retrospective item:',
'response_type': 'in_channel',
'attachments': [
{'title': category.capitalize(), 'color': color},
{'color': color, 'text': expected_text},
],
},
robo_response.json,
)
items = self.airtable_client.get('Items', view='Current View')
self.assertEqual(1, len(items['records']), msg=items)
item = items['records'][0]['fields']
self.assertEqual(category, item['Category'])
self.assertEqual('retroman', item['Creator'])
self.assertEqual(expected_text, item['Object'])
def test_list(self):
""" Test getting the list of all items with POST."""
# Check list is empty at first
robo_response = self._post_command(text='list', slash_command='retro')
self.assertEqual(200, robo_response.status_code, msg=robo_response.data)
expected_list = {
'text': 'No retrospective items yet.',
'response_type': 'in_channel',
'attachments': [],
}
self.assertEqual(robo_response.json, expected_list)
check = self._post_command(text='The coffee was great', slash_command='good')
self.assertEqual(200, check.status_code, msg=check.data)
self._post_command(text='The coffee was bad', slash_command='bad')
self._post_command(text='The tea was great', slash_command='good')
# Check list is filled later
robo_response = self._post_command(text='list', slash_command='retro')
attachments = robo_response.json.get('attachments', [])
self.assertEqual(7, len(attachments), msg=attachments)
expected_list = [
{'color': 'good', 'title': 'Good'},
{'color': 'good', 'text': 'The coffee was great'},
{'color': 'good', 'text': 'The tea was great'},
# Here should be the button to mark good as reviewed.
{'color': 'danger', 'title': 'Bad'},
{'color': 'danger', 'text': 'The coffee was bad'},
# Here should be the button to mark bad as reviewed.
]
good_button = attachments.pop(3)
bad_button = attachments.pop(5)
self.assertEqual(expected_list, attachments)
good_item_ids = good_button.pop('callback_id', '')
self.assertEqual(2, len(good_item_ids.split(',')), msg=good_item_ids)
bad_item_ids = bad_button.pop('callback_id', '')
self.assertEqual(1, len(bad_item_ids.split(',')), msg=bad_item_ids)
expected_button = {
'attachment_type': 'default',
'actions': [{
'name': 'new',
'type': 'button',
}],
}
expected_button['actions'][0]['text'] = '✅ Mark good items as read'
expected_button['actions'][0]['value'] = 'Good'
self.assertEqual(expected_button, good_button)
expected_button['actions'][0]['text'] = '✅ Mark bad items as read'
expected_button['actions'][0]['value'] = 'Bad'
self.assertEqual(expected_button, bad_button)
def test_list_good(self):
""" Test getting the list of all good items with POST."""
self._post_command(text='The coffee was great', slash_command='good')
self._post_command(text='The coffee was bad', slash_command='bad')
self._post_command(text='The tea was great', slash_command='good')
self._post_command(text='Improve the coffee', slash_command='try')
robo_response = self._post_command(text='list good', slash_command='retro')
good_button = robo_response.json['attachments'].pop(-1)
expected_list = {
'text': 'Retrospective items:',
'response_type': 'in_channel',
'attachments': [
{'color': 'good', 'title': 'Good'},
{'color': 'good', 'text': 'The coffee was great'},
{'color': 'good', 'text': 'The tea was great'},
],
}
self.assertEqual(expected_list, robo_response.json)
callback_id = good_button.pop('callback_id')
self.assertEqual(2, len(callback_id.split(',')), msg=callback_id)
expected_button = {
'attachment_type': 'default',
'actions': [{
'name': 'new',
'text': '✅ Mark good items as read',
'type': 'button',
'value': 'Good',
}],
}
self.assertEqual(expected_button, good_button)
def test_list_wrong_category(self):
""" Test listing by an unknown category."""
self._post_command(text='The coffee was great', slash_command='good')
self._post_command(text='The coffee was bad', slash_command='bad')
self._post_command(text='The tea was great', slash_command='good')
self._post_command(text='Improve the coffee', slash_command='try')
robo_response = self._post_command(text='list maybe good', slash_command='retro')
expected_list = {
'text': 'Wrong category "maybe good", should be "good", "bad", "try" or empty.',
'response_type': 'in_channel',
'attachments': [],
}
self.assertEqual(expected_list, robo_response.json)
def test_mood(self):
"""Test listing the mood for everyone."""
self.airtable_client.create('Moods', {
'Name': 'Cyrille',
'How are you feeling at Bayes': "I don't know, \nI'm ok",
'Feeling at bayes free text': 'NTD',
'How is your work going': "I'm doing a good job, \nI am quite productive",
})
robo_response = self._post_command(text='mood', slash_command='retro')
expected_response = textwrap.dedent('''\
:mag: Dear team, here is the weekly check in of this week :mag_right:
*Cyrille*
• _Feeling_
:face_with_rolling_eyes: I don't know
:no_mouth: I'm ok
> NTD
• _Work at Bayes_
:relaxed: I'm doing a good job
:nerd_face: I am quite productive
''')
self.assertEqual(
{
'attachments': [],
'response_type': 'in_channel',
'text': expected_response,
},
robo_response.json)
# def test_help(self):
# """ Test getting the help for the command.
# """
# robo_response = self.post_command(text='help', slash_command='retro')
# self.assertTrue('to see this message' in robo_response.data)
# def test_start_new_sprint(self):
# """ Test starting a new sprint with POST.
# """
# date = self._get_sprint_date()
# # Test first sprint logs 'good' item correctly
# robo_response = self.post_command(
# text='The coffee was great', slash_command='good')
# robo_response = self.post_command(text='list', slash_command='retro')
# expected_list = \
# '{{"text": "Retrospective items for *Sprint 1, started on {}*:", '.format(date) +\
# '"response_type": "in_channel", "attachments": [' +\
# '{"color": "good", "text": "The coffee was great", "title": "Good"}]}'
# self.assertEqual(robo_response.data, expected_list)
# # Start a new sprint and check that no item is in it
# robo_response = self.post_command(text='new', slash_command='retro')
# robo_response = self.post_command(text='list', slash_command='retro')
# expected_list = '{"text": "' +\
# 'No retrospective items yet for *Sprint 2, started on {}*.'.format(date) +\
# '", "response_type": "in_channel", "attachments": []}'
# self.assertEqual(robo_response.data, expected_list)
# # Test second sprint logs another 'good' item correctly
# robo_response = self.post_command(
# text='The coffee was great again', slash_command='good')
# robo_response = self.post_command(text='list', slash_command='retro')
# expected_list = \
# '{{"text": "Retrospective items for *Sprint 2, started on {}*:", '.format(date) +\
# '"response_type": "in_channel", "attachments": [' +\
# '{"color": "good", "text": "The coffee was great again", "title": "Good"}]}'
# self.assertEqual(robo_response.data, expected_list)
# def test_reset_current_sprint(self):
# """ Test deleting all retrospective items in the current sprint with POST.
# """
# # Test first sprint logs 'good' item correctly
# date = self._get_sprint_date()
# robo_response = self.post_command(
# text='The coffee was great', slash_command='good')
# robo_response = self.post_command(text='list', slash_command='retro')
# expected_list = \
# '{{"text": "Retrospective items for *Sprint 1, started on {}*:", '.format(date) +\
# '"response_type": "in_channel", "attachments": [' +\
# '{"color": "good", "text": "The coffee was great", "title": "Good"}]}'
# self.assertEqual(robo_response.data, expected_list)
# # Reset sprint
# robo_response = self.post_command(text='reset', slash_command='retro')
# expected_response = '{{"text": ' +\
# '"All retrospective items have been deleted for *Sprint 1, started on {}*.", '\
# .format(date) +\
# '"response_type": "in_channel", "attachments": []}'
# self.assertEqual(robo_response.data, expected_response)
# # And check that no items are found anymore
# date = self._get_sprint_date()
# robo_response = self.post_command(text='list', slash_command='retro')
# expected_list = '{"text": "' +\
# 'No retrospective items yet for *Sprint 1, started on {}*.'.format(date) +\
# '", "response_type": "in_channel", "attachments": []}'
# self.assertEqual(robo_response.data, expected_list)
# def _get_sprint_date(self):
# sprint = Sprint.get_current_sprint('test_user')
# date = sprint.creation_date.date()
# return date
if __name__ == '__main__':
unittest.main()