mardi 7 juillet 2015

Django-Rest-Framework JWT Unit Test Saying "No Authentication Provided"

When I attempt to do a unit a test I get the error:

"detail":"Authentication credentials were not provided."

However, I am using django-rest-framework-jwt, which wants the web token placed at the header as "Authorization: JWT ". I did a wireshark on a working, accepted packet from my website and it had the Authorization: JWT header. As does my current request which is rejected. I can't figure out why it's failing.

Here is the reference from the Documentation:

Now in order to access protected api urls you must include the Authorization: JWT header.

django-rest-framework-jwt documentation

Here is the request from 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 at 0x106689748>
}

Here is the code:

class ModifyTest(APITestCase):
    """ Test modifying users, to 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, 201)

Aucun commentaire:

Enregistrer un commentaire