From e115bae9b725215945fda7d50805fe3ba6e597f9 Mon Sep 17 00:00:00 2001 From: arithmetic1728 <58957152+arithmetic1728@users.noreply.github.com> Date: Wed, 6 May 2020 16:00:17 -0700 Subject: [PATCH] chore: support string type response.data for gcloud (#503) --- google/auth/impersonated_credentials.py | 8 ++++++-- tests/test_impersonated_credentials.py | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/google/auth/impersonated_credentials.py b/google/auth/impersonated_credentials.py index 1bb6b8268..c2351e7f7 100644 --- a/google/auth/impersonated_credentials.py +++ b/google/auth/impersonated_credentials.py @@ -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") diff --git a/tests/test_impersonated_credentials.py b/tests/test_impersonated_credentials.py index 31075ca84..19e2f3421 100644 --- a/tests/test_impersonated_credentials.py +++ b/tests/test_impersonated_credentials.py @@ -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) @@ -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" @@ -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)