Skip to content

Commit

Permalink
fix: fix error in sign_bytes (#905)
Browse files Browse the repository at this point in the history
* fix: fix error in sign_bytes

* fix test
  • Loading branch information
arithmetic1728 committed Nov 1, 2021
1 parent bd0ccc5 commit ef31284
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -5,6 +5,7 @@ branch = True
omit =
*/samples/*
*/conftest.py
*/google-cloud-sdk/lib/*
exclude_lines =
# Re-enable the standard pragma
pragma: NO COVER
Expand Down
5 changes: 5 additions & 0 deletions google/auth/impersonated_credentials.py
Expand Up @@ -290,6 +290,11 @@ def sign_bytes(self, message):
url=iam_sign_endpoint, headers=headers, json=body
)

if response.status_code != http_client.OK:
raise exceptions.TransportError(
"Error calling sign_bytes: {}".format(response.json())
)

return base64.b64decode(response.json()["signedBlob"])

@property
Expand Down
13 changes: 13 additions & 0 deletions tests/test_impersonated_credentials.py
Expand Up @@ -345,6 +345,19 @@ def test_sign_bytes(self, mock_donor_credentials, mock_authorizedsession_sign):
signature = credentials.sign_bytes(b"signed bytes")
assert signature == b"signature"

def test_sign_bytes_failure(self):
credentials = self.make_credentials(lifetime=None)

with mock.patch(
"google.auth.transport.requests.AuthorizedSession.request", autospec=True
) as auth_session:
data = {"error": {"code": 403, "message": "unauthorized"}}
auth_session.return_value = MockResponse(data, http_client.FORBIDDEN)

with pytest.raises(exceptions.TransportError) as excinfo:
credentials.sign_bytes(b"foo")
assert excinfo.match("'code': 403")

def test_with_quota_project(self):
credentials = self.make_credentials()

Expand Down

0 comments on commit ef31284

Please sign in to comment.