Skip to content

Commit c7d7b56

Browse files
committed
ruff format
1 parent 393dd6f commit c7d7b56

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _default_token_expiry_date(self) -> AirbyteDateTime:
153153
Returns the default token expiry date
154154
"""
155155
# 1 hour was chosen as a middle ground to avoid unnecessary frequent refreshes and token expiration
156-
default_token_expiry_duration_hours = 1 # 1 hour
156+
default_token_expiry_duration_hours = 1 # 1 hour
157157
return ab_datetime_now() + timedelta(hours=default_token_expiry_duration_hours)
158158

159159
def _wrap_refresh_token_exception(
@@ -315,7 +315,7 @@ def _extract_refresh_token(self, response_data: Mapping[str, Any]) -> Any:
315315
def _extract_token_expiry_date(self, response_data: Mapping[str, Any]) -> AirbyteDateTime:
316316
"""
317317
Extracts the token_expiry_date, like `expires_in` or `expires_at`, etc from the given response data.
318-
318+
319319
If the token_expiry_date is not found, it will return an existing token expiry date if set, or a default token expiry date.
320320
321321
Args:

unit_tests/sources/declarative/auth/test_oauth.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,15 @@ def test_given_no_access_token_but_expiry_in_the_future_when_refresh_token_then_
340340
url="https://refresh_endpoint.com/",
341341
body="grant_type=client&client_id=some_client_id&client_secret=some_client_secret&refresh_token=some_refresh_token",
342342
),
343-
HttpResponse(body=json.dumps({"access_token": "new_access_token", "expires_in": 1000})),
343+
HttpResponse(
344+
body=json.dumps({"access_token": "new_access_token", "expires_in": 1000})
345+
),
344346
)
345347
oauth.get_access_token()
346348

347349
assert oauth.access_token == "new_access_token"
348350
assert oauth._token_expiry_date == ab_datetime_now() + timedelta(seconds=1000)
349-
351+
350352
@freezegun.freeze_time("2022-01-01")
351353
@pytest.mark.parametrize(
352354
"initial_expiry_date_delta, expected_new_expiry_date_delta, expected_access_token",
@@ -355,15 +357,23 @@ def test_given_no_access_token_but_expiry_in_the_future_when_refresh_token_then_
355357
(timedelta(days=-1), timedelta(hours=1), "new_access_token"),
356358
(None, timedelta(hours=1), "new_access_token"),
357359
],
358-
ids=["initial_expiry_date_in_future", "initial_expiry_date_in_past", "no_initial_expiry_date"],
360+
ids=[
361+
"initial_expiry_date_in_future",
362+
"initial_expiry_date_in_past",
363+
"no_initial_expiry_date",
364+
],
359365
)
360366
def test_no_expiry_date_provided_by_auth_server(
361367
self,
362368
initial_expiry_date_delta,
363369
expected_new_expiry_date_delta,
364370
expected_access_token,
365371
) -> None:
366-
initial_expiry_date = ab_datetime_now().add(initial_expiry_date_delta).isoformat() if initial_expiry_date_delta else 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+
)
367377
expected_new_expiry_date = ab_datetime_now().add(expected_new_expiry_date_delta)
368378
oauth = DeclarativeOauth2Authenticator(
369379
token_refresh_endpoint="https://refresh_endpoint.com/",

unit_tests/sources/streams/http/requests_native_auth/test_requests_native_auth.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ def test_refresh_access_token(self, mocker):
252252
"scopes": ["no_override"],
253253
},
254254
)
255-
256-
oauth_with_expired_token= Oauth2Authenticator(
255+
256+
oauth_with_expired_token = Oauth2Authenticator(
257257
token_refresh_endpoint="https://refresh_endpoint.com",
258258
client_id="some_client_id",
259259
client_secret="some_client_secret",
@@ -266,7 +266,6 @@ def test_refresh_access_token(self, mocker):
266266
"scopes": ["no_override"],
267267
},
268268
)
269-
270269

271270
resp.status_code = 200
272271
mocker.patch.object(
@@ -288,7 +287,7 @@ def test_refresh_access_token(self, mocker):
288287
assert isinstance(expires_in, AirbyteDateTime)
289288
assert expires_in == ab_datetime_now().add(timedelta(seconds=2000))
290289
assert token == "access_token"
291-
290+
292291
# Test with expires_in as datetime(str)
293292
mocker.patch.object(
294293
resp,
@@ -298,24 +297,24 @@ def test_refresh_access_token(self, mocker):
298297
# This should raise a ValueError because the token_expiry_is_time_of_expiration is False by default
299298
with pytest.raises(ValueError):
300299
token, expires_in = oauth.refresh_access_token()
301-
302-
# Test with no expires_in
300+
301+
# Test with no expires_in
303302
mocker.patch.object(
304303
resp,
305304
"json",
306305
return_value={"access_token": "access_token"},
307306
)
308-
307+
309308
# Since the initialized token is not expired (now + 3 days), we don't expect the expiration date to be updated
310309
token, expires_in = oauth.refresh_access_token()
311-
310+
312311
assert isinstance(expires_in, AirbyteDateTime)
313312
assert expires_in == ab_datetime_now().add(timedelta(days=3))
314313
assert token == "access_token"
315-
314+
316315
# Since the initialized token is expired (now - 3 days), we expect the expiration date to be updated to the default value (now + 1 hour)
317316
token, expires_in = oauth_with_expired_token.refresh_access_token()
318-
317+
319318
assert isinstance(expires_in, AirbyteDateTime)
320319
assert expires_in == ab_datetime_now().add(timedelta(hours=1))
321320
assert token == "access_token"
@@ -356,7 +355,7 @@ def test_refresh_access_token(self, mocker):
356355
},
357356
)
358357
token, expires_in = oauth.refresh_access_token()
359-
358+
360359
assert isinstance(expires_in, AirbyteDateTime)
361360
assert expires_in == ab_datetime_now().add(timedelta(seconds=2002))
362361
assert token == "access_token_deeply_nested"
@@ -579,6 +578,7 @@ def test_refresh_access_token_wrapped(
579578
assert exc_info.value.message == error_message
580579
assert exc_info.value.failure_type == FailureType.config_error
581580

581+
582582
@freezegun.freeze_time("2022-12-31")
583583
class TestSingleUseRefreshTokenOauth2Authenticator:
584584
@pytest.fixture
@@ -636,14 +636,20 @@ def test_given_no_message_repository_get_access_token(
636636
token_expiry_date_format=expiry_date_format,
637637
token_expiry_is_time_of_expiration=bool(expiry_date_format),
638638
)
639-
639+
640640
# Mock the response from the refresh token endpoint
641641
resp.status_code = 200
642642
mocker.patch.object(
643-
resp, "json", return_value={"access_token": "new_access_token", "expires_in": expires_in_value, "refresh_token": "new_refresh_token"}
643+
resp,
644+
"json",
645+
return_value={
646+
authenticator.get_access_token_name(): "new_access_token",
647+
authenticator.get_expires_in_name(): expires_in_value,
648+
authenticator.get_refresh_token_name(): "new_refresh_token",
649+
},
644650
)
645651
mocker.patch.object(requests, "request", side_effect=mock_request, autospec=True)
646-
652+
647653
authenticator.token_has_expired = mocker.Mock(return_value=True)
648654
access_token = authenticator.get_access_token()
649655
captured = capsys.readouterr()
@@ -678,10 +684,16 @@ def test_given_message_repository_when_get_access_token_then_emit_message(
678684
# Mock the response from the refresh token endpoint
679685
resp.status_code = 200
680686
mocker.patch.object(
681-
resp, "json", return_value={"access_token": "new_access_token", "expires_in": "2023-04-04", "refresh_token": "new_refresh_token"}
687+
resp,
688+
"json",
689+
return_value={
690+
authenticator.get_access_token_name(): "new_access_token",
691+
authenticator.get_expires_in_name(): "2023-04-04",
692+
authenticator.get_refresh_token_name(): "new_refresh_token",
693+
},
682694
)
683695
mocker.patch.object(requests, "request", side_effect=mock_request, autospec=True)
684-
696+
685697
authenticator.token_has_expired = mocker.Mock(return_value=True)
686698

687699
authenticator.get_access_token()
@@ -746,16 +758,17 @@ def test_refresh_access_token(self, mocker, connector_config):
746758
client_id=connector_config["credentials"]["client_id"],
747759
client_secret=connector_config["credentials"]["client_secret"],
748760
)
749-
761+
750762
# Mock the response from the refresh token endpoint
751763
resp.status_code = 200
752764
mocker.patch.object(
753-
resp, "json",
765+
resp,
766+
"json",
754767
return_value={
755768
authenticator.get_access_token_name(): "new_access_token",
756769
authenticator.get_expires_in_name(): "42",
757770
authenticator.get_refresh_token_name(): "new_refresh_token",
758-
}
771+
},
759772
)
760773
mocker.patch.object(requests, "request", side_effect=mock_request, autospec=True)
761774

0 commit comments

Comments
 (0)