Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 43 additions & 27 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,23 +957,31 @@ def test_mtls_with_provided_client_cert_unset_environment_variable(
with mock.patch.dict(
"os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env}
):
# Clear CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE so its fallback in
# google-auth (often "true" in Cloud SDK environments) does not bypass
# certificate_config.json auto-discovery.
with mock.patch.dict(
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert}
"os.environ",
{
"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert,
"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "",
},
):
with mock.patch("builtins.open", m):
with mock.patch.dict(
"os.environ",
{"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
):
plus = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
client_options={
"client_encrypted_cert_source": self.client_encrypted_cert_source
},
)
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)
with mock.patch("os.path.exists", return_value=True):
with mock.patch.dict(
"os.environ",
{"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
):
plus = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
client_options={
"client_encrypted_cert_source": self.client_encrypted_cert_source
},
)
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)
Comment on lines +971 to +984

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Mocking os.path.exists globally to always return True can cause unexpected side effects if other parts of the test or standard library rely on it to verify the existence of real files. A safer and more robust approach is to use a side_effect that only returns True for the specific mock configuration file (config_filename) and delegates to the original os.path.exists for all other paths.

                        original_exists = os.path.exists
                        with mock.patch(
                            "os.path.exists",
                            side_effect=lambda p: True if p == config_filename else original_exists(p),
                        ):
                            with mock.patch.dict(
                                "os.environ",
                                {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
                            ):
                                plus = build_from_document(
                                    discovery,
                                    credentials=self.MOCK_CREDENTIALS,
                                    client_options={
                                        "client_encrypted_cert_source": self.client_encrypted_cert_source
                                    },
                                )
                                self.assertIsNotNone(plus)
                                self.assertEqual(plus._baseUrl, base_url)


@parameterized.expand(
[
Expand Down Expand Up @@ -1105,22 +1113,30 @@ def test_mtls_with_default_client_cert_with_unset_environment_variable(
with mock.patch.dict(
"os.environ", {"GOOGLE_API_USE_MTLS_ENDPOINT": use_mtls_env}
):
# Clear CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE so its fallback in
# google-auth (often "true" in Cloud SDK environments) does not bypass
# certificate_config.json auto-discovery.
with mock.patch.dict(
"os.environ", {"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert}
"os.environ",
{
"GOOGLE_API_USE_CLIENT_CERTIFICATE": use_client_cert,
"CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "",
},
):
with mock.patch("builtins.open", m):
with mock.patch.dict(
"os.environ",
{"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
):
plus = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
adc_cert_path=self.ADC_CERT_PATH,
adc_key_path=self.ADC_KEY_PATH,
)
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)
with mock.patch("os.path.exists", return_value=True):
with mock.patch.dict(
"os.environ",
{"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
):
plus = build_from_document(
discovery,
credentials=self.MOCK_CREDENTIALS,
adc_cert_path=self.ADC_CERT_PATH,
adc_key_path=self.ADC_KEY_PATH,
)
self.assertIsNotNone(plus)
self.assertEqual(plus._baseUrl, base_url)
Comment on lines +1127 to +1139

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Mocking os.path.exists globally to always return True can cause unexpected side effects if other parts of the test or standard library rely on it to verify the existence of real files. A safer and more robust approach is to use a side_effect that only returns True for the specific mock configuration file (config_filename) and delegates to the original os.path.exists for all other paths.

                        original_exists = os.path.exists
                        with mock.patch(
                            "os.path.exists",
                            side_effect=lambda p: True if p == config_filename else original_exists(p),
                        ):
                            with mock.patch.dict(
                                "os.environ",
                                {"GOOGLE_API_CERTIFICATE_CONFIG": config_filename},
                            ):
                                plus = build_from_document(
                                    discovery,
                                    credentials=self.MOCK_CREDENTIALS,
                                    adc_cert_path=self.ADC_CERT_PATH,
                                    adc_key_path=self.ADC_KEY_PATH,
                                )
                                self.assertIsNotNone(plus)
                                self.assertEqual(plus._baseUrl, base_url)


@parameterized.expand(
[
Expand Down
Loading