Skip to content

Commit

Permalink
Move token backend selection into separate method (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianlange committed Jun 20, 2021
1 parent cba4a96 commit 5997c1a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions rest_framework_simplejwt/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, token=None, verify=True):
# Set up token
if token is not None:
# An encoded token was provided
from .state import token_backend
token_backend = self.get_token_backend()

# Decode token
try:
Expand Down Expand Up @@ -77,9 +77,7 @@ def __str__(self):
"""
Signs and returns a token as a base64 encoded string.
"""
from .state import token_backend

return token_backend.encode(self.payload)
return self.get_token_backend().encode(self.payload)

def verify(self):
"""
Expand Down Expand Up @@ -167,6 +165,10 @@ def for_user(cls, user):

return token

def get_token_backend(self):
from .state import token_backend
return token_backend


class BlacklistMixin:
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/test_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from rest_framework_simplejwt.exceptions import TokenError
from rest_framework_simplejwt.settings import api_settings
from rest_framework_simplejwt.state import token_backend
from rest_framework_simplejwt.tokens import (
AccessToken, RefreshToken, SlidingToken, Token, UntypedToken,
)
Expand Down Expand Up @@ -301,6 +302,11 @@ def test_for_user(self):

self.assertEqual(token[api_settings.USER_ID_CLAIM], username)

def test_get_token_backend(self):
token = MyToken()

self.assertEqual(token.get_token_backend(), token_backend)


class TestSlidingToken(TestCase):
def test_init(self):
Expand Down

0 comments on commit 5997c1a

Please sign in to comment.