Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 15, 2022 08:40 am GMT

Authentication in Tests with DRF

Writing tests with Django and Django REST is made easier with the tools and classes provided.
And if you are writing tests to test the endpoints of your API, using the APIClient class from Django REST is a simple way to write the tests and make requests.

And if you are looking to test a protected resource, you can directly use the .login(**kwargs) method.

client = APIClient()client.login(username='john', password='12345')

And if you want to bypass the authentication process without having to provide a username or password, you can use the .force_authenticate(user=None)

user = User.objects.first()client = APIClient()client.force_authenticate(user=user)

Article posted using bloggu.io. Try it for free.


Original Link: https://dev.to/koladev/authentication-in-tests-with-drf-4jin

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To