-
Notifications
You must be signed in to change notification settings - Fork 0
/
slacklib_test.py
271 lines (229 loc) · 11 KB
/
slacklib_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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import textwrap
import unittest
# Update sys.path so it can find these. We just need to add
# 'google_appengine', but we add all of $PATH to be easy. This
# assumes the google_appengine directory is on the path.
import os
import sys
sys.path.extend(os.environ['PATH'].split(':'))
import dev_appserver
dev_appserver.fix_sys_path()
from google.appengine.ext import db
from google.appengine.ext import testbed
import models
import slacklib
class SlashCommandTest(unittest.TestCase):
def _mock_data(self):
# The fictional day for these tests Wednesday, July 29, 2015
slacklib._TODAY_FN = lambda: datetime.datetime(2015, 7, 29)
# Stuart created his account, but has never once filled out a snippet
db.put(models.User(email='[email protected]'))
# Fleetwood has two recent snippets, and always uses markdown lists,
# but sometimes uses different list indicators or indention.
db.put(models.User(email='[email protected]'))
db.put(models.Snippet(
email='[email protected]',
week=datetime.date(2015, 7, 27),
text=textwrap.dedent("""
* went for a walk
* sniffed some things
* hoping to sniff more things! #yolo
""")
))
db.put(models.Snippet(
email='[email protected]',
week=datetime.date(2015, 7, 20),
text=textwrap.dedent("""
- lots of walks this week
- not enough sniffing, hope to remedy next week!
""")
))
# Toby has filled out two snippets, but missed a week in-between while
# on vacation. When he got back from vacation he was still jetlagged so
# he wrote a longform paragraph instead of a list.
db.put(models.User(email='[email protected]'))
db.put(models.Snippet(
email='[email protected]',
week=datetime.date(2015, 7, 13),
text=textwrap.dedent("""
- going on vacation next week, so excited!
""")
))
db.put(models.Snippet(
email='[email protected]',
week=datetime.date(2015, 7, 27),
text=textwrap.dedent("""
I JUST GOT BACK FROM VACATION IT WAS TOTALLY AWESOME AND I SNIFFED
ALL SORTS OF THINGS. I GUESS I NEED TO WRITE SOMETHING HERE, HUH?
OK THEN:
- I had fun.
LUNCHTIME SUCKERS!
""")
))
# Fozzie tried hard to create an entry manually in the previous week,
# but didn't understand markdown list syntax and got discouraged (so
# has no entry this week, and a malformed one last week).
db.put(models.User(email='[email protected]'))
db.put(models.Snippet(
email='[email protected]',
week=datetime.date(2015, 7, 20),
text=textwrap.dedent("""
-is this how I list?
-why is it not formatting??!?
""")
))
def _most_recent_snippet(self, user_email):
snippets_q = models.Snippet.all()
snippets_q.filter('email = ', user_email)
snippets_q.order('-week') # newest snippet first
return snippets_q.fetch(1)[0]
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self._mock_data()
def tearDown(self):
self.testbed.deactivate()
def testDumpCommand_empty(self):
# user without a recent snippet should just see null text
response = slacklib.command_dump('[email protected]')
self.assertIn('No snippet yet for this week', response)
def testDumpCommand_formatting(self):
# user with a snippet should just get it back unformatted
response = slacklib.command_dump('[email protected]')
self.assertIn('#yolo', response)
def testDumpCommand_noAccount(self):
# user without an account should get a helpful error message
response = slacklib.command_dump('[email protected]')
self.assertIn("You don't appear to have a snippets account", response)
self.assertIn("Slack email address: [email protected]", response)
def testListCommand_empty(self):
# user without a recent snippet should get a helpful message
response = slacklib.command_list('[email protected]')
self.assertIn(
"You don't have any snippets for this week yet!", response
)
def testListCommand_formatting(self):
# user with snippet should get back a formatted, numbered list
response = slacklib.command_list('[email protected]')
self.assertIn('> :pushpin: *[0]* went for a walk', response)
self.assertIn(
'> :pushpin: *[2]* hoping to sniff more things! #yolo',
response
)
def testListCommand_noAccount(self):
# user without an account should get a helpful error message
response = slacklib.command_list('[email protected]')
self.assertIn("You don't appear to have a snippets account", response)
def testLastCommand_empty(self):
# user without a snippet last week should get a helpful message
expect = "You didn't have any snippets last week!"
# stuart never fills out
self.assertIn(expect, slacklib.command_last('[email protected]'))
# toby skipped last week
self.assertIn(expect, slacklib.command_last('[email protected]'))
def testLastCommand_formatting(self):
# user with snippet should get back a formatted, numbered list
response = slacklib.command_last('[email protected]')
self.assertIn('> :pushpin: *[0]* lots of walks this week', response)
def testLastCommand_noAccount(self):
# user without an account should get a helpful error message
response = slacklib.command_last('[email protected]')
self.assertIn("You don't appear to have a snippets account", response)
def testBadMarkdown_listCommand(self):
toby_recent = slacklib.command_list('[email protected]')
self.assertIn("not in a format I understand", toby_recent)
def testBadMarkdown_lastCommand(self):
fozzie_last = slacklib.command_last('[email protected]')
self.assertIn("not in a format I understand", fozzie_last)
def testAddCommand_blank(self):
# blank slate should be easy...
r = slacklib.command_add('[email protected]', 'went to the park')
t = self._most_recent_snippet('[email protected]')
self.assertIn("Added *went to the park* to your weekly snippets", r)
self.assertEquals('- went to the park', t.text)
self.assertEquals(True, t.is_markdown)
def testAddCommand_existing(self):
# on this one, the user markdown formatting gets altered/standardized
slacklib.command_add('[email protected]', 'went to the park')
t = self._most_recent_snippet('[email protected]')
expected = textwrap.dedent("""
- went for a walk
- sniffed some things
- hoping to sniff more things! #yolo
- went to the park
""").strip()
self.assertEqual(expected, t.text)
self.assertEquals(True, t.is_markdown)
def testAddCommand_existingIsMalformed(self):
# we should be told we cannot to add to a snippet that is malformed!
toby_email = '[email protected]'
r = slacklib.command_add(toby_email, 'went to the park')
self.assertIn("Your snippets are not in a format I understand", r)
# ...and the existing snippets should not have been touched
t = self._most_recent_snippet(toby_email)
self.assertNotIn("went to the park", t.text)
self.assertIn("LUNCHTIME SUCKERS!", t.text)
self.assertEquals(False, t.is_markdown)
def testAddCommand_noArgs(self):
# we need to handle when they try to add nothing!
r = slacklib.command_add('[email protected]', '')
self.assertIn("*what* do you want me to add exactly?", r)
def testAddCommand_noAccount(self):
# dont crash horribly if user doesnt exist
r = slacklib.command_add('[email protected]', 'how is account formed?')
self.assertIn("You don't appear to have a snippets account", r)
def testAddCommand_markupUsernames(self):
# usernames should be marked up properly so they get syntax highlighted
r = slacklib.command_add('[email protected]', 'ate w/ @toby, yay')
t = self._most_recent_snippet('[email protected]')
self.assertIn("ate w/ <@toby>, yay", r)
self.assertIn("- ate w/ <@toby>, yay", t.text)
def testAddCommand_unicode(self):
r = slacklib.command_add('[email protected]', u'i “like” food')
t = self._most_recent_snippet('[email protected]')
self.assertIn('i “like” food', r)
self.assertIn('i “like” food', t.text)
def testDelCommand_noArgs(self):
# we need to handle when they try to add nothing!
r = slacklib.command_del('[email protected]', [])
self.assertIn("*what* do you want me to delete exactly?", r)
def testDelCommand_noAccount(self):
# dont crash horribly if user doesnt exist
r = slacklib.command_del('[email protected]', ['1'])
self.assertIn("You don't appear to have a snippets account", r)
def testDelCommand_normalCase(self):
r = slacklib.command_del('[email protected]', ['1'])
t = self._most_recent_snippet('[email protected]')
self.assertIn(
"Removed *sniffed some things* from your weekly snippets", r)
expected = textwrap.dedent("""
- went for a walk
- hoping to sniff more things! #yolo
""").strip()
self.assertEqual(expected, t.text)
self.assertEquals(True, t.is_markdown)
def testDelCommand_nonexistentIndex(self):
r1 = slacklib.command_del('[email protected]', ['0'])
r2 = slacklib.command_del('[email protected]', ['4'])
expected = "You don't have anything at that index"
self.assertIn(expected, r1)
self.assertIn(expected, r2)
def testDelCommand_indexNaN(self):
r = slacklib.command_del('[email protected]', ['one'])
self.assertIn("*what* do you want me to delete exactly?", r)
def testDelCommand_existingIsMalformed(self):
# we should be told we cannot to add to a snippet that is malformed!
r = slacklib.command_del('[email protected]', ['0'])
self.assertIn("Your snippets are not in a format I understand", r)
# ...and the existing snippets should not have been touched
t = self._most_recent_snippet('[email protected]')
self.assertIn("I had fun", t.text)
self.assertEquals(False, t.is_markdown)
if __name__ == '__main__':
unittest.main()