]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/user.py
bundles: Remove separate public bundle views
[patchwork] / apps / patchwork / tests / 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
29
30 def _confirmation_url(conf):
31     return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
32
33 class TestUser(object):
34     username = 'testuser'
35     email = 'test@example.com'
36     secondary_email = 'test2@example.com'
37     password = None
38
39     def __init__(self):
40         self.password = User.objects.make_random_password()
41         self.user = User.objects.create_user(self.username,
42                             self.email, self.password)
43
44 class UserPersonRequestTest(TestCase):
45     def setUp(self):
46         self.user = TestUser()
47         self.client.login(username = self.user.username,
48                           password = self.user.password)
49         EmailConfirmation.objects.all().delete()
50
51     def testUserPersonRequestForm(self):
52         response = self.client.get('/user/link/')
53         self.assertEquals(response.status_code, 200)
54         self.assertTrue(response.context['linkform'])
55
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.')
62
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                 'Enter a valid e-mail address.')
69
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'])
75
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')
82
83         # check that an email has gone out...
84         self.assertEquals(len(mail.outbox), 1)
85         msg = mail.outbox[0]
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)
89
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')
94
95 class UserPersonConfirmTest(TestCase):
96     def setUp(self):
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)
105         self.conf.save()
106
107     def testUserPersonConfirm(self):
108         self.assertEquals(Person.objects.count(), 1)
109         response = self.client.get(_confirmation_url(self.conf))
110         self.assertEquals(response.status_code, 200)
111
112         # check that the Person object has been created and linked
113         self.assertEquals(Person.objects.count(), 2)
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)
117
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)
122
123 class UserLoginRedirectTest(TestCase):
124     
125     def testUserLoginRedirect(self):
126         url = '/user/'
127         response = self.client.get(url)
128         self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
129
130 class UserProfileTest(TestCase):
131
132     def setUp(self):
133         self.user = TestUser()
134         self.client.login(username = self.user.username,
135                           password = self.user.password)
136
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)
141
142     def testUserProfileNoBundles(self):
143         response = self.client.get('/user/')
144         self.assertContains(response, 'You have no bundles')
145
146     def testUserProfileBundles(self):
147         project = defaults.project
148         project.save()
149
150         bundle = Bundle(project = project, name = 'test-1',
151                         owner = self.user.user)
152         bundle.save()
153
154         response = self.client.get('/user/')
155
156         self.assertContains(response, 'You have the following bundle')
157         self.assertContains(response, bundle.get_absolute_url())