-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cred info to auth error details
- Loading branch information
1 parent
11e3967
commit f77061b
Showing
17 changed files
with
801 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,8 @@ from grpc.experimental import aio | |
{% if "rest" in opts.transport %} | ||
from collections.abc import Iterable | ||
from google.protobuf import json_format | ||
import json | ||
{% endif %} | ||
import json | ||
import math | ||
import pytest | ||
from google.api_core import api_core_version | ||
|
@@ -89,6 +89,13 @@ from google.iam.v1 import policy_pb2 # type: ignore | |
{% endfilter %} | ||
{{ shared_macros.add_google_api_core_version_header_import(service.version) }} | ||
|
||
CRED_INFO_JSON = { | ||
"credential_source": "/path/to/file", | ||
"credential_type": "service account credentials", | ||
"principal": "[email protected]", | ||
} | ||
CRED_INFO_STRING = json.dumps(CRED_INFO_JSON) | ||
|
||
def client_cert_source_callback(): | ||
return b"cert bytes", b"key bytes" | ||
|
||
|
@@ -250,6 +257,44 @@ def test__get_universe_domain(): | |
{{ service.client_name }}._get_universe_domain("", None) | ||
assert str(excinfo.value) == "Universe Domain cannot be an empty string." | ||
|
||
@pytest.mark.parametrize("error_code,cred_info_json,show_cred_info", [ | ||
(401, CRED_INFO_JSON, True), | ||
(403, CRED_INFO_JSON, True), | ||
(404, CRED_INFO_JSON, True), | ||
(500, CRED_INFO_JSON, False), | ||
(401, None, False), | ||
(403, None, False), | ||
(404, None, False), | ||
(500, None, False) | ||
]) | ||
def test__add_cred_info_for_auth_errors(error_code, cred_info_json, show_cred_info): | ||
cred = mock.Mock(["get_cred_info"]) | ||
cred.get_cred_info = mock.Mock(return_value=cred_info_json) | ||
client = {{ service.client_name }}(credentials=cred) | ||
client._transport._credentials = cred | ||
|
||
error = core_exceptions.GoogleAPICallError("message", details=[]) | ||
error.code = error_code | ||
|
||
client._add_cred_info_for_auth_errors(error) | ||
if show_cred_info: | ||
assert error.details == [CRED_INFO_STRING] | ||
else: | ||
assert error.details == [] | ||
|
||
@pytest.mark.parametrize("error_code", [401,403,404,500]) | ||
def test__add_cred_info_for_auth_errors_no_get_cred_info(error_code): | ||
cred = mock.Mock([]) | ||
assert not hasattr(cred, "get_cred_info") | ||
client = {{ service.client_name }}(credentials=cred) | ||
client._transport._credentials = cred | ||
|
||
error = core_exceptions.GoogleAPICallError("message", details=[]) | ||
error.code = error_code | ||
|
||
client._add_cred_info_for_auth_errors(error) | ||
assert error.details == [] | ||
|
||
@pytest.mark.parametrize("client_class,transport_class,transport_name", [ | ||
{% if 'grpc' in opts.transport %} | ||
({{ service.client_name }}, transports.{{ service.grpc_transport_name }}, "grpc"), | ||
|
Oops, something went wrong.