1
1
import datetime
2
+ import logging
3
+ import pathlib
2
4
3
5
import pytest
4
6
import jwt
8
10
9
11
10
12
@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" )
13
15
yield _token_cache
14
16
_token_cache .clear ()
15
17
@@ -36,7 +38,7 @@ def test_add_bearer_tokens(token_cache):
36
38
assert token_cache .get_token ("key2" ) == token2
37
39
38
40
39
- def test_save_bearer_tokens (token_cache , request ):
41
+ def test_save_bearer_tokens (token_cache , tmp_path ):
40
42
expiry_in_1_hour = datetime .datetime .now (
41
43
datetime .timezone .utc
42
44
) + datetime .timedelta (hours = 1 )
@@ -49,12 +51,14 @@ def test_save_bearer_tokens(token_cache, request):
49
51
token2 = jwt .encode ({"exp" : expiry_in_2_hour }, "secret" )
50
52
token_cache ._add_bearer_token ("key2" , token2 )
51
53
52
- same_cache = requests_auth .JsonTokenFileCache (request . node . name + " .cache" )
54
+ same_cache = requests_auth .JsonTokenFileCache (tmp_path / "my_tokens .cache" )
53
55
assert same_cache .get_token ("key1" ) == token1
54
56
assert same_cache .get_token ("key2" ) == token2
55
57
56
58
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
+ ):
58
62
def failing_dump (* args ):
59
63
raise Exception ("Failure" )
60
64
@@ -65,18 +69,53 @@ def failing_dump(*args):
65
69
) + datetime .timedelta (hours = 1 )
66
70
token1 = jwt .encode ({"exp" : expiry_in_1_hour }, "secret" )
67
71
72
+ caplog .set_level (logging .DEBUG )
73
+
68
74
# Assert that the exception is not thrown
69
75
token_cache ._add_bearer_token ("key1" , token1 )
70
76
71
- same_cache = requests_auth .JsonTokenFileCache (request . node . name + " .cache" )
77
+ same_cache = requests_auth .JsonTokenFileCache (tmp_path / "my_tokens .cache" )
72
78
with pytest .raises (requests_auth .AuthenticationFailed ) as exception_info :
73
79
same_cache .get_token ("key1" )
74
80
assert str (exception_info .value ) == "User was not authenticated."
75
81
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
+
76
91
77
- def test_missing_token (token_cache ):
92
+ def test_missing_token_on_empty_cache (token_cache , caplog ):
93
+ caplog .set_level (logging .DEBUG )
78
94
with pytest .raises (requests_auth .AuthenticationFailed ):
79
95
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
+ ]
80
119
81
120
82
121
def test_missing_token_function (token_cache ):
@@ -105,3 +144,16 @@ def test_token_without_refresh_token(token_cache):
105
144
# try to retrieve it
106
145
retrieved_token = token_cache .get_token ("key1" )
107
146
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