Skip to content

Commit 6290c35

Browse files
committed
begin updating tests
1 parent c973e02 commit 6290c35

File tree

3 files changed

+142
-51
lines changed

3 files changed

+142
-51
lines changed

airbyte_cdk/sources/streams/http/requests_native_auth/abstract_oauth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def _find_and_get_value_from_response(
361361
"""
362362
if current_depth > max_depth:
363363
# this is needed to avoid an inf loop, possible with a very deep nesting observed.
364-
message = f"The maximum level of recursion is reached. Couldn't find the speficied `{key_name}` in the response."
364+
message = f"The maximum level of recursion is reached. Couldn't find the specified `{key_name}` in the response."
365365
raise ResponseKeysMaxRecurtionReached(
366366
internal_message=message, message=message, failure_type=FailureType.config_error
367367
)

unit_tests/sources/declarative/auth/test_oauth.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def test_error_on_refresh_token_grant_without_refresh_token(self):
203203
grant_type="refresh_token",
204204
)
205205

206+
@freezegun.freeze_time("2022-01-01")
206207
def test_refresh_access_token(self, mocker):
207208
oauth = DeclarativeOauth2Authenticator(
208209
token_refresh_endpoint="{{ config['refresh_endpoint'] }}",
@@ -225,13 +226,15 @@ def test_refresh_access_token(self, mocker):
225226
resp, "json", return_value={"access_token": "access_token", "expires_in": 1000}
226227
)
227228
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()
229230

230-
assert ("access_token", 1000) == token
231+
assert access_token == "access_token"
232+
assert token_expiry_date == ab_datetime_now() + timedelta(seconds=1000)
231233

232234
filtered = filter_secrets("access_token")
233235
assert filtered == "****"
234236

237+
@freezegun.freeze_time("2022-01-01")
235238
def test_refresh_access_token_when_headers_provided(self, mocker):
236239
expected_headers = {
237240
"Authorization": "Bearer some_access_token",
@@ -256,9 +259,10 @@ def test_refresh_access_token_when_headers_provided(self, mocker):
256259
mocked_request = mocker.patch.object(
257260
requests, "request", side_effect=mock_request, autospec=True
258261
)
259-
token = oauth.refresh_access_token()
262+
access_token, token_expiry_date = oauth.refresh_access_token()
260263

261-
assert ("access_token", 1000) == token
264+
assert access_token == "access_token"
265+
assert token_expiry_date == ab_datetime_now() + timedelta(seconds=1000)
262266

263267
assert mocked_request.call_args.kwargs["headers"] == expected_headers
264268

@@ -314,6 +318,7 @@ def test_initialize_declarative_oauth_with_token_expiry_date_as_timestamp(
314318
assert isinstance(oauth._token_expiry_date, AirbyteDateTime)
315319
assert oauth.get_token_expiry_date() == ab_datetime_parse(expected_date)
316320

321+
@freezegun.freeze_time("2022-01-01")
317322
def test_given_no_access_token_but_expiry_in_the_future_when_refresh_token_then_fetch_access_token(
318323
self,
319324
) -> None:
@@ -335,12 +340,55 @@ def test_given_no_access_token_but_expiry_in_the_future_when_refresh_token_then_
335340
url="https://refresh_endpoint.com/",
336341
body="grant_type=client&client_id=some_client_id&client_secret=some_client_secret&refresh_token=some_refresh_token",
337342
),
338-
HttpResponse(body=json.dumps({"access_token": "new_access_token"})),
343+
HttpResponse(body=json.dumps({"access_token": "new_access_token", "expires_in": 1000})),
339344
)
340345
oauth.get_access_token()
341346

342347
assert oauth.access_token == "new_access_token"
343-
assert oauth._token_expiry_date == expiry_date
348+
assert oauth._token_expiry_date == ab_datetime_now() + timedelta(seconds=1000)
349+
350+
@freezegun.freeze_time("2022-01-01")
351+
@pytest.mark.parametrize(
352+
"initial_expiry_date_delta, expected_new_expiry_date_delta, expected_access_token",
353+
[
354+
(timedelta(days=1), timedelta(days=1), "some_access_token"),
355+
(timedelta(days=-1), timedelta(hours=1), "new_access_token"),
356+
(None, timedelta(hours=1), "new_access_token"),
357+
],
358+
ids=["initial_expiry_date_in_future", "initial_expiry_date_in_past", "no_initial_expiry_date"],
359+
)
360+
def test_no_expiry_date_provided_by_auth_server(
361+
self,
362+
initial_expiry_date_delta,
363+
expected_new_expiry_date_delta,
364+
expected_access_token,
365+
) -> None:
366+
initial_expiry_date = ab_datetime_now().add(initial_expiry_date_delta).isoformat() if initial_expiry_date_delta else None
367+
expected_new_expiry_date = ab_datetime_now().add(expected_new_expiry_date_delta)
368+
oauth = DeclarativeOauth2Authenticator(
369+
token_refresh_endpoint="https://refresh_endpoint.com/",
370+
client_id="some_client_id",
371+
client_secret="some_client_secret",
372+
token_expiry_date=initial_expiry_date,
373+
access_token_value="some_access_token",
374+
refresh_token="some_refresh_token",
375+
config={},
376+
parameters={},
377+
grant_type="client",
378+
)
379+
380+
with HttpMocker() as http_mocker:
381+
http_mocker.post(
382+
HttpRequest(
383+
url="https://refresh_endpoint.com/",
384+
body="grant_type=client&client_id=some_client_id&client_secret=some_client_secret&refresh_token=some_refresh_token",
385+
),
386+
HttpResponse(body=json.dumps({"access_token": "new_access_token"})),
387+
)
388+
oauth.get_access_token()
389+
390+
assert oauth.access_token == expected_access_token
391+
assert oauth._token_expiry_date == expected_new_expiry_date
344392

345393
@pytest.mark.parametrize(
346394
"expires_in_response, token_expiry_date_format",
@@ -443,6 +491,7 @@ def test_set_token_expiry_date_no_format(self, mocker, expires_in_response, next
443491
assert "access_token" == token
444492
assert oauth.get_token_expiry_date() == ab_datetime_parse(next_day)
445493

494+
@freezegun.freeze_time("2022-01-01")
446495
def test_profile_assertion(self, mocker):
447496
with HttpMocker() as http_mocker:
448497
jwt = JwtAuthenticator(
@@ -477,7 +526,7 @@ def test_profile_assertion(self, mocker):
477526

478527
token = oauth.refresh_access_token()
479528

480-
assert ("access_token", 1000) == token
529+
assert ("access_token", ab_datetime_now().add(timedelta(seconds=1000))) == token
481530

482531
filtered = filter_secrets("access_token")
483532
assert filtered == "****"

0 commit comments

Comments
 (0)