Skip to content

Commit

Permalink
Ensure logs dont leak token
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin-b committed Jun 17, 2024
1 parent 0fb60e4 commit 81563e7
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions tests/test_json_token_file_cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import datetime
import logging
import pathlib

import pytest
import jwt
Expand All @@ -8,8 +10,8 @@


@pytest.fixture
def token_cache(request) -> requests_auth.JsonTokenFileCache:
_token_cache = requests_auth.JsonTokenFileCache(request.node.name + ".cache")
def token_cache(tmp_path) -> requests_auth.JsonTokenFileCache:
_token_cache = requests_auth.JsonTokenFileCache(tmp_path / "my_tokens.cache")
yield _token_cache
_token_cache.clear()

Expand All @@ -36,7 +38,7 @@ def test_add_bearer_tokens(token_cache):
assert token_cache.get_token("key2") == token2


def test_save_bearer_tokens(token_cache, request):
def test_save_bearer_tokens(token_cache, tmp_path):
expiry_in_1_hour = datetime.datetime.now(
datetime.timezone.utc
) + datetime.timedelta(hours=1)
Expand All @@ -49,12 +51,14 @@ def test_save_bearer_tokens(token_cache, request):
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")
same_cache = requests_auth.JsonTokenFileCache(tmp_path / "my_tokens.cache")
assert same_cache.get_token("key1") == token1
assert same_cache.get_token("key2") == token2


def test_save_bearer_token_exception_handling(token_cache, request, monkeypatch):
def test_save_bearer_token_exception_handling(
token_cache, tmp_path, monkeypatch, caplog
):
def failing_dump(*args):
raise Exception("Failure")

Expand All @@ -65,18 +69,53 @@ def failing_dump(*args):
) + datetime.timedelta(hours=1)
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret")

caplog.set_level(logging.DEBUG)

# Assert that the exception is not thrown
token_cache._add_bearer_token("key1", token1)

same_cache = requests_auth.JsonTokenFileCache(request.node.name + ".cache")
same_cache = requests_auth.JsonTokenFileCache(tmp_path / "my_tokens.cache")
with pytest.raises(requests_auth.AuthenticationFailed) as exception_info:
same_cache.get_token("key1")
assert str(exception_info.value) == "User was not authenticated."

assert caplog.messages == [
"Cannot save tokens.",
f'Inserting token expiring on {expiry_in_1_hour:%Y-%m-%d %H:%M:%S+00:00} with "key1" key.',
"Cannot load tokens.",
'Retrieving token with "key1" key.',
"Token cannot be found in cache.",
"User was not authenticated: key key1 cannot be found in [].",
]


def test_missing_token(token_cache):
def test_missing_token_on_empty_cache(token_cache, caplog):
caplog.set_level(logging.DEBUG)
with pytest.raises(requests_auth.AuthenticationFailed):
token_cache.get_token("key1")
assert caplog.messages == [
'Retrieving token with "key1" key.',
"No token loaded. Token cache does not exists.",
"Token cannot be found in cache.",
"User was not authenticated: key key1 cannot be found in [].",
]


def test_missing_token_on_non_empty_cache(token_cache, caplog):
expiry_in_1_hour = datetime.datetime.now(
datetime.timezone.utc
) + datetime.timedelta(hours=1)
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret")
token_cache._add_bearer_token("key0", token1)

caplog.set_level(logging.DEBUG)
with pytest.raises(requests_auth.AuthenticationFailed):
token_cache.get_token("key1")
assert caplog.messages == [
'Retrieving token with "key1" key.',
"Token cannot be found in cache.",
"User was not authenticated: key key1 cannot be found in ['key0'].",
]


def test_missing_token_function(token_cache):
Expand Down Expand Up @@ -105,3 +144,16 @@ def test_token_without_refresh_token(token_cache):
# try to retrieve it
retrieved_token = token_cache.get_token("key1")
assert token == retrieved_token


def test_unable_to_remove_cache(token_cache, tmp_path, monkeypatch, caplog):
def unlink_failure(*args):
raise PermissionError("You can create but can't delete")

monkeypatch.setattr(pathlib.Path, "unlink", unlink_failure)

caplog.set_level(logging.DEBUG)
# Assert that the exception is not thrown
token_cache.clear()

assert caplog.messages == ["Clearing token cache.", "Cannot remove tokens file."]

0 comments on commit 81563e7

Please sign in to comment.