Skip to content

Client.logout() also cancels any existing force_authenticate. #2259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions rest_framework/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ def options(self, path, data=None, format=None, content_type=None,

def logout(self):
self._credentials = {}

# Also clear any `force_authenticate`
self.handler._force_user = None
self.handler._force_token = None

return super(APIClient, self).logout()


Expand Down
20 changes: 15 additions & 5 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# -- coding: utf-8 --

# encoding: utf-8
from __future__ import unicode_literals
from django.conf.urls import patterns, url
from io import BytesIO

from django.contrib.auth.models import User
from django.shortcuts import redirect
from django.test import TestCase
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.test import APIClient, APIRequestFactory, force_authenticate
from io import BytesIO


@api_view(['GET', 'POST'])
Expand Down Expand Up @@ -109,7 +107,7 @@ def test_explicitly_enforce_csrf_checks(self):

def test_can_logout(self):
"""
`logout()` reset stored credentials
`logout()` resets stored credentials
"""
self.client.credentials(HTTP_AUTHORIZATION='example')
response = self.client.get('/view/')
Expand All @@ -118,6 +116,18 @@ def test_can_logout(self):
response = self.client.get('/view/')
self.assertEqual(response.data['auth'], b'')

def test_logout_resets_force_authenticate(self):
"""
`logout()` resets any `force_authenticate`
"""
user = User.objects.create_user('example', '[email protected]', 'password')
self.client.force_authenticate(user)
response = self.client.get('/view/')
self.assertEqual(response.data['user'], 'example')
self.client.logout()
response = self.client.get('/view/')
self.assertEqual(response.data['user'], '')

def test_follow_redirect(self):
"""
Follow redirect by setting follow argument.
Expand Down