Skip to content

Commit 81563e7

Browse files
committed
Ensure logs dont leak token
1 parent 0fb60e4 commit 81563e7

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

tests/test_json_token_file_cache.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import datetime
2+
import logging
3+
import pathlib
24

35
import pytest
46
import jwt
@@ -8,8 +10,8 @@
810

911

1012
@pytest.fixture
11-
def token_cache(request) -> requests_auth.JsonTokenFileCache:
12-
_token_cache = requests_auth.JsonTokenFileCache(request.node.name + ".cache")
13+
def token_cache(tmp_path) -> requests_auth.JsonTokenFileCache:
14+
_token_cache = requests_auth.JsonTokenFileCache(tmp_path / "my_tokens.cache")
1315
yield _token_cache
1416
_token_cache.clear()
1517

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

3840

39-
def test_save_bearer_tokens(token_cache, request):
41+
def test_save_bearer_tokens(token_cache, tmp_path):
4042
expiry_in_1_hour = datetime.datetime.now(
4143
datetime.timezone.utc
4244
) + datetime.timedelta(hours=1)
@@ -49,12 +51,14 @@ def test_save_bearer_tokens(token_cache, request):
4951
token2 = jwt.encode({"exp": expiry_in_2_hour}, "secret")
5052
token_cache._add_bearer_token("key2", token2)
5153

52-
same_cache = requests_auth.JsonTokenFileCache(request.node.name + ".cache")
54+
same_cache = requests_auth.JsonTokenFileCache(tmp_path / "my_tokens.cache")
5355
assert same_cache.get_token("key1") == token1
5456
assert same_cache.get_token("key2") == token2
5557

5658

57-
def test_save_bearer_token_exception_handling(token_cache, request, monkeypatch):
59+
def test_save_bearer_token_exception_handling(
60+
token_cache, tmp_path, monkeypatch, caplog
61+
):
5862
def failing_dump(*args):
5963
raise Exception("Failure")
6064

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

72+
caplog.set_level(logging.DEBUG)
73+
6874
# Assert that the exception is not thrown
6975
token_cache._add_bearer_token("key1", token1)
7076

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

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

77-
def test_missing_token(token_cache):
92+
def test_missing_token_on_empty_cache(token_cache, caplog):
93+
caplog.set_level(logging.DEBUG)
7894
with pytest.raises(requests_auth.AuthenticationFailed):
7995
token_cache.get_token("key1")
96+
assert caplog.messages == [
97+
'Retrieving token with "key1" key.',
98+
"No token loaded. Token cache does not exists.",
99+
"Token cannot be found in cache.",
100+
"User was not authenticated: key key1 cannot be found in [].",
101+
]
102+
103+
104+
def test_missing_token_on_non_empty_cache(token_cache, caplog):
105+
expiry_in_1_hour = datetime.datetime.now(
106+
datetime.timezone.utc
107+
) + datetime.timedelta(hours=1)
108+
token1 = jwt.encode({"exp": expiry_in_1_hour}, "secret")
109+
token_cache._add_bearer_token("key0", token1)
110+
111+
caplog.set_level(logging.DEBUG)
112+
with pytest.raises(requests_auth.AuthenticationFailed):
113+
token_cache.get_token("key1")
114+
assert caplog.messages == [
115+
'Retrieving token with "key1" key.',
116+
"Token cannot be found in cache.",
117+
"User was not authenticated: key key1 cannot be found in ['key0'].",
118+
]
80119

81120

82121
def test_missing_token_function(token_cache):
@@ -105,3 +144,16 @@ def test_token_without_refresh_token(token_cache):
105144
# try to retrieve it
106145
retrieved_token = token_cache.get_token("key1")
107146
assert token == retrieved_token
147+
148+
149+
def test_unable_to_remove_cache(token_cache, tmp_path, monkeypatch, caplog):
150+
def unlink_failure(*args):
151+
raise PermissionError("You can create but can't delete")
152+
153+
monkeypatch.setattr(pathlib.Path, "unlink", unlink_failure)
154+
155+
caplog.set_level(logging.DEBUG)
156+
# Assert that the exception is not thrown
157+
token_cache.clear()
158+
159+
assert caplog.messages == ["Clearing token cache.", "Cannot remove tokens file."]

0 commit comments

Comments
 (0)