Commits

Mikhail Korobov committed 38673e1

More tests

Comments (0)

Files changed (2)

django_webtest_tests/settings.py

     'django.contrib.messages.middleware.MessageMiddleware',
 )
 
-ROOT_URLCONF = 'django_webtest_tests.urls'
+ROOT_URLCONF = 'urls'
 
 TEMPLATE_DIRS = (
     join('templates'),

django_webtest_tests/testapp_tests/tests.py

 # -*- coding: utf-8 -*-
 from webtest import AppError
 from django_webtest import WebTest
+from django.contrib.auth.models import User
 
 
 class GetRequestTest(WebTest):
         self.assertRaises(AppError, self.app.get, '/404/')
 
 
+class CsrfProtectionTest(WebTest):
+    def test_csrf_failed(self):
+        response = self.app.post('/', expect_errors=True)
+        self.assertEqual(response.status_int, 403)
+
+
 class TemplateContextTest(WebTest):
     def test_rendered_templates(self):
         response = self.app.get('/template/index.html')
         self.assertEqual(response.context['foo'], ('a', 'b', 'c'))
         self.assertEqual(response.context['bar'], True)
         self.assertEqual(response.context['spam'], None)
+
+
+class AuthTest(WebTest):
+    def setUp(self):
+        self.user = User.objects.create_user('foo', 'example@example.com', '123')
+
+    def test_not_logged_in(self):
+        response = self.app.get('/template/index.html')
+        user = response.context['user']
+        assert not user.is_authenticated()
+
+    def test_logged_using_username(self):
+        response = self.app.get('/template/index.html', user='foo')
+        user = response.context['user']
+        assert user.is_authenticated()
+        self.assertEqual(user, self.user)
+
+    def test_logged_using_instance(self):
+        response = self.app.get('/template/index.html', user=self.user)
+        user = response.context['user']
+        assert user.is_authenticated()
+        self.assertEqual(user, self.user)
+
+    def test_auth_is_enabled(self):
+        from django.conf import settings
+        assert 'django.contrib.auth.middleware.RemoteUserMiddleware' in settings.MIDDLEWARE_CLASSES
+        assert 'django.contrib.auth.backends.RemoteUserBackend' in settings.AUTHENTICATION_BACKENDS
+
+
+class DisableAuthSetupTest(WebTest):
+    setup_auth = False
+
+    def test_no_auth(self):
+        from django.conf import settings
+        assert 'django.contrib.auth.middleware.RemoteUserMiddleware' not in settings.MIDDLEWARE_CLASSES
+        assert 'django.contrib.auth.backends.RemoteUserBackend' not in settings.AUTHENTICATION_BACKENDS