|
6 | 6 | import os.path
|
7 | 7 | import tempfile
|
8 | 8 |
|
| 9 | +import pytest |
9 | 10 | from django.conf.urls import url
|
10 | 11 | from django.contrib.auth import authenticate, login, logout
|
11 | 12 | from django.contrib.auth.middleware import AuthenticationMiddleware
|
@@ -217,22 +218,20 @@ def test_calling_user_fails_when_attribute_error_is_raised(self):
|
217 | 218 | """
|
218 | 219 | class AuthRaisesAttributeError(object):
|
219 | 220 | def authenticate(self, request):
|
220 |
| - import rest_framework |
221 |
| - rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST |
| 221 | + self.MISSPELLED_NAME_THAT_DOESNT_EXIST |
222 | 222 |
|
223 |
| - self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),)) |
224 |
| - SessionMiddleware().process_request(self.request) |
| 223 | + request = Request(self.wrapped_request, authenticators=(AuthRaisesAttributeError(),)) |
225 | 224 |
|
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) |
236 | 235 |
|
237 | 236 |
|
238 | 237 | class TestAuthSetter(TestCase):
|
|
0 commit comments