-
Notifications
You must be signed in to change notification settings - Fork 349
Refactor tests: Consolidate env var tests into test__mtls_helper.py #1952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |||||||||||||||||||||||||||
| from OpenSSL import crypto | ||||||||||||||||||||||||||||
| import pytest # type: ignore | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from google.auth import environment_vars | ||||||||||||||||||||||||||||
| from google.auth import exceptions | ||||||||||||||||||||||||||||
| from google.auth.transport import _mtls_helper | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -681,6 +682,31 @@ def test_env_variable_file_does_not_exist(self, mock_path_exists): | |||||||||||||||||||||||||||
| returned_path = _mtls_helper._get_cert_config_path() | ||||||||||||||||||||||||||||
| assert returned_path is None | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_cert_config_path_precedence(self): | ||||||||||||||||||||||||||||
| # GOOGLE_API_CERTIFICATE_CONFIG takes precedence | ||||||||||||||||||||||||||||
| google_path = "/path/to/google/config" | ||||||||||||||||||||||||||||
| cloudsdk_path = "/path/to/cloudsdk/config" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.GOOGLE_API_CERTIFICATE_CONFIG: google_path, | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| with mock.patch("os.path.exists", return_value=True): | ||||||||||||||||||||||||||||
| assert _mtls_helper._get_cert_config_path() == google_path | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_cert_config_path_fallback(self): | ||||||||||||||||||||||||||||
| # Fallback to CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH if GOOGLE_API_CERTIFICATE_CONFIG is unset | ||||||||||||||||||||||||||||
| cloudsdk_path = "/path/to/cloudsdk/config" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| if environment_vars.GOOGLE_API_CERTIFICATE_CONFIG in os.environ: | ||||||||||||||||||||||||||||
| del os.environ[environment_vars.GOOGLE_API_CERTIFICATE_CONFIG] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with mock.patch("os.path.exists", return_value=True): | ||||||||||||||||||||||||||||
| assert _mtls_helper._get_cert_config_path() == cloudsdk_path | ||||||||||||||||||||||||||||
|
Comment on lines
+701
to
+708
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To simplify the test setup and avoid modifying
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @mock.patch.dict( | ||||||||||||||||||||||||||||
| os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": "path/to/config/file"} | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
@@ -756,6 +782,22 @@ def test_env_var_explicit_false(self): | |||||||||||||||||||||||||||
| def test_env_var_explicit_garbage(self): | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is False | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_env_var_explicit_empty_string_prevents_fallback(self): | ||||||||||||||||||||||||||||
| # GOOGLE_API_USE_CLIENT_CERTIFICATE is present but empty. | ||||||||||||||||||||||||||||
| # It is falsy, but it SHOULD NOT fallback to CLOUDSDK var because | ||||||||||||||||||||||||||||
| # GOOGLE_API_... is explicitly set (though empty). | ||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "", | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "true" | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| # Current implementation: | ||||||||||||||||||||||||||||
| # use_client_cert = "" -> if use_client_cert: False. | ||||||||||||||||||||||||||||
| # It checks if value is None to decide fallback. | ||||||||||||||||||||||||||||
| # value is "", so it is not None. | ||||||||||||||||||||||||||||
| # Fallback is skipped. | ||||||||||||||||||||||||||||
| # Returns False. | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is False | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @mock.patch("builtins.open", autospec=True) | ||||||||||||||||||||||||||||
| @mock.patch.dict( | ||||||||||||||||||||||||||||
| os.environ, | ||||||||||||||||||||||||||||
|
|
@@ -811,6 +853,58 @@ def test_config_file_not_found(self, mock_file): | |||||||||||||||||||||||||||
| def test_no_env_vars_set(self): | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is False | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_use_client_cert_precedence(self): | ||||||||||||||||||||||||||||
| # GOOGLE_API_USE_CLIENT_CERTIFICATE takes precedence | ||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "true", | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "false" | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is True | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE: "false", | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "true" | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is False | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def test_use_client_cert_fallback(self): | ||||||||||||||||||||||||||||
| # Fallback to CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE if GOOGLE_API_USE_CLIENT_CERTIFICATE is unset | ||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "true" | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| # Ensure GOOGLE_API_USE_CLIENT_CERTIFICATE is not set | ||||||||||||||||||||||||||||
| if environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE in os.environ: | ||||||||||||||||||||||||||||
| del os.environ[environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE] | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is True | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "false" | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| if environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE in os.environ: | ||||||||||||||||||||||||||||
| del os.environ[environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE] | ||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is False | ||||||||||||||||||||||||||||
|
Comment on lines
+870
to
+885
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make the test setup cleaner and more reliable, you can use def test_use_client_cert_fallback(self):
# Fallback to CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE if GOOGLE_API_USE_CLIENT_CERTIFICATE is unset
with mock.patch.dict(os.environ, {
environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "true"
}, clear=True):
assert _mtls_helper.check_use_client_cert() is True
with mock.patch.dict(os.environ, {
environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE: "false"
}, clear=True):
assert _mtls_helper.check_use_client_cert() is False |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @mock.patch("builtins.open", autospec=True) | ||||||||||||||||||||||||||||
| def test_check_use_client_cert_config_fallback(self, mock_file): | ||||||||||||||||||||||||||||
| # Test fallback for config file when determining if client cert should be used | ||||||||||||||||||||||||||||
| cloudsdk_path = "/path/to/cloudsdk/config" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| mock_file.side_effect = mock.mock_open( | ||||||||||||||||||||||||||||
| read_data='{"cert_configs": {"workload": "exists"}}' | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| with mock.patch.dict(os.environ, { | ||||||||||||||||||||||||||||
| environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path | ||||||||||||||||||||||||||||
| }): | ||||||||||||||||||||||||||||
| if environment_vars.GOOGLE_API_CERTIFICATE_CONFIG in os.environ: | ||||||||||||||||||||||||||||
| del os.environ[environment_vars.GOOGLE_API_CERTIFICATE_CONFIG] | ||||||||||||||||||||||||||||
| if environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE in os.environ: | ||||||||||||||||||||||||||||
| del os.environ[environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE] | ||||||||||||||||||||||||||||
| if environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE in os.environ: | ||||||||||||||||||||||||||||
| del os.environ[environment_vars.CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| assert _mtls_helper.check_use_client_cert() is True | ||||||||||||||||||||||||||||
|
Comment on lines
+896
to
+906
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To simplify this test, you can use with mock.patch.dict(os.environ, {
environment_vars.CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH: cloudsdk_path
}, clear=True):
assert _mtls_helper.check_use_client_cert() is True |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class TestMtlsHelper: | ||||||||||||||||||||||||||||
| @mock.patch.object(_mtls_helper, "call_client_cert_callback") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the code more concise, you can use the
oroperator to chain thegetenvcalls. This will achieve the same fallback logic in a single line.