diff --git a/rest_framework/test.py b/rest_framework/test.py index 07df743c8..04409f962 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -277,7 +277,7 @@ class APIClient(APIRequestFactory, DjangoClient): """ self.handler._force_user = user self.handler._force_token = token - if user is None: + if user is None and token is None: self.logout() # Also clear any possible session info if required def request(self, **kwargs): diff --git a/tests/test_testing.py b/tests/test_testing.py index b6579e369..9ea34c4c5 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -102,8 +102,9 @@ class TestAPITestClient(TestCase): response = self.client.get('/session-view/') assert response.data['active_session'] is True - # Force authenticating as `None` should also logout the user session. - self.client.force_authenticate(None) + # Force authenticating with `None` user and token should also logout + # the user session. + self.client.force_authenticate(user=None, token=None) response = self.client.get('/session-view/') assert response.data['active_session'] is False