@@ -640,12 +640,12 @@ def test_crypto_error(self):
640640 ENCRYPTED_EC_PRIVATE_KEY , b"wrong_password"
641641 )
642642
643- def test_check_use_client_cert (self ):
644- os . environ [ "GOOGLE_API_USE_CLIENT_CERTIFICATE" ] = "true"
643+ def test_check_use_client_cert (self , monkeypatch ):
644+ monkeypatch . setenv ( "GOOGLE_API_USE_CLIENT_CERTIFICATE" , "true" )
645645 use_client_cert = _mtls_helper .check_use_client_cert ()
646646 assert use_client_cert == "true"
647647
648- def test_check_use_client_cert_for_workload_with_config_file (self ):
648+ def test_check_use_client_cert_for_workload_with_config_file (self , monkeypatch ):
649649 config_data = {
650650 "version" : 1 ,
651651 "cert_configs" : {
@@ -657,11 +657,56 @@ def test_check_use_client_cert_for_workload_with_config_file(self):
657657 }
658658 config_filename = "mock_certificate_config.json"
659659 config_file_content = json .dumps (config_data )
660+ monkeypatch .setenv ("GOOGLE_API_CERTIFICATE_CONFIG" , config_filename )
661+ monkeypatch .setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "" )
660662 # Use mock_open to simulate the file in memory
661- m = mock .mock_open (read_data = config_file_content )
662- with mock .patch ("builtins.open" , m ):
663- os .environ ["GOOGLE_API_CERTIFICATE_CONFIG" ] = config_filename
664- os .environ ["GOOGLE_API_USE_CLIENT_CERTIFICATE" ] = ""
663+ mock_file_handle = mock .mock_open (read_data = config_file_content )
664+ with mock .patch ("builtins.open" , mock_file_handle ):
665665 use_client_cert = _mtls_helper .check_use_client_cert ()
666666 assert use_client_cert == "true"
667667
668+ def test_check_use_client_cert_false (self , monkeypatch ):
669+ monkeypatch .setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" )
670+ use_client_cert = _mtls_helper .check_use_client_cert ()
671+ assert use_client_cert == "false"
672+
673+ def test_check_use_client_cert_for_workload_with_config_file_not_found (
674+ self , monkeypatch
675+ ):
676+ monkeypatch .setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "" )
677+ use_client_cert = _mtls_helper .check_use_client_cert ()
678+ assert use_client_cert == "false"
679+
680+ def test_check_use_client_cert_for_workload_with_config_file_not_json (
681+ self , monkeypatch
682+ ):
683+ config_filename = "mock_certificate_config.json"
684+ config_file_content = "not_valid_json"
685+ monkeypatch .setenv ("GOOGLE_API_CERTIFICATE_CONFIG" , config_filename )
686+ monkeypatch .setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "" )
687+ # Use mock_open to simulate the file in memory
688+ mock_file_handle = mock .mock_open (read_data = config_file_content )
689+ with mock .patch ("builtins.open" , mock_file_handle ):
690+ use_client_cert = _mtls_helper .check_use_client_cert ()
691+ assert use_client_cert == "false"
692+
693+ def test_check_use_client_cert_for_workload_with_config_file_no_workload (
694+ self , monkeypatch
695+ ):
696+ config_data = {"version" : 1 , "cert_configs" : {"dummy_key" : {}}}
697+ config_filename = "mock_certificate_config.json"
698+ config_file_content = json .dumps (config_data )
699+ monkeypatch .setenv ("GOOGLE_API_CERTIFICATE_CONFIG" , config_filename )
700+ monkeypatch .setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "" )
701+ # Use mock_open to simulate the file in memory
702+ mock_file_handle = mock .mock_open (read_data = config_file_content )
703+ with mock .patch ("builtins.open" , mock_file_handle ):
704+ use_client_cert = _mtls_helper .check_use_client_cert ()
705+ assert use_client_cert == "false"
706+
707+ def test_check_use_client_cert_when_file_does_not_exist (self , monkeypatch ):
708+ config_filename = "mock_certificate_config.json"
709+ monkeypatch .setenv ("GOOGLE_API_CERTIFICATE_CONFIG" , config_filename )
710+ monkeypatch .setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "" )
711+ use_client_cert = _mtls_helper .check_use_client_cert ()
712+ assert use_client_cert == "false"
0 commit comments