Skip to content

add test for force_authenticate about cache of user attr #4102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import uuid

from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext_lazy as _

Expand Down Expand Up @@ -87,3 +88,8 @@ class OneToOnePKSource(RESTFrameworkModel):
target = models.OneToOneField(
OneToOneTarget, primary_key=True,
related_name='required_source', on_delete=models.CASCADE)


class Hat(RESTFrameworkModel):
user = models.OneToOneField(User)
some_key = models.CharField(max_length=100)
72 changes: 71 additions & 1 deletion tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rest_framework.test import (
APIClient, APIRequestFactory, force_authenticate
)
from tests.models import Hat


@api_view(['GET', 'POST'])
Expand Down Expand Up @@ -49,11 +50,26 @@ def post_view(request):
return Response(serializer.validated_data)


@api_view(['GET'])
def get_my_hat(request):
if hasattr(request.user, 'hat'):
return Response({
'id': request.user.hat.id,
'some_key': request.user.hat.some_key,
})
else:
return Response(
{},
status=404,
)


urlpatterns = [
url(r'^view/$', view),
url(r'^session-view/$', session_view),
url(r'^redirect-view/$', redirect_view),
url(r'^post-view/$', post_view)
url(r'^post-view/$', post_view),
url(r'^my_hat/$', get_my_hat),
]


Expand Down Expand Up @@ -203,6 +219,60 @@ def test_empty_post_uses_default_boolean_value(self):
assert response.status_code == 200
assert response.data == {"flag": True}

def test_get_my_hat_with_force_authenticate(self):
user = User.objects.create_user('example', '[email protected]', 'password')
self.client.force_authenticate(user)

response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 404)

hat = Hat.objects.create(user=user, some_key='some_value')
response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, {'id': hat.id, 'some_key': 'some_value'})

another_user = User.objects.create_user('another_example', '[email protected]', 'password')
hat.user = another_user
hat.save()
response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 404)

def test_get_my_hat_with_django_force_login(self):
user = User.objects.create_user('example', '[email protected]', 'password')
self.client.force_login(user)

response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 404)

hat = Hat.objects.create(user=user, some_key='some_value')
response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, {'id': hat.id, 'some_key': 'some_value'})

another_user = User.objects.create_user('another_example', '[email protected]', 'password')
hat.user = another_user
hat.save()
response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 404)

def test_get_my_hat_with_login(self):
user = User.objects.create_user('example', '[email protected]', 'password')
self.client.login(username='example', password='password')

response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 404)

hat = Hat.objects.create(user=user, some_key='some_value')
response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, {'id': hat.id, 'some_key': 'some_value'})

another_user = User.objects.create_user('another_example', '[email protected]', 'password')
hat.user = another_user
hat.save()
response = self.client.get('/my_hat/')
self.assertEqual(response.status_code, 404)


class TestAPIRequestFactory(TestCase):
def test_csrf_exempt_by_default(self):
Expand Down