Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix check for 'internal_failure' condition #387

Merged
merged 2 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions google/oauth2/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,21 @@ def _token_endpoint_request(request, token_uri, body):
while True:
response = request(method="POST", url=token_uri, headers=headers, body=body)
response_body = response.data.decode("utf-8")
response_data = json.loads(response_body)

if response.status == http_client.OK:
break
else:
error_desc = json.loads(response_body).get("error_description") or ""
if error_desc == "internal_failure" and retry < 1:
error_desc = response_data.get("error_description") or ""
error_code = response_data.get("error") or ""
if (
any(e == "internal_failure" for e in (error_code, error_desc))
and retry < 1
):
retry += 1
continue
_handle_error_response(response_body)

response_data = json.loads(response_body)

return response_data


Expand Down
16 changes: 11 additions & 5 deletions tests/oauth2/test__client.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,21 @@ def test__token_endpoint_request_error():

def test__token_endpoint_request_internal_failure_error():
request = make_request(
{"error": "internal_failure", "error_description": "internal_failure"},
status=http_client.BAD_REQUEST,
{"error_description": "internal_failure"}, status=http_client.BAD_REQUEST
)

with pytest.raises(exceptions.RefreshError):
_client._token_endpoint_request(
request,
"http://example.com",
{"error": "internal_failure", "error_description": "internal_failure"},
request, "http://example.com", {"error_description": "internal_failure"}
)

request = make_request(
{"error": "internal_failure"}, status=http_client.BAD_REQUEST
)

with pytest.raises(exceptions.RefreshError):
_client._token_endpoint_request(
request, "http://example.com", {"error": "internal_failure"}
)


Expand Down