@@ -138,8 +138,55 @@ def test_get_refresh_failure(monkeypatch, tmpdir):
138
138
assert tok == 'oldtok'
139
139
logger .warn .assert_called_with ('Failed to refresh access token "%s" (but it is still valid): %s' , 'mytok' , exc )
140
140
141
- tokens .TOKENS = {'mytok' : {'scopes' : ['myscope' ]}}
141
+ tokens .TOKENS = {'mytok' : {'scopes' : ['myscope' ], 'expires_at' : 0 }}
142
142
with pytest .raises (Exception ) as exc_info :
143
143
tok = tokens .get ('mytok' )
144
144
assert exc_info .value == exc
145
145
146
+
147
+ def test_get_refresh_failure_ignore_expiration_no_access_token (monkeypatch , tmpdir ):
148
+ tokens .configure (dir = str (tmpdir ), url = 'https://example.org' )
149
+
150
+ with open (os .path .join (str (tmpdir ), 'user.json' ), 'w' ) as fd :
151
+ json .dump ({'application_username' : 'app' , 'application_password' : 'pass' }, fd )
152
+
153
+ with open (os .path .join (str (tmpdir ), 'client.json' ), 'w' ) as fd :
154
+ json .dump ({'client_id' : 'cid' , 'client_secret' : 'sec' }, fd )
155
+
156
+ exc = Exception ('FAIL' )
157
+ response = MagicMock ()
158
+ response .raise_for_status .side_effect = exc
159
+ monkeypatch .setattr ('requests.post' , lambda url , ** kwargs : response )
160
+ # we never got any access token
161
+ tokens .TOKENS = {'mytok' : {'ignore_expiration' : True ,
162
+ 'scopes' : ['myscope' ],
163
+ # expired a long time ago..
164
+ 'expires_at' : 0 }}
165
+ with pytest .raises (Exception ) as exc_info :
166
+ tok = tokens .get ('mytok' )
167
+ assert exc_info .value == exc
168
+
169
+
170
+ def test_get_refresh_failure_ignore_expiration (monkeypatch , tmpdir ):
171
+ tokens .configure (dir = str (tmpdir ), url = 'https://example.org' )
172
+
173
+ with open (os .path .join (str (tmpdir ), 'user.json' ), 'w' ) as fd :
174
+ json .dump ({'application_username' : 'app' , 'application_password' : 'pass' }, fd )
175
+
176
+ with open (os .path .join (str (tmpdir ), 'client.json' ), 'w' ) as fd :
177
+ json .dump ({'client_id' : 'cid' , 'client_secret' : 'sec' }, fd )
178
+
179
+ exc = Exception ('FAIL' )
180
+ response = MagicMock ()
181
+ response .raise_for_status .side_effect = exc
182
+ monkeypatch .setattr ('requests.post' , lambda url , ** kwargs : response )
183
+ logger = MagicMock ()
184
+ monkeypatch .setattr ('tokens.logger' , logger )
185
+ tokens .TOKENS = {'mytok' : {'access_token' : 'expired-token' ,
186
+ 'ignore_expiration' : True ,
187
+ 'scopes' : ['myscope' ],
188
+ # expired a long time ago..
189
+ 'expires_at' : 0 }}
190
+ tok = tokens .get ('mytok' )
191
+ assert tok == 'expired-token'
192
+ logger .warn .assert_called_with ('Failed to refresh access token "%s" (ignoring expiration): %s' , 'mytok' , exc )
0 commit comments