1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import unicode_literals |
3
|
|
|
""" |
4
|
|
|
""" |
5
|
|
|
__author__ = 'Alisue <[email protected]>' |
6
|
|
|
from django.test import TestCase |
7
|
|
|
from registration.compat import get_user_model |
8
|
|
|
from registration import forms |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ActivationFormTests(TestCase): |
12
|
|
|
""" |
13
|
|
|
Test the default registration forms. |
14
|
|
|
|
15
|
|
|
""" |
16
|
|
|
def test_activation_form(self): |
17
|
|
|
""" |
18
|
|
|
Test that ``ActivationForm`` enforces username constraints |
19
|
|
|
and matching passwords. |
20
|
|
|
|
21
|
|
|
""" |
22
|
|
|
User = get_user_model() |
23
|
|
|
# Create a user so we can verify that duplicate usernames aren't |
24
|
|
|
# permitted. |
25
|
|
|
User.objects.create_user('alice', '[email protected]', 'secret') |
26
|
|
|
|
27
|
|
|
invalid_data_dicts = [ |
28
|
|
|
# Mismatched passwords. |
29
|
|
|
{'data': {'password1': 'foo', |
30
|
|
|
'password2': 'bar'}, |
31
|
|
|
'error': ('__all__', ["The two password fields didn't match."])}, |
32
|
|
|
] |
33
|
|
|
|
34
|
|
|
for invalid_dict in invalid_data_dicts: |
35
|
|
|
form = forms.ActivationForm(data=invalid_dict['data']) |
36
|
|
|
self.failIf(form.is_valid()) |
37
|
|
|
self.assertEqual(form.errors[invalid_dict['error'][0]], |
38
|
|
|
invalid_dict['error'][1]) |
39
|
|
|
|
40
|
|
|
form = forms.ActivationForm(data={'password1': 'foo', |
41
|
|
|
'password2': 'foo'}) |
42
|
|
|
self.failUnless(form.is_valid()) |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
class RegistrationFormTests(TestCase): |
46
|
|
|
""" |
47
|
|
|
Test the default registration forms. |
48
|
|
|
|
49
|
|
|
""" |
50
|
|
|
def test_registration_form(self): |
51
|
|
|
""" |
52
|
|
|
Test that ``RegistrationForm`` enforces username constraints |
53
|
|
|
and matching passwords. |
54
|
|
|
|
55
|
|
|
""" |
56
|
|
|
User = get_user_model() |
57
|
|
|
# Create a user so we can verify that duplicate usernames aren't |
58
|
|
|
# permitted. |
59
|
|
|
User.objects.create_user('alice', '[email protected]', 'secret') |
60
|
|
|
|
61
|
|
|
invalid_data_dicts = [ |
62
|
|
|
# Non-alphanumeric username. |
63
|
|
|
{'data': {'username': 'foo/bar', |
64
|
|
|
'email1': '[email protected]', |
65
|
|
|
'email2': '[email protected]'}, |
66
|
|
|
'error': ('username', ["This value must contain only letters, numbers and underscores."])}, |
67
|
|
|
# Already-existing username. |
68
|
|
|
{'data': {'username': 'alice', |
69
|
|
|
'email1': '[email protected]', |
70
|
|
|
'email2': '[email protected]'}, |
71
|
|
|
'error': ('username', ["A user with that username already exists."])}, |
72
|
|
|
# Mismatched email. |
73
|
|
|
{'data': {'username': 'foo', |
74
|
|
|
'email1': '[email protected]', |
75
|
|
|
'email2': '[email protected]'}, |
76
|
|
|
'error': ('__all__', ["The two email fields didn't match."])}, |
77
|
|
|
] |
78
|
|
|
|
79
|
|
|
for invalid_dict in invalid_data_dicts: |
80
|
|
|
form = forms.RegistrationForm(data=invalid_dict['data']) |
81
|
|
|
self.failIf(form.is_valid()) |
82
|
|
|
self.assertEqual(form.errors[invalid_dict['error'][0]], |
83
|
|
|
invalid_dict['error'][1]) |
84
|
|
|
|
85
|
|
|
form = forms.RegistrationForm(data={'username': 'foofoohogehoge', |
86
|
|
|
'email1': '[email protected]', |
87
|
|
|
'email2': '[email protected]'}) |
88
|
|
|
self.failUnless(form.is_valid()) |
89
|
|
|
|
90
|
|
|
def test_registration_form_tos(self): |
91
|
|
|
""" |
92
|
|
|
Test that ``RegistrationFormTermsOfService`` requires |
93
|
|
|
agreement to the terms of service. |
94
|
|
|
|
95
|
|
|
""" |
96
|
|
|
form = forms.RegistrationFormTermsOfService(data={'username': 'foo', |
97
|
|
|
'email1': '[email protected]', |
98
|
|
|
'email2': '[email protected]'}) |
99
|
|
|
self.failIf(form.is_valid()) |
100
|
|
|
self.assertEqual(form.errors['tos'], |
101
|
|
|
["You must agree to the terms to register"]) |
102
|
|
|
|
103
|
|
|
form = forms.RegistrationFormTermsOfService(data={'username': 'foofoohogehoge', |
104
|
|
|
'email1': '[email protected]', |
105
|
|
|
'email2': '[email protected]', |
106
|
|
|
'tos': 'on'}) |
107
|
|
|
self.failUnless(form.is_valid()) |
108
|
|
|
|
109
|
|
|
def test_registration_form_unique_email(self): |
110
|
|
|
""" |
111
|
|
|
Test that ``RegistrationFormUniqueEmail`` validates uniqueness |
112
|
|
|
of email addresses. |
113
|
|
|
|
114
|
|
|
""" |
115
|
|
|
User = get_user_model() |
116
|
|
|
# Create a user so we can verify that duplicate addresses |
117
|
|
|
# aren't permitted. |
118
|
|
|
User.objects.create_user('alice', '[email protected]', 'secret') |
119
|
|
|
|
120
|
|
|
form = forms.RegistrationFormUniqueEmail(data={'username': 'foo', |
121
|
|
|
'email1': '[email protected]', |
122
|
|
|
'email2': '[email protected]'}) |
123
|
|
|
self.failIf(form.is_valid()) |
124
|
|
|
self.assertEqual(form.errors['email1'], |
125
|
|
|
["This email address is already in use. Please supply a different email address."]) |
126
|
|
|
|
127
|
|
|
form = forms.RegistrationFormUniqueEmail(data={'username': 'foofoohogehoge', |
128
|
|
|
'email1': '[email protected]', |
129
|
|
|
'email2': '[email protected]'}) |
130
|
|
|
self.failUnless(form.is_valid()) |
131
|
|
|
|
132
|
|
|
def test_registration_form_no_free_email(self): |
133
|
|
|
""" |
134
|
|
|
Test that ``RegistrationFormNoFreeEmail`` disallows |
135
|
|
|
registration with free email addresses. |
136
|
|
|
|
137
|
|
|
""" |
138
|
|
|
base_data = {'username': 'foofoohogehoge'} |
139
|
|
|
for domain in forms.RegistrationFormNoFreeEmail.bad_domains: |
140
|
|
|
invalid_data = base_data.copy() |
141
|
|
|
invalid_data['email1'] = "foo@%s" % domain |
142
|
|
|
invalid_data['email2'] = invalid_data['email1'] |
143
|
|
|
form = forms.RegistrationFormNoFreeEmail(data=invalid_data) |
144
|
|
|
self.failIf(form.is_valid()) |
145
|
|
|
self.assertEqual(form.errors['email1'], |
146
|
|
|
["Registration using free email addresses is prohibited. Please supply a different email address."]) |
147
|
|
|
|
148
|
|
|
base_data['email1'] = '[email protected]' |
149
|
|
|
base_data['email2'] = base_data['email1'] |
150
|
|
|
form = forms.RegistrationFormNoFreeEmail(data=base_data) |
151
|
|
|
self.failUnless(form.is_valid()) |
152
|
|
|
|