Search code examples
pythondjangodjango-testing

Django - How to add access token to Client.post in Django test?


So I have some code below. Every endpoint has an authentication process, which is below as well. I want to be able to attach an access token, which is in cls.user to the Client.post so that I can test all the endpoints and ensure they are authenticating properly as well. How can I do this? So ideally I'd be attaching <bearer> <access token> to request.Meta['HTTP_AUTHORIZATION']

test.py

import json
from cheers.models import *
from warrant import Cognito
from django.urls import reverse
from django.test import TestCase
from rest_framework import status
from cheers.models import GoalCategory, Post
from dummy_factory.Factories import UserFactory, GoalFactory


class PostTest(TestCase):
    @classmethod
    # Generates Test DB data to persist throughout all tests
    def setUpTestData(cls) -> None:
        cls.goal_category = 'health'
        GoalCategory.objects.create(category=cls.goal_category, emoji_url='url')
        cls.user = UserFactory()
        cls.goal = GoalFactory()
        user_obj = User.objects.get(pk=cls.user.phone_number)
        goal_obj = Goal.objects.get(pk=cls.goal.uuid)
        Post.objects.create(creator_id=user_obj, goal_id=goal_obj, body='Some text')
        cls.user = Cognito(<Some login credentials>)
        cls.user.authenticate(password=<password>)

    def test_create(self):
        response = self.client.post(reverse('post'),
                                    data=json.dumps({'creator_id': str(self.user.uuid),
                                                     'goal_id': str(self.goal.uuid),
                                                     'body': 'Some text #Test'}),
                                    content_type='application/json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

Test authenticator function

def cognito_authenticator(view_func):
    def wrapped_view(request, *args, **kwargs):
        # Check the cognito token from the request.
        token = request.META['HTTP_AUTHORIZATION'].split(' ')[1]

        try:
            jwt.decode_cognito_jwt(token)
        except Exception:
            # Fail if invalid
            return Response("Invalid JWT", status=status.HTTP_401_UNAUTHORIZED)  # Or HttpResponseForbidden()
        else:
            # Proceed with the view if valid
            return view_func(request, *args, **kwargs)

    return wrapped_view

Solution

  • You can set the header like this:

    token = 'sometoken'
    response = self.client.post(
        reverse('post'),
        data=json.dumps({
            'creator_id': str(self.user.uuid),
            'goal_id': str(self.goal.uuid),
            'body': 'Some text #Test'
        }),
        content_type='application/json',
        **{'HTTP_AUTHORIZATION': f'Bearer {token}'}
    )
    

    And then access the header using:

    request.META['HTTP_AUTHORIZATION']