@@ -29,6 +29,18 @@ def test_api_key_passed_via_client_options(self):
2929 client_opts = client ._client_manager .client_config ["client_options" ]
3030 self .assertEqual (client_opts .api_key , "AIzA_client_opts" )
3131
32+ @mock .patch .dict (os .environ , {"GEMINI_API_KEY" : "AIzA_env" })
33+ def test_api_key_from_environment (self ):
34+ # Default to API key loaded from environment.
35+ client .configure ()
36+ client_opts = client ._client_manager .client_config ["client_options" ]
37+ self .assertEqual (client_opts .api_key , "AIzA_env" )
38+
39+ # But not when a key is provided explicitly.
40+ client .configure (api_key = "AIzA_client" )
41+ client_opts = client ._client_manager .client_config ["client_options" ]
42+ self .assertEqual (client_opts .api_key , "AIzA_client" )
43+
3244 @mock .patch .dict (os .environ , {"GOOGLE_API_KEY" : "AIzA_env" })
3345 def test_api_key_from_environment (self ):
3446 # Default to API key loaded from environment.
@@ -41,6 +53,30 @@ def test_api_key_from_environment(self):
4153 client_opts = client ._client_manager .client_config ["client_options" ]
4254 self .assertEqual (client_opts .api_key , "AIzA_client" )
4355
56+ @mock .patch .dict (os .environ , {"GEMINI_API_KEY" : "" , "GOOGLE_API_KEY" : "AIzA_env" })
57+ def test_empty_gemini_api_key_doesnt_shadow (self ):
58+ # Default to API key loaded from environment.
59+ client .configure ()
60+ client_opts = client ._client_manager .client_config ["client_options" ]
61+ self .assertEqual (client_opts .api_key , "AIzA_env" )
62+
63+ # But not when a key is provided explicitly.
64+ client .configure (api_key = "AIzA_client" )
65+ client_opts = client ._client_manager .client_config ["client_options" ]
66+ self .assertEqual (client_opts .api_key , "AIzA_client" )
67+
68+ @mock .patch .dict (os .environ , {"GEMINI_API_KEY" : "" , "GOOGLE_API_KEY" : "AIzA_env" })
69+ def test_empty_google_api_key_doesnt_shadow (self ):
70+ # Default to API key loaded from environment.
71+ client .configure ()
72+ client_opts = client ._client_manager .client_config ["client_options" ]
73+ self .assertEqual (client_opts .api_key , "AIzA_env" )
74+
75+ # But not when a key is provided explicitly.
76+ client .configure (api_key = "AIzA_client" )
77+ client_opts = client ._client_manager .client_config ["client_options" ]
78+ self .assertEqual (client_opts .api_key , "AIzA_client" )
79+
4480 def test_api_key_cannot_be_set_twice (self ):
4581 client_opts = client_options .ClientOptions (api_key = "AIzA_client_opts" )
4682
0 commit comments