0

Although this is perhaps not a minimal example, I'm building on How to test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication. I have a model Session which I'd like to be able to update using the Django REST framework. To this end I wrote the following view:

from rest_framework import generics

from oauth2_provider.contrib.rest_framework import OAuth2Authentication

from ..models import Session
from ..serializers import SessionSerializer


class SessionDetail(generics.UpdateAPIView):
    authentication_classes = [OAuth2Authentication]
    queryset = Session.objects.all()
    serializer_class = SessionSerializer

where the default permission_classes are set in settings.py:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}

I've written the following test case:

import json

from django.contrib.auth.models import User
from datetime import timedelta

from django.urls import reverse
from django.test import TestCase, Client
from django.utils import timezone

from rest_framework import status

from ..models import Family, Session, SessionType, SessionCategory

from oauth2_provider.models import get_application_model, get_access_token_model


Application = get_application_model()
AccessToken = get_access_token_model()


class OAuth2TestCase(TestCase):
    def setUp(self):
        self.username = "test_user"
        self.password = "123456"

        self.user = User.objects.create_user(username=self.username, password=self.password)

        self.application = Application(
            name="Test Application",
            redirect_uris="http://localhost",
            user=self.user,
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
        )
        self.application.save()


        self.TOKEN = '1234567890'
        self.token = AccessToken.objects.create(
            user=self.user,
            token=self.TOKEN,
            application=self.application,
            scope='read write',
            expires=timezone.now() + timedelta(days=1)
        )

        self._create_session()

    def _create_session(self):
        self.session_category = SessionCategory.objects.create(
            name='Birth Prep')
        self.session_type = SessionType.objects.create(
            title='Initial Postpartum Lactation',
            category=self.session_category)
        self.family = Family.objects.create()
        self.session = Session.objects.create(
            family=self.family,
            session_type=self.session_type)

    def test_make_patch_request(self):
        data = {'feedback_expert': 'Yes'}
        url = reverse('sessions-feedback', kwargs={'pk': self.session.id})

        auth = f"Bearer {self.TOKEN}"
        response = self.client.patch(
            path=url,
            data=json.dumps(data),
            content_type="application/json",
            HTTP_AUTHORIZATION=auth,
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        self.assertEqual(Session.objects.get(pk=self.session.id).feedback_expert, 'Yes')
        # self.assertEqual(self.session.feedback_expert, 'Yes')

The test passes, but what puzzles me is that if I comment in the last line and instead do

self.assertEqual(self.session.feedback_expert, 'Yes')

I get a test failure:

(venv) Kurts-MacBook-Pro:lucy-web kurtpeek$ python manage.py test lucy_web.tests.test_feedback
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_make_patch_request (lucy_web.tests.test_feedback.OAuth2TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kurtpeek/Documents/Dev/lucy/lucy-web/lucy_web/tests/test_feedback.py", line 295, in test_make_patch_request
    self.assertEqual(self.session.feedback_expert, 'Yes')
AssertionError: '' != 'Yes'
+ Yes

----------------------------------------------------------------------
Ran 1 test in 1.299s

FAILED (failures=1)
Destroying test database for alias 'default'...

Apparently, the self.session doesn't have its feedback_expert field updated yet, whereas the one returned by Session.objects.get(pk=self.session.id) does. Any idea why this is the case?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

1 Answers1

2

It turns out I needed to call refresh_from_db() on the self.session. So the following works:

    self.session.refresh_from_db()
    self.assertEqual(self.session.feedback_expert, 'Yes')

Now the test passes.

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526