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
28 def _confirmation_url(conf):
29 return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
31 class TestUser(object):
33 def __init__(self, username='testuser', email='test@example.com',
34 secondary_email='test2@example.com'):
35 self.username = username
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)
43 class UserPersonRequestTest(TestCase):
45 self.user = TestUser()
46 self.client.login(username = self.user.username,
47 password = self.user.password)
48 EmailConfirmation.objects.all().delete()
50 def testUserPersonRequestForm(self):
51 response = self.client.get('/user/link/')
52 self.assertEquals(response.status_code, 200)
53 self.assertTrue(response.context['linkform'])
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.')
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'])
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'])
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')
82 # check that an email has gone out...
83 self.assertEquals(len(mail.outbox), 1)
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)
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')
94 class UserPersonConfirmTest(TestCase):
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)
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)
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)
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)
122 class UserLoginRedirectTest(TestCase):
124 def testUserLoginRedirect(self):
126 response = self.client.get(url)
127 self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
129 class UserProfileTest(TestCase):
132 self.user = TestUser()
133 self.client.login(username = self.user.username,
134 password = self.user.password)
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)
141 def testUserProfileNoBundles(self):
142 response = self.client.get('/user/')
143 self.assertContains(response, 'You have no bundles')
145 def testUserProfileBundles(self):
146 project = defaults.project
149 bundle = Bundle(project = project, name = 'test-1',
150 owner = self.user.user)
153 response = self.client.get('/user/')
155 self.assertContains(response, 'You have the following bundle')
156 self.assertContains(response, bundle.get_absolute_url())
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
163 response = self.client.post('/user/', {'patches_per_page': new_ppp})
165 user_profile = UserProfile.objects.get(user=self.user.user.id)
166 self.assertEquals(user_profile.patches_per_page, new_ppp)
168 def testUserProfileInvalidPost(self):
169 user_profile = UserProfile.objects.get(user=self.user.user.id)
170 old_ppp = user_profile.patches_per_page
173 response = self.client.post('/user/', {'patches_per_page': new_ppp})
175 user_profile = UserProfile.objects.get(user=self.user.user.id)
176 self.assertEquals(user_profile.patches_per_page, old_ppp)
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')
183 def testPasswordChangeForm(self):
184 self.user = TestUser()
185 self.client.login(username = self.user.username,
186 password = self.user.password)
188 response = self.client.get(self.form_url)
189 self.assertContains(response, 'Change my password')
191 def testPasswordChange(self):
192 self.user = TestUser()
193 self.client.login(username = self.user.username,
194 password = self.user.password)
196 old_password = self.user.password
197 new_password = User.objects.make_random_password()
200 'old_password': old_password,
201 'new_password1': new_password,
202 'new_password2': new_password,
205 response = self.client.post(self.form_url, data)
206 self.assertRedirects(response, self.done_url)
208 user = User.objects.get(id = self.user.user.id)
210 self.assertFalse(user.check_password(old_password))
211 self.assertTrue(user.check_password(new_password))
213 response = self.client.get(self.done_url)
214 self.assertContains(response,
215 "Your password has been changed sucessfully")
217 class UserUnlinkTest(TestCase):
219 self.form_url = '/user/unlink/{pid}/'
220 self.done_url = '/user/'
221 EmailConfirmation.objects.all().delete()
222 Person.objects.all().delete()
224 def testUnlinkPrimaryEmail(self):
226 self.client.login(username=user.username,
227 password=user.password)
228 conf = EmailConfirmation(type='userperson',
232 self.client.get(_confirmation_url(conf))
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)
238 person = Person.objects.get(email=user.email)
239 self.assertEquals(person.user, user.user)
241 def testUnlinkSecondaryEmail(self):
243 self.client.login(username=user.username,
244 password=user.password)
245 conf = EmailConfirmation(type='userperson',
246 email=user.secondary_email,
249 self.client.get(_confirmation_url(conf))
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)
255 person = Person.objects.get(email=user.secondary_email)
256 self.assertEquals(person.user, None)
258 def testUnlinkAnotherUser(self):
260 self.client.login(username=user.username,
261 password=user.password)
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)
269 self.client.get(_confirmation_url(conf))
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)
275 person = Person.objects.get(email=other_user.email)
276 self.assertEquals(person.user, None)
278 def testUnlinkNonPost(self):
280 self.client.login(username=user.username,
281 password=user.password)
282 conf = EmailConfirmation(type='userperson',
283 email=user.secondary_email,
286 self.client.get(_confirmation_url(conf))
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)
292 person = Person.objects.get(email=user.secondary_email)
293 self.assertEquals(person.user, user.user)