1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
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
29 def _confirmation_url(conf):
30 return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
32 class TestUser(object):
34 def __init__(self, username='testuser', email='test@example.com',
35 secondary_email='test2@example.com'):
36 self.username = username
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)
44 class UserPersonRequestTest(TestCase):
46 self.user = TestUser()
47 self.client.login(username = self.user.username,
48 password = self.user.password)
49 EmailConfirmation.objects.all().delete()
51 def testUserPersonRequestForm(self):
52 response = self.client.get('/user/link/')
53 self.assertEquals(response.status_code, 200)
54 self.assertTrue(response.context['linkform'])
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.')
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'])
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'])
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')
83 # check that an email has gone out...
84 self.assertEquals(len(mail.outbox), 1)
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)
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')
95 class UserPersonConfirmTest(TestCase):
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)
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)
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)
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)
123 class UserLoginRedirectTest(TestCase):
125 def testUserLoginRedirect(self):
127 response = self.client.get(url)
128 self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
130 class UserProfileTest(TestCase):
133 self.user = TestUser()
134 self.client.login(username = self.user.username,
135 password = self.user.password)
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)
142 def testUserProfileNoBundles(self):
143 response = self.client.get('/user/')
144 self.assertContains(response, 'You have no bundles')
146 def testUserProfileBundles(self):
147 project = defaults.project
150 bundle = Bundle(project = project, name = 'test-1',
151 owner = self.user.user)
154 response = self.client.get('/user/')
156 self.assertContains(response, 'You have the following bundle')
157 self.assertContains(response, bundle.get_absolute_url())
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
164 response = self.client.post('/user/', {'patches_per_page': new_ppp})
166 user_profile = UserProfile.objects.get(user=self.user.user.id)
167 self.assertEquals(user_profile.patches_per_page, new_ppp)
169 def testUserProfileInvalidPost(self):
170 user_profile = UserProfile.objects.get(user=self.user.user.id)
171 old_ppp = user_profile.patches_per_page
174 response = self.client.post('/user/', {'patches_per_page': new_ppp})
176 user_profile = UserProfile.objects.get(user=self.user.user.id)
177 self.assertEquals(user_profile.patches_per_page, old_ppp)
180 class UserPasswordChangeTest(TestCase):
184 self.form_url = reverse('django.contrib.auth.views.password_change')
185 self.done_url = reverse(
186 'django.contrib.auth.views.password_change_done')
188 def testPasswordChangeForm(self):
189 self.user = TestUser()
190 self.client.login(username = self.user.username,
191 password = self.user.password)
193 response = self.client.get(self.form_url)
194 self.assertContains(response, 'Change my password')
196 def testPasswordChange(self):
197 self.user = TestUser()
198 self.client.login(username = self.user.username,
199 password = self.user.password)
201 old_password = self.user.password
202 new_password = User.objects.make_random_password()
205 'old_password': old_password,
206 'new_password1': new_password,
207 'new_password2': new_password,
210 response = self.client.post(self.form_url, data)
211 self.assertRedirects(response, self.done_url)
213 user = User.objects.get(id = self.user.user.id)
215 self.assertFalse(user.check_password(old_password))
216 self.assertTrue(user.check_password(new_password))
218 response = self.client.get(self.done_url)
219 self.assertContains(response,
220 "Your password has been changed sucessfully")
222 class UserUnlinkTest(TestCase):
224 self.form_url = '/user/unlink/{pid}/'
225 self.done_url = '/user/'
226 EmailConfirmation.objects.all().delete()
227 Person.objects.all().delete()
229 def testUnlinkPrimaryEmail(self):
231 self.client.login(username=user.username,
232 password=user.password)
233 conf = EmailConfirmation(type='userperson',
237 self.client.get(_confirmation_url(conf))
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)
243 person = Person.objects.get(email=user.email)
244 self.assertEquals(person.user, user.user)
246 def testUnlinkSecondaryEmail(self):
248 self.client.login(username=user.username,
249 password=user.password)
250 conf = EmailConfirmation(type='userperson',
251 email=user.secondary_email,
254 self.client.get(_confirmation_url(conf))
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)
260 person = Person.objects.get(email=user.secondary_email)
261 self.assertEquals(person.user, None)
263 def testUnlinkAnotherUser(self):
265 self.client.login(username=user.username,
266 password=user.password)
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)
274 self.client.get(_confirmation_url(conf))
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)
280 person = Person.objects.get(email=other_user.email)
281 self.assertEquals(person.user, None)
283 def testUnlinkNonPost(self):
285 self.client.login(username=user.username,
286 password=user.password)
287 conf = EmailConfirmation(type='userperson',
288 email=user.secondary_email,
291 self.client.get(_confirmation_url(conf))
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)
297 person = Person.objects.get(email=user.secondary_email)
298 self.assertEquals(person.user, user.user)