Fake auth middleware in tests
Some tests require the session and auth middleware.
The auth middleware inject the AnonymousUser.
It helps to pass some tests in the corporate site to update a
dependency
from corsheaders.middleware import CorsMiddleware
from django.conf import settings
-from django.contrib.auth.models import AnonymousUser
+from django.contrib.auth.middleware import AuthenticationMiddleware
+from django.contrib.sessions.middleware import SessionMiddleware
from django.http import Http404
from django.test import TestCase
from django.test.client import RequestFactory
@@ -30,10 +31,14 @@ class MiddlewareTests(TestCase):
privacy_level='public'
)
+ def process_request_auth_middleware(self, request):
+ SessionMiddleware().process_request(request)
+ AuthenticationMiddleware().process_request(request)
+
def test_failey_cname(self):
self.assertFalse(Domain.objects.filter(domain='my.host.com').exists())
request = self.factory.get(self.url, HTTP_HOST='my.host.com')
- request.user = AnonymousUser()
+ self.process_request_auth_middleware(request)
r = self.middleware.process_request(request)
self.assertEqual(r.status_code, 404)
self.assertEqual(request.cname, True)
@@ -98,7 +103,7 @@ class MiddlewareTests(TestCase):
def test_domain_object_missing(self):
self.domain = get(Domain, domain='docs.foobar2.com', project=self.pip)
request = self.factory.get(self.url, HTTP_HOST='docs.foobar.com')
- request.user = AnonymousUser()
+ self.process_request_auth_middleware(request)
r = self.middleware.process_request(request)
self.assertEqual(r.status_code, 404)
@@ -144,7 +149,7 @@ class MiddlewareTests(TestCase):
def test_long_bad_subdomain(self):
domain = 'www.pip.readthedocs.org'
request = self.factory.get(self.url, HTTP_HOST=domain)
- request.user = AnonymousUser()
+ self.process_request_auth_middleware(request)
res = self.middleware.process_request(request)
self.assertEqual(res.status_code, 400)
@@ -152,7 +157,7 @@ class MiddlewareTests(TestCase):
def test_long_subdomain(self):
domain = 'some.long.readthedocs.org'
request = self.factory.get(self.url, HTTP_HOST=domain)
- request.user = AnonymousUser()
+ self.process_request_auth_middleware(request)
res = self.middleware.process_request(request)
self.assertIsNone(res)
