@@ -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,11 @@ 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 (
493+ self , mock_path_exists , mock_get_cert_config_path , mock_load_json_file
494+ ):
495+ mock_path_exists .return_value = True
486496 mock_get_cert_config_path .return_value = "/path/to/cert"
487497 mock_load_json_file .return_value = {}
488498
@@ -505,7 +515,11 @@ def test_no_workload(self, mock_get_cert_config_path, mock_load_json_file):
505515 @mock .patch (
506516 "google.auth.transport._mtls_helper._get_cert_config_path" , autospec = True
507517 )
508- def test_no_cert_file (self , mock_get_cert_config_path , mock_load_json_file ):
518+ @mock .patch ("os.path.exists" , autospec = True )
519+ def test_no_cert_file (
520+ self , mock_path_exists , mock_get_cert_config_path , mock_load_json_file
521+ ):
522+ mock_path_exists .return_value = True
509523 mock_get_cert_config_path .return_value = "/path/to/cert"
510524 mock_load_json_file .return_value = {
511525 "cert_configs" : {"workload" : {"key_path" : "path/to/key" }}
@@ -518,7 +532,11 @@ def test_no_cert_file(self, mock_get_cert_config_path, mock_load_json_file):
518532 @mock .patch (
519533 "google.auth.transport._mtls_helper._get_cert_config_path" , autospec = True
520534 )
521- def test_no_key_file (self , mock_get_cert_config_path , mock_load_json_file ):
535+ @mock .patch ("os.path.exists" , autospec = True )
536+ def test_no_key_file (
537+ self , mock_path_exists , mock_get_cert_config_path , mock_load_json_file
538+ ):
539+ mock_path_exists .return_value = True
522540 mock_get_cert_config_path .return_value = "/path/to/cert"
523541 mock_load_json_file .return_value = {
524542 "cert_configs" : {"workload" : {"cert_path" : "path/to/key" }}
@@ -573,7 +591,7 @@ def test_success_with_override(self):
573591 def test_override_does_not_exist (self ):
574592 config_path = "fake/file/path"
575593 returned_path = _mtls_helper ._get_cert_config_path (config_path )
576- assert returned_path is None
594+ assert returned_path == config_path
577595
578596 @mock .patch .dict (
579597 os .environ ,
@@ -601,12 +619,21 @@ def test_env_variable(self, mock_path_exists):
601619 expected_path = "path/to/config/file"
602620 assert returned_path == expected_path
603621
604- @mock .patch .dict (os .environ , {"GOOGLE_API_CERTIFICATE_CONFIG" : "" })
622+ @mock .patch .dict (
623+ os .environ ,
624+ {
625+ "GOOGLE_API_CERTIFICATE_CONFIG" : "" ,
626+ "CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH" : "" ,
627+ },
628+ )
605629 @mock .patch ("os.path.exists" , autospec = True )
606- def test_env_variable_file_does_not_exist (self , mock_path_exists ):
630+ def test_default_file_does_not_exist (self , mock_path_exists ):
607631 mock_path_exists .return_value = False
608632 returned_path = _mtls_helper ._get_cert_config_path ()
609- assert returned_path is None
633+ expected_path = os .path .expanduser (
634+ _mtls_helper .CERTIFICATE_CONFIGURATION_DEFAULT_PATH
635+ )
636+ assert returned_path == expected_path
610637
611638 def test_cert_config_path_precedence (self ):
612639 # GOOGLE_API_CERTIFICATE_CONFIG takes precedence
@@ -643,10 +670,26 @@ def test_cert_config_path_fallback(self):
643670 os .environ , {"GOOGLE_API_CERTIFICATE_CONFIG" : "path/to/config/file" }
644671 )
645672 @mock .patch ("os.path.exists" , autospec = True )
646- def test_default_file_does_not_exist (self , mock_path_exists ):
673+ def test_env_variable_file_does_not_exist (self , mock_path_exists ):
647674 mock_path_exists .return_value = False
648675 returned_path = _mtls_helper ._get_cert_config_path ()
649- assert returned_path is None
676+ assert returned_path == "path/to/config/file"
677+
678+ @mock .patch .dict (
679+ os .environ ,
680+ {
681+ "GOOGLE_API_CERTIFICATE_CONFIG" : "" ,
682+ "CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH" : "path/to/context/aware/config" ,
683+ },
684+ )
685+ @mock .patch ("os.path.exists" , autospec = True )
686+ def test_cert_config_path_ignore_context_aware (self , mock_path_exists ):
687+ mock_path_exists .return_value = True
688+ returned_path = _mtls_helper ._get_cert_config_path (include_context_aware = False )
689+ expected_path = os .path .expanduser (
690+ _mtls_helper .CERTIFICATE_CONFIGURATION_DEFAULT_PATH
691+ )
692+ assert returned_path == expected_path
650693
651694
652695class TestGetClientCertAndKey (object ):
@@ -818,10 +861,13 @@ def test_config_file_not_found(self, mock_exists, mock_file):
818861 mock_file .side_effect = FileNotFoundError
819862 assert _mtls_helper .check_use_client_cert () is False
820863
864+ @mock .patch ("builtins.open" , autospec = True )
821865 @mock .patch .dict (os .environ , {}, clear = True )
822866 @mock .patch ("os.path.exists" , autospec = True )
823- def test_no_env_vars_set (self , mock_exists ):
867+ def test_no_env_vars_set (self , mock_exists , mock_open ):
868+ _mtls_helper ._has_logged_mtls_warning = False
824869 mock_exists .return_value = False
870+ mock_open .side_effect = FileNotFoundError ()
825871 assert _mtls_helper .check_use_client_cert () is False
826872
827873 def test_use_client_cert_precedence (self ):
0 commit comments