Answer the question
In order to leave comments, you need to log in
Why doesn't JWT authorization work in APITestCase tests?
I'm writing tests for a restfull application using djangorestframework-jwt. Through postman and the application, everything works, but when I try to impose tests on the code, the tests say that the token is not valid.
Here is the code:
class AdminPanelAuthTest(APITestCase):
def setUp(self) -> None:
user = User(
email="[email protected]",
is_staff=True,
is_active=True,
roles=[POINT_MANAGER]
)
user.set_password("password")
user.save()
self.login = user.email
self.password = "password"
self.client = APIClient()
self.app_header = APPLICATIONS_HEADERS[POINT_MANAGER] # заголовок приложения, так как API разрабатывалось для двух мобильных приложений
def test_admin_auth(self):
self.client.credentials(HTTP_APPLICATION=self.app_header)
response = self.client.post(path="/auth/email/", data={
"email": self.login,
"password": self.password
})
json = response.json()
self.assertIn("token", json) # здесь проходит
self.assertIn("user", json) # и здесь проходит, а значит пользователь авторизовался
token = json["token"]
self.client.credentials(
HTTP_APPLICATION=APPLICATIONS_HEADERS[POINT_MANAGER],
HTTP_AUTHORIZATION="Bearer " + token
)
response = self.client.get(path="/users/users/")
self.assertEqual(response.status_code, 200) # а вот здесь вылетает ошибка, статус 401
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question