Commits

Anonymous committed a0403b8

Fixes #81 -- splits view tests into integration tests and application tests, and provides blank templates for testing purposes only so that application tests pass while integration tests will failif the requisit templates are not in the user's project.

Comments (0)

Files changed (10)

registration/tests/templates/registration/activate.html

Empty file added.

registration/tests/templates/registration/activation_complete.html

Empty file added.

registration/tests/templates/registration/activation_email.txt

Empty file added.

registration/tests/templates/registration/activation_email_subject.txt

Empty file added.

registration/tests/templates/registration/login.html

Empty file added.

registration/tests/templates/registration/registration_closed.html

Empty file added.

registration/tests/templates/registration/registration_complete.html

Empty file added.

registration/tests/templates/registration/registration_form.html

Empty file added.

registration/tests/templates/registration/test_template_name.html

Empty file added.

registration/tests/views.py

-import datetime
+import datetime, os
 
 from django.conf import settings
 from django.contrib.auth.models import User
         available; that needs to have ``ACCOUNT_ACTIVATION_DAYS`` set.
 
         """
+        self.old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
+        settings.REGISTRATION_BACKEND = 'registration.backends.default.DefaultBackend'
         self.old_activation = getattr(settings, 'ACCOUNT_ACTIVATION_DAYS', None)
-        if self.old_activation is None:
-            settings.ACCOUNT_ACTIVATION_DAYS = 7
+        settings.ACCOUNT_ACTIVATION_DAYS = 7
+        self.old_template_dirs = getattr(settings, 'TEMPLATE_DIRS')
+        settings.TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)
 
     def tearDown(self):
         """
         originally set.
 
         """
-        if self.old_activation is None:
-            settings.ACCOUNT_ACTIVATION_DAYS = self.old_activation
-
-    def test_registration_view_initial(self):
-        """
-        A ``GET`` to the ``register`` view uses the appropriate
-        template and populates the registration form into the context.
-
-        """
-        response = self.client.get(reverse('registration_register'))
-        self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response,
-                                'registration/registration_form.html')
-        self.failUnless(isinstance(response.context['form'],
-                                   forms.RegistrationForm))
-
-    def test_registration_view_success(self):
-        """
-        A ``POST`` to the ``register`` view with valid data properly
-        creates a new user and issues a redirect.
-
-        """
-        response = self.client.post(reverse('registration_register'),
-                                    data={'username': 'alice',
-                                          'email': 'alice@example.com',
-                                          'password1': 'swordfish',
-                                          'password2': 'swordfish'})
-        self.assertRedirects(response,
-                             'http://testserver%s' % reverse('registration_complete'))
-        self.assertEqual(RegistrationProfile.objects.count(), 1)
-        self.assertEqual(len(mail.outbox), 1)
-
-    def test_registration_view_failure(self):
-        """
-        A ``POST`` to the ``register`` view with invalid data does not
-        create a user, and displays appropriate error messages.
-
-        """
-        response = self.client.post(reverse('registration_register'),
-                                    data={'username': 'bob',
-                                          'email': 'bobe@example.com',
-                                          'password1': 'foo',
-                                          'password2': 'bar'})
-        self.assertEqual(response.status_code, 200)
-        self.failIf(response.context['form'].is_valid())
-        self.assertFormError(response, 'form', field=None,
-                             errors=u"The two password fields didn't match.")
-        self.assertEqual(len(mail.outbox), 0)
+        settings.REGISTRATION_BACKEND = self.old_backend
+        settings.ACCOUNT_ACTIVATION_DAYS = self.old_activation
+        settings.TEMPLATE_DIRS = self.old_template_dirs
 
     def test_registration_view_closed(self):
         """
                                           'password2': 'swordfish'})
         self.assertRedirects(response, success_redirect)
 
+    def test_activation_success_url(self):
+        """
+        Passing ``success_url`` to the ``activate`` view and
+        successfully activating will result in that URL being used for
+        the redirect.
+        
+        """
+        success_redirect = 'http://testserver%s' % reverse('registration_test_custom_success_url')
+        self.client.post(reverse('registration_register'),
+                         data={'username': 'alice',
+                               'email': 'alice@example.com',
+                               'password1': 'swordfish',
+                               'password2': 'swordfish'})
+        profile = RegistrationProfile.objects.get(user__username='alice')
+        response = self.client.get(reverse('registration_test_activate_success_url',
+                                           kwargs={'activation_key': profile.activation_key}))
+        self.assertRedirects(response, success_redirect)
+        
+    def test_activation_template_name(self):
+        """
+        Passing ``template_name`` to the ``activate`` view will result
+        in that template being used.
+
+        """
+        response = self.client.get(reverse('registration_test_activate_template_name',
+                                   kwargs={'activation_key': 'foo'}))
+        self.assertTemplateUsed(response, 'registration/test_template_name.html')
+
+    def test_activation_extra_context(self):
+        """
+        Passing ``extra_context`` to the ``activate`` view will
+        correctly populate the context.
+
+        """
+        response = self.client.get(reverse('registration_test_activate_extra_context',
+                                           kwargs={'activation_key': 'foo'}))
+        self.assertEqual(response.context['foo'], 'bar')
+        # Callables in extra_context are called to obtain the value.
+        self.assertEqual(response.context['callable'], 'called')
+
+
+class RegistrationViewsIntegrationTests(TestCase):
+    def setUp(self):
+        self.old_registration_open = settings.REGISTRATION_OPEN
+        settings.REGISTRATION_OPEN = True
+
+    def tearDown(self):
+        settings.REGISTRATION_OPEN = self.old_registration_open
+
+    def test_registration_view_initial(self):
+        """
+        A ``GET`` to the ``register`` view uses the appropriate
+        template and populates the registration form into the context.
+
+        """
+        response = self.client.get(reverse('registration_register'))
+        self.assertEqual(response.status_code, 200)
+        self.assertTemplateUsed(response,
+                                'registration/registration_form.html')
+        self.failUnless(isinstance(response.context['form'],
+                                   forms.RegistrationForm))
+
+    def test_registration_view_success(self):
+        """
+        A ``POST`` to the ``register`` view with valid data properly
+        creates a new user and issues a redirect.
+
+        """
+        response = self.client.post(reverse('registration_register'),
+                                    data={'username': 'alice',
+                                          'email': 'alice@example.com',
+                                          'password1': 'swordfish',
+                                          'password2': 'swordfish'})
+        self.assertRedirects(response,
+                             'http://testserver%s' % reverse('registration_complete'))
+        self.assertEqual(RegistrationProfile.objects.count(), 1)
+        self.assertEqual(len(mail.outbox), 1)
+
+    def test_registration_view_failure(self):
+        """
+        A ``POST`` to the ``register`` view with invalid data does not
+        create a user, and displays appropriate error messages.
+
+        """
+        response = self.client.post(reverse('registration_register'),
+                                    data={'username': 'bob',
+                                          'email': 'bobe@example.com',
+                                          'password1': 'foo',
+                                          'password2': 'bar'})
+        self.assertEqual(response.status_code, 200)
+        self.failIf(response.context['form'].is_valid())
+        self.assertFormError(response, 'form', field=None,
+                             errors=u"The two password fields didn't match.")
+        self.assertEqual(len(mail.outbox), 0)
+
     def test_valid_activation(self):
         """
         Test that the ``activate`` view properly handles a valid
         self.assertEqual(response.status_code, 200)
         self.assertEqual(response.context['activation_key'],
                          expired_profile.activation_key)
-        self.failIf(User.objects.get(username='bob').is_active)
-
-    def test_activation_success_url(self):
-        """
-        Passing ``success_url`` to the ``activate`` view and
-        successfully activating will result in that URL being used for
-        the redirect.
-        
-        """
-        success_redirect = 'http://testserver%s' % reverse('registration_test_custom_success_url')
-        self.client.post(reverse('registration_register'),
-                         data={'username': 'alice',
-                               'email': 'alice@example.com',
-                               'password1': 'swordfish',
-                               'password2': 'swordfish'})
-        profile = RegistrationProfile.objects.get(user__username='alice')
-        response = self.client.get(reverse('registration_test_activate_success_url',
-                                           kwargs={'activation_key': profile.activation_key}))
-        self.assertRedirects(response, success_redirect)
-        
-    def test_activation_template_name(self):
-        """
-        Passing ``template_name`` to the ``activate`` view will result
-        in that template being used.
-
-        """
-        response = self.client.get(reverse('registration_test_activate_template_name',
-                                   kwargs={'activation_key': 'foo'}))
-        self.assertTemplateUsed(response, 'registration/test_template_name.html')
-
-    def test_activation_extra_context(self):
-        """
-        Passing ``extra_context`` to the ``activate`` view will
-        correctly populate the context.
-
-        """
-        response = self.client.get(reverse('registration_test_activate_extra_context',
-                                           kwargs={'activation_key': 'foo'}))
-        self.assertEqual(response.context['foo'], 'bar')
-        # Callables in extra_context are called to obtain the value.
-        self.assertEqual(response.context['callable'], 'called')
+        self.failIf(User.objects.get(username='bob').is_active)