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
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 class UserPasswordChangeTest(TestCase):
161 form_url = reverse('django.contrib.auth.views.password_change')
162 done_url = reverse('django.contrib.auth.views.password_change_done')
164 def testPasswordChangeForm(self):
165 self.user = TestUser()
166 self.client.login(username = self.user.username,
167 password = self.user.password)
169 response = self.client.get(self.form_url)
170 self.assertContains(response, 'Change my password')
172 def testPasswordChange(self):
173 self.user = TestUser()
174 self.client.login(username = self.user.username,
175 password = self.user.password)
177 old_password = self.user.password
178 new_password = User.objects.make_random_password()
181 'old_password': old_password,
182 'new_password1': new_password,
183 'new_password2': new_password,
186 response = self.client.post(self.form_url, data)
187 self.assertRedirects(response, self.done_url)
189 user = User.objects.get(id = self.user.user.id)
191 self.assertFalse(user.check_password(old_password))
192 self.assertTrue(user.check_password(new_password))
194 response = self.client.get(self.done_url)
195 self.assertContains(response,
196 "Your password has been changed sucessfully")
198 class UserUnlinkTest(TestCase):
200 self.form_url = '/user/unlink/{pid}/'
201 self.done_url = '/user/'
202 EmailConfirmation.objects.all().delete()
203 Person.objects.all().delete()
205 def testUnlinkPrimaryEmail(self):
207 self.client.login(username=user.username,
208 password=user.password)
209 conf = EmailConfirmation(type='userperson',
213 self.client.get(_confirmation_url(conf))
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)
219 person = Person.objects.get(email=user.email)
220 self.assertEquals(person.user, user.user)
222 def testUnlinkSecondaryEmail(self):
224 self.client.login(username=user.username,
225 password=user.password)
226 conf = EmailConfirmation(type='userperson',
227 email=user.secondary_email,
230 self.client.get(_confirmation_url(conf))
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)
236 person = Person.objects.get(email=user.secondary_email)
237 self.assertEquals(person.user, None)
239 def testUnlinkAnotherUser(self):
241 self.client.login(username=user.username,
242 password=user.password)
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)
250 self.client.get(_confirmation_url(conf))
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)
256 person = Person.objects.get(email=other_user.email)
257 self.assertEquals(person.user, None)
259 def testUnlinkNonPost(self):
261 self.client.login(username=user.username,
262 password=user.password)
263 conf = EmailConfirmation(type='userperson',
264 email=user.secondary_email,
267 self.client.get(_confirmation_url(conf))
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)
273 person = Person.objects.get(email=user.secondary_email)
274 self.assertEquals(person.user, user.user)