forked from danhammer/pif-updates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
306 lines (250 loc) · 11.1 KB
/
tests.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
"""Unit test covereage."""
import datetime
import unittest
from google.appengine.ext import testbed
import admin
import cron
import model
import update
class TestModel(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.testbed.init_mail_stub()
self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)
def tearDown(self):
self.testbed.deactivate()
def test_Subscriber(self):
data = [dict(name='dan', mail='dan@hammer.com', team='pif',
status='subscribe', role='admin'),
dict(name='aaron', mail='aaron@hammer.com', team='pif',
status='subscribe')]
for x in data:
model.Subscriber.get_or_insert(**x)
self.assertEqual(2, len(model.Subscriber.subscribed('gfw')))
class TestUpdateHandler(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.testbed.init_mail_stub()
self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)
def tearDown(self):
self.testbed.deactivate()
def test_get_update(self):
bodies = {
"*line1\n*line2\n*line3": "* line1\n* line2\n* line3",
"*line1\r\n*line2\r\n*line3": "* line1\n* line2\n* line3",
"*line1\r\n": "* line1",
"*line1*line2 *line3": "* line1\n* line2\n* line3"}
for body, expected in bodies.iteritems():
message = update.UpdateHandler.get_update(body)
self.assertEqual(message, expected)
def test_get_urlsafe(self):
f = update.UpdateHandler.get_urlsafe
tests = {
'PIFfer <update+ag5kZXZ@pif-update.appspotmail.com>': 'ag5kZXZ',
'<update+ag5kZXZ@pif-update.appspotmail.com>': 'ag5kZXZ',
'update+ag5kZXZ@pif-update.appspotmail.com': 'ag5kZXZ',
}
urlsafe = 'ag5kZXZ'
for address, urlsafe in tests.iteritems():
self.assertEqual(f(address), urlsafe)
def test_process_update(self):
f = update.UpdateHandler.process_update
# Create SubscriberUpdate
date = datetime.datetime.now()
x = model.SubscriberUpdate.get_or_insert(
'dan', 'dan@hammer.com', 'pif', date)
urlsafe = x.key.urlsafe()
address = 'PIF <update+%s@pif-update.appspotmail.com>' % urlsafe
body = '* did nothing\n* met nobody'
f(address, body)
# Check that the update message made it
key_name = '%s+%s+%s' % ('pif', 'dan@hammer.com', date.isoformat())
x = model.SubscriberUpdate.get_by_id(key_name)
self.assertIsNotNone(x)
self.assertEqual(x.message, body)
class TestCronDigestHandler(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.testbed.init_mail_stub()
self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)
def tearDown(self):
self.testbed.deactivate()
def test_get_update(self):
f = cron.CronDigestHandler.get_update
update = f(model.SubscriberUpdate(
name='dan', mail='dan@hammer.com', message='* dude'))
self.assertEqual(update, 'dan <dan@hammer.com>\n* dude\n\n')
def test_get_digest_message(self):
f = cron.CronDigestHandler.get_digest_message
date = datetime.datetime.now()
msg = f('pif', 'digest', date, 'dan@hammer.com')
msg.send()
messages = self.mail_stub.get_sent_messages(to='dan@hammer.com')
self.assertEqual(1, len(messages))
message = messages[0]
self.assertEqual('dan@hammer.com', message.to)
body = [b.decode() for t, b in message.bodies('text/plain')][0]
self.assertEqual(body, 'digest')
def test_get_subscriber_update(self):
f = cron.CronDigestHandler.get_subscriber_updates
date = datetime.datetime.now()
x = model.SubscriberUpdate.get_or_insert(
name='dan', team='pif', mail='dan@hammer.com', date=date)
x.message = '* dude'
x.put()
x = model.SubscriberUpdate.get_or_insert(
name='dan', team='pif', mail='dan@hammer.com', date=date)
updates = f('pif', date)
self.assertEqual(len(updates), 1)
self.assertEqual(updates[0].message, '* dude')
def test_process_digest(self):
f = cron.CronDigestHandler.process_digest
# Test no latest update
f('pif')
self.assertIsNone(model.SubscriberDigest.query().get())
self.assertEqual([], self.mail_stub.get_sent_messages(to='dan@hammer.com'))
# Test has update but no subscribers
date = datetime.datetime.now()
model.Update.get_or_insert('pif', date)
f('pif')
self.assertIsNone(model.SubscriberDigest.query().get())
self.assertEqual([], self.mail_stub.get_sent_messages(to='dan@hammer.com'))
# Test has update and subscriber but no digest
model.Subscriber.get_or_insert(
name='dan', team='pif', mail='dan@hammer.com', status='subscribe')
digest = f('pif', test=True)
self.assertIsNone(model.SubscriberDigest.query().get())
self.assertIs(digest, '')
self.assertEqual([], self.mail_stub.get_sent_messages(to='dan@hammer.com'))
# Test has update and subscriber and digest
x = model.SubscriberUpdate.get_or_insert(
name='dan', team='pif', mail='dan@hammer.com', date=date)
x.message = '* dude'
x.put()
digest = f('pif')
key_name = '%s+%s+%s' % ('pif', 'dan@hammer.com', date.isoformat())
sd = model.SubscriberDigest.get_by_id(key_name)
self.assertIsNotNone(sd)
self.assertTrue(sd.sent)
self.assertEqual(digest, 'dan <dan@hammer.com>\n* dude\n\n')
self.assertNotEqual([], self.mail_stub.get_sent_messages(to='dan@hammer.com'))
class TestCronUpdateHandler(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.testbed.init_mail_stub()
self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)
def tearDown(self):
self.testbed.deactivate()
def test_get_reply_address(self):
f = cron.CronUpdateHandler.get_reply_address
urlsafe = 'foo'
expected = 'PIF <update+%s@pif-update.appspotmail.com>' % urlsafe
self.assertEqual(expected, f(urlsafe))
def test_get_update_message(self):
f = cron.CronUpdateHandler.get_update_message
team = 'pif'
to = 'daniel.hammer@gsa.gov'
sender = 'update+foo@pif-update.appspotmail.com'
date = datetime.datetime.now()
msg = f(team, to, sender, date)
msg.send()
messages = self.mail_stub.get_sent_messages(to='daniel.hammer@gsa.gov')
self.assertEqual(1, len(messages))
message = messages[0]
self.assertEqual('daniel.hammer@gsa.gov', message.to)
self.assertEqual(
'update+foo@pif-update.appspotmail.com', message.sender)
body = [b.decode() for t, b in message.bodies('text/plain')][0]
self.assertIsNot(body, '')
def test_process_subscriber_update(self):
f = cron.CronUpdateHandler.process_subscriber_update
data = dict(name='dan', mail='dan@hammer.com', team='pif',
status='subscribe', role='admin')
sub = model.Subscriber.get_or_insert(**data)
date = datetime.datetime.now()
f(date, sub)
key_name = '%s+%s+%s' % ('pif', 'dan@hammer.com', date.isoformat())
subup = model.SubscriberUpdate.get_by_id(key_name)
self.assertTrue(subup.sent)
messages = self.mail_stub.get_sent_messages(to='dan@hammer.com')
self.assertEqual(1, len(messages))
message = messages[0]
self.assertEqual('dan@hammer.com', message.to)
expect = "Just reply with a few brief bullets starting with *"
body = [b.decode() for t, b in message.bodies('text/plain')][0]
self.assertEqual(expect, body)
def test_process_update(self):
f = cron.CronUpdateHandler.process_update
data = [dict(name='dan', mail='dan@hammer.com', team='pif',
status='subscribe', role='admin'),
dict(name='aaron', mail='aaron@hammer.com', team='pif',
status='subscribe')]
date = datetime.datetime.now()
for x in data:
model.Subscriber.get_or_insert(**x)
f('pif', date)
subups = model.SubscriberUpdate.get_updates(date, 'pif')
self.assertEqual(len(subups), 2)
class TestAdminHandler(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.testbed.init_mail_stub()
self.mail_stub = self.testbed.get_stub(testbed.MAIL_SERVICE_NAME)
self.body = 'dan,dan@hammer.com,pif,subscribe,admin\n'
self.body += '\naaron, aaron@hammer.com , pif, subscribe'
def tearDown(self):
self.testbed.deactivate()
def test_is_admin(self):
f = admin.AdminHandler.is_admin
map(self.assertTrue,
map(f, ['daniel.hammer@gsa.gov', 'michelle.hood@gsa.gov']))
self.assertFalse(f('wannabe@admin.com'))
def test_get_subscriptions(self):
f = admin.AdminHandler.get_subscriptions
subs = [x for x in f(self.body)]
sub = dict(name='dan', mail='dan@hammer.com', team='pif',
status='subscribe', role='admin')
self.assertIn(sub, subs)
sub = dict(name='aaron', mail='aaron@hammer.com', team='pif',
status='subscribe')
self.assertIn(sub, subs)
self.assertTrue(len(subs) == 2)
def test_update_subscriptions(self):
f = admin.AdminHandler.update_subscription
# Create new subscription
data = dict(name='dan', mail='DAN@HAMMER.COM', team='PIF',
status='subscribe', role='admin')
f(data)
sub = model.Subscriber.get_by_id('dan@hammer.com+gfw')
expected = dict(name='dan', mail='dan@hammer.com', team='pif',
status='subscribe', role='admin')
self.assertDictContainsSubset(sub.to_dict(), expected)
# Update existing subscription
data = dict(name='DAN', mail='DAN@HAMMER.COM', team='PIF',
status='subscribe')
f(data)
sub = model.Subscriber.get_by_id('dan@hammer.com+gfw')
expected = dict(name='dan', mail='dan@hammer.com', team='pif',
status='subscribe', role=None)
self.assertDictContainsSubset(sub.to_dict(), expected)
if __name__ == '__main__':
reload(update)
reload(admin)
reload(cron)
reload(model)
unittest.main(exit=False)