]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/registration.py
models: Update Person & UserProfile object whenever User is saved
[patchwork] / apps / patchwork / tests / registration.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
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.
10 #
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.
15 #
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
19
20 import unittest
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
28
29 def _confirmation_url(conf):
30     return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
31
32 class TestUser(object):
33     firstname = 'Test'
34     lastname = 'User'
35     username = 'testuser'
36     email = 'test@example.com'
37     password = 'foobar'
38
39 class RegistrationTest(TestCase):
40     def setUp(self):
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.'
50
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')
55
56     def testBlankFields(self):
57         for field in ['username', 'email', 'password']:
58             data = self.default_data.copy()
59             del data[field]
60             response = self.client.post('/register/', data)
61             self.assertEquals(response.status_code, 200)
62             self.assertFormError(response, 'form', field, self.required_error)
63
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)
70
71     def testExistingUsername(self):
72         user = create_user()
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.')
79
80     def testExistingEmail(self):
81         user = create_user()
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)
89
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')
94
95         # check for presence of an inactive user object
96         users = User.objects.filter(username = self.user.username)
97         self.assertEquals(users.count(), 1)
98         user = users[0]
99         self.assertEquals(user.username, self.user.username)
100         self.assertEquals(user.email, self.user.email)
101         self.assertEquals(user.is_active, False)
102
103         # check for confirmation object
104         confs = EmailConfirmation.objects.filter(user = user,
105                                                  type = 'registration')
106         self.assertEquals(len(confs), 1)
107         conf = confs[0]
108         self.assertEquals(conf.email, self.user.email)
109
110         # check for a sent mail
111         self.assertEquals(len(mail.outbox), 1)
112         msg = mail.outbox[0]
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)
116
117         # ...and that the URL is valid
118         response = self.client.get(_confirmation_url(conf))
119         self.assertEquals(response.status_code, 200)
120
121 class RegistrationConfirmationTest(TestCase):
122
123     def setUp(self):
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}
130
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')
136
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)
141
142         response = self.client.get(_confirmation_url(conf))
143         self.assertEquals(response.status_code, 200)
144         self.assertTemplateUsed(response, 'patchwork/registration-confirm.html')
145
146         conf = EmailConfirmation.objects.get(pk = conf.pk)
147         self.assertTrue(conf.user.is_active)
148         self.assertFalse(conf.active)
149
150     def testRegistrationNewPersonSetup(self):
151         """ Check that the person object created after registration has the
152             correct details """
153
154         # register
155         self.assertEqual(EmailConfirmation.objects.count(), 0)
156         response = self.client.post('/register/', self.default_data)
157         self.assertEquals(response.status_code, 200)
158
159         # confirm
160         conf = EmailConfirmation.objects.filter()[0]
161         response = self.client.get(_confirmation_url(conf))
162         self.assertEquals(response.status_code, 200)
163
164         person = Person.objects.get(email = self.user.email)
165
166         self.assertEquals(person.name,
167                     self.user.firstname + ' ' + self.user.lastname)
168
169     def testRegistrationExistingPersonSetup(self):
170         """ Check that the person object created after registration has the
171             correct details """
172
173         fullname = self.user.firstname + ' '  + self.user.lastname
174         person = Person(name = fullname, email = self.user.email)
175         person.save()
176
177         # register
178         self.assertEqual(EmailConfirmation.objects.count(), 0)
179         response = self.client.post('/register/', self.default_data)
180         self.assertEquals(response.status_code, 200)
181
182         # confirm
183         conf = EmailConfirmation.objects.filter()[0]
184         response = self.client.get(_confirmation_url(conf))
185         self.assertEquals(response.status_code, 200)
186
187         person = Person.objects.get(email = self.user.email)
188
189         self.assertEquals(person.name, fullname)
190