python - Django-Rest-Framework JWT Unit Test Saying "No Authentication Provided" -
when attempt unit test error:
"detail":"authentication credentials not provided."
however, using django-rest-framework-jwt, wants web token placed @ header "authorization: jwt ". did wireshark on working, accepted packet website , had authorization: jwt header. current request rejected. can't figure out why it's failing.
adding force_authenticate allows run, i'd test permissions.
here reference documentation:
now in order access protected api urls must include authorization: jwt header.
django-rest-framework-jwt documentation
here request pycharm:
{'request_method': 'patch', 'content_type': 'application/json; charset=none', 'content_length': 74, 'path_info': '/api/v1/members/5', 'query_string': '', 'authorization': 'jwt eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyj1c2vybmftzsi6invzzxiilcjlbwfpbci6imvtywlsqgdtywlslmnvbsisinvzzxjfawqiojusimv4cci6mtqznzuzote4mh0.cgcxw9sroygbeicdbmw8ey_ya9ebrd-ptrsgwd2jzlk', 'wsgi.input': <django.test.client.fakepayload object @ 0x106689748> } here code:
class modifytest(apitestcase): """ test modifying users, include member_profile """ username = 'user' password = 'passwd' token = 0 user_id = 0 def setup(self): data = {'username': self.username, 'password': self.password, 'email': 'email@gmail.com'} url = reverse('members:auth-user-create') response = self.client.post(url, data, format='json') self.user_id = response.data['id'] data = {'username': self.username, 'password': self.password} url = reverse('token_auth') response = self.client.post(url, data, format='json') self.token = response.data['token'] def test_auth_user_info(self): data = {'id': str(self.user_id), 'password': self.password, 'email': 'email@gmail.com', 'username': self.username, } url = reverse('members:auth-user-detail', kwargs={'pk': self.user_id}) response = self.client.patch(url, data, authorization='jwt ' + self.token, format='son') self.assertequal(response.status_code, 200) update: when stepping through authentication realized view not using correct authentication classes. below settings though...
rest_framework = { 'default_permission_classes': ( 'rest_framework.permissions.isauthenticated', ), 'default_authentication_classes': ( #'rest_framework.authentication.basicauthentication', #'rest_framework.authentication.sessionauthentication', 'rest_framework_jwt.authentication.jsonwebtokenauthentication', ), } rest_framework = { 'default_filter_backends': ('rest_framework.filters.djangofilterbackend',) } and view:
class authuserdetail(generics.retrieveupdatedestroyapiview): permission_classes = (isowneroradmin,) queryset = get_user_model().objects.all() serializer_class = authusermodelserializer however, permission classes in debugger when view called are:
<class 'rest_framework.authentication.sessionauthentication'>, <class 'rest_framework.authentication.basicauthentication'> so adding line view fixes it:
authentication_classes = (jsonwebtokenauthentication,) but shouldn't needed default classes selected... ideas why isn't using defaults?
i figured out... didn't recognize had 2 sections of rest_framework second overwriting first erasing defaults... new settings file:
rest_framework = { 'default_permission_classes': ( 'rest_framework.permissions.isauthenticated', ), 'default_authentication_classes': ( 'rest_framework_jwt.authentication.jsonwebtokenauthentication', ), 'default_filter_backends': ('rest_framework.filters.djangofilterbackend',) }
Comments
Post a Comment