@@ -29,6 +29,18 @@ def test_api_key_passed_via_client_options(self):
29
29
client_opts = client ._client_manager .client_config ["client_options" ]
30
30
self .assertEqual (client_opts .api_key , "AIzA_client_opts" )
31
31
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
+
32
44
@mock .patch .dict (os .environ , {"GOOGLE_API_KEY" : "AIzA_env" })
33
45
def test_api_key_from_environment (self ):
34
46
# Default to API key loaded from environment.
@@ -41,6 +53,30 @@ def test_api_key_from_environment(self):
41
53
client_opts = client ._client_manager .client_config ["client_options" ]
42
54
self .assertEqual (client_opts .api_key , "AIzA_client" )
43
55
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
+
44
80
def test_api_key_cannot_be_set_twice (self ):
45
81
client_opts = client_options .ClientOptions (api_key = "AIzA_client_opts" )
46
82
0 commit comments