summaryrefslogtreecommitdiffstats
path: root/patchwork/tests/test_registration.py
blob: 845b60b152576ab809a160f1172e5b5c315b9b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Patchwork - automated patch tracking system
# Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org>
#
# This file is part of the Patchwork package.
#
# Patchwork is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Patchwork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import unittest
from django.test import TestCase
from django.test.client import Client
from django.core import mail
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from patchwork.models import EmailConfirmation, Person
from patchwork.tests.utils import create_user

def _confirmation_url(conf):
    return reverse('patchwork.views.confirm', kwargs = {'key': conf.key})

class TestUser(object):
    firstname = 'Test'
    lastname = 'User'
    username = 'testuser'
    email = 'test@example.com'
    password = 'foobar'

class RegistrationTest(TestCase):
    def setUp(self):
        self.user = TestUser()
        self.client = Client()
        self.default_data = {'username': self.user.username,
                             'first_name': self.user.firstname,
                             'last_name': self.user.lastname,
                             'email': self.user.email,
                             'password': self.user.password}
        self.required_error = 'This field is required.'
        self.invalid_error = 'Enter a valid value.'

    def testRegistrationForm(self):
        response = self.client.get('/register/')
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'patchwork/registration_form.html')

    def testBlankFields(self):
        for field in ['username', 'email', 'password']:
            data = self.default_data.copy()
            del data[field]
            response = self.client.post('/register/', data)
            self.assertEquals(response.status_code, 200)
            self.assertFormError(response, 'form', field, self.required_error)

    def testInvalidUsername(self):
        data = self.default_data.copy()
        data['username'] = 'invalid user'
        response = self.client.post('/register/', data)
        self.assertEquals(response.status_code, 200)
        self.assertFormError(response, 'form', 'username', self.invalid_error)

    def testExistingUsername(self):
        user = create_user()
        data = self.default_data.copy()
        data['username'] = user.username
        response = self.client.post('/register/', data)
        self.assertEquals(response.status_code, 200)
        self.assertFormError(response, 'form', 'username',
                'This username is already taken. Please choose another.')

    def testExistingEmail(self):
        user = create_user()
        data = self.default_data.copy()
        data['email'] = user.email
        response = self.client.post('/register/', data)
        self.assertEquals(response.status_code, 200)
        self.assertFormError(response, 'form', 'email',
                'This email address is already in use ' + \
                'for the account "%s".\n' % user.username)

    def testValidRegistration(self):
        response = self.client.post('/register/', self.default_data)
        self.assertEquals(response.status_code, 200)
        self.assertContains(response, 'confirmation email has been sent')

        # check for presence of an inactive user object
        users = User.objects.filter(username = self.user.username)
        self.assertEquals(users.count(), 1)
        user = users[0]
        self.assertEquals(user.username, self.user.username)
        self.assertEquals(user.email, self.user.email)
        self.assertEquals(user.is_active, False)

        # check for confirmation object
        confs = EmailConfirmation.objects.filter(user = user,
                                                 type = 'registration')
        self.assertEquals(len(confs), 1)
        conf = confs[0]
        self.assertEquals(conf.email, self.user.email)

        # check for a sent mail
        self.assertEquals(len(mail.outbox), 1)
        msg = mail.outbox[0]
        self.assertEquals(msg.subject, 'Patchwork account confirmation')
        self.assertTrue(self.user.email in msg.to)
        self.assertTrue(_confirmation_url(conf) in msg.body)

        # ...and that the URL is valid
        response = self.client.get(_confirmation_url(conf))
        self.assertEquals(response.status_code, 200)

class RegistrationConfirmationTest(TestCase):

    def setUp(self):
        self.user = TestUser()
        self.default_data = {'username': self.user.username,
                             'first_name': self.user.firstname,
                             'last_name': self.user.lastname,
                             'email': self.user.email,
                             'password': self.user.password}

    def testRegistrationConfirmation(self):
        self.assertEqual(EmailConfirmation.objects.count(), 0)
        response = self.client.post('/register/', self.default_data)
        self.assertEquals(response.status_code, 200)
        self.assertContains(response, 'confirmation email has been sent')

        self.assertEqual(EmailConfirmation.objects.count(), 1)
        conf = EmailConfirmation.objects.filter()[0]
        self.assertFalse(conf.user.is_active)
        self.assertTrue(conf.active)

        response = self.client.get(_confirmation_url(conf))
        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'patchwork/registration-confirm.html')

        conf = EmailConfirmation.objects.get(pk = conf.pk)
        self.assertTrue(conf.user.is_active)
        self.assertFalse(conf.active)

    def testRegistrationNewPersonSetup(self):
        """ Check that the person object created after registration has the
            correct details """

        # register
        self.assertEqual(EmailConfirmation.objects.count(), 0)
        response = self.client.post('/register/', self.default_data)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(Person.objects.exists())

        # confirm
        conf = EmailConfirmation.objects.filter()[0]
        response = self.client.get(_confirmation_url(conf))
        self.assertEquals(response.status_code, 200)

        qs = Person.objects.filter(email = self.user.email)
        self.assertTrue(qs.exists())
        person = Person.objects.get(email = self.user.email)

        self.assertEquals(person.name,
                    self.user.firstname + ' ' + self.user.lastname)

    def testRegistrationExistingPersonSetup(self):
        """ Check that the person object created after registration has the
            correct details """

        fullname = self.user.firstname + ' '  + self.user.lastname
        person = Person(name = fullname, email = self.user.email)
        person.save()

        # register
        self.assertEqual(EmailConfirmation.objects.count(), 0)
        response = self.client.post('/register/', self.default_data)
        self.assertEquals(response.status_code, 200)

        # confirm
        conf = EmailConfirmation.objects.filter()[0]
        response = self.client.get(_confirmation_url(conf))
        self.assertEquals(response.status_code, 200)

        person = Person.objects.get(email = self.user.email)

        self.assertEquals(person.name, fullname)

    def testRegistrationExistingPersonUnmodified(self):
        """ Check that an unconfirmed registration can't modify an existing
            Person object"""

        fullname = self.user.firstname + ' '  + self.user.lastname
        person = Person(name = fullname, email = self.user.email)
        person.save()

        # register
        data = self.default_data.copy()
        data['first_name'] = 'invalid'
        data['last_name'] = 'invalid'
        self.assertEquals(data['email'], person.email)
        response = self.client.post('/register/', data)
        self.assertEquals(response.status_code, 200)

        self.assertEquals(Person.objects.get(pk = person.pk).name, fullname)