@@ -315,13 +315,16 @@ def test_success_with_context_aware_metadata(
315315 )
316316 @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
317317 @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
318+ @mock .patch ("os.path.exists" , autospec = True )
318319 def test_success_with_certificate_config (
319320 self ,
321+ mock_path_exists ,
320322 mock_check_config_path ,
321323 mock_load_json_file ,
322324 mock_get_cert_config_path ,
323325 mock_read_cert_and_key_files ,
324326 ):
327+ mock_path_exists .return_value = True
325328 cert_config_path = "/path/to/config"
326329 mock_check_config_path .return_value = cert_config_path
327330 mock_load_json_file .return_value = {
@@ -444,12 +447,15 @@ class TestGetWorkloadCertAndKey(object):
444447 @mock .patch (
445448 "google.auth.transport._mtls_helper._read_cert_and_key_files" , autospec = True
446449 )
450+ @mock .patch ("os.path.exists" , autospec = True )
447451 def test_success (
448452 self ,
453+ mock_path_exists ,
449454 mock_read_cert_and_key_files ,
450455 mock_get_cert_config_path ,
451456 mock_load_json_file ,
452457 ):
458+ mock_path_exists .return_value = True
453459 cert_config_path = "/path/to/cert"
454460 mock_get_cert_config_path .return_value = "/path/to/cert"
455461 mock_load_json_file .return_value = {
@@ -482,7 +488,9 @@ def test_file_not_found_returns_none(self, mock_get_cert_config_path):
482488 @mock .patch (
483489 "google.auth.transport._mtls_helper._get_cert_config_path" , autospec = True
484490 )
485- def test_no_cert_configs (self , mock_get_cert_config_path , mock_load_json_file ):
491+ @mock .patch ("os.path.exists" , autospec = True )
492+ def test_no_cert_configs (self , mock_path_exists , mock_get_cert_config_path , mock_load_json_file ):
493+ mock_path_exists .return_value = True
486494 mock_get_cert_config_path .return_value = "/path/to/cert"
487495 mock_load_json_file .return_value = {}
488496
@@ -505,7 +513,9 @@ def test_no_workload(self, mock_get_cert_config_path, mock_load_json_file):
505513 @mock .patch (
506514 "google.auth.transport._mtls_helper._get_cert_config_path" , autospec = True
507515 )
508- def test_no_cert_file (self , mock_get_cert_config_path , mock_load_json_file ):
516+ @mock .patch ("os.path.exists" , autospec = True )
517+ def test_no_cert_file (self , mock_path_exists , mock_get_cert_config_path , mock_load_json_file ):
518+ mock_path_exists .return_value = True
509519 mock_get_cert_config_path .return_value = "/path/to/cert"
510520 mock_load_json_file .return_value = {
511521 "cert_configs" : {"workload" : {"key_path" : "path/to/key" }}
@@ -518,7 +528,9 @@ def test_no_cert_file(self, mock_get_cert_config_path, mock_load_json_file):
518528 @mock .patch (
519529 "google.auth.transport._mtls_helper._get_cert_config_path" , autospec = True
520530 )
521- def test_no_key_file (self , mock_get_cert_config_path , mock_load_json_file ):
531+ @mock .patch ("os.path.exists" , autospec = True )
532+ def test_no_key_file (self , mock_path_exists , mock_get_cert_config_path , mock_load_json_file ):
533+ mock_path_exists .return_value = True
522534 mock_get_cert_config_path .return_value = "/path/to/cert"
523535 mock_load_json_file .return_value = {
524536 "cert_configs" : {"workload" : {"cert_path" : "path/to/key" }}
@@ -573,7 +585,7 @@ def test_success_with_override(self):
573585 def test_override_does_not_exist (self ):
574586 config_path = "fake/file/path"
575587 returned_path = _mtls_helper ._get_cert_config_path (config_path )
576- assert returned_path is None
588+ assert returned_path == config_path
577589
578590 @mock .patch .dict (
579591 os .environ ,
@@ -601,12 +613,21 @@ def test_env_variable(self, mock_path_exists):
601613 expected_path = "path/to/config/file"
602614 assert returned_path == expected_path
603615
604- @mock .patch .dict (os .environ , {"GOOGLE_API_CERTIFICATE_CONFIG" : "" })
616+ @mock .patch .dict (
617+ os .environ ,
618+ {
619+ "GOOGLE_API_CERTIFICATE_CONFIG" : "" ,
620+ "CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH" : "" ,
621+ },
622+ )
605623 @mock .patch ("os.path.exists" , autospec = True )
606- def test_env_variable_file_does_not_exist (self , mock_path_exists ):
624+ def test_default_file_does_not_exist (self , mock_path_exists ):
607625 mock_path_exists .return_value = False
608626 returned_path = _mtls_helper ._get_cert_config_path ()
609- assert returned_path is None
627+ expected_path = os .path .expanduser (
628+ _mtls_helper .CERTIFICATE_CONFIGURATION_DEFAULT_PATH
629+ )
630+ assert returned_path == expected_path
610631
611632 def test_cert_config_path_precedence (self ):
612633 # GOOGLE_API_CERTIFICATE_CONFIG takes precedence
@@ -643,10 +664,26 @@ def test_cert_config_path_fallback(self):
643664 os .environ , {"GOOGLE_API_CERTIFICATE_CONFIG" : "path/to/config/file" }
644665 )
645666 @mock .patch ("os.path.exists" , autospec = True )
646- def test_default_file_does_not_exist (self , mock_path_exists ):
667+ def test_env_variable_file_does_not_exist (self , mock_path_exists ):
647668 mock_path_exists .return_value = False
648669 returned_path = _mtls_helper ._get_cert_config_path ()
649- assert returned_path is None
670+ assert returned_path == "path/to/config/file"
671+
672+ @mock .patch .dict (
673+ os .environ ,
674+ {
675+ "GOOGLE_API_CERTIFICATE_CONFIG" : "" ,
676+ "CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH" : "path/to/context/aware/config" ,
677+ },
678+ )
679+ @mock .patch ("os.path.exists" , autospec = True )
680+ def test_cert_config_path_ignore_context_aware (self , mock_path_exists ):
681+ mock_path_exists .return_value = True
682+ returned_path = _mtls_helper ._get_cert_config_path (include_context_aware = False )
683+ expected_path = os .path .expanduser (
684+ _mtls_helper .CERTIFICATE_CONFIGURATION_DEFAULT_PATH
685+ )
686+ assert returned_path == expected_path
650687
651688
652689class TestGetClientCertAndKey (object ):
@@ -818,10 +855,13 @@ def test_config_file_not_found(self, mock_exists, mock_file):
818855 mock_file .side_effect = FileNotFoundError
819856 assert _mtls_helper .check_use_client_cert () is False
820857
858+ @mock .patch ("builtins.open" , autospec = True )
821859 @mock .patch .dict (os .environ , {}, clear = True )
822860 @mock .patch ("os.path.exists" , autospec = True )
823- def test_no_env_vars_set (self , mock_exists ):
861+ def test_no_env_vars_set (self , mock_exists , mock_open ):
862+ _mtls_helper ._has_logged_mtls_warning = False
824863 mock_exists .return_value = False
864+ mock_open .side_effect = FileNotFoundError ()
825865 assert _mtls_helper .check_use_client_cert () is False
826866
827867 def test_use_client_cert_precedence (self ):
0 commit comments