1010from django .urls import path
1111
1212from rest_framework import fields , serializers
13+ from rest_framework .authtoken .models import Token
1314from rest_framework .decorators import api_view
1415from rest_framework .response import Response
1516from rest_framework .test import (
1920
2021@api_view (['GET' , 'POST' , 'PUT' , 'PATCH' , 'DELETE' , 'OPTIONS' ])
2122def view (request ):
22- return Response ({
23- 'auth' : request .META .get ('HTTP_AUTHORIZATION' , b'' ),
24- 'user' : request .user .username
25- })
23+ data = {'auth' : request .META .get ('HTTP_AUTHORIZATION' , b'' )}
24+ if request .user :
25+ data ['user' ] = request .user .username
26+ if request .auth :
27+ data ['token' ] = request .auth .key
28+ return Response (data )
2629
2730
2831@api_view (['GET' , 'POST' ])
@@ -78,14 +81,46 @@ def test_credentials(self):
7881 response = self .client .get ('/view/' )
7982 assert response .data ['auth' ] == 'example'
8083
81- def test_force_authenticate (self ):
84+ def test_force_authenticate_with_user (self ):
8285 """
83- Setting `.force_authenticate()` forcibly authenticates each request.
86+ Setting `.force_authenticate()` with a user forcibly authenticates each
87+ request with that user.
8488 """
8589 user = User .objects .create_user ('example' , 'example@example.com' )
86- self .client .force_authenticate (user )
90+
91+ self .client .force_authenticate (user = user )
92+ response = self .client .get ('/view/' )
93+
94+ assert response .data ['user' ] == 'example'
95+ assert 'token' not in response .data
96+
97+ def test_force_authenticate_with_token (self ):
98+ """
99+ Setting `.force_authenticate()` with a token forcibly authenticates each
100+ request with that token.
101+ """
102+ user = User .objects .create_user ('example' , 'example@example.com' )
103+ token = Token .objects .create (key = 'xyz' , user = user )
104+
105+ self .client .force_authenticate (token = token )
87106 response = self .client .get ('/view/' )
107+
108+ assert response .data ['token' ] == 'xyz'
109+ assert 'user' not in response .data
110+
111+ def test_force_authenticate_with_user_and_token (self ):
112+ """
113+ Setting `.force_authenticate()` with a user and token forcibly
114+ authenticates each request with that user and token.
115+ """
116+ user = User .objects .create_user ('example' , 'example@example.com' )
117+ token = Token .objects .create (key = 'xyz' , user = user )
118+
119+ self .client .force_authenticate (user = user , token = token )
120+ response = self .client .get ('/view/' )
121+
88122 assert response .data ['user' ] == 'example'
123+ assert response .data ['token' ] == 'xyz'
89124
90125 def test_force_authenticate_with_sessions (self ):
91126 """
@@ -102,8 +137,9 @@ def test_force_authenticate_with_sessions(self):
102137 response = self .client .get ('/session-view/' )
103138 assert response .data ['active_session' ] is True
104139
105- # Force authenticating as `None` should also logout the user session.
106- self .client .force_authenticate (None )
140+ # Force authenticating with `None` user and token should also logout
141+ # the user session.
142+ self .client .force_authenticate (user = None , token = None )
107143 response = self .client .get ('/session-view/' )
108144 assert response .data ['active_session' ] is False
109145
0 commit comments