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