I'm trying to test a couple of endpoints that require a user to be authorised in Django:
def test_one(self) -> None:
client = Client()
user = client.post('/create', {
'username': 'bob.smith',
'email': '[email protected]',
'password': 'password',
})
token = client.post('/gettoken', {
'identity': 'bob.smith',
'password': 'password',
})
x = token.json()
resp = client.get('/changepassword', headers={"Authorization": f"Bearer {x['access']}"})
self.assertEqual(resp.status_code, 200)
However, it keeps returning a 403:
{"detail":"Authentication credentials were not provided."}
The content of x is:
{'refresh': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYzMTI0MTIwNSwianRpIjoiNDdjNzM5NThkNmYyNGJiMzkxMzRlM2Y2NThhZWI1MjEiLCJ1c2VyX2lkIjoxLCJuYW1lIjoiYm9iYnkuc21pdGgiLCJlbWFpbCI6ImJvYkBzZWNkaW0uY29tIiwiZ3VpZCI6NjA2NzU1ODI4NX0.lqdU5EfSA3DRbfxqum6XyvBc28t-vam8GVDq4W-NOsz7j9VTcg-i9AYQO0V-PLF7Lo89i6Pq50u0tZop7X5PMUryc3sCWPPnEsPyTm7Fi_cVV054E8chd-BP9fzykwW7ef7ufLTjzzPJfQdniq48jlRmce8Yjv_SspwoAhU2uYO54jtn2NNMRk80NckKyzxy9TwU9tItlPMCtoeK1SCucVsv-PfTJscxEXcaC347AZ0Dytqm2DVyDzQbt8RE9MKOXbHkhijeaftNBuiOMqJXE7NWQQ37iyF8bF2iPsc0M-T7ECFwsfFgPK_TD93ETCl9k_o6_6yxOHvx83dk6vGyzONJgl0MJ_W_y_B1UibQttBMoDfMwCxkM7akQ1OhL3ccb7GW-nGgqlOVREfxxW5xCN6h7O9cPMVk74jQ4xv2N6SlkEWIqb5UhQ4ZIJvFVrw-BOHLY_AqvnMadD9gYn7MaDPGXlpCX2EqOvOmKinASuSTJ94kXZ9pwMzuo_b-j5DnQWKgShZydpMZTt77TZ8_MZUtBBf7W-E_z71auQrgopgq82hHEpDfq3FoY4N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ideas why it's saying that authentication details were not provided?
CodePudding user response:
If you are using django rest framework use APIClient
rather than Client
client.credentials(HTTP_AUTHORIZATION='Bearer ' x['access'])