]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/test_user.py
61a844377bb6fd1e2bc4756bc5841d9e1882335e
[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
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 class UserPasswordChangeTest(TestCase):
161     form_url = reverse('django.contrib.auth.views.password_change')
162     done_url = reverse('django.contrib.auth.views.password_change_done')
163
164     def testPasswordChangeForm(self):
165         self.user = TestUser()
166         self.client.login(username = self.user.username,
167                           password = self.user.password)
168
169         response = self.client.get(self.form_url)
170         self.assertContains(response, 'Change my password')
171
172     def testPasswordChange(self):
173         self.user = TestUser()
174         self.client.login(username = self.user.username,
175                           password = self.user.password)
176
177         old_password = self.user.password
178         new_password = User.objects.make_random_password()
179
180         data = {
181             'old_password': old_password,
182             'new_password1': new_password,
183             'new_password2': new_password,
184         }
185
186         response = self.client.post(self.form_url, data)
187         self.assertRedirects(response, self.done_url)
188
189         user = User.objects.get(id = self.user.user.id)
190
191         self.assertFalse(user.check_password(old_password))
192         self.assertTrue(user.check_password(new_password))
193
194         response = self.client.get(self.done_url)
195         self.assertContains(response,
196                 "Your password has been changed sucessfully")
197
198 class UserUnlinkTest(TestCase):
199     def setUp(self):
200         self.form_url = '/user/unlink/{pid}/'
201         self.done_url = '/user/'
202         EmailConfirmation.objects.all().delete()
203         Person.objects.all().delete()
204
205     def testUnlinkPrimaryEmail(self):
206         user = TestUser()
207         self.client.login(username=user.username,
208                           password=user.password)
209         conf = EmailConfirmation(type='userperson',
210                                  email=user.email,
211                                  user=user.user)
212         conf.save()
213         self.client.get(_confirmation_url(conf))
214
215         person = Person.objects.get(email=user.email)
216         response = self.client.post(self.form_url.format(pid=str(person.id)))
217         self.assertRedirects(response, self.done_url)
218
219         person = Person.objects.get(email=user.email)
220         self.assertEquals(person.user, user.user)
221
222     def testUnlinkSecondaryEmail(self):
223         user = TestUser()
224         self.client.login(username=user.username,
225                           password=user.password)
226         conf = EmailConfirmation(type='userperson',
227                                  email=user.secondary_email,
228                                  user=user.user)
229         conf.save()
230         self.client.get(_confirmation_url(conf))
231
232         person = Person.objects.get(email=user.secondary_email)
233         response = self.client.post(self.form_url.format(pid=str(person.id)))
234         self.assertRedirects(response, self.done_url)
235
236         person = Person.objects.get(email=user.secondary_email)
237         self.assertEquals(person.user, None)
238
239     def testUnlinkAnotherUser(self):
240         user = TestUser()
241         self.client.login(username=user.username,
242                           password=user.password)
243
244         other_user = TestUser('testuser_other', 'test_other@example.com',
245                               'test_other2@example.com')
246         conf = EmailConfirmation(type='userperson',
247                                  email=other_user.email,
248                                  user=other_user.user)
249         conf.save()
250         self.client.get(_confirmation_url(conf))
251
252         person = Person.objects.get(email=other_user.email)
253         response = self.client.post(self.form_url.format(pid=str(person.id)))
254         self.assertRedirects(response, self.done_url)
255
256         person = Person.objects.get(email=other_user.email)
257         self.assertEquals(person.user, None)
258
259     def testUnlinkNonPost(self):
260         user = TestUser()
261         self.client.login(username=user.username,
262                           password=user.password)
263         conf = EmailConfirmation(type='userperson',
264                                  email=user.secondary_email,
265                                  user=user.user)
266         conf.save()
267         self.client.get(_confirmation_url(conf))
268
269         person = Person.objects.get(email=user.secondary_email)
270         response = self.client.get(self.form_url.format(pid=str(person.id)))
271         self.assertRedirects(response, self.done_url)
272
273         person = Person.objects.get(email=user.secondary_email)
274         self.assertEquals(person.user, user.user)