]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/user.py
e96e6c537f4fce0e4b4e80d2056bf8b6d24ce0d3
[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
28
29 def _confirmation_url(conf):
30     return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})
31
32 class TestUser(object):
33     username = 'testuser'
34     email = 'test@example.com'
35     secondary_email = 'test2@example.com'
36     password = None
37
38     def __init__(self):
39         self.password = User.objects.make_random_password()
40         self.user = User.objects.create_user(self.username,
41                             self.email, self.password)
42
43 class UserPersonRequestTest(TestCase):
44     def setUp(self):
45         self.user = TestUser()
46         self.client.login(username = self.user.username,
47                           password = self.user.password)
48         EmailConfirmation.objects.all().delete()
49
50     def testUserPersonRequestForm(self):
51         response = self.client.get('/user/link/')
52         self.assertEquals(response.status_code, 200)
53         self.assertTrue(response.context['linkform'])
54
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.')
61
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                 'Enter a valid e-mail address.')
68
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'])
74
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')
81
82         # check that an email has gone out...
83         self.assertEquals(len(mail.outbox), 1)
84         msg = mail.outbox[0]
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)
88
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')
93
94 class UserPersonConfirmTest(TestCase):
95     def setUp(self):
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)
104         self.conf.save()
105
106     def testUserPersonConfirm(self):
107         self.assertEquals(Person.objects.count(), 1)
108         response = self.client.get(_confirmation_url(self.conf))
109         self.assertEquals(response.status_code, 200)
110
111         # check that the Person object has been created and linked
112         self.assertEquals(Person.objects.count(), 2)
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)
116
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)
121
122 class UserLoginRedirectTest(TestCase):
123     
124     def testUserLoginRedirect(self):
125         url = '/user/'
126         response = self.client.get(url)
127         self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url)
128