@@ -236,6 +236,65 @@ def test_ctor_mtls(self):
236236 self .assertEqual (client ._connection .ALLOW_AUTO_SWITCH_TO_MTLS_URL , False )
237237 self .assertEqual (client ._connection .API_BASE_URL , "http://foo" )
238238
239+ def test_ctor_w_emulator_wo_project (self ):
240+ from google .auth .credentials import AnonymousCredentials
241+ from google .cloud .storage ._helpers import STORAGE_EMULATOR_ENV_VAR
242+
243+ # avoids authentication if STORAGE_EMULATOR_ENV_VAR is set
244+ host = "http://localhost:8080"
245+ environ = {STORAGE_EMULATOR_ENV_VAR : host }
246+ with mock .patch ("os.environ" , environ ):
247+ client = self ._make_one ()
248+
249+ self .assertIsNone (client .project )
250+ self .assertEqual (client ._connection .API_BASE_URL , host )
251+ self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
252+
253+ # avoids authentication if storage emulator is set through api_endpoint
254+ client = self ._make_one (
255+ client_options = {"api_endpoint" : "http://localhost:8080" }
256+ )
257+ self .assertIsNone (client .project )
258+ self .assertEqual (client ._connection .API_BASE_URL , host )
259+ self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
260+
261+ def test_ctor_w_emulator_w_environ_project (self ):
262+ from google .auth .credentials import AnonymousCredentials
263+ from google .cloud .storage ._helpers import STORAGE_EMULATOR_ENV_VAR
264+
265+ # avoids authentication and infers the project from the environment
266+ host = "http://localhost:8080"
267+ environ_project = "environ-project"
268+ environ = {
269+ STORAGE_EMULATOR_ENV_VAR : host ,
270+ "GOOGLE_CLOUD_PROJECT" : environ_project ,
271+ }
272+ with mock .patch ("os.environ" , environ ):
273+ client = self ._make_one ()
274+
275+ self .assertEqual (client .project , environ_project )
276+ self .assertEqual (client ._connection .API_BASE_URL , host )
277+ self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
278+
279+ def test_ctor_w_emulator_w_project_arg (self ):
280+ from google .auth .credentials import AnonymousCredentials
281+ from google .cloud .storage ._helpers import STORAGE_EMULATOR_ENV_VAR
282+
283+ # project argument overrides project set in the enviroment
284+ host = "http://localhost:8080"
285+ environ_project = "environ-project"
286+ project = "my-test-project"
287+ environ = {
288+ STORAGE_EMULATOR_ENV_VAR : host ,
289+ "GOOGLE_CLOUD_PROJECT" : environ_project ,
290+ }
291+ with mock .patch ("os.environ" , environ ):
292+ client = self ._make_one (project = project )
293+
294+ self .assertEqual (client .project , project )
295+ self .assertEqual (client ._connection .API_BASE_URL , host )
296+ self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
297+
239298 def test_create_anonymous_client (self ):
240299 from google .auth .credentials import AnonymousCredentials
241300 from google .cloud .storage ._http import Connection
0 commit comments