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
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
30 def _confirmation_url(conf):
31 return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
33 class TestUser(object):
35 def __init__(self, username='testuser', email='test@example.com',
36 secondary_email='test2@example.com'):
37 self.username = username
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)
45 class UserPersonRequestTest(TestCase):
47 self.user = TestUser()
48 self.client.login(username = self.user.username,
49 password = self.user.password)
50 EmailConfirmation.objects.all().delete()
52 def testUserPersonRequestForm(self):
53 response = self.client.get('/user/link/')
54 self.assertEquals(response.status_code, 200)
55 self.assertTrue(response.context['linkform'])
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.')
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'])
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'])
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')
84 # check that an email has gone out...
85 self.assertEquals(len(mail.outbox), 1)
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)
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')
96 class UserPersonConfirmTest(TestCase):
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)
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)
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)
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)
124 class UserLoginRedirectTest(TestCase):
126 def testUserLoginRedirect(self):
128 response = self.client.get(url)
129 self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
131 class UserProfileTest(TestCase):
134 self.user = TestUser()
135 self.client.login(username = self.user.username,
136 password = self.user.password)
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)
143 def testUserProfileNoBundles(self):
144 response = self.client.get('/user/')
145 self.assertContains(response, 'You have no bundles')
147 def testUserProfileBundles(self):
148 project = defaults.project
151 bundle = Bundle(project = project, name = 'test-1',
152 owner = self.user.user)
155 response = self.client.get('/user/')
157 self.assertContains(response, 'You have the following bundle')
158 self.assertContains(response, bundle.get_absolute_url())
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
165 response = self.client.post('/user/', {'patches_per_page': new_ppp})
167 user_profile = UserProfile.objects.get(user=self.user.user.id)
168 self.assertEquals(user_profile.patches_per_page, new_ppp)
170 def testUserProfileInvalidPost(self):
171 user_profile = UserProfile.objects.get(user=self.user.user.id)
172 old_ppp = user_profile.patches_per_page
175 response = self.client.post('/user/', {'patches_per_page': new_ppp})
177 user_profile = UserProfile.objects.get(user=self.user.user.id)
178 self.assertEquals(user_profile.patches_per_page, old_ppp)
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')
185 def testPasswordChangeForm(self):
186 self.user = TestUser()
187 self.client.login(username = self.user.username,
188 password = self.user.password)
190 response = self.client.get(self.form_url)
191 self.assertContains(response, 'Change my password')
193 def testPasswordChange(self):
194 self.user = TestUser()
195 self.client.login(username = self.user.username,
196 password = self.user.password)
198 old_password = self.user.password
199 new_password = User.objects.make_random_password()
202 'old_password': old_password,
203 'new_password1': new_password,
204 'new_password2': new_password,
207 response = self.client.post(self.form_url, data)
208 self.assertRedirects(response, self.done_url)
210 user = User.objects.get(id = self.user.user.id)
212 self.assertFalse(user.check_password(old_password))
213 self.assertTrue(user.check_password(new_password))
215 response = self.client.get(self.done_url)
216 self.assertContains(response,
217 "Your password has been changed sucessfully")
219 class UserUnlinkTest(TestCase):
221 self.form_url = '/user/unlink/{pid}/'
222 self.done_url = '/user/'
223 EmailConfirmation.objects.all().delete()
224 Person.objects.all().delete()
226 def testUnlinkPrimaryEmail(self):
228 self.client.login(username=user.username,
229 password=user.password)
230 conf = EmailConfirmation(type='userperson',
234 self.client.get(_confirmation_url(conf))
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)
240 person = Person.objects.get(email=user.email)
241 self.assertEquals(person.user, user.user)
243 def testUnlinkSecondaryEmail(self):
245 self.client.login(username=user.username,
246 password=user.password)
247 conf = EmailConfirmation(type='userperson',
248 email=user.secondary_email,
251 self.client.get(_confirmation_url(conf))
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)
257 person = Person.objects.get(email=user.secondary_email)
258 self.assertEquals(person.user, None)
260 def testUnlinkAnotherUser(self):
262 self.client.login(username=user.username,
263 password=user.password)
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)
271 self.client.get(_confirmation_url(conf))
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)
277 person = Person.objects.get(email=other_user.email)
278 self.assertEquals(person.user, None)
280 def testUnlinkNonPost(self):
282 self.client.login(username=user.username,
283 password=user.password)
284 conf = EmailConfirmation(type='userperson',
285 email=user.secondary_email,
288 self.client.get(_confirmation_url(conf))
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)
294 person = Person.objects.get(email=user.secondary_email)
295 self.assertEquals(person.user, user.user)