]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/test_user.py
login: Focus the username field on load
[patchwork] / patchwork / tests / test_user.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 from django.test import TestCase
21 from django.core import mail
22 from django.core.urlresolvers import reverse
23 from django.conf import settings
24 from django.contrib.auth.models import User
25 from patchwork.models import EmailConfirmation, Person, Bundle, UserProfile
26 from patchwork.tests.utils import defaults, error_strings
27
28
29 def _confirmation_url(conf):
30     return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
31
32 class TestUser(object):
33
34     def __init__(self, username='testuser', email='test@example.com',
35                  secondary_email='test2@example.com'):
36         self.username = username
37         self.email = email
38         self.secondary_email = secondary_email
39         self.password = User.objects.make_random_password()
40         self.user = User.objects.create_user(
41             self.username, self.email, self.password)
42
43
44 class UserPersonRequestTest(TestCase):
45     def setUp(self):
46         self.user = TestUser()
47         self.client.login(username = self.user.username,
48                           password = self.user.password)
49         EmailConfirmation.objects.all().delete()
50
51     def testUserPersonRequestForm(self):
52         response = self.client.get('/user/link/')
53         self.assertEquals(response.status_code, 200)
54         self.assertTrue(response.context['linkform'])
55
56     def testUserPersonRequestEmpty(self):
57         response = self.client.post('/user/link/', {'email': ''})
58         self.assertEquals(response.status_code, 200)
59         self.assertTrue(response.context['linkform'])
60         self.assertFormError(response, 'linkform', 'email',
61                 'This field is required.')
62
63     def testUserPersonRequestInvalid(self):
64         response = self.client.post('/user/link/', {'email': 'foo'})
65         self.assertEquals(response.status_code, 200)
66         self.assertTrue(response.context['linkform'])
67         self.assertFormError(response, 'linkform', 'email',
68                                 error_strings['email'])
69
70     def testUserPersonRequestValid(self):
71         response = self.client.post('/user/link/',
72                                 {'email': self.user.secondary_email})
73         self.assertEquals(response.status_code, 200)
74         self.assertTrue(response.context['confirmation'])
75
76         # check that we have a confirmation saved
77         self.assertEquals(EmailConfirmation.objects.count(), 1)
78         conf = EmailConfirmation.objects.all()[0]
79         self.assertEquals(conf.user, self.user.user)
80         self.assertEquals(conf.email, self.user.secondary_email)
81         self.assertEquals(conf.type, 'userperson')
82
83         # check that an email has gone out...
84         self.assertEquals(len(mail.outbox), 1)
85         msg = mail.outbox[0]
86         self.assertEquals(msg.subject, 'Patchwork email address confirmation')
87         self.assertTrue(self.user.secondary_email in msg.to)
88         self.assertTrue(_confirmation_url(conf) in msg.body)
89
90         # ...and that the URL is valid
91         response = self.client.get(_confirmation_url(conf))
92         self.assertEquals(response.status_code, 200)
93         self.assertTemplateUsed(response, 'patchwork/user-link-confirm.html')
94
95 class UserPersonConfirmTest(TestCase):
96     def setUp(self):
97         EmailConfirmation.objects.all().delete()
98         Person.objects.all().delete()
99         self.user = TestUser()
100         self.client.login(username = self.user.username,
101                           password = self.user.password)
102         self.conf = EmailConfirmation(type = 'userperson',
103                                       email = self.user.secondary_email,
104                                       user = self.user.user)
105         self.conf.save()
106
107     def testUserPersonConfirm(self):
108         self.assertEquals(Person.objects.count(), 0)
109         response = self.client.get(_confirmation_url(self.conf))
110         self.assertEquals(response.status_code, 200)
111
112         # check that the Person object has been created and linked
113         self.assertEquals(Person.objects.count(), 1)
114         person = Person.objects.get(email = self.user.secondary_email)
115         self.assertEquals(person.email, self.user.secondary_email)
116         self.assertEquals(person.user, self.user.user)
117
118         # check that the confirmation has been marked as inactive. We
119         # need to reload the confirmation to check this.
120         conf = EmailConfirmation.objects.get(pk = self.conf.pk)
121         self.assertEquals(conf.active, False)
122
123 class UserLoginRedirectTest(TestCase):
124
125     def testUserLoginRedirect(self):
126         url = '/user/'
127         response = self.client.get(url)
128         self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
129
130 class UserProfileTest(TestCase):
131
132     def setUp(self):
133         self.user = TestUser()
134         self.client.login(username = self.user.username,
135                           password = self.user.password)
136
137     def testUserProfile(self):
138         response = self.client.get('/user/')
139         self.assertContains(response, 'User Profile: %s' % self.user.username)
140         self.assertContains(response, 'User Profile: %s' % self.user.username)
141
142     def testUserProfileNoBundles(self):
143         response = self.client.get('/user/')
144         self.assertContains(response, 'You have no bundles')
145
146     def testUserProfileBundles(self):
147         project = defaults.project
148         project.save()
149
150         bundle = Bundle(project = project, name = 'test-1',
151                         owner = self.user.user)
152         bundle.save()
153
154         response = self.client.get('/user/')
155
156         self.assertContains(response, 'You have the following bundle')
157         self.assertContains(response, bundle.get_absolute_url())
158
159     def testUserProfileValidPost(self):
160         user_profile = UserProfile.objects.get(user=self.user.user.id)
161         old_ppp = user_profile.patches_per_page
162         new_ppp = old_ppp + 1
163
164         response = self.client.post('/user/', {'patches_per_page': new_ppp})
165
166         user_profile = UserProfile.objects.get(user=self.user.user.id)
167         self.assertEquals(user_profile.patches_per_page, new_ppp)
168
169     def testUserProfileInvalidPost(self):
170         user_profile = UserProfile.objects.get(user=self.user.user.id)
171         old_ppp = user_profile.patches_per_page
172         new_ppp = -1
173
174         response = self.client.post('/user/', {'patches_per_page': new_ppp})
175
176         user_profile = UserProfile.objects.get(user=self.user.user.id)
177         self.assertEquals(user_profile.patches_per_page, old_ppp)
178
179
180 class UserPasswordChangeTest(TestCase):
181     user = None
182
183     def setUp(self):
184         self.form_url = reverse('django.contrib.auth.views.password_change')
185         self.done_url = reverse(
186             'django.contrib.auth.views.password_change_done')
187
188     def testPasswordChangeForm(self):
189         self.user = TestUser()
190         self.client.login(username = self.user.username,
191                           password = self.user.password)
192
193         response = self.client.get(self.form_url)
194         self.assertContains(response, 'Change my password')
195
196     def testPasswordChange(self):
197         self.user = TestUser()
198         self.client.login(username = self.user.username,
199                           password = self.user.password)
200
201         old_password = self.user.password
202         new_password = User.objects.make_random_password()
203
204         data = {
205             'old_password': old_password,
206             'new_password1': new_password,
207             'new_password2': new_password,
208         }
209
210         response = self.client.post(self.form_url, data)
211         self.assertRedirects(response, self.done_url)
212
213         user = User.objects.get(id = self.user.user.id)
214
215         self.assertFalse(user.check_password(old_password))
216         self.assertTrue(user.check_password(new_password))
217
218         response = self.client.get(self.done_url)
219         self.assertContains(response,
220                 "Your password has been changed sucessfully")
221
222 class UserUnlinkTest(TestCase):
223     def setUp(self):
224         self.form_url = '/user/unlink/{pid}/'
225         self.done_url = '/user/'
226         EmailConfirmation.objects.all().delete()
227         Person.objects.all().delete()
228
229     def testUnlinkPrimaryEmail(self):
230         user = TestUser()
231         self.client.login(username=user.username,
232                           password=user.password)
233         conf = EmailConfirmation(type='userperson',
234                                  email=user.email,
235                                  user=user.user)
236         conf.save()
237         self.client.get(_confirmation_url(conf))
238
239         person = Person.objects.get(email=user.email)
240         response = self.client.post(self.form_url.format(pid=str(person.id)))
241         self.assertRedirects(response, self.done_url)
242
243         person = Person.objects.get(email=user.email)
244         self.assertEquals(person.user, user.user)
245
246     def testUnlinkSecondaryEmail(self):
247         user = TestUser()
248         self.client.login(username=user.username,
249                           password=user.password)
250         conf = EmailConfirmation(type='userperson',
251                                  email=user.secondary_email,
252                                  user=user.user)
253         conf.save()
254         self.client.get(_confirmation_url(conf))
255
256         person = Person.objects.get(email=user.secondary_email)
257         response = self.client.post(self.form_url.format(pid=str(person.id)))
258         self.assertRedirects(response, self.done_url)
259
260         person = Person.objects.get(email=user.secondary_email)
261         self.assertEquals(person.user, None)
262
263     def testUnlinkAnotherUser(self):
264         user = TestUser()
265         self.client.login(username=user.username,
266                           password=user.password)
267
268         other_user = TestUser('testuser_other', 'test_other@example.com',
269                               'test_other2@example.com')
270         conf = EmailConfirmation(type='userperson',
271                                  email=other_user.email,
272                                  user=other_user.user)
273         conf.save()
274         self.client.get(_confirmation_url(conf))
275
276         person = Person.objects.get(email=other_user.email)
277         response = self.client.post(self.form_url.format(pid=str(person.id)))
278         self.assertRedirects(response, self.done_url)
279
280         person = Person.objects.get(email=other_user.email)
281         self.assertEquals(person.user, None)
282
283     def testUnlinkNonPost(self):
284         user = TestUser()
285         self.client.login(username=user.username,
286                           password=user.password)
287         conf = EmailConfirmation(type='userperson',
288                                  email=user.secondary_email,
289                                  user=user.user)
290         conf.save()
291         self.client.get(_confirmation_url(conf))
292
293         person = Person.objects.get(email=user.secondary_email)
294         response = self.client.get(self.form_url.format(pid=str(person.id)))
295         self.assertRedirects(response, self.done_url)
296
297         person = Person.objects.get(email=user.secondary_email)
298         self.assertEquals(person.user, user.user)