import unittest
from distutils.version import StrictVersion

import rest_framework
from django.test import TestCase

from rest_framework_jwt.compat import get_user_model
from rest_framework_jwt.serializers import JSONWebTokenSerializer
from rest_framework_jwt import utils

User = get_user_model()

drf2 = rest_framework.VERSION < StrictVersion('3.0.0')
drf3 = rest_framework.VERSION >= StrictVersion('3.0.0')


class JSONWebTokenSerializerTests(TestCase):
    def setUp(self):
        self.email = 'jpueblo@example.com'
        self.username = 'jpueblo'
        self.password = 'password'
        self.user = User.objects.create_user(
            self.username, self.email, self.password)

        self.data = {
            'username': self.username,
            'password': self.password
        }

    @unittest.skipUnless(drf2, 'not supported in this version')
    def test_empty_drf2(self):
        serializer = JSONWebTokenSerializer()
        expected = {
            'username': ''
        }

        self.assertEqual(serializer.data, expected)

    @unittest.skipUnless(drf3, 'not supported in this version')
    def test_empty_drf3(self):
        serializer = JSONWebTokenSerializer()
        expected = {
            'username': '',
            'password': ''
        }

        self.assertEqual(serializer.data, expected)

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

        token = serializer.object['token']
        decoded_payload = utils.jwt_decode_handler(token)

        self.assertTrue(is_valid)
        self.assertEqual(decoded_payload['username'], self.username)

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

        expected_error = {
            'non_field_errors': ['Unable to login with provided credentials.']
        }

        self.assertFalse(is_valid)
        self.assertEqual(serializer.errors, expected_error)

    def test_disabled_user(self):
        self.user.is_active = False
        self.user.save()

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

        expected_error = {
            'non_field_errors': ['User account is disabled.']
        }

        self.assertFalse(is_valid)
        self.assertEqual(serializer.errors, expected_error)

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

        expected_error = {
            'username': ['This field is required.'],
            'password': ['This field is required.']
        }

        self.assertFalse(is_valid)
        self.assertEqual(serializer.errors, expected_error)

@@ -61,30 +61,55 @@
 %0Aimport 
-rest_framework
+django%0Afrom django.test import TestCase
 %0Afrom dj
@@ -113,32 +113,38 @@
  django.test
+.utils
  import 
 TestCase%0A%0Afr
@@ -131,24 +131,55 @@
  import 
-TestCase
+override_settings%0Aimport rest_framework
 %0A%0Afrom r
@@ -2128,30 +2128,841 @@
     
-def test_disabled_user
+@unittest.skipIf(%0A        django.VERSION%5B1%5D %3E= 10,%0A        reason='The ModelBackend does not permit login when is_active is False.')%0A    def test_disabled_user(self):%0A        self.user.is_active = False%0A        self.user.save()%0A%0A        serializer = JSONWebTokenSerializer(data=self.data)%0A        is_valid = serializer.is_valid()%0A%0A        expected_error = %7B%0A            'non_field_errors': %5B'User account is disabled.'%5D%0A        %7D%0A%0A        self.assertFalse(is_valid)%0A        self.assertEqual(serializer.errors, expected_error)%0A%0A    @unittest.skipUnless(%0A        django.VERSION%5B1%5D %3E= 10,%0A        reason='The AllowAllUsersModelBackend permits login when is_active is False.')%0A    @override_settings(AUTHENTICATION_BACKENDS=%5B%0A        'django.contrib.auth.backends.AllowAllUsersModelBackend'%5D)%0A    def test_disabled_user_all_users_backend
 (sel

Use different backends depending on the version.