對我的端點的查詢工作正常(只要我傳遞一個有效的標記),它就會返回我的響應數據的json表示。使用APIRequestFactory測試基於令牌的身份驗證的正確方法是什麼?
調用我的終點,在頭傳遞一個身份驗證令牌在服務API代碼:
headers = {'content-type': 'application/json',
'Authorization': 'Token {}'.format(myToken)}
url = 'http://localhost:8000/my_endpoint/'
r = session.get(url=url, params=params, headers=headers)
在views.py,我有包裝的調度方法的視圖的方法裝飾(viewsets .ReadOnlyModelViewSet):
def login_required(f):
def check_login_and_call(request, *args, **kwargs):
authentication = request.META.get('HTTP_AUTHORIZATION', b'')
if isinstance(authentication, str):
authentication = authentication.encode(HTTP_HEADER_ENCODING)
key = authentication.split()
if not key or len(key) != 2:
raise PermissionDenied('Authentication failed.')
user, token = authenticate_credentials(key[1])
return f(request, *args, **kwargs)
return check_login_and_call
我試圖寫一個測試使用令牌驗證請求:
from rest_framework.authtoken.models import Token
from rest_framework.test import APIRequestFactory
from rest_framework.test import APITestCase
from rest_framework.test import force_authenticate
class EndpointViewTest(APITestCase):
def setUp(self):
self.factory = APIRequestFactory()
self.user = User.objects.create_user(
username='[email protected]', email='[email protected]', password='top_secret')
self.token = Token.objects.create(user=self.user)
self.token.save()
def test_token_auth(self):
request = self.factory.get('/my_endpoint')
force_authenticate(request, token=self.token.key)
view = views.EndpointViewSet.as_view({'get': 'list'})
response = view(request)
self.assertEqual(response.status_code, 200)
json_response = json.loads(response.render().content)['results']
出於某種原因,我無法獲得正確傳遞此測試令牌的請求。使用force_authenticate似乎不會更改我用於驗證令牌的標頭。當前輸出正在提高「PermissionDenied:身份驗證失敗。」因爲令牌沒有在請求中設置。
在我的測試中,是否有正確的方法在請求頭中設置此值,或者重構我首先使用它的方式?
'self.client = APIClient()'這行不是必需的,因爲你從已經擁有它的'APITestCase'繼承。 – 2017-11-08 16:13:48