@@ -203,6 +203,7 @@ def test_error_on_refresh_token_grant_without_refresh_token(self):
203
203
grant_type = "refresh_token" ,
204
204
)
205
205
206
+ @freezegun .freeze_time ("2022-01-01" )
206
207
def test_refresh_access_token (self , mocker ):
207
208
oauth = DeclarativeOauth2Authenticator (
208
209
token_refresh_endpoint = "{{ config['refresh_endpoint'] }}" ,
@@ -225,13 +226,15 @@ def test_refresh_access_token(self, mocker):
225
226
resp , "json" , return_value = {"access_token" : "access_token" , "expires_in" : 1000 }
226
227
)
227
228
mocker .patch .object (requests , "request" , side_effect = mock_request , autospec = True )
228
- token = oauth .refresh_access_token ()
229
+ access_token , token_expiry_date = oauth .refresh_access_token ()
229
230
230
- assert ("access_token" , 1000 ) == token
231
+ assert access_token == "access_token"
232
+ assert token_expiry_date == ab_datetime_now () + timedelta (seconds = 1000 )
231
233
232
234
filtered = filter_secrets ("access_token" )
233
235
assert filtered == "****"
234
236
237
+ @freezegun .freeze_time ("2022-01-01" )
235
238
def test_refresh_access_token_when_headers_provided (self , mocker ):
236
239
expected_headers = {
237
240
"Authorization" : "Bearer some_access_token" ,
@@ -256,9 +259,10 @@ def test_refresh_access_token_when_headers_provided(self, mocker):
256
259
mocked_request = mocker .patch .object (
257
260
requests , "request" , side_effect = mock_request , autospec = True
258
261
)
259
- token = oauth .refresh_access_token ()
262
+ access_token , token_expiry_date = oauth .refresh_access_token ()
260
263
261
- assert ("access_token" , 1000 ) == token
264
+ assert access_token == "access_token"
265
+ assert token_expiry_date == ab_datetime_now () + timedelta (seconds = 1000 )
262
266
263
267
assert mocked_request .call_args .kwargs ["headers" ] == expected_headers
264
268
@@ -314,6 +318,7 @@ def test_initialize_declarative_oauth_with_token_expiry_date_as_timestamp(
314
318
assert isinstance (oauth ._token_expiry_date , AirbyteDateTime )
315
319
assert oauth .get_token_expiry_date () == ab_datetime_parse (expected_date )
316
320
321
+ @freezegun .freeze_time ("2022-01-01" )
317
322
def test_given_no_access_token_but_expiry_in_the_future_when_refresh_token_then_fetch_access_token (
318
323
self ,
319
324
) -> None :
@@ -335,12 +340,65 @@ def test_given_no_access_token_but_expiry_in_the_future_when_refresh_token_then_
335
340
url = "https://refresh_endpoint.com/" ,
336
341
body = "grant_type=client&client_id=some_client_id&client_secret=some_client_secret&refresh_token=some_refresh_token" ,
337
342
),
338
- HttpResponse (body = json .dumps ({"access_token" : "new_access_token" })),
343
+ HttpResponse (
344
+ body = json .dumps ({"access_token" : "new_access_token" , "expires_in" : 1000 })
345
+ ),
339
346
)
340
347
oauth .get_access_token ()
341
348
342
349
assert oauth .access_token == "new_access_token"
343
- assert oauth ._token_expiry_date == expiry_date
350
+ assert oauth ._token_expiry_date == ab_datetime_now () + timedelta (seconds = 1000 )
351
+
352
+ @freezegun .freeze_time ("2022-01-01" )
353
+ @pytest .mark .parametrize (
354
+ "initial_expiry_date_delta, expected_new_expiry_date_delta, expected_access_token" ,
355
+ [
356
+ (timedelta (days = 1 ), timedelta (days = 1 ), "some_access_token" ),
357
+ (timedelta (days = - 1 ), timedelta (hours = 1 ), "new_access_token" ),
358
+ (None , timedelta (hours = 1 ), "new_access_token" ),
359
+ ],
360
+ ids = [
361
+ "initial_expiry_date_in_future" ,
362
+ "initial_expiry_date_in_past" ,
363
+ "no_initial_expiry_date" ,
364
+ ],
365
+ )
366
+ def test_no_expiry_date_provided_by_auth_server (
367
+ self ,
368
+ initial_expiry_date_delta ,
369
+ expected_new_expiry_date_delta ,
370
+ expected_access_token ,
371
+ ) -> None :
372
+ initial_expiry_date = (
373
+ ab_datetime_now ().add (initial_expiry_date_delta ).isoformat ()
374
+ if initial_expiry_date_delta
375
+ else None
376
+ )
377
+ expected_new_expiry_date = ab_datetime_now ().add (expected_new_expiry_date_delta )
378
+ oauth = DeclarativeOauth2Authenticator (
379
+ token_refresh_endpoint = "https://refresh_endpoint.com/" ,
380
+ client_id = "some_client_id" ,
381
+ client_secret = "some_client_secret" ,
382
+ token_expiry_date = initial_expiry_date ,
383
+ access_token_value = "some_access_token" ,
384
+ refresh_token = "some_refresh_token" ,
385
+ config = {},
386
+ parameters = {},
387
+ grant_type = "client" ,
388
+ )
389
+
390
+ with HttpMocker () as http_mocker :
391
+ http_mocker .post (
392
+ HttpRequest (
393
+ url = "https://refresh_endpoint.com/" ,
394
+ body = "grant_type=client&client_id=some_client_id&client_secret=some_client_secret&refresh_token=some_refresh_token" ,
395
+ ),
396
+ HttpResponse (body = json .dumps ({"access_token" : "new_access_token" })),
397
+ )
398
+ oauth .get_access_token ()
399
+
400
+ assert oauth .access_token == expected_access_token
401
+ assert oauth ._token_expiry_date == expected_new_expiry_date
344
402
345
403
@pytest .mark .parametrize (
346
404
"expires_in_response, token_expiry_date_format" ,
@@ -443,6 +501,7 @@ def test_set_token_expiry_date_no_format(self, mocker, expires_in_response, next
443
501
assert "access_token" == token
444
502
assert oauth .get_token_expiry_date () == ab_datetime_parse (next_day )
445
503
504
+ @freezegun .freeze_time ("2022-01-01" )
446
505
def test_profile_assertion (self , mocker ):
447
506
with HttpMocker () as http_mocker :
448
507
jwt = JwtAuthenticator (
@@ -477,7 +536,7 @@ def test_profile_assertion(self, mocker):
477
536
478
537
token = oauth .refresh_access_token ()
479
538
480
- assert ("access_token" , 1000 ) == token
539
+ assert ("access_token" , ab_datetime_now (). add ( timedelta ( seconds = 1000 )) ) == token
481
540
482
541
filtered = filter_secrets ("access_token" )
483
542
assert filtered == "****"
0 commit comments