Skip to content

Commit 4c29776

Browse files
author
Ryan P Kilby
committed
Fix test for request auth hiding AttributeErrors
1 parent 7848f8d commit 4c29776

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

tests/test_request.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os.path
77
import tempfile
88

9+
import pytest
910
from django.conf.urls import url
1011
from django.contrib.auth import authenticate, login, logout
1112
from django.contrib.auth.middleware import AuthenticationMiddleware
@@ -217,22 +218,20 @@ def test_calling_user_fails_when_attribute_error_is_raised(self):
217218
"""
218219
class AuthRaisesAttributeError(object):
219220
def authenticate(self, request):
220-
import rest_framework
221-
rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST
221+
self.MISSPELLED_NAME_THAT_DOESNT_EXIST
222222

223-
self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),))
224-
SessionMiddleware().process_request(self.request)
223+
request = Request(self.wrapped_request, authenticators=(AuthRaisesAttributeError(),))
225224

226-
login(self.request, self.user)
227-
try:
228-
self.request.user
229-
except AttributeError as error:
230-
assert str(error) in (
231-
"'module' object has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'", # Python < 3.5
232-
"module 'rest_framework' has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'", # Python >= 3.5
233-
)
234-
else:
235-
assert False, 'AttributeError not raised'
225+
# The middleware processes the underlying Django request, sets anonymous user
226+
assert self.wrapped_request.user.is_anonymous
227+
228+
# The DRF request object does not have a user and should run authenticators
229+
expected = r"no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'"
230+
with pytest.raises(AttributeError, match=expected):
231+
request.user
232+
233+
with pytest.raises(AttributeError, match=expected):
234+
login(request, self.user)
236235

237236

238237
class TestAuthSetter(TestCase):

0 commit comments

Comments
 (0)