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 django.core import mail |
8
|
|
|
from django.core.urlresolvers import reverse |
9
|
|
|
from django.core.exceptions import ImproperlyConfigured |
10
|
|
|
|
11
|
|
|
from registration import forms |
12
|
|
|
from registration.supplements import get_supplement_class |
13
|
|
|
from registration.models import RegistrationProfile |
14
|
|
|
from registration.tests.utils import with_apps |
15
|
|
|
from registration.tests.compat import override_settings |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class RegistrationSupplementRetrievalTests(TestCase): |
19
|
|
|
|
20
|
|
|
def test_get_supplement_class(self): |
21
|
|
|
from registration.supplements.default.models import DefaultRegistrationSupplement |
22
|
|
|
supplement_class = get_supplement_class( |
23
|
|
|
'registration.supplements.default.models.DefaultRegistrationSupplement') |
24
|
|
|
self.failUnless(supplement_class is DefaultRegistrationSupplement) |
25
|
|
|
|
26
|
|
|
def test_supplement_error_invalid(self): |
27
|
|
|
self.assertRaises(ImproperlyConfigured, get_supplement_class, |
28
|
|
|
'registration.supplements.doesnotexist.NonExistenBackend') |
29
|
|
|
|
30
|
|
|
def test_supplement_attribute_error(self): |
31
|
|
|
self.assertRaises(ImproperlyConfigured, get_supplement_class, |
32
|
|
|
'registration.supplements.default.NonexistenBackend') |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
@with_apps( |
36
|
|
|
'django.contrib.contenttypes', |
37
|
|
|
'registration.supplements.default' |
38
|
|
|
) |
39
|
|
|
@override_settings( |
40
|
|
|
ACCOUNT_ACTIVATION_DAYS=7, |
41
|
|
|
REGISTRATION_OPEN=True, |
42
|
|
|
REGISTRATION_SUPPLEMENT_CLASS=( |
43
|
|
|
'registration.supplements.default.models.DefaultRegistrationSupplement'), |
44
|
|
|
REGISTRATION_BACKEND_CLASS=( |
45
|
|
|
'registration.backends.default.DefaultRegistrationBackend'), |
46
|
|
|
) |
47
|
|
|
class RegistrationViewWithDefaultRegistrationSupplementTestCase(TestCase): |
48
|
|
|
def test_registration_view_get(self): |
49
|
|
|
""" |
50
|
|
|
A ``GET`` to the ``register`` view uses the appropriate |
51
|
|
|
template and populates the registration form into the context. |
52
|
|
|
|
53
|
|
|
""" |
54
|
|
|
from registration.supplements.default.models import DefaultRegistrationSupplement |
55
|
|
|
response = self.client.get(reverse('registration_register')) |
56
|
|
|
self.assertEqual(response.status_code, 200) |
57
|
|
|
self.assertTemplateUsed(response, |
58
|
|
|
'registration/registration_form.html') |
59
|
|
|
self.failUnless(isinstance(response.context['form'], |
60
|
|
|
forms.RegistrationForm)) |
61
|
|
|
self.failUnless(isinstance(response.context['supplement_form'].instance, |
62
|
|
|
DefaultRegistrationSupplement)) |
63
|
|
|
|
64
|
|
|
def test_registration_view_post_success(self): |
65
|
|
|
""" |
66
|
|
|
A ``POST`` to the ``register`` view with valid data properly |
67
|
|
|
creates a new user and issues a redirect. |
68
|
|
|
|
69
|
|
|
""" |
70
|
|
|
from registration.supplements.default.models import DefaultRegistrationSupplement |
71
|
|
|
response = self.client.post(reverse('registration_register'), |
72
|
|
|
data={'username': 'alice', |
73
|
|
|
'email1': '[email protected]', |
74
|
|
|
'email2': '[email protected]', |
75
|
|
|
'remarks': 'Hello'}) |
76
|
|
|
self.assertRedirects(response, |
77
|
|
|
'http://testserver%s' % reverse('registration_complete')) |
78
|
|
|
self.assertEqual(RegistrationProfile.objects.count(), 1) |
79
|
|
|
self.assertEqual(DefaultRegistrationSupplement.objects.count(), 1) |
80
|
|
|
self.assertEqual(len(mail.outbox), 1) |
81
|
|
|
|
82
|
|
|
profile = RegistrationProfile.objects.get(user__username='alice') |
83
|
|
|
self.assertEqual(profile.supplement.remarks, 'Hello') |
84
|
|
|
|
85
|
|
View Code Duplication |
def test_registration_view_post_failure(self): |
|
|
|
|
86
|
|
|
""" |
87
|
|
|
A ``POST`` to the ``register`` view with invalid data does not |
88
|
|
|
create a user, and displays appropriate error messages. |
89
|
|
|
|
90
|
|
|
""" |
91
|
|
|
response = self.client.post(reverse('registration_register'), |
92
|
|
|
data={'username': 'bob', |
93
|
|
|
'email1': '[email protected]', |
94
|
|
|
'email2': '[email protected]', |
95
|
|
|
'remarks': 'Hello'}) |
96
|
|
|
self.assertEqual(response.status_code, 200) |
97
|
|
|
self.failIf(response.context['form'].is_valid()) |
98
|
|
|
self.failUnless(response.context['supplement_form'].is_valid()) |
99
|
|
|
self.assertFormError(response, 'form', field=None, |
100
|
|
|
errors="The two email fields didn't match.") |
101
|
|
|
self.assertEqual(len(mail.outbox), 0) |
102
|
|
|
|
103
|
|
View Code Duplication |
def test_registration_view_post_no_remarks_failure(self): |
|
|
|
|
104
|
|
|
""" |
105
|
|
|
A ``POST`` to the ``register`` view with invalid data does not |
106
|
|
|
create a user, and displays appropriate error messages. |
107
|
|
|
|
108
|
|
|
""" |
109
|
|
|
response = self.client.post(reverse('registration_register'), |
110
|
|
|
data={'username': 'bob', |
111
|
|
|
'email1': '[email protected]', |
112
|
|
|
'email2': '[email protected]'}) |
113
|
|
|
self.assertEqual(response.status_code, 200) |
114
|
|
|
self.failUnless(response.context['form'].is_valid()) |
115
|
|
|
self.failIf(response.context['supplement_form'].is_valid()) |
116
|
|
|
self.assertFormError(response, 'supplement_form', field='remarks', |
117
|
|
|
errors="This field is required.") |
118
|
|
|
self.assertEqual(len(mail.outbox), 0) |
119
|
|
|
|