An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 15, 2022 08:40 am GMT
Original Link: https://dev.to/koladev/authentication-in-tests-with-drf-4jin
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To