0

I created some unit tests with basic asserts checking for status codes to get a few tests done and working. The endpoints themselves work flawlessly when I test them individually outside of the Django unit tests using manage.py test test_file However, I am getting a 401 Unauthorized on all endpoints when I run the tests. I have tried writing in several auth methods to correct the issue but no matter what I try, a 401 is returned.

I have tried adding a force auth function, changing TestCase to APITestCase, and creating a superuser manually in the setUp() for each of my test cases. All of the solutions I have found online for similar problems still yield the 401, unauthorized error.

Using:

Django 3.2.14

Python 3.10.4

I have also followed the Django Rest API documentation for test cases to the letter. I get 401 errors all the way through.

1 Answer 1

0

you need to send jwt tokens in unit test

get jwt tokens and call like this

response = self.client.post("path", {}, Authorization='Bearer ' + token)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.