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
21 from django.test import TestCase
22 from django.test.client import Client
23 from django.core import mail
24 from django.core.urlresolvers import reverse
25 from django.contrib.auth.models import User
26 from patchwork.models import EmailConfirmation, Person
27 from patchwork.tests.utils import create_user
29 def _confirmation_url(conf):
30 return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
32 class TestUser(object):
36 email = 'test@example.com'
39 class RegistrationTest(TestCase):
41 self.user = TestUser()
42 self.client = Client()
43 self.default_data = {'username': self.user.username,
44 'first_name': self.user.firstname,
45 'last_name': self.user.lastname,
46 'email': self.user.email,
47 'password': self.user.password}
48 self.required_error = 'This field is required.'
49 self.invalid_error = 'Enter a valid value.'
51 def testRegistrationForm(self):
52 response = self.client.get('/register/')
53 self.assertEquals(response.status_code, 200)
54 self.assertTemplateUsed(response, 'patchwork/registration_form.html')
56 def testBlankFields(self):
57 for field in ['username', 'email', 'password']:
58 data = self.default_data.copy()
60 response = self.client.post('/register/', data)
61 self.assertEquals(response.status_code, 200)
62 self.assertFormError(response, 'form', field, self.required_error)
64 def testInvalidUsername(self):
65 data = self.default_data.copy()
66 data['username'] = 'invalid user'
67 response = self.client.post('/register/', data)
68 self.assertEquals(response.status_code, 200)
69 self.assertFormError(response, 'form', 'username', self.invalid_error)
71 def testExistingUsername(self):
73 data = self.default_data.copy()
74 data['username'] = user.username
75 response = self.client.post('/register/', data)
76 self.assertEquals(response.status_code, 200)
77 self.assertFormError(response, 'form', 'username',
78 'This username is already taken. Please choose another.')
80 def testExistingEmail(self):
82 data = self.default_data.copy()
83 data['email'] = user.email
84 response = self.client.post('/register/', data)
85 self.assertEquals(response.status_code, 200)
86 self.assertFormError(response, 'form', 'email',
87 'This email address is already in use ' + \
88 'for the account "%s".\n' % user.username)
90 def testValidRegistration(self):
91 response = self.client.post('/register/', self.default_data)
92 self.assertEquals(response.status_code, 200)
93 self.assertContains(response, 'confirmation email has been sent')
95 # check for presence of an inactive user object
96 users = User.objects.filter(username = self.user.username)
97 self.assertEquals(users.count(), 1)
99 self.assertEquals(user.username, self.user.username)
100 self.assertEquals(user.email, self.user.email)
101 self.assertEquals(user.is_active, False)
103 # check for confirmation object
104 confs = EmailConfirmation.objects.filter(user = user,
105 type = 'registration')
106 self.assertEquals(len(confs), 1)
108 self.assertEquals(conf.email, self.user.email)
110 # check for a sent mail
111 self.assertEquals(len(mail.outbox), 1)
113 self.assertEquals(msg.subject, 'Patchwork account confirmation')
114 self.assertTrue(self.user.email in msg.to)
115 self.assertTrue(_confirmation_url(conf) in msg.body)
117 # ...and that the URL is valid
118 response = self.client.get(_confirmation_url(conf))
119 self.assertEquals(response.status_code, 200)
121 class RegistrationConfirmationTest(TestCase):
124 self.user = TestUser()
125 self.default_data = {'username': self.user.username,
126 'first_name': self.user.firstname,
127 'last_name': self.user.lastname,
128 'email': self.user.email,
129 'password': self.user.password}
131 def testRegistrationConfirmation(self):
132 self.assertEqual(EmailConfirmation.objects.count(), 0)
133 response = self.client.post('/register/', self.default_data)
134 self.assertEquals(response.status_code, 200)
135 self.assertContains(response, 'confirmation email has been sent')
137 self.assertEqual(EmailConfirmation.objects.count(), 1)
138 conf = EmailConfirmation.objects.filter()[0]
139 self.assertFalse(conf.user.is_active)
140 self.assertTrue(conf.active)
142 response = self.client.get(_confirmation_url(conf))
143 self.assertEquals(response.status_code, 200)
144 self.assertTemplateUsed(response, 'patchwork/registration-confirm.html')
146 conf = EmailConfirmation.objects.get(pk = conf.pk)
147 self.assertTrue(conf.user.is_active)
148 self.assertFalse(conf.active)
150 def testRegistrationNewPersonSetup(self):
151 """ Check that the person object created after registration has the
155 self.assertEqual(EmailConfirmation.objects.count(), 0)
156 response = self.client.post('/register/', self.default_data)
157 self.assertEquals(response.status_code, 200)
160 conf = EmailConfirmation.objects.filter()[0]
161 response = self.client.get(_confirmation_url(conf))
162 self.assertEquals(response.status_code, 200)
164 person = Person.objects.get(email = self.user.email)
166 self.assertEquals(person.name,
167 self.user.firstname + ' ' + self.user.lastname)
169 def testRegistrationExistingPersonSetup(self):
170 """ Check that the person object created after registration has the
173 fullname = self.user.firstname + ' ' + self.user.lastname
174 person = Person(name = fullname, email = self.user.email)
178 self.assertEqual(EmailConfirmation.objects.count(), 0)
179 response = self.client.post('/register/', self.default_data)
180 self.assertEquals(response.status_code, 200)
183 conf = EmailConfirmation.objects.filter()[0]
184 response = self.client.get(_confirmation_url(conf))
185 self.assertEquals(response.status_code, 200)
187 person = Person.objects.get(email = self.user.email)
189 self.assertEquals(person.name, fullname)