| Total Complexity | 10 |
| Total Lines | 178 |
| Duplicated Lines | 9.55 % |
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 -*- |
||
| 21 | @override_settings( |
||
| 22 | ACCOUNT_ACTIVATION_DAYS=7, |
||
| 23 | REGISTRATION_OPEN=True, |
||
| 24 | REGISTRATION_SUPPLEMENT_CLASS=None, |
||
| 25 | REGISTRATION_BACKEND_CLASS=( |
||
| 26 | 'registration.backends.default.DefaultRegistrationBackend'), |
||
| 27 | ) |
||
| 28 | class RegistrationViewTestCase(TestCase): |
||
| 29 | |||
| 30 | def setUp(self): |
||
| 31 | self.backend = DefaultRegistrationBackend() |
||
| 32 | self.mock_request = mock_request() |
||
| 33 | |||
| 34 | |||
| 35 | def test_registration_view_get(self): |
||
| 36 | """ |
||
| 37 | A ``GET`` to the ``register`` view uses the appropriate |
||
| 38 | template and populates the registration form into the context. |
||
| 39 | |||
| 40 | """ |
||
| 41 | response = self.client.get(reverse('registration_register')) |
||
| 42 | self.assertEqual(response.status_code, 200) |
||
| 43 | self.assertTemplateUsed(response, |
||
| 44 | 'registration/registration_form.html') |
||
| 45 | self.failUnless(isinstance(response.context['form'], |
||
| 46 | forms.RegistrationForm)) |
||
| 47 | |||
| 48 | def test_registration_view_post_success(self): |
||
| 49 | """ |
||
| 50 | A ``POST`` to the ``register`` view with valid data properly |
||
| 51 | creates a new user and issues a redirect. |
||
| 52 | |||
| 53 | """ |
||
| 54 | response = self.client.post(reverse('registration_register'), |
||
| 55 | data={'username': 'alice', |
||
| 56 | 'email1': '[email protected]', |
||
| 57 | 'email2': '[email protected]'}) |
||
| 58 | self.assertRedirects(response, |
||
| 59 | 'http://testserver%s' % reverse('registration_complete')) |
||
| 60 | self.assertEqual(RegistrationProfile.objects.count(), 1) |
||
| 61 | self.assertEqual(len(mail.outbox), 1) |
||
| 62 | |||
| 63 | def test_registration_view_post_failure(self): |
||
| 64 | """ |
||
| 65 | A ``POST`` to the ``register`` view with invalid data does not |
||
| 66 | create a user, and displays appropriate error messages. |
||
| 67 | |||
| 68 | """ |
||
| 69 | response = self.client.post(reverse('registration_register'), |
||
| 70 | data={'username': 'bob', |
||
| 71 | 'email1': '[email protected]', |
||
| 72 | 'email2': '[email protected]'}) |
||
| 73 | self.assertEqual(response.status_code, 200) |
||
| 74 | self.failIf(response.context['form'].is_valid()) |
||
| 75 | self.assertFormError(response, 'form', field=None, |
||
| 76 | errors="The two email fields didn't match.") |
||
| 77 | self.assertEqual(len(mail.outbox), 0) |
||
| 78 | |||
| 79 | def test_registration_complete_view_get(self): |
||
| 80 | """ |
||
| 81 | A ``GET`` to the ``complete`` view uses the appropriate |
||
| 82 | template and populates the registration form into the context. |
||
| 83 | |||
| 84 | """ |
||
| 85 | # register save registration_profile in the session |
||
| 86 | response = self.client.post(reverse('registration_register'), |
||
| 87 | data={'username': 'alice', |
||
| 88 | 'email1': '[email protected]', |
||
| 89 | 'email2': '[email protected]'}) |
||
| 90 | response = self.client.get(reverse('registration_complete')) |
||
| 91 | self.assertEqual(response.status_code, 200) |
||
| 92 | self.assertTemplateUsed(response, |
||
| 93 | 'registration/registration_complete.html') |
||
| 94 | self.failUnless(isinstance(response.context['registration_profile'], |
||
| 95 | RegistrationProfile)) |
||
| 96 | profile = response.context['registration_profile'] |
||
| 97 | self.assertEqual(profile.user.username, 'alice') |
||
| 98 | self.assertEqual(profile.user.email, '[email protected]') |
||
| 99 | |||
| 100 | def test_registration_view_closed(self): |
||
| 101 | """ |
||
| 102 | Any attempt to access the ``register`` view when registration |
||
| 103 | is closed fails and redirects. |
||
| 104 | |||
| 105 | """ |
||
| 106 | settings.REGISTRATION_OPEN = False |
||
| 107 | |||
| 108 | closed_redirect = 'http://testserver%s' % reverse('registration_disallowed') |
||
| 109 | |||
| 110 | response = self.client.get(reverse('registration_register')) |
||
| 111 | self.assertRedirects(response, closed_redirect) |
||
| 112 | |||
| 113 | # Even if valid data is posted, it still shouldn't work. |
||
| 114 | response = self.client.post(reverse('registration_register'), |
||
| 115 | data={'username': 'alice', |
||
| 116 | 'email1': '[email protected]', |
||
| 117 | 'email2': '[email protected]'}) |
||
| 118 | self.assertRedirects(response, closed_redirect) |
||
| 119 | self.assertEqual(RegistrationProfile.objects.count(), 0) |
||
| 120 | |||
| 121 | settings.REGISTRATION_OPEN = True |
||
| 122 | |||
| 123 | View Code Duplication | def test_activation_view_get_success(self): |
|
|
|
|||
| 124 | """ |
||
| 125 | A ``GET`` to the ``ActivationView`` view with valid activation_key |
||
| 126 | |||
| 127 | """ |
||
| 128 | new_user = self.backend.register(username='alice', email='[email protected]', request=self.mock_request) |
||
| 129 | new_user = self.backend.accept(new_user.registration_profile, request=self.mock_request) |
||
| 130 | |||
| 131 | activation_url = reverse('registration_activate', kwargs={ |
||
| 132 | 'activation_key': new_user.registration_profile.activation_key}) |
||
| 133 | |||
| 134 | response = self.client.get(activation_url) |
||
| 135 | self.assertEqual(response.status_code, 200) |
||
| 136 | self.assertTemplateUsed(response, |
||
| 137 | 'registration/activation_form.html') |
||
| 138 | self.failUnless(isinstance(response.context['form'], |
||
| 139 | forms.ActivationForm)) |
||
| 140 | |||
| 141 | def test_activation_view_get_fail(self): |
||
| 142 | """ |
||
| 143 | A ``GET`` to the ``ActivationView`` view wht invalid activation_key |
||
| 144 | raise Http404 |
||
| 145 | |||
| 146 | """ |
||
| 147 | self.backend.register(username='alice', email='[email protected]', request=self.mock_request) |
||
| 148 | |||
| 149 | activation_url = reverse('registration_activate', kwargs={ |
||
| 150 | 'activation_key': 'invalidactivationkey'}) |
||
| 151 | |||
| 152 | response = self.client.get(activation_url) |
||
| 153 | self.assertEqual(response.status_code, 404) |
||
| 154 | |||
| 155 | def test_activation_view_post_success(self): |
||
| 156 | """ |
||
| 157 | A ``POST`` to the ``ActivationView`` view with valid data properly |
||
| 158 | handles a valid activation |
||
| 159 | |||
| 160 | """ |
||
| 161 | User = get_user_model() |
||
| 162 | new_user = self.backend.register(username='alice', email='[email protected]', request=self.mock_request) |
||
| 163 | new_user = self.backend.accept(new_user.registration_profile, request=self.mock_request) |
||
| 164 | |||
| 165 | activation_url = reverse('registration_activate', kwargs={ |
||
| 166 | 'activation_key': new_user.registration_profile.activation_key}) |
||
| 167 | |||
| 168 | response = self.client.post(activation_url,{ |
||
| 169 | 'password1': 'swordfish', |
||
| 170 | 'password2': 'swordfish'}) |
||
| 171 | |||
| 172 | success_redirect = 'http://testserver%s' % reverse('registration_activation_complete') |
||
| 173 | self.assertRedirects(response, success_redirect) |
||
| 174 | # RegistrationProfile should be removed with activation |
||
| 175 | self.assertEqual(RegistrationProfile.objects.count(), 0) |
||
| 176 | # registration, acceptance and activation |
||
| 177 | self.assertEqual(len(mail.outbox), 3) |
||
| 178 | self.failUnless(User.objects.get(username='alice').is_active) |
||
| 179 | |||
| 180 | def test_activation_view_post_failure(self): |
||
| 181 | """ |
||
| 182 | A ``POST`` to the ``ActivationView`` view with invalid data does not |
||
| 183 | activate a user, and raise Http404 |
||
| 184 | |||
| 185 | """ |
||
| 186 | expired_user = self.backend.register(username='alice', email='[email protected]', request=self.mock_request) |
||
| 187 | expired_user = self.backend.accept(expired_user.registration_profile, request=self.mock_request) |
||
| 188 | expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS+1) |
||
| 189 | expired_user.save() |
||
| 190 | |||
| 191 | activation_url = reverse('registration_activate', kwargs={ |
||
| 192 | 'activation_key': expired_user.registration_profile.activation_key}) |
||
| 193 | |||
| 194 | response = self.client.post(activation_url,{ |
||
| 195 | 'password1': 'swordfish', |
||
| 196 | 'password2': 'swordfish'}) |
||
| 197 | |||
| 198 | self.assertEqual(response.status_code, 404) |
||
| 199 |