From ad7f7b39e93cba229f9a571fa9ac458f2ec7b494 Mon Sep 17 00:00:00 2001 From: arithmetic1728 Date: Tue, 20 Jul 2021 21:49:27 -0700 Subject: [PATCH] fix: enable self signed jwt for grpc --- .../%sub/services/%service/client.py.j2 | 6 +++ .../%name_%version/%sub/test_%service.py.j2 | 48 +++++++++++++------ .../asset_v1/services/asset_service/client.py | 4 ++ .../unit/gapic/asset_v1/test_asset_service.py | 28 ++++++----- .../services/iam_credentials/client.py | 4 ++ .../credentials_v1/test_iam_credentials.py | 28 ++++++----- .../services/config_service_v2/client.py | 4 ++ .../services/logging_service_v2/client.py | 4 ++ .../services/metrics_service_v2/client.py | 4 ++ .../logging_v2/test_config_service_v2.py | 28 ++++++----- .../logging_v2/test_logging_service_v2.py | 28 ++++++----- .../logging_v2/test_metrics_service_v2.py | 28 ++++++----- .../redis_v1/services/cloud_redis/client.py | 4 ++ .../unit/gapic/redis_v1/test_cloud_redis.py | 28 ++++++----- 14 files changed, 160 insertions(+), 86 deletions(-) diff --git a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 index 5b6c2c03ae..9bae12313c 100644 --- a/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 +++ b/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2 @@ -304,6 +304,12 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta): client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + {% if "grpc" in opts.transport %} + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), + {% endif %} ) diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index cd6c6bb44d..61618036d4 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -113,19 +113,6 @@ def test_{{ service.client_name|snake_case }}_from_service_account_info(client_c {% endif %} -@pytest.mark.parametrize("client_class", [ - {{ service.client_name }}, - {% if 'grpc' in opts.transport %} - {{ service.async_client_name }}, - {% endif %} -]) -def test_{{ service.client_name|snake_case }}_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ {% if 'grpc' in opts.transport %} (transports.{{ service.grpc_transport_name }}, "grpc"), @@ -134,12 +121,17 @@ def test_{{ service.client_name|snake_case }}_service_account_always_use_jwt(cli (transports.{{ service.rest_transport_name }}, "rest"), {% endif %} ]) -def test_{{ service.client_name|snake_case }}_service_account_always_use_jwt_true(transport_class, transport_name): +def test_{{ service.client_name|snake_case }}_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ {{ service.client_name }}, @@ -216,6 +208,9 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -232,6 +227,9 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -248,6 +246,9 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -274,6 +275,9 @@ def test_{{ service.client_name|snake_case }}_client_options(client_class, trans client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -319,6 +323,9 @@ def test_{{ service.client_name|snake_case }}_mtls_env_auto(client_class, transp client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -344,6 +351,9 @@ def test_{{ service.client_name|snake_case }}_mtls_env_auto(client_class, transp client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) # Check the case client_cert_source and ADC client cert are not provided. @@ -360,6 +370,9 @@ def test_{{ service.client_name|snake_case }}_mtls_env_auto(client_class, transp client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) @@ -387,6 +400,9 @@ def test_{{ service.client_name|snake_case }}_client_options_scopes(client_class client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -413,6 +429,9 @@ def test_{{ service.client_name|snake_case }}_client_options_credentials_file(cl client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + {% if 'grpc' in opts.transport %} + always_use_jwt_access=True, + {% endif %} ) {% if 'grpc' in opts.transport %} @@ -431,6 +450,7 @@ def test_{{ service.client_name|snake_case }}_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) {% endif %} diff --git a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py index aa285d830e..bcc7059502 100644 --- a/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py +++ b/tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py @@ -344,6 +344,10 @@ def __init__(self, *, client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), ) def export_assets(self, diff --git a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py index 5d5e291df2..87c110ebcb 100644 --- a/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py +++ b/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py @@ -105,27 +105,21 @@ def test_asset_service_client_from_service_account_info(client_class): assert client.transport._host == 'cloudasset.googleapis.com:443' -@pytest.mark.parametrize("client_class", [ - AssetServiceClient, - AssetServiceAsyncClient, -]) -def test_asset_service_client_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ (transports.AssetServiceGrpcTransport, "grpc"), (transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio"), ]) -def test_asset_service_client_service_account_always_use_jwt_true(transport_class, transport_name): +def test_asset_service_client_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ AssetServiceClient, @@ -190,6 +184,7 @@ def test_asset_service_client_client_options(client_class, transport_class, tran client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -206,6 +201,7 @@ def test_asset_service_client_client_options(client_class, transport_class, tran client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -222,6 +218,7 @@ def test_asset_service_client_client_options(client_class, transport_class, tran client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -248,6 +245,7 @@ def test_asset_service_client_client_options(client_class, transport_class, tran client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -286,6 +284,7 @@ def test_asset_service_client_mtls_env_auto(client_class, transport_class, trans client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -311,6 +310,7 @@ def test_asset_service_client_mtls_env_auto(client_class, transport_class, trans client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case client_cert_source and ADC client cert are not provided. @@ -327,6 +327,7 @@ def test_asset_service_client_mtls_env_auto(client_class, transport_class, trans client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -350,6 +351,7 @@ def test_asset_service_client_client_options_scopes(client_class, transport_clas client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -372,6 +374,7 @@ def test_asset_service_client_client_options_credentials_file(client_class, tran client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -389,6 +392,7 @@ def test_asset_service_client_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) diff --git a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py index 8fe89f514f..4238eb763d 100644 --- a/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py +++ b/tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/client.py @@ -340,6 +340,10 @@ def __init__(self, *, client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), ) def generate_access_token(self, diff --git a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py index 1603f0e7e6..74dacb0923 100644 --- a/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py +++ b/tests/integration/goldens/credentials/tests/unit/gapic/credentials_v1/test_iam_credentials.py @@ -97,27 +97,21 @@ def test_iam_credentials_client_from_service_account_info(client_class): assert client.transport._host == 'iamcredentials.googleapis.com:443' -@pytest.mark.parametrize("client_class", [ - IAMCredentialsClient, - IAMCredentialsAsyncClient, -]) -def test_iam_credentials_client_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ (transports.IAMCredentialsGrpcTransport, "grpc"), (transports.IAMCredentialsGrpcAsyncIOTransport, "grpc_asyncio"), ]) -def test_iam_credentials_client_service_account_always_use_jwt_true(transport_class, transport_name): +def test_iam_credentials_client_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ IAMCredentialsClient, @@ -182,6 +176,7 @@ def test_iam_credentials_client_client_options(client_class, transport_class, tr client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -198,6 +193,7 @@ def test_iam_credentials_client_client_options(client_class, transport_class, tr client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -214,6 +210,7 @@ def test_iam_credentials_client_client_options(client_class, transport_class, tr client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -240,6 +237,7 @@ def test_iam_credentials_client_client_options(client_class, transport_class, tr client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -278,6 +276,7 @@ def test_iam_credentials_client_mtls_env_auto(client_class, transport_class, tra client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -303,6 +302,7 @@ def test_iam_credentials_client_mtls_env_auto(client_class, transport_class, tra client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case client_cert_source and ADC client cert are not provided. @@ -319,6 +319,7 @@ def test_iam_credentials_client_mtls_env_auto(client_class, transport_class, tra client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -342,6 +343,7 @@ def test_iam_credentials_client_client_options_scopes(client_class, transport_cl client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -364,6 +366,7 @@ def test_iam_credentials_client_client_options_credentials_file(client_class, tr client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -381,6 +384,7 @@ def test_iam_credentials_client_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py index 67658652a6..ad8f294462 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/client.py @@ -375,6 +375,10 @@ def __init__(self, *, client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), ) def list_buckets(self, diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py index 8c16313ba9..03b5f832f0 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/client.py @@ -331,6 +331,10 @@ def __init__(self, *, client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), ) def delete_log(self, diff --git a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py index d733b400b3..a4d4efcda7 100644 --- a/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py +++ b/tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/client.py @@ -332,6 +332,10 @@ def __init__(self, *, client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), ) def list_log_metrics(self, diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py index 0b47d5216e..979cbd3605 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_config_service_v2.py @@ -98,27 +98,21 @@ def test_config_service_v2_client_from_service_account_info(client_class): assert client.transport._host == 'logging.googleapis.com:443' -@pytest.mark.parametrize("client_class", [ - ConfigServiceV2Client, - ConfigServiceV2AsyncClient, -]) -def test_config_service_v2_client_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ (transports.ConfigServiceV2GrpcTransport, "grpc"), (transports.ConfigServiceV2GrpcAsyncIOTransport, "grpc_asyncio"), ]) -def test_config_service_v2_client_service_account_always_use_jwt_true(transport_class, transport_name): +def test_config_service_v2_client_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ ConfigServiceV2Client, @@ -183,6 +177,7 @@ def test_config_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -199,6 +194,7 @@ def test_config_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -215,6 +211,7 @@ def test_config_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -241,6 +238,7 @@ def test_config_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -279,6 +277,7 @@ def test_config_service_v2_client_mtls_env_auto(client_class, transport_class, t client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -304,6 +303,7 @@ def test_config_service_v2_client_mtls_env_auto(client_class, transport_class, t client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case client_cert_source and ADC client cert are not provided. @@ -320,6 +320,7 @@ def test_config_service_v2_client_mtls_env_auto(client_class, transport_class, t client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -343,6 +344,7 @@ def test_config_service_v2_client_client_options_scopes(client_class, transport_ client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -365,6 +367,7 @@ def test_config_service_v2_client_client_options_credentials_file(client_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -382,6 +385,7 @@ def test_config_service_v2_client_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py index 817eb6260f..b952814609 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_logging_service_v2.py @@ -104,27 +104,21 @@ def test_logging_service_v2_client_from_service_account_info(client_class): assert client.transport._host == 'logging.googleapis.com:443' -@pytest.mark.parametrize("client_class", [ - LoggingServiceV2Client, - LoggingServiceV2AsyncClient, -]) -def test_logging_service_v2_client_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ (transports.LoggingServiceV2GrpcTransport, "grpc"), (transports.LoggingServiceV2GrpcAsyncIOTransport, "grpc_asyncio"), ]) -def test_logging_service_v2_client_service_account_always_use_jwt_true(transport_class, transport_name): +def test_logging_service_v2_client_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ LoggingServiceV2Client, @@ -189,6 +183,7 @@ def test_logging_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -205,6 +200,7 @@ def test_logging_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -221,6 +217,7 @@ def test_logging_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -247,6 +244,7 @@ def test_logging_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -285,6 +283,7 @@ def test_logging_service_v2_client_mtls_env_auto(client_class, transport_class, client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -310,6 +309,7 @@ def test_logging_service_v2_client_mtls_env_auto(client_class, transport_class, client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case client_cert_source and ADC client cert are not provided. @@ -326,6 +326,7 @@ def test_logging_service_v2_client_mtls_env_auto(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -349,6 +350,7 @@ def test_logging_service_v2_client_client_options_scopes(client_class, transport client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -371,6 +373,7 @@ def test_logging_service_v2_client_client_options_credentials_file(client_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -388,6 +391,7 @@ def test_logging_service_v2_client_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) diff --git a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py index eb61169d73..5ce85b4284 100644 --- a/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py +++ b/tests/integration/goldens/logging/tests/unit/gapic/logging_v2/test_metrics_service_v2.py @@ -102,27 +102,21 @@ def test_metrics_service_v2_client_from_service_account_info(client_class): assert client.transport._host == 'logging.googleapis.com:443' -@pytest.mark.parametrize("client_class", [ - MetricsServiceV2Client, - MetricsServiceV2AsyncClient, -]) -def test_metrics_service_v2_client_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ (transports.MetricsServiceV2GrpcTransport, "grpc"), (transports.MetricsServiceV2GrpcAsyncIOTransport, "grpc_asyncio"), ]) -def test_metrics_service_v2_client_service_account_always_use_jwt_true(transport_class, transport_name): +def test_metrics_service_v2_client_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ MetricsServiceV2Client, @@ -187,6 +181,7 @@ def test_metrics_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -203,6 +198,7 @@ def test_metrics_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -219,6 +215,7 @@ def test_metrics_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -245,6 +242,7 @@ def test_metrics_service_v2_client_client_options(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -283,6 +281,7 @@ def test_metrics_service_v2_client_mtls_env_auto(client_class, transport_class, client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -308,6 +307,7 @@ def test_metrics_service_v2_client_mtls_env_auto(client_class, transport_class, client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case client_cert_source and ADC client cert are not provided. @@ -324,6 +324,7 @@ def test_metrics_service_v2_client_mtls_env_auto(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -347,6 +348,7 @@ def test_metrics_service_v2_client_client_options_scopes(client_class, transport client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -369,6 +371,7 @@ def test_metrics_service_v2_client_client_options_credentials_file(client_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -386,6 +389,7 @@ def test_metrics_service_v2_client_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) diff --git a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py index 87892f6c85..dca3739773 100644 --- a/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py +++ b/tests/integration/goldens/redis/google/cloud/redis_v1/services/cloud_redis/client.py @@ -355,6 +355,10 @@ def __init__(self, *, client_cert_source_for_mtls=client_cert_source_func, quota_project_id=client_options.quota_project_id, client_info=client_info, + always_use_jwt_access=( + Transport == type(self).get_transport_class("grpc") + or Transport == type(self).get_transport_class("grpc_asyncio") + ), ) def list_instances(self, diff --git a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py index d39ceb9cbf..a1c9cc2e64 100644 --- a/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py +++ b/tests/integration/goldens/redis/tests/unit/gapic/redis_v1/test_cloud_redis.py @@ -102,27 +102,21 @@ def test_cloud_redis_client_from_service_account_info(client_class): assert client.transport._host == 'redis.googleapis.com:443' -@pytest.mark.parametrize("client_class", [ - CloudRedisClient, - CloudRedisAsyncClient, -]) -def test_cloud_redis_client_service_account_always_use_jwt(client_class): - with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: - creds = service_account.Credentials(None, None, None) - client = client_class(credentials=creds) - use_jwt.assert_not_called() - - @pytest.mark.parametrize("transport_class,transport_name", [ (transports.CloudRedisGrpcTransport, "grpc"), (transports.CloudRedisGrpcAsyncIOTransport, "grpc_asyncio"), ]) -def test_cloud_redis_client_service_account_always_use_jwt_true(transport_class, transport_name): +def test_cloud_redis_client_service_account_always_use_jwt(transport_class, transport_name): with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: creds = service_account.Credentials(None, None, None) transport = transport_class(credentials=creds, always_use_jwt_access=True) use_jwt.assert_called_once_with(True) + with mock.patch.object(service_account.Credentials, 'with_always_use_jwt_access', create=True) as use_jwt: + creds = service_account.Credentials(None, None, None) + transport = transport_class(credentials=creds, always_use_jwt_access=False) + use_jwt.assert_not_called() + @pytest.mark.parametrize("client_class", [ CloudRedisClient, @@ -187,6 +181,7 @@ def test_cloud_redis_client_client_options(client_class, transport_class, transp client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -203,6 +198,7 @@ def test_cloud_redis_client_client_options(client_class, transport_class, transp client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is @@ -219,6 +215,7 @@ def test_cloud_redis_client_client_options(client_class, transport_class, transp client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has @@ -245,6 +242,7 @@ def test_cloud_redis_client_client_options(client_class, transport_class, transp client_cert_source_for_mtls=None, quota_project_id="octopus", client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name,use_client_cert_env", [ @@ -283,6 +281,7 @@ def test_cloud_redis_client_mtls_env_auto(client_class, transport_class, transpo client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case ADC client cert is provided. Whether client cert is used depends on @@ -308,6 +307,7 @@ def test_cloud_redis_client_mtls_env_auto(client_class, transport_class, transpo client_cert_source_for_mtls=expected_client_cert_source, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) # Check the case client_cert_source and ADC client cert are not provided. @@ -324,6 +324,7 @@ def test_cloud_redis_client_mtls_env_auto(client_class, transport_class, transpo client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -347,6 +348,7 @@ def test_cloud_redis_client_client_options_scopes(client_class, transport_class, client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @pytest.mark.parametrize("client_class,transport_class,transport_name", [ @@ -369,6 +371,7 @@ def test_cloud_redis_client_client_options_credentials_file(client_class, transp client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, ) @@ -386,6 +389,7 @@ def test_cloud_redis_client_client_options_from_dict(): client_cert_source_for_mtls=None, quota_project_id=None, client_info=transports.base.DEFAULT_CLIENT_INFO, + always_use_jwt_access=True, )