Skip to content

Commit

Permalink
Update token cache test to pyjwt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-b committed Jan 11, 2022
1 parent 146afb0 commit 091c09e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tests/test_json_token_file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def token_cache(request):

def test_add_bearer_tokens(token_cache):
expiry_in_1_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret").decode("unicode_escape")
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret")
token_cache.add_bearer_token("key1", token1)

expiry_in_2_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=2)
token2 = jwt.encode({"exp": expiry_in_2_hour}, "secret").decode("unicode_escape")
token2 = jwt.encode({"exp": expiry_in_2_hour}, "secret")
token_cache.add_bearer_token("key2", token2)

# Assert that tokens can be retrieved properly even after other token were inserted
Expand All @@ -33,11 +33,11 @@ def test_add_bearer_tokens(token_cache):

def test_save_bearer_tokens(token_cache, request):
expiry_in_1_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret").decode("unicode_escape")
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret")
token_cache.add_bearer_token("key1", token1)

expiry_in_2_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=2)
token2 = jwt.encode({"exp": expiry_in_2_hour}, "secret").decode("unicode_escape")
token2 = jwt.encode({"exp": expiry_in_2_hour}, "secret")
token_cache.add_bearer_token("key2", token2)

same_cache = requests_auth.JsonTokenFileCache(request.node.name + ".cache")
Expand All @@ -52,7 +52,7 @@ def failing_dump(*args):
monkeypatch.setattr(requests_auth.oauth2_tokens.json, "dump", failing_dump)

expiry_in_1_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret").decode("unicode_escape")
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret")

# Assert that the exception is not thrown
token_cache.add_bearer_token("key1", token1)
Expand All @@ -70,7 +70,7 @@ def test_missing_token(token_cache):

def test_missing_token_function(token_cache):
expiry_in_1_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
token = jwt.encode({"exp": expiry_in_1_hour}, "secret").decode("unicode_escape")
token = jwt.encode({"exp": expiry_in_1_hour}, "secret")
retrieved_token = token_cache.get_token(
"key1", on_missing_token=lambda: ("key1", token)
)
Expand All @@ -80,7 +80,7 @@ def test_missing_token_function(token_cache):
def test_token_without_refresh_token(token_cache):
expiry_in_1_hour = datetime.datetime.utcnow() + datetime.timedelta(hours=1)
# add token without refresh token
token = jwt.encode({"exp": expiry_in_1_hour}, "secret").decode("unicode_escape")
token = jwt.encode({"exp": expiry_in_1_hour}, "secret")
token_cache.tokens["key1"] = (
token,
expiry_in_1_hour.replace(tzinfo=datetime.timezone.utc).timestamp(),
Expand Down

0 comments on commit 091c09e

Please sign in to comment.