Skip to content

Commit

Permalink
tests: improve coverage when generating clients with transport=rest
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Oct 30, 2024
1 parent 1e6a14f commit a5a5680
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ class {{ service.name }}Transport(abc.ABC):
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
be used for service account credentials.
"""
{% if 'grpc' in opts.transport %}
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
if ':' not in host:
host += ':443'
{% endif %}
self._host = host

scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}

# Save the scopes.
self._scopes = scopes

{% if 'grpc' in opts.transport %}
if not hasattr(self, "_ignore_credentials"):
self._ignore_credentials: bool = False
{% endif %}

# If no credentials are provided, then determine the appropriate
# defaults.
Expand All @@ -117,7 +121,7 @@ class {{ service.name }}Transport(abc.ABC):
**scopes_kwargs,
quota_project_id=quota_project_id
)
elif credentials is None and not self._ignore_credentials:
elif credentials is None{% if 'grpc' in opts.transport %} and not self._ignore_credentials{% endif %}:
credentials, _ = google.auth.default(**scopes_kwargs, quota_project_id=quota_project_id)

# If the credentials are service account credentials, then always try to use self signed JWT.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ class {{ service.name }}Transport(abc.ABC):

# Save the scopes.
self._scopes = scopes
{% if 'grpc' in opts.transport %}
if not hasattr(self, "_ignore_credentials"):
self._ignore_credentials: bool = False
{% endif %}

# If no credentials are provided, then determine the appropriate
# defaults.
Expand All @@ -121,7 +123,7 @@ class {{ service.name }}Transport(abc.ABC):
**scopes_kwargs,
quota_project_id=quota_project_id
)
elif credentials is None and not self._ignore_credentials:
elif credentials is None{% if 'grpc' in opts.transport %} and not self._ignore_credentials{% endif %}:
credentials, _ = google.auth.default(**scopes_kwargs, quota_project_id=quota_project_id)
# Don't apply audience if the credentials file passed from user.
if hasattr(credentials, "with_gdch_audience"):
Expand All @@ -134,9 +136,11 @@ class {{ service.name }}Transport(abc.ABC):
# Save the credentials.
self._credentials = credentials

{% if 'grpc' in opts.transport %}
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
if ':' not in host:
host += ':443'
{% endif %}
self._host = host

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1160,30 +1160,6 @@ def test_{{ service.name|snake_case }}_auth_adc():
)


{% if 'grpc' in opts.transport %}
@pytest.mark.parametrize(
"transport_class",
[
transports.{{ service.name }}GrpcTransport,
transports.{{ service.name }}GrpcAsyncIOTransport,
],
)
def test_{{ service.name|snake_case }}_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=(
{%- for scope in service.oauth_scopes %}
'{{ scope }}',
{%- endfor %}),
quota_project_id="octopus",
)


{% if 'grpc' in opts.transport and 'rest' in opts.transport %}
@pytest.mark.parametrize(
"transport_class",
Expand Down Expand Up @@ -1224,6 +1200,30 @@ def test_{{ service.name|snake_case }}_transport_auth_gdch_credentials(transport
)


{% if 'grpc' in opts.transport %}
@pytest.mark.parametrize(
"transport_class",
[
transports.{{ service.name }}GrpcTransport,
transports.{{ service.name }}GrpcAsyncIOTransport,
],
)
def test_{{ service.name|snake_case }}_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=(
{%- for scope in service.oauth_scopes %}
'{{ scope }}',
{%- endfor %}),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class,grpc_helpers",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3670,26 +3670,6 @@ def test_iam_credentials_auth_adc():
)


@pytest.mark.parametrize(
"transport_class",
[
transports.IAMCredentialsGrpcTransport,
transports.IAMCredentialsGrpcAsyncIOTransport,
],
)
def test_iam_credentials_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class",
[
Expand All @@ -3713,6 +3693,26 @@ def test_iam_credentials_transport_auth_gdch_credentials(transport_class):
)


@pytest.mark.parametrize(
"transport_class",
[
transports.IAMCredentialsGrpcTransport,
transports.IAMCredentialsGrpcAsyncIOTransport,
],
)
def test_iam_credentials_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class,grpc_helpers",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14890,26 +14890,6 @@ def test_eventarc_auth_adc():
)


@pytest.mark.parametrize(
"transport_class",
[
transports.EventarcGrpcTransport,
transports.EventarcGrpcAsyncIOTransport,
],
)
def test_eventarc_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class",
[
Expand All @@ -14933,6 +14913,26 @@ def test_eventarc_transport_auth_gdch_credentials(transport_class):
)


@pytest.mark.parametrize(
"transport_class",
[
transports.EventarcGrpcTransport,
transports.EventarcGrpcAsyncIOTransport,
],
)
def test_eventarc_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class,grpc_helpers",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12669,26 +12669,6 @@ def test_config_service_v2_auth_adc():
)


@pytest.mark.parametrize(
"transport_class",
[
transports.ConfigServiceV2GrpcTransport,
transports.ConfigServiceV2GrpcAsyncIOTransport,
],
)
def test_config_service_v2_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-platform.read-only', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/logging.read',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class",
[
Expand All @@ -12711,6 +12691,26 @@ def test_config_service_v2_transport_auth_gdch_credentials(transport_class):
)


@pytest.mark.parametrize(
"transport_class",
[
transports.ConfigServiceV2GrpcTransport,
transports.ConfigServiceV2GrpcAsyncIOTransport,
],
)
def test_config_service_v2_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-platform.read-only', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/logging.read',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class,grpc_helpers",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3216,26 +3216,6 @@ def test_logging_service_v2_auth_adc():
)


@pytest.mark.parametrize(
"transport_class",
[
transports.LoggingServiceV2GrpcTransport,
transports.LoggingServiceV2GrpcAsyncIOTransport,
],
)
def test_logging_service_v2_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-platform.read-only', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/logging.read', 'https://www.googleapis.com/auth/logging.write',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class",
[
Expand All @@ -3258,6 +3238,26 @@ def test_logging_service_v2_transport_auth_gdch_credentials(transport_class):
)


@pytest.mark.parametrize(
"transport_class",
[
transports.LoggingServiceV2GrpcTransport,
transports.LoggingServiceV2GrpcAsyncIOTransport,
],
)
def test_logging_service_v2_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-platform.read-only', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/logging.read', 'https://www.googleapis.com/auth/logging.write',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class,grpc_helpers",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3020,26 +3020,6 @@ def test_metrics_service_v2_auth_adc():
)


@pytest.mark.parametrize(
"transport_class",
[
transports.MetricsServiceV2GrpcTransport,
transports.MetricsServiceV2GrpcAsyncIOTransport,
],
)
def test_metrics_service_v2_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-platform.read-only', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/logging.read', 'https://www.googleapis.com/auth/logging.write',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class",
[
Expand All @@ -3062,6 +3042,26 @@ def test_metrics_service_v2_transport_auth_gdch_credentials(transport_class):
)


@pytest.mark.parametrize(
"transport_class",
[
transports.MetricsServiceV2GrpcTransport,
transports.MetricsServiceV2GrpcAsyncIOTransport,
],
)
def test_metrics_service_v2_transport_auth_adc(transport_class):
# If credentials and host are not provided, the transport class should use
# ADC credentials.
with mock.patch.object(google.auth, 'default', autospec=True) as adc:
adc.return_value = (ga_credentials.AnonymousCredentials(), None)
transport_class(quota_project_id="octopus", scopes=["1", "2"])
adc.assert_called_once_with(
scopes=["1", "2"],
default_scopes=( 'https://www.googleapis.com/auth/cloud-platform', 'https://www.googleapis.com/auth/cloud-platform.read-only', 'https://www.googleapis.com/auth/logging.admin', 'https://www.googleapis.com/auth/logging.read', 'https://www.googleapis.com/auth/logging.write',),
quota_project_id="octopus",
)


@pytest.mark.parametrize(
"transport_class,grpc_helpers",
[
Expand Down
Loading

0 comments on commit a5a5680

Please sign in to comment.