Skip to content

Commit

Permalink
chore: support string type response.data for gcloud (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 authored May 6, 2020
1 parent 866d926 commit e115bae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 6 additions & 2 deletions google/auth/impersonated_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ def _make_iam_token_request(request, principal, headers, body):

response = request(url=iam_endpoint, method="POST", headers=headers, body=body)

response_body = response.data.decode("utf-8")
response_body = (
response.data.decode("utf-8")
if hasattr(response.data, "decode")
else response.data
)

if response.status != http_client.OK:
exceptions.RefreshError(_REFRESH_ERROR, response_body)

try:
token_response = json.loads(response.data.decode("utf-8"))
token_response = json.loads(response_body)
token = token_response["accessToken"]
expiry = datetime.strptime(token_response["expireTime"], "%Y-%m-%dT%H:%M:%SZ")

Expand Down
18 changes: 14 additions & 4 deletions tests/test_impersonated_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,17 @@ def test_default_state(self):
assert not credentials.valid
assert credentials.expired

def make_request(self, data, status=http_client.OK, headers=None, side_effect=None):
def make_request(
self,
data,
status=http_client.OK,
headers=None,
side_effect=None,
use_data_bytes=True,
):
response = mock.create_autospec(transport.Response, instance=False)
response.status = status
response.data = _helpers.to_bytes(data)
response.data = _helpers.to_bytes(data) if use_data_bytes else data
response.headers = headers or {}

request = mock.create_autospec(transport.Request, instance=False)
Expand All @@ -144,7 +151,8 @@ def make_request(self, data, status=http_client.OK, headers=None, side_effect=No

return request

def test_refresh_success(self, mock_donor_credentials):
@pytest.mark.parametrize("use_data_bytes", [True, False])
def test_refresh_success(self, use_data_bytes, mock_donor_credentials):
credentials = self.make_credentials(lifetime=None)
token = "token"

Expand All @@ -154,7 +162,9 @@ def test_refresh_success(self, mock_donor_credentials):
response_body = {"accessToken": token, "expireTime": expire_time}

request = self.make_request(
data=json.dumps(response_body), status=http_client.OK
data=json.dumps(response_body),
status=http_client.OK,
use_data_bytes=use_data_bytes,
)

credentials.refresh(request)
Expand Down

0 comments on commit e115bae

Please # to comment.