Search code examples
djangopython-3.xdjango-rest-framework-jwt

How to add JWT token into credentials in Django


In testing, I cannot authenticate my test user. How to add JWT token into self.client.credentials.

def test_retrive_profile_success(self):
    """Test retriving profile for authenticated user"""
    res1 = self.client.post(TOKEN_URL, {'email':'test@greatsoft.uz', 'password':'password'})
    token = res1.data['token']
    self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
    res = self.client.get(ME_URL)
    self.assertEqual(res.status_code, status.HTTP_200_OK)
    self.assertEqual(res.data, {
        'email':self.user.email
    })

I am getting a response of 401.


Solution

  • You need change this line

    self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
    

    to this

    self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)