1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from django.test import TestCase
23 from django.test.client import Client
24 from django.core import mail
25 from django.core.urlresolvers import reverse
26 from django.contrib.auth.models import User
27 from patchwork.models import EmailOptout, EmailConfirmation, Person
28 from patchwork.tests.utils import create_user
30 class MailSettingsTest(TestCase):
31 view = 'patchwork.views.mail.settings'
34 def testMailSettingsGET(self):
35 response = self.client.get(self.url)
36 self.assertEquals(response.status_code, 200)
37 self.assertTrue(response.context['form'])
39 def testMailSettingsPOST(self):
40 email = u'foo@example.com'
41 response = self.client.post(self.url, {'email': email})
42 self.assertEquals(response.status_code, 200)
43 self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
44 self.assertEquals(response.context['email'], email)
46 def testMailSettingsPOSTEmpty(self):
47 response = self.client.post(self.url, {'email': ''})
48 self.assertEquals(response.status_code, 200)
49 self.assertTemplateUsed(response, 'patchwork/mail-form.html')
50 self.assertFormError(response, 'form', 'email',
51 'This field is required.')
53 def testMailSettingsPOSTInvalid(self):
54 response = self.client.post(self.url, {'email': 'foo'})
55 self.assertEquals(response.status_code, 200)
56 self.assertTemplateUsed(response, 'patchwork/mail-form.html')
57 self.assertFormError(response, 'form', 'email',
58 'Enter a valid e-mail address.')
60 def testMailSettingsPOSTOptedIn(self):
61 email = u'foo@example.com'
62 response = self.client.post(self.url, {'email': email})
63 self.assertEquals(response.status_code, 200)
64 self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
65 self.assertEquals(response.context['is_optout'], False)
66 self.assertTrue('<strong>may</strong>' in response.content)
67 optout_url = reverse('patchwork.views.mail.optout')
68 self.assertTrue(('action="%s"' % optout_url) in response.content)
70 def testMailSettingsPOSTOptedOut(self):
71 email = u'foo@example.com'
72 EmailOptout(email = email).save()
73 response = self.client.post(self.url, {'email': email})
74 self.assertEquals(response.status_code, 200)
75 self.assertTemplateUsed(response, 'patchwork/mail-settings.html')
76 self.assertEquals(response.context['is_optout'], True)
77 self.assertTrue('<strong>may not</strong>' in response.content)
78 optin_url = reverse('patchwork.views.mail.optin')
79 self.assertTrue(('action="%s"' % optin_url) in response.content)
81 class OptoutRequestTest(TestCase):
82 view = 'patchwork.views.mail.optout'
85 def testOptOutRequestGET(self):
86 response = self.client.get(self.url)
87 self.assertRedirects(response, reverse('patchwork.views.mail.settings'))
89 def testOptoutRequestValidPOST(self):
90 email = u'foo@example.com'
91 response = self.client.post(self.url, {'email': email})
93 # check for a confirmation object
94 self.assertEquals(EmailConfirmation.objects.count(), 1)
95 conf = EmailConfirmation.objects.get(email = email)
97 # check confirmation page
98 self.assertEquals(response.status_code, 200)
99 self.assertEquals(response.context['confirmation'], conf)
100 self.assertTrue(email in response.content)
103 url = reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
104 self.assertEquals(len(mail.outbox), 1)
106 self.assertEquals(msg.to, [email])
107 self.assertEquals(msg.subject, 'Patchwork opt-out confirmation')
108 self.assertTrue(url in msg.body)
110 def testOptoutRequestInvalidPOSTEmpty(self):
111 response = self.client.post(self.url, {'email': ''})
112 self.assertEquals(response.status_code, 200)
113 self.assertFormError(response, 'form', 'email',
114 'This field is required.')
115 self.assertTrue(response.context['error'])
116 self.assertTrue('email_sent' not in response.context)
117 self.assertEquals(len(mail.outbox), 0)
119 def testOptoutRequestInvalidPOSTNonEmail(self):
120 response = self.client.post(self.url, {'email': 'foo'})
121 self.assertEquals(response.status_code, 200)
122 self.assertFormError(response, 'form', 'email',
123 'Enter a valid e-mail address.')
124 self.assertTrue(response.context['error'])
125 self.assertTrue('email_sent' not in response.context)
126 self.assertEquals(len(mail.outbox), 0)
128 class OptoutTest(TestCase):
129 view = 'patchwork.views.mail.optout'
133 self.email = u'foo@example.com'
134 self.conf = EmailConfirmation(type = 'optout', email = self.email)
137 def testOptoutValidHash(self):
138 url = reverse('patchwork.views.confirm',
139 kwargs = {'key': self.conf.key})
140 response = self.client.get(url)
142 self.assertEquals(response.status_code, 200)
143 self.assertTemplateUsed(response, 'patchwork/optout.html')
144 self.assertTrue(self.email in response.content)
146 # check that we've got an optout in the list
147 self.assertEquals(EmailOptout.objects.count(), 1)
148 self.assertEquals(EmailOptout.objects.all()[0].email, self.email)
150 # check that the confirmation is now inactive
151 self.assertFalse(EmailConfirmation.objects.get(
152 pk = self.conf.pk).active)
155 class OptoutPreexistingTest(OptoutTest):
156 """Test that a duplicated opt-out behaves the same as the initial one"""
158 super(OptoutPreexistingTest, self).setUp()
159 EmailOptout(email = self.email).save()
161 class OptinRequestTest(TestCase):
162 view = 'patchwork.views.mail.optin'
166 self.email = u'foo@example.com'
167 EmailOptout(email = self.email).save()
169 def testOptInRequestGET(self):
170 response = self.client.get(self.url)
171 self.assertRedirects(response, reverse('patchwork.views.mail.settings'))
173 def testOptInRequestValidPOST(self):
174 response = self.client.post(self.url, {'email': self.email})
176 # check for a confirmation object
177 self.assertEquals(EmailConfirmation.objects.count(), 1)
178 conf = EmailConfirmation.objects.get(email = self.email)
180 # check confirmation page
181 self.assertEquals(response.status_code, 200)
182 self.assertEquals(response.context['confirmation'], conf)
183 self.assertTrue(self.email in response.content)
186 url = reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
187 self.assertEquals(len(mail.outbox), 1)
189 self.assertEquals(msg.to, [self.email])
190 self.assertEquals(msg.subject, 'Patchwork opt-in confirmation')
191 self.assertTrue(url in msg.body)
193 def testOptoutRequestInvalidPOSTEmpty(self):
194 response = self.client.post(self.url, {'email': ''})
195 self.assertEquals(response.status_code, 200)
196 self.assertFormError(response, 'form', 'email',
197 'This field is required.')
198 self.assertTrue(response.context['error'])
199 self.assertTrue('email_sent' not in response.context)
200 self.assertEquals(len(mail.outbox), 0)
202 def testOptoutRequestInvalidPOSTNonEmail(self):
203 response = self.client.post(self.url, {'email': 'foo'})
204 self.assertEquals(response.status_code, 200)
205 self.assertFormError(response, 'form', 'email',
206 'Enter a valid e-mail address.')
207 self.assertTrue(response.context['error'])
208 self.assertTrue('email_sent' not in response.context)
209 self.assertEquals(len(mail.outbox), 0)
211 class OptinTest(TestCase):
214 self.email = u'foo@example.com'
215 self.optout = EmailOptout(email = self.email)
217 self.conf = EmailConfirmation(type = 'optin', email = self.email)
220 def testOptinValidHash(self):
221 url = reverse('patchwork.views.confirm',
222 kwargs = {'key': self.conf.key})
223 response = self.client.get(url)
225 self.assertEquals(response.status_code, 200)
226 self.assertTemplateUsed(response, 'patchwork/optin.html')
227 self.assertTrue(self.email in response.content)
229 # check that there's no optout remaining
230 self.assertEquals(EmailOptout.objects.count(), 0)
232 # check that the confirmation is now inactive
233 self.assertFalse(EmailConfirmation.objects.get(
234 pk = self.conf.pk).active)
236 class OptinWithoutOptoutTest(TestCase):
237 """Test an opt-in with no existing opt-out"""
238 view = 'patchwork.views.mail.optin'
241 def testOptInWithoutOptout(self):
242 email = u'foo@example.com'
243 response = self.client.post(self.url, {'email': email})
245 # check for an error message
246 self.assertEquals(response.status_code, 200)
247 self.assertTrue(bool(response.context['error']))
248 self.assertTrue('not on the patchwork opt-out list' in response.content)
250 class UserProfileOptoutFormTest(TestCase):
251 """Test that the correct optin/optout forms appear on the user profile
252 page, for logged-in users"""
254 view = 'patchwork.views.user.profile'
256 optout_url = reverse('patchwork.views.mail.optout')
257 optin_url = reverse('patchwork.views.mail.optin')
258 form_re_template = ('<form\s+[^>]*action="%(url)s"[^>]*>'
259 '.*?<input\s+[^>]*value="%(email)s"[^>]*>.*?'
261 secondary_email = 'test2@example.com'
264 self.user = create_user()
265 self.client.login(username = self.user.username,
266 password = self.user.username)
268 def _form_re(self, url, email):
269 return re.compile(self.form_re_template % {'url': url, 'email': email},
272 def testMainEmailOptoutForm(self):
273 form_re = self._form_re(self.optout_url, self.user.email)
274 response = self.client.get(self.url)
275 self.assertEquals(response.status_code, 200)
276 self.assertTrue(form_re.search(response.content) is not None)
278 def testMainEmailOptinForm(self):
279 EmailOptout(email = self.user.email).save()
280 form_re = self._form_re(self.optin_url, self.user.email)
281 response = self.client.get(self.url)
282 self.assertEquals(response.status_code, 200)
283 self.assertTrue(form_re.search(response.content) is not None)
285 def testSecondaryEmailOptoutForm(self):
286 p = Person(email = self.secondary_email, user = self.user)
289 form_re = self._form_re(self.optout_url, p.email)
290 response = self.client.get(self.url)
291 self.assertEquals(response.status_code, 200)
292 self.assertTrue(form_re.search(response.content) is not None)
294 def testSecondaryEmailOptinForm(self):
295 p = Person(email = self.secondary_email, user = self.user)
297 EmailOptout(email = p.email).save()
299 form_re = self._form_re(self.optin_url, self.user.email)
300 response = self.client.get(self.url)
301 self.assertEquals(response.status_code, 200)
302 self.assertTrue(form_re.search(response.content) is not None)