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