Total Complexity | 4 |
Total Lines | 84 |
Duplicated Lines | 39.29 % |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | # -*- coding: utf-8 -*- |
||
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 |