Fix test for request auth hiding AttributeErrors

This commit is contained in:
Ryan P Kilby 2017-11-15 17:44:20 -05:00
parent cddaa38b28
commit 0d5d11b277

View File

@ -6,6 +6,7 @@ from __future__ import unicode_literals
import os.path import os.path
import tempfile import tempfile
import pytest
from django.conf.urls import url from django.conf.urls import url
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.middleware import AuthenticationMiddleware from django.contrib.auth.middleware import AuthenticationMiddleware
@ -217,22 +218,20 @@ class TestUserSetter(TestCase):
""" """
class AuthRaisesAttributeError(object): class AuthRaisesAttributeError(object):
def authenticate(self, request): def authenticate(self, request):
import rest_framework self.MISSPELLED_NAME_THAT_DOESNT_EXIST
rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST
self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),)) request = Request(self.wrapped_request, authenticators=(AuthRaisesAttributeError(),))
SessionMiddleware().process_request(self.request)
login(self.request, self.user) # The middleware processes the underlying Django request, sets anonymous user
try: assert self.wrapped_request.user.is_anonymous
self.request.user
except AttributeError as error: # The DRF request object does not have a user and should run authenticators
assert str(error) in ( expected = r"no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'"
"'module' object has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'", # Python < 3.5 with pytest.raises(AttributeError, match=expected):
"module 'rest_framework' has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'", # Python >= 3.5 request.user
)
else: with pytest.raises(AttributeError, match=expected):
assert False, 'AttributeError not raised' login(request, self.user)
class TestAuthSetter(TestCase): class TestAuthSetter(TestCase):