]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/user.py
c9e5be3feb2d549a03f5ae4728483c017d2cfea2
[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.contrib.auth.models import User
26 from patchwork.models import EmailConfirmation, Person
27 from patchwork.utils import userprofile_register_callback
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         userprofile_register_callback(self.user)
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)