Skip to content

Commit

Permalink
support reading "expires_in" when the API passes the value as string (a…
Browse files Browse the repository at this point in the history
…irbytehq#23921)

Co-authored-by: Sherif A. Nada <snadalive@gmail.com>
  • Loading branch information
2 people authored and adriennevermorel committed Mar 17, 2023
1 parent 73c51bc commit 2c903ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def refresh_access_token(self) -> Tuple[str, int]:
)
response.raise_for_status()
response_json = response.json()
return response_json["access_token"], response_json["expires_in"]
return response_json["access_token"], int(response_json["expires_in"])
except requests.exceptions.RequestException as e:
if e.response.status_code == 429 or e.response.status_code >= 500:
raise DefaultBackoffException(request=e.response.request, response=e.response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def test_refresh_access_token(self, requests_mock):
refresh_access_token_headers=TestOauth2Authenticator.refresh_access_token_headers,
)

token = oauth.refresh_access_token()
token, expires_in = oauth.refresh_access_token()

assert ("token", 10) == token
assert isinstance(expires_in, int)
assert ("token", 10) == (token, expires_in)
for header in self.refresh_access_token_headers:
assert header in mock_refresh_token_call.last_request.headers
assert self.refresh_access_token_headers[header] == mock_refresh_token_call.last_request.headers[header]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ def test_refresh_access_token(self, mocker):
resp.status_code = 200
mocker.patch.object(resp, "json", return_value={"access_token": "access_token", "expires_in": 1000})
mocker.patch.object(requests, "request", side_effect=mock_request, autospec=True)
token = oauth.refresh_access_token()
token, expires_in = oauth.refresh_access_token()

assert ("access_token", 1000) == token
assert isinstance(expires_in, int)
assert ("access_token", 1000) == (token, expires_in)

# Test with expires_in as str
mocker.patch.object(resp, "json", return_value={"access_token": "access_token", "expires_in": "2000"})
token = oauth.refresh_access_token()
token, expires_in = oauth.refresh_access_token()

assert ("access_token", 2000) == token
assert isinstance(expires_in, int)
assert ("access_token", 2000) == (token, expires_in)

@pytest.mark.parametrize("error_code", (429, 500, 502, 504))
def test_refresh_access_token_retry(self, error_code, requests_mock):
Expand All @@ -184,6 +186,7 @@ def test_refresh_access_token_retry(self, error_code, requests_mock):
]
)
token, expires_in = oauth.refresh_access_token()
assert isinstance(expires_in, int)
assert (token, expires_in) == ("token", 10)
assert requests_mock.call_count == 3

Expand Down

0 comments on commit 2c903ec

Please sign in to comment.