This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tests.py
377 lines (309 loc) · 15.3 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import urlparse
from datetime import datetime
import mock
from django.core.urlresolvers import reverse
from django.core import mail
from django.conf import settings
from go.account.utils import send_user_account_summary
from go.account.tasks import send_scheduled_account_summary
from go.base.tests.helpers import GoDjangoTestCase, DjangoVumiApiHelper
from go.vumitools.tests.helpers import GoMessageHelper
from go.billing.tests.helpers import mk_statement, get_billing_account
def get_statement_html_url(statement_id):
return reverse(
'billing:html_statement',
kwargs={'statement_id': statement_id})
def contains_in_order(haystack, needles):
indices = [haystack.find(needle) for needle in needles]
return (-1 not in indices) and (indices == sorted(indices))
class TestAccountViews(GoDjangoTestCase):
def setUp(self):
self.vumi_helper = self.add_helper(DjangoVumiApiHelper())
self.user_helper = self.vumi_helper.make_django_user()
self.client = self.vumi_helper.get_client()
def confirm(self, token_url):
url = urlparse.urlsplit(token_url)
return self.client.get(url.path, follow=True)
def test_view_details(self):
response = self.client.get(reverse('account:details'))
self.assertContains(response, "Account key")
self.assertContains(response, self.user_helper.account_key)
def test_update_details(self):
response = self.client.post(reverse('account:details'), {
'name': 'foo',
'surname': 'bar',
'email_address': 'foo@bar.com',
'existing_password': 'password',
'_account': True,
})
token_url = response.context['token_url']
[email] = mail.outbox
self.assertTrue(token_url in email.body)
# reload from db
self.confirm(token_url)
user = self.user_helper.get_django_user()
self.assertEqual(user.first_name, 'foo')
self.assertEqual(user.last_name, 'bar')
self.assertEqual(user.email, 'foo@bar.com')
self.assertTrue(user.check_password('password'))
def test_update_password(self):
self.client.post(reverse('account:details'), {
'email_address': 'user@domain.com',
'old_password': 'password',
'new_password1': 'new_password',
'new_password2': 'new_password',
'_password': True,
})
# reload from db
user = self.user_helper.get_django_user()
self.assertTrue(user.check_password('new_password'))
def test_update_msisdn_valid(self):
valid = ['+27761234567', '27761234567']
for msisdn in valid:
response = self.client.post(reverse('account:details'), {
'name': 'foo',
'surname': 'bar',
'email_address': 'user@domain.com',
'msisdn': msisdn,
'existing_password': 'password',
'_account': True,
})
token_url = response.context['token_url']
self.confirm(token_url)
user_account = self.user_helper.get_user_account()
self.assertEqual(user_account.msisdn, '+27761234567')
def test_update_msisdn_invalid(self):
invalid = ['+123', '123', 'abc']
for msisdn in invalid:
response = self.client.post(reverse('account:details'), {
'name': 'foo',
'surname': 'bar',
'email_address': 'user@domain.com',
'msisdn': msisdn,
'existing_password': 'password',
'_account': True,
})
self.assertFormError(response, 'account_form', 'msisdn',
'Please provide a valid phone number.')
self.assertEqual([], mail.outbox)
user_account = self.user_helper.get_user_account()
self.assertEqual(user_account.msisdn, None)
@staticmethod
def extract_notification_messages(response):
return [m.message for m in list(response.context['messages'])]
def test_confirm_start_conversation(self):
response = self.client.post(reverse('account:details'), {
'name': 'foo',
'surname': 'bar',
'email_address': 'user@domain.com',
'existing_password': 'password',
'msisdn': '+27761234567',
'confirm_start_conversation': True,
'_account': True,
})
token_url = response.context['token_url']
response = self.client.get(reverse('account:details'))
notifications = self.extract_notification_messages(response)
self.assertTrue(
'Please confirm this change by clicking on the link that was '
'just sent to your mailbox.' in notifications)
user_account = self.user_helper.get_user_account()
self.assertFalse(user_account.confirm_start_conversation)
[email] = mail.outbox
self.assertTrue(token_url in email.body)
response = self.confirm(token_url)
notifications = self.extract_notification_messages(response)
self.assertTrue('Your details are being updated' in notifications)
user_account = self.user_helper.get_user_account()
self.assertTrue(user_account.confirm_start_conversation)
def test_email_summary(self):
user_account = self.user_helper.get_user_account()
response = self.client.post(reverse('account:details'), {
'name': 'foo',
'surname': 'bar',
'email_address': 'user@domain.com',
'existing_password': 'password',
'msisdn': '+27761234567',
'email_summary': 'daily',
'_account': True,
})
token_url = response.context['token_url']
self.assertNotEqual(user_account.email_summary, 'daily')
response = self.confirm(token_url)
notifications = self.extract_notification_messages(response)
self.assertTrue('Your details are being updated' in notifications)
user_account = self.user_helper.get_user_account()
self.assertEqual(user_account.email_summary, 'daily')
def test_require_msisdn_if_confirm_start_conversation(self):
response = self.client.post(reverse('account:details'), {
'name': 'foo',
'surname': 'bar',
'email_address': 'user@domain.com',
'confirm_start_conversation': True,
'existing_password': 'password',
'_account': True,
})
self.assertFormError(response, 'account_form', 'msisdn',
'Please provide a valid phone number.')
self.assertEqual([], mail.outbox)
class TestEmail(GoDjangoTestCase):
def setUp(self):
self.vumi_helper = self.add_helper(DjangoVumiApiHelper())
self.user_helper = self.vumi_helper.make_django_user()
self.client = self.vumi_helper.get_client()
def test_email_sending(self):
response = self.client.post(reverse('account:details'), {
'_email': True,
'subject': 'foo',
'message': 'bar',
})
self.assertRedirects(response, reverse('account:details'))
[email] = mail.outbox
self.assertEqual(email.subject, 'foo')
self.assertEqual(
email.from_email, self.user_helper.get_django_user().email)
self.assertTrue('bar' in email.body)
def test_daily_account_summary(self):
msg_helper = GoMessageHelper(vumi_helper=self.vumi_helper)
conv = self.user_helper.create_conversation(
u'bulk_message', name=u'Test Conversation', started=True)
self.user_helper.contact_store.new_contact(
name=u'Contact', surname=u'One', msisdn=u"+27761234567")
self.user_helper.contact_store.new_contact(
name=u'Contact', surname=u'Two', msisdn=u"+27761234567")
msgs = msg_helper.add_inbound_to_conv(conv, 5)
msg_helper.add_replies_to_conv(conv, msgs)
# create a second conversation to test sorting
self.user_helper.create_conversation(u'bulk_message')
django_user = self.user_helper.get_django_user()
# schedule the task
send_user_account_summary(django_user)
[email] = mail.outbox
self.assertEqual(email.subject, 'Vumi Go Account Summary')
self.assertEqual(email.from_email, settings.DEFAULT_FROM_EMAIL)
self.assertEqual(email.recipients(), [django_user.email])
self.assertTrue('number of contacts: 2' in email.body)
self.assertTrue('number of unique contacts by contact number: 1'
in email.body)
self.assertTrue('number of messages sent: 5' in email.body)
self.assertTrue('number of messages received: 5' in email.body)
self.assertTrue('Group Message' in email.body)
self.assertTrue('Test Conversation' in email.body)
self.assertTrue('"Group Message" Sent: 5' in email.body)
self.assertTrue('"Group Message" Received: 5' in email.body)
# TODO fix once we support uniques properly again
self.assertTrue('Sent: 5 to 0 uniques.' in email.body)
self.assertTrue('Received: 5 from 0 uniques.' in email.body)
def test_send_scheduled_account_summary_task(self):
user_account = self.user_helper.get_user_account()
user_account.email_summary = u'daily'
user_account.save()
send_scheduled_account_summary('daily')
send_scheduled_account_summary('weekly')
[daily] = mail.outbox
self.assertEqual(daily.subject, 'Vumi Go Account Summary')
user_account.email_summary = u'weekly'
user_account.save()
send_scheduled_account_summary('daily')
send_scheduled_account_summary('weekly')
[daily, weekly] = mail.outbox
self.assertEqual(weekly.subject, 'Vumi Go Account Summary')
def test_billing_statements(self):
account = get_billing_account(self.user_helper.get_django_user())
s1 = mk_statement(account, from_date=datetime(2014, 1, 28))
s2 = mk_statement(account, from_date=datetime(2014, 2, 28))
s3 = mk_statement(account, from_date=datetime(2014, 3, 28))
resp = self.client.get(reverse('account:billing'))
self.assertContains(resp, '>Billing Statements<')
self.assertContains(resp, '>html<', count=3)
self.assertContains(resp, '>January 2014<')
self.assertContains(resp, get_statement_html_url(s1.id))
self.assertContains(resp, '>February 2014<')
self.assertContains(resp, get_statement_html_url(s2.id))
self.assertContains(resp, '>March 2014<')
self.assertContains(resp, get_statement_html_url(s3.id))
@mock.patch('go.billing.settings.STATEMENTS_PER_PAGE', 2)
@mock.patch('go.billing.settings.STATEMENTS_DEFAULT_ORDER_BY', 'from_date')
def test_billing_statements_pagination(self):
account = get_billing_account(self.user_helper.get_django_user())
mk_statement(account, from_date=datetime(2014, 1, 28))
mk_statement(account, from_date=datetime(2014, 2, 28))
mk_statement(account, from_date=datetime(2014, 3, 28))
mk_statement(account, from_date=datetime(2014, 4, 28))
mk_statement(account, from_date=datetime(2014, 5, 28))
resp = self.client.get(reverse('account:billing'), {'p': 1})
self.assertContains(resp, '>January 2014<')
self.assertContains(resp, '>February 2014<')
self.assertNotContains(resp, '>March 2014<')
self.assertNotContains(resp, '>April 2014<')
self.assertNotContains(resp, '>May 2014<')
resp = self.client.get(reverse('account:billing'), {'p': 2})
self.assertNotContains(resp, '>January 2014<')
self.assertNotContains(resp, '>February 2014<')
self.assertContains(resp, '>March 2014<')
self.assertContains(resp, '>April 2014<')
self.assertNotContains(resp, '>May 2014<')
resp = self.client.get(reverse('account:billing'), {'p': 3})
self.assertNotContains(resp, '>January 2014<')
self.assertNotContains(resp, '>February 2014<')
self.assertNotContains(resp, '>March 2014<')
self.assertNotContains(resp, '>April 2014<')
self.assertContains(resp, '>May 2014<')
@mock.patch('go.billing.settings.STATEMENTS_PER_PAGE', 2)
@mock.patch('go.billing.settings.STATEMENTS_DEFAULT_ORDER_BY', 'from_date')
def test_billing_statements_pagination_default(self):
account = get_billing_account(self.user_helper.get_django_user())
mk_statement(account, from_date=datetime(2014, 1, 28))
mk_statement(account, from_date=datetime(2014, 2, 28))
mk_statement(account, from_date=datetime(2014, 3, 28))
resp = self.client.get(reverse('account:billing'))
self.assertContains(resp, '>January 2014<')
self.assertContains(resp, '>February 2014<')
self.assertNotContains(resp, '>March 2014<')
@mock.patch('go.billing.settings.STATEMENTS_DEFAULT_ORDER_BY', 'from_date')
def test_billing_statements_default_order(self):
account = get_billing_account(self.user_helper.get_django_user())
mk_statement(account, from_date=datetime(2014, 3, 28))
mk_statement(account, from_date=datetime(2014, 1, 28))
mk_statement(account, from_date=datetime(2014, 2, 28))
resp = self.client.get(reverse('account:billing'))
self.assertTrue(contains_in_order(resp.content, [
'>January 2014<',
'>February 2014<',
'>March 2014<',
]))
def test_billing_statements_ascending_order(self):
account = get_billing_account(self.user_helper.get_django_user())
mk_statement(account, from_date=datetime(2014, 3, 28))
mk_statement(account, from_date=datetime(2014, 1, 28))
mk_statement(account, from_date=datetime(2014, 2, 28))
resp = self.client.get(reverse('account:billing'), {'o': 'from_date'})
self.assertTrue(contains_in_order(resp.content, [
'>January 2014<',
'>February 2014<',
'>March 2014<',
]))
def test_billing_statements_descending_order(self):
account = get_billing_account(self.user_helper.get_django_user())
mk_statement(account, from_date=datetime(2014, 3, 28))
mk_statement(account, from_date=datetime(2014, 1, 28))
mk_statement(account, from_date=datetime(2014, 2, 28))
resp = self.client.get(reverse('account:billing'), {'o': '-from_date'})
self.assertTrue(contains_in_order(resp.content, [
'>March 2014<',
'>February 2014<',
'>January 2014<',
]))
@mock.patch('go.billing.settings.STATEMENTS_DEFAULT_ORDER_BY', 'from_date')
def test_billing_statements_default_order_action(self):
resp = self.client.get(reverse('account:billing'))
self.assertContains(resp, 'href="?o=-from_date"')
self.assertNotContains(resp, 'href="?o=from_date"')
def test_billing_statements_ascending_order_action(self):
resp = self.client.get(reverse('account:billing'), {'o': '-from_date'})
self.assertNotContains(resp, 'href="?o=-from_date"')
self.assertContains(resp, 'href="?o=from_date"')
def test_billing_statements_descending_order_action(self):
resp = self.client.get(reverse('account:billing'), {'o': 'from_date'})
self.assertContains(resp, 'href="?o=-from_date"')
self.assertNotContains(resp, 'href="?o=from_date"')