From 0768339e57e32d13a2e2359aa395d464b7de0534 Mon Sep 17 00:00:00 2001 From: Will Beaufoy Date: Sun, 17 Oct 2021 15:33:34 +0100 Subject: [PATCH] Fix testing with token --- rest_framework/test.py | 2 +- tests/test_testing.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) 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