Ítalo Holanda
Ítalo Holanda

Reputation: 21

How to programmatically test Django Auth0 protected api's?

I want to do unit tests for my Django app. I'm using the Django-oauth-toolkit library to protect my views, but I can't test my APIs because they are protected by a Bearer Token.

def test_get_meeting_list(self):
        response = self.client.get('/meetings/')
    # (I need to pass a token, 
    # but I won't have the token)
        self.assertEqual(response.status_code, 200)
    # (AssertionError: 401 != 200)

So, how could I test that?

I tried to make a fake authentication like the official lib tests https://github.com/jazzband/django-oauth-toolkit/blob/master/tests/test_application_views.py but I realized that's will be very complex.

Upvotes: 0

Views: 208

Answers (1)

StefanoTrv
StefanoTrv

Reputation: 197

It should be something like this:

def test_get_meeting_list(self):
    author = User.objects.create_user(username='test', password='test')
    self.client.login(username='test', password='test')
    response = self.client.get('/meetings/')
    self.assertEqual(response.status_code, 200)

Upvotes: 0

Related Questions