Skip to content

Commit d16b37f

Browse files
feat: Add docstring, update version check and lint errors fix
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent 997188a commit d16b37f

File tree

1 file changed

+79
-88
lines changed

1 file changed

+79
-88
lines changed

tests/test_discovery.py

Lines changed: 79 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -62,46 +62,29 @@
6262
HAS_UNIVERSE = False
6363

6464
from googleapiclient import _helpers as util
65-
from googleapiclient.discovery import (
66-
DISCOVERY_URI,
67-
MEDIA_BODY_PARAMETER_DEFAULT_VALUE,
68-
MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE,
69-
STACK_QUERY_PARAMETER_DEFAULT_VALUE,
70-
STACK_QUERY_PARAMETERS,
71-
V1_DISCOVERY_URI,
72-
V2_DISCOVERY_URI,
73-
APICoreVersionError,
74-
ResourceMethodParameters,
75-
_fix_up_media_path_base_url,
76-
_fix_up_media_upload,
77-
_fix_up_method_description,
78-
_fix_up_parameters,
79-
_urljoin,
80-
build,
81-
build_from_document,
82-
key2param,
83-
)
65+
from googleapiclient.discovery import (DISCOVERY_URI,
66+
MEDIA_BODY_PARAMETER_DEFAULT_VALUE,
67+
MEDIA_MIME_TYPE_PARAMETER_DEFAULT_VALUE,
68+
STACK_QUERY_PARAMETER_DEFAULT_VALUE,
69+
STACK_QUERY_PARAMETERS,
70+
V1_DISCOVERY_URI, V2_DISCOVERY_URI,
71+
APICoreVersionError,
72+
ResourceMethodParameters,
73+
_fix_up_media_path_base_url,
74+
_fix_up_media_upload,
75+
_fix_up_method_description,
76+
_fix_up_parameters, _urljoin, build,
77+
build_from_document, key2param)
8478
from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE
8579
from googleapiclient.discovery_cache.base import Cache
86-
from googleapiclient.errors import (
87-
HttpError,
88-
InvalidJsonError,
89-
MediaUploadSizeError,
90-
ResumableUploadError,
91-
UnacceptableMimeTypeError,
92-
UnknownApiNameOrVersion,
93-
UnknownFileType,
94-
)
95-
from googleapiclient.http import (
96-
HttpMock,
97-
HttpMockSequence,
98-
MediaFileUpload,
99-
MediaIoBaseUpload,
100-
MediaUpload,
101-
MediaUploadProgress,
102-
build_http,
103-
tunnel_patch,
104-
)
80+
from googleapiclient.errors import (HttpError, InvalidJsonError,
81+
MediaUploadSizeError, ResumableUploadError,
82+
UnacceptableMimeTypeError,
83+
UnknownApiNameOrVersion, UnknownFileType)
84+
from googleapiclient.http import (HttpMock, HttpMockSequence, MediaFileUpload,
85+
MediaIoBaseUpload, MediaUpload,
86+
MediaUploadProgress, build_http,
87+
tunnel_patch)
10588
from googleapiclient.model import JsonModel
10689
from googleapiclient.schema import Schemas
10790

@@ -909,33 +892,36 @@ def test_mtls_with_provided_client_cert(
909892
def test_mtls_with_provided_client_cert_unset_environment_variable(
910893
self, use_mtls_env, use_client_cert, config_data, base_url
911894
):
912-
if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
913-
self.skipTest(
914-
"The should_use_client_cert function is not available in this "
915-
"version of google-auth."
916-
)
917-
discovery = read_datafile("plus.json")
918-
config_filename = "mock_certificate_config.json"
919-
config_file_content = json.dumps(config_data)
920-
m = mock.mock_open(read_data=config_file_content)
895+
"""Tests that mTLS is correctly handled when a client certificate is provided.
896+
897+
This test case verifies that when a client certificate is explicitly provided
898+
via `client_options` and GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the
899+
discovery document build process correctly configures the base URL for mTLS
900+
or regular endpoints based on the `GOOGLE_API_USE_MTLS_ENDPOINT` environment variable.
901+
"""
902+
if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
903+
discovery = read_datafile("plus.json")
904+
config_filename = "mock_certificate_config.json"
905+
config_file_content = json.dumps(config_data)
906+
m = mock.mock_open(read_data=config_file_content)
921907

922-
with mock.patch.dict(
923-
"os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env}
924-
):
925908
with mock.patch.dict(
926-
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert}
909+
"os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env}
927910
):
928-
with mock.patch("builtins.open", m):
929-
with mock.patch.dict("os.environ", {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}):
930-
plus = build_from_document(
931-
discovery,
932-
credentials=self.MOCK_CREDENTIALS,
933-
client_options={
934-
"client_encrypted_cert_source": self.client_encrypted_cert_source
935-
},
936-
)
937-
self.assertIsNotNone(plus)
938-
self.assertEqual(plus._baseUrl, base_url)
911+
with mock.patch.dict(
912+
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert}
913+
):
914+
with mock.patch("builtins.open", m):
915+
with mock.patch.dict("os.environ", {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}):
916+
plus = build_from_document(
917+
discovery,
918+
credentials=self.MOCK_CREDENTIALS,
919+
client_options={
920+
"client_encrypted_cert_source": self.client_encrypted_cert_source
921+
},
922+
)
923+
self.assertIsNotNone(plus)
924+
self.assertEqual(plus._baseUrl, base_url)
939925

940926
@parameterized.expand(
941927
[
@@ -1039,36 +1025,41 @@ def test_mtls_with_default_client_cert_with_unset_environment_variable(
10391025
default_client_encrypted_cert_source,
10401026
has_default_client_cert_source,
10411027
):
1042-
if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
1043-
self.skipTest(
1044-
"The should_use_client_cert function is not available in this "
1045-
"version of google-auth."
1028+
"""Tests mTLS handling when falling back to a default client certificate.
1029+
1030+
This test simulates the scenario where no client certificate is explicitly
1031+
provided, and the library successfully finds and uses a default client
1032+
certificate when GOOGLE_API_USE_CLIENT_CERTIFICATE is unset. It mocks the
1033+
default certificate discovery process and checks that the base URL is
1034+
correctly set for mTLS or regular endpoints depending on the
1035+
`GOOGLE_API_USE_MTLS_ENDPOINT` environment variable.
1036+
"""
1037+
if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
1038+
has_default_client_cert_source.return_value = True
1039+
default_client_encrypted_cert_source.return_value = (
1040+
self.client_encrypted_cert_source
10461041
)
1047-
has_default_client_cert_source.return_value = True
1048-
default_client_encrypted_cert_source.return_value = (
1049-
self.client_encrypted_cert_source
1050-
)
1051-
discovery = read_datafile("plus.json")
1052-
config_filename = "mock_certificate_config.json"
1053-
config_file_content = json.dumps(config_data)
1054-
m = mock.mock_open(read_data=config_file_content)
1042+
discovery = read_datafile("plus.json")
1043+
config_filename = "mock_certificate_config.json"
1044+
config_file_content = json.dumps(config_data)
1045+
m = mock.mock_open(read_data=config_file_content)
10551046

1056-
with mock.patch.dict(
1057-
"os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env}
1058-
):
10591047
with mock.patch.dict(
1060-
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert}
1048+
"os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env}
10611049
):
1062-
with mock.patch("builtins.open", m):
1063-
with mock.patch.dict("os.environ", {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}):
1064-
plus = build_from_document(
1065-
discovery,
1066-
credentials=self.MOCK_CREDENTIALS,
1067-
adc_cert_path=self.ADC_CERT_PATH,
1068-
adc_key_path=self.ADC_KEY_PATH,
1069-
)
1070-
self.assertIsNotNone(plus)
1071-
self.assertEqual(plus._baseUrl, base_url)
1050+
with mock.patch.dict(
1051+
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert}
1052+
):
1053+
with mock.patch("builtins.open", m):
1054+
with mock.patch.dict("os.environ", {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename}):
1055+
plus = build_from_document(
1056+
discovery,
1057+
credentials=self.MOCK_CREDENTIALS,
1058+
adc_cert_path=self.ADC_CERT_PATH,
1059+
adc_key_path=self.ADC_KEY_PATH,
1060+
)
1061+
self.assertIsNotNone(plus)
1062+
self.assertEqual(plus._baseUrl, base_url)
10721063

10731064
@parameterized.expand(
10741065
[

0 commit comments

Comments
 (0)