-
Notifications
You must be signed in to change notification settings - Fork 32
/
subscription_tests.py
374 lines (329 loc) · 17.7 KB
/
subscription_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
# coding=utf-8
from popular_proposal.tests import ProposingCycleTestCaseBase as TestCase
from popular_proposal.models import (ProposalLike,
Commitment,
PopularProposal,
)
from popular_proposal.subscriptions import (SubscriptionEventBase,
EventDispatcher,
ManyCitizensSupportingNotification,
YouAreAHeroNotification,
NewCommitmentNotificationToProposer,
)
from django.core import mail
from elections.models import Candidate, Election
from backend_candidate.models import CandidacyContact, Candidacy
from django.test import override_settings
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.utils import timezone
class TestNewCandidateCommitment(SubscriptionEventBase):
mail_template = 'new_commitment'
def get_who(self):
return self.proposal.likers.all()
def get_mail_from(self, person):
return person.email
class SubscriptionTestCaseBase(TestCase):
def setUp(self):
super(SubscriptionTestCaseBase, self).setUp()
self.proposal = PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=u'This is a title'
)
self.like = ProposalLike.objects.create(user=self.fiera,
proposal=self.proposal)
self.election = Election.objects.create(name=u'Elección para Arica')
self.election.area = self.arica
self.election.save()
self.candidate = Candidate.objects.get(id=1)
self.contact = CandidacyContact.objects.create(candidate=self.candidate,
mail='mail@perrito.cl')
self.election.candidates.add(self.candidate)
self.candidate2 = Candidate.objects.get(id=2)
self.contact2 = CandidacyContact.objects.create(candidate=self.candidate2,
mail='mail@gatito.cl')
self.election.candidates.add(self.candidate2)
class SubscriptionEventsTestCase(SubscriptionTestCaseBase):
def setUp(self):
super(SubscriptionEventsTestCase, self).setUp()
def test_raises_not_implemented_error(self):
class OnlyForThisTest(SubscriptionEventBase):
pass
bad_dispatcher = OnlyForThisTest(None)
with self.assertRaises(NotImplementedError):
bad_dispatcher.get_who()
with self.assertRaises(NotImplementedError):
bad_dispatcher.get_mail_from(None)
def test_triggering_an_event(self):
dispatcher = EventDispatcher()
dispatcher.register('test-event', [TestNewCandidateCommitment, ])
dispatcher.trigger('test-event', proposal=self.proposal)
self.assertEquals(len(mail.outbox), 1)
the_mail = mail.outbox[0]
self.assertIn(self.fiera.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
def test_letting_the_proposer_know_of_his_commitment(self):
commitment = Commitment.objects.create(candidate=self.candidate,
proposal=self.proposal,
commited=True)
previous_amount = len(mail.outbox)
notifier = NewCommitmentNotificationToProposer(proposal=self.proposal,
commitment=commitment)
notifier.notify()
self.assertEquals(len(mail.outbox), previous_amount + 1)
the_mail = mail.outbox[previous_amount]
self.assertIn(self.proposal.proposer.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertIn(self.candidate.name, the_mail.body)
def test_letting_the_proposer_know_of_his_non_commitment(self):
commitment = Commitment.objects.create(candidate=self.candidate,
proposal=self.proposal,
commited=False)
previous_amount = len(mail.outbox)
notifier = NewCommitmentNotificationToProposer(proposal=self.proposal,
commitment=commitment)
notifier.notify()
self.assertEquals(len(mail.outbox), previous_amount + 1)
the_mail = mail.outbox[previous_amount]
self.assertIn(self.proposal.proposer.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertIn(self.candidate.name, the_mail.body)
def test_notification_trigger_candidate_commit(self):
ProposalLike.objects.create(user=self.feli,
proposal=self.proposal)
Commitment.objects.create(candidate=self.candidate,
proposal=self.proposal,
commited=True)
self.assertEquals(len(mail.outbox), 1)
the_mail = mail.outbox[0]
# Citizens are not notified if a candidate commits to a proposal
# until further notice
# self.assertIn(self.feli.email, the_mail.to)
# self.assertEquals(len(the_mail.to), 1)
# self.assertIn(self.proposal.title, the_mail.body)
# self.assertIn(self.candidate.name, the_mail.body)
# the_mail = mail.outbox[1]
self.assertIn(self.fiera.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertIn(self.candidate.name, the_mail.body)
def test_there_are_two_different_emails_sent_if_a_candidate_has_not_commited(self):
ProposalLike.objects.create(user=self.feli,
proposal=self.proposal)
commitment = Commitment.objects.create(candidate=self.candidate,
proposal=self.proposal,
commited=True)
self.assertEquals(len(mail.outbox), 1)
the_mail = mail.outbox[0]
commitment.delete()
commitment = Commitment.objects.create(candidate=self.candidate,
proposal=self.proposal,
detail=u'Yo No me comprometo',
commited=False)
self.assertEquals(len(mail.outbox), 2)
the_mail2 = mail.outbox[1]
self.assertNotEqual(the_mail.subject, the_mail2.subject)
self.assertNotEqual(the_mail.body, the_mail2.body)
self.assertTrue(the_mail2.body)
self.assertTrue(the_mail2.subject)
def test_letting_a_candidate_know_about_how_many_citizens_are_supporting_a_proposal(self):
# According to setUp there should be at least one notification to
# self.candidate
Commitment.objects.create(candidate=self.candidate2,
proposal=self.proposal,
commited=True)
previous_amount = len(mail.outbox)
# We should not notify candidates that have already been commited
notifier = ManyCitizensSupportingNotification(proposal=self.proposal,
number=4)
notifier.notify()
self.assertEquals(len(mail.outbox), previous_amount + 1)
the_mail = mail.outbox[previous_amount]
self.assertIn(self.contact.mail, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertIn(self.candidate.name, the_mail.body)
self.assertIn(str(notifier.number), the_mail.body)
def test_letting_the_proposer_know_that_there_are_many_proposers(self):
notifier = YouAreAHeroNotification(proposal=self.proposal,
number=4)
notifier.notify()
self.assertEquals(len(mail.outbox), 1)
the_mail = mail.outbox[0]
self.assertIn(self.proposal.proposer.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertNotIn(self.candidate.name, the_mail.body)
self.assertIn(str(notifier.number), the_mail.body)
@override_settings(WHEN_TO_NOTIFY=[2, 3])
def test_like_a_proposal_signal(self):
ProposalLike.objects.create(user=self.fiera,
proposal=self.proposal)
# There is already a proposallike in the setUp
# so with the previous one we make 2
self.assertEquals(len(mail.outbox), 3)
# this should be the email to the proposer
the_mail = mail.outbox[0]
self.assertIn(self.proposal.proposer.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertNotIn(self.candidate.name, the_mail.body)
self.assertIn(str(2), the_mail.body)
# this should be the email to the candidate
the_mail = mail.outbox[1]
self.assertIn(the_mail.to[0], [self.contact.mail, self.contact2.mail])
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertTrue(self.candidate.name in the_mail.body or self.candidate2.name in the_mail.body)
self.assertIn(str(2), the_mail.body)
# this should be the email to the other candidate
the_mail = mail.outbox[2]
self.assertIn(the_mail.to[0], [self.contact.mail, self.contact2.mail])
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertTrue(self.candidate.name in the_mail.body or self.candidate2.name in the_mail.body)
self.assertIn(str(2), the_mail.body)
def test_proposal_notification_for_candidates(self):
previous_amount = len(mail.outbox)
proposal = PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=u'This is a title'
)
proposal.notify_candidates_of_new()
self.assertEquals(len(mail.outbox), previous_amount + 2)
first_mail = mail.outbox[previous_amount]
self.assertEquals(len(first_mail.to), 1)
self.assertIn(first_mail.to[0], [self.contact.mail, self.contact2.mail])
self.assertIn(proposal.title, first_mail.body)
self.assertIn(self.arica.name, first_mail.body)
self.assertIn(proposal.get_absolute_url(), first_mail.body)
second_mail = mail.outbox[previous_amount]
self.assertEquals(len(second_mail.to), 1)
self.assertIn(second_mail.to[0], [self.contact.mail, self.contact2.mail])
self.assertIn(proposal.title, second_mail.body)
self.assertIn(self.arica.name, second_mail.body)
self.assertIn(proposal.get_absolute_url(), first_mail.body)
def test_new_proposal_notification_with_login_info(self):
self.feli.last_login = None
self.feli.save()
CandidacyContact.objects.all().delete()
candidacy = Candidacy.objects.create(user=self.feli,
candidate=self.candidate
)
contact = CandidacyContact.objects.create(candidate=self.candidate,
mail='mail@perrito.cl',
initial_password='perrito',
candidacy=candidacy)
previous_amount = len(mail.outbox)
proposal = PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=u'This is a title'
)
proposal.notify_candidates_of_new()
first_mail = mail.outbox[previous_amount]
contact = self.candidate.contacts.all().first()
self.assertIn(self.feli.username, first_mail.body)
self.assertIn(contact.initial_password, first_mail.body)
def test_new_proposal_notification_with_media_naranja(self):
self.feli.last_login = timezone.now()
self.feli.save()
self.candidate.taken_positions.all().delete()
candidacy = Candidacy.objects.create(user=self.feli,
candidate=self.candidate
)
CandidacyContact.objects.create(candidate=self.candidate,
mail='mail@perrito.cl',
initial_password='perrito',
candidacy=candidacy)
previous_amount = len(mail.outbox)
proposal = PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=u'This is a title'
)
proposal.notify_candidates_of_new()
first_mail = mail.outbox[previous_amount]
self.candidate.contacts.all().first()
self.assertNotIn(self.feli.username, first_mail.body)
@override_settings(NOTIFY_CANDIDATES_OF_NEW_PROPOSAL=False)
def test_dont_notify_candidates_of_new_proposal(self):
PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=u'This is a title'
)
self.assertEquals(len(mail.outbox), 0)
@override_settings(NOTIFY_CANDIDATES=False)
class NotNotifyCandidatesUnlessToldSo(SubscriptionTestCaseBase):
def setUp(self):
super(NotNotifyCandidatesUnlessToldSo, self).setUp()
@override_settings(WHEN_TO_NOTIFY=[2, 3])
def test_not_notify_when_proposal_like(self):
ProposalLike.objects.create(user=self.fiera,
proposal=self.proposal)
# There is already a proposallike in the setUp
# so with the previous one we make 2
self.assertEquals(len(mail.outbox), 1)
# this should be the email to the proposer
the_mail = mail.outbox[0]
self.assertIn(self.proposal.proposer.email, the_mail.to)
self.assertEquals(len(the_mail.to), 1)
self.assertIn(self.proposal.title, the_mail.body)
self.assertNotIn(self.candidate.name, the_mail.body)
self.assertIn(str(2), the_mail.body)
def test_dont_notify_candidates_of_new_proposal(self):
PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=u'This is a title'
)
self.assertEquals(len(mail.outbox), 0)
class HomeWithProposalsViewTestCase(TestCase):
def setUp(self):
super(HomeWithProposalsViewTestCase, self).setUp()
amount_of_likers = [2, 4, 1, 3]
for i in range(1, 5):
title = 'this is a title' + str(i)
proposal = PopularProposal.objects.create(proposer=self.fiera,
area=self.arica,
data=self.data,
title=title
)
for j in range(0, amount_of_likers[i - 1]):
password = User.objects.make_random_password()
username = 'user_' + str(j) + '_' + str(i)
user = User.objects.create_user(username=username, password=password)
ProposalLike.objects.create(user=user,
proposal=proposal)
setattr(self, 'proposal' + str(i), proposal)
def test_manager_adds_extra_attr(self):
ps = PopularProposal.ordered.all()
p = ps.get(id=self.proposal2.id)
self.assertTrue(hasattr(p, 'num_likers'))
self.assertEquals(p.num_likers, 4)
def test_manager_order_by_likes(self):
ps = PopularProposal.ordered.by_likers()
# this proposal has 4 likes
self.assertEquals(ps.all()[0], self.proposal2)
# this proposal has 4 likes
self.assertEquals(ps.all()[1], self.proposal4)
# this proposal has 4 likes
self.assertEquals(ps.all()[2], self.proposal1)
# this proposal has 4 likes
self.assertEquals(ps.all()[3], self.proposal3)
def test_get_home_with_the_proposals(self):
url = reverse('home')
response = self.client.get(url)
ps = response.context['proposals_with_likers']
# this proposal has 4 likes
self.assertEquals(ps.all()[0], self.proposal2)
# this proposal has 4 likes
self.assertEquals(ps.all()[1], self.proposal4)
# this proposal has 4 likes
self.assertEquals(ps.all()[2], self.proposal1)