Madiyor
Madiyor

Reputation: 811

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':'[email protected]', '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.

Upvotes: 1

Views: 229

Answers (1)

alamshafi2263
alamshafi2263

Reputation: 659

You need change this line

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

to this

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

Upvotes: 2

Related Questions