from __future__ import unicode_literals

from rest_framework_simplejwt.compat import reverse
from rest_framework_simplejwt.settings import api_settings
from rest_framework_simplejwt.state import User

from .utils import APIViewTestCase, override_api_settings


class TestTestView(APIViewTestCase):
    view_name = 'test_view'

    def setUp(self):
        self.username = 'test_user'
        self.password = 'test_password'

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

    def test_no_authorization(self):
        res = self.view_get()

        self.assertEqual(res.status_code, 401)
        self.assertIn('credentials were not provided', res.data['detail'])

    def test_wrong_auth_type(self):
        res = self.client.post(
            reverse('token_obtain_sliding'),
            data={
                User.USERNAME_FIELD: self.username,
                'password': self.password,
            },
        )

        token = res.data['token']
        self.authenticate_with_token('Wrong', token)

        res = self.view_get()

        self.assertEqual(res.status_code, 401)
        self.assertIn('credentials were not provided', res.data['detail'])

    def test_user_can_get_sliding_token_and_use_it(self):
        res = self.client.post(
            reverse('token_obtain_sliding'),
            data={
                User.USERNAME_FIELD: self.username,
                'password': self.password,
            },
        )

        token = res.data['token']
        self.authenticate_with_token(api_settings.AUTH_HEADER_TYPE, token)

        with override_api_settings(AUTH_TOKEN_CLASSES=('rest_framework_simplejwt.tokens.SlidingToken',)):
            res = self.view_get()

        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.data['foo'], 'bar')

    def test_user_can_get_access_and_refresh_tokens_and_use_them(self):
        res = self.client.post(
            reverse('token_obtain_pair'),
            data={
                User.USERNAME_FIELD: self.username,
                'password': self.password,
            },
        )

        access = res.data['access']
        refresh = res.data['refresh']

        self.authenticate_with_token(api_settings.AUTH_HEADER_TYPE, access)

        with override_api_settings(AUTH_TOKEN_CLASSES=('rest_framework_simplejwt.tokens.AccessToken',)):
            res = self.view_get()

        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.data['foo'], 'bar')

        res = self.client.post(
            reverse('token_refresh'),
            data={'refresh': refresh},
        )

        access = res.data['access']

        self.authenticate_with_token(api_settings.AUTH_HEADER_TYPE, access)

        with override_api_settings(AUTH_TOKEN_CLASSES=('rest_framework_simplejwt.tokens.AccessToken',)):
            res = self.view_get()

        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.data['foo'], 'bar')

@@ -34,16 +34,48 @@
 terals%0A%0A
+from datetime import timedelta%0A%0A
 from res
@@ -118,16 +118,16 @@
 reverse%0A
-
 from res
@@ -224,16 +224,72 @@
 ort User
+%0Afrom rest_framework_simplejwt.tokens import AccessToken
 %0A%0Afrom .
@@ -1237,32 +1237,32 @@
 atus_code, 401)%0A
-
         self.ass
@@ -1313,32 +1313,840 @@
 ata%5B'detail'%5D)%0A%0A
+    def test_expired_token(self):%0A        old_lifetime = AccessToken.lifetime%0A        AccessToken.lifetime = timedelta(seconds=0)%0A        try:%0A            res = self.client.post(%0A                reverse('token_obtain_pair'),%0A                data=%7B%0A                    User.USERNAME_FIELD: self.username,%0A                    'password': self.password,%0A                %7D,%0A            )%0A        finally:%0A            AccessToken.lifetime = old_lifetime%0A%0A        access = res.data%5B'access'%5D%0A        self.authenticate_with_token(api_settings.AUTH_HEADER_TYPE, access)%0A%0A        with override_api_settings(AUTH_TOKEN_CLASSES=('rest_framework_simplejwt.tokens.AccessToken',)):%0A            res = self.view_get()%0A%0A        self.assertEqual(res.status_code, 401)%0A        self.assertIn('has expired', res.data%5B'detail'%5D)%0A%0A
     def test_use

Add integration test for token expiration