Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Pass Request from JSONWebTokenSerializer to django.contrib.auth #412

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion rest_framework_jwt/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def validate(self, attrs):
}

if all(credentials.values()):
user = authenticate(**credentials)
user = authenticate(request=self.context.get('request', None), **credentials)

if user:
if not user.is_active:
Expand Down
23 changes: 16 additions & 7 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
from distutils.version import StrictVersion

import django
from django.http import HttpRequest
from django.test import TestCase
from django.test.utils import override_settings

import rest_framework
from rest_framework.request import Request


from rest_framework_jwt.compat import get_user_model
from rest_framework_jwt.serializers import JSONWebTokenSerializer
Expand All @@ -29,9 +33,14 @@ def setUp(self):
'password': self.password
}

def get_serializer(self, **kwargs):
serializer = JSONWebTokenSerializer(**kwargs)
serializer.context['request'] = Request(HttpRequest()),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the , at the end of this line a mistake? That creates a tuple for the value of serializer.context['request'] which doesn't seem quite right

return serializer

@unittest.skipUnless(drf2, 'not supported in this version')
def test_empty_drf2(self):
serializer = JSONWebTokenSerializer()
serializer = self.get_serializer()
expected = {
'username': ''
}
Expand All @@ -40,7 +49,7 @@ def test_empty_drf2(self):

@unittest.skipUnless(drf3, 'not supported in this version')
def test_empty_drf3(self):
serializer = JSONWebTokenSerializer()
serializer = self.get_serializer()
expected = {
'username': '',
'password': ''
Expand All @@ -49,7 +58,7 @@ def test_empty_drf3(self):
self.assertEqual(serializer.data, expected)

def test_create(self):
serializer = JSONWebTokenSerializer(data=self.data)
serializer = self.get_serializer(data=self.data)
is_valid = serializer.is_valid()

token = serializer.object['token']
Expand All @@ -60,7 +69,7 @@ def test_create(self):

def test_invalid_credentials(self):
self.data['password'] = 'wrong'
serializer = JSONWebTokenSerializer(data=self.data)
serializer = self.get_serializer(data=self.data)
is_valid = serializer.is_valid()

expected_error = {
Expand All @@ -77,7 +86,7 @@ def test_disabled_user(self):
self.user.is_active = False
self.user.save()

serializer = JSONWebTokenSerializer(data=self.data)
serializer = self.get_serializer(data=self.data)
is_valid = serializer.is_valid()

expected_error = {
Expand All @@ -96,7 +105,7 @@ def test_disabled_user_all_users_backend(self):
self.user.is_active = False
self.user.save()

serializer = JSONWebTokenSerializer(data=self.data)
serializer = self.get_serializer(data=self.data)
is_valid = serializer.is_valid()

expected_error = {
Expand All @@ -107,7 +116,7 @@ def test_disabled_user_all_users_backend(self):
self.assertEqual(serializer.errors, expected_error)

def test_required_fields(self):
serializer = JSONWebTokenSerializer(data={})
serializer = self.get_serializer(data={})
is_valid = serializer.is_valid()

expected_error = {
Expand Down