@@ -75,6 +75,38 @@ def test_ctor_connection_type(self):
7575 self .assertIsNone (client .current_batch )
7676 self .assertEqual (list (client ._batch_stack ), [])
7777
78+ def test_ctor_wo_project (self ):
79+ from google .cloud .storage ._http import Connection
80+
81+ PROJECT = 'PROJECT'
82+ CREDENTIALS = _make_credentials ()
83+
84+ ddp_patch = mock .patch (
85+ 'google.cloud.client._determine_default_project' ,
86+ return_value = PROJECT )
87+
88+ with ddp_patch :
89+ client = self ._make_one (credentials = CREDENTIALS )
90+
91+ self .assertEqual (client .project , PROJECT )
92+ self .assertIsInstance (client ._connection , Connection )
93+ self .assertIs (client ._connection .credentials , CREDENTIALS )
94+ self .assertIsNone (client .current_batch )
95+ self .assertEqual (list (client ._batch_stack ), [])
96+
97+ def test_ctor_w_project_explicit_none (self ):
98+ from google .cloud .storage ._http import Connection
99+
100+ CREDENTIALS = _make_credentials ()
101+
102+ client = self ._make_one (project = None , credentials = CREDENTIALS )
103+
104+ self .assertIsNone (client .project )
105+ self .assertIsInstance (client ._connection , Connection )
106+ self .assertIs (client ._connection .credentials , CREDENTIALS )
107+ self .assertIsNone (client .current_batch )
108+ self .assertEqual (list (client ._batch_stack ), [])
109+
78110 def test_create_anonymous_client (self ):
79111 from google .auth .credentials import AnonymousCredentials
80112 from google .cloud .storage ._http import Connection
@@ -286,6 +318,7 @@ def test_create_bucket_conflict(self):
286318 from google .cloud .exceptions import Conflict
287319
288320 PROJECT = 'PROJECT'
321+ OTHER_PROJECT = 'OTHER_PROJECT'
289322 CREDENTIALS = _make_credentials ()
290323 client = self ._make_one (project = PROJECT , credentials = CREDENTIALS )
291324
@@ -294,15 +327,17 @@ def test_create_bucket_conflict(self):
294327 client ._connection .API_BASE_URL ,
295328 'storage' ,
296329 client ._connection .API_VERSION ,
297- 'b?project=%s' % (PROJECT ,),
330+ 'b?project=%s' % (OTHER_PROJECT ,),
298331 ])
299332 data = {'error' : {'message' : 'Conflict' }}
300333 sent = {'name' : BUCKET_NAME }
301334 http = _make_requests_session ([
302335 _make_json_response (data , status = http_client .CONFLICT )])
303336 client ._http_internal = http
304337
305- self .assertRaises (Conflict , client .create_bucket , BUCKET_NAME )
338+ with self .assertRaises (Conflict ):
339+ client .create_bucket (BUCKET_NAME , project = OTHER_PROJECT )
340+
306341 http .request .assert_called_once_with (
307342 method = 'POST' , url = URI , data = mock .ANY , headers = mock .ANY )
308343 json_sent = http .request .call_args_list [0 ][1 ]['data' ]
@@ -337,6 +372,13 @@ def test_create_bucket_success(self):
337372 json_sent = http .request .call_args_list [0 ][1 ]['data' ]
338373 self .assertEqual (sent , json .loads (json_sent ))
339374
375+ def test_list_buckets_wo_project (self ):
376+ CREDENTIALS = _make_credentials ()
377+ client = self ._make_one (project = None , credentials = CREDENTIALS )
378+
379+ with self .assertRaises (ValueError ):
380+ client .list_buckets ()
381+
340382 def test_list_buckets_empty (self ):
341383 from six .moves .urllib .parse import parse_qs
342384 from six .moves .urllib .parse import urlparse
@@ -371,6 +413,41 @@ def test_list_buckets_empty(self):
371413 uri_parts = urlparse (requested_url )
372414 self .assertEqual (parse_qs (uri_parts .query ), expected_query )
373415
416+ def test_list_buckets_explicit_project (self ):
417+ from six .moves .urllib .parse import parse_qs
418+ from six .moves .urllib .parse import urlparse
419+
420+ PROJECT = 'PROJECT'
421+ OTHER_PROJECT = 'OTHER_PROJECT'
422+ CREDENTIALS = _make_credentials ()
423+ client = self ._make_one (project = PROJECT , credentials = CREDENTIALS )
424+
425+ http = _make_requests_session ([_make_json_response ({})])
426+ client ._http_internal = http
427+
428+ buckets = list (client .list_buckets (project = OTHER_PROJECT ))
429+
430+ self .assertEqual (len (buckets ), 0 )
431+
432+ http .request .assert_called_once_with (
433+ method = 'GET' , url = mock .ANY , data = mock .ANY , headers = mock .ANY )
434+
435+ requested_url = http .request .mock_calls [0 ][2 ]['url' ]
436+ expected_base_url = '/' .join ([
437+ client ._connection .API_BASE_URL ,
438+ 'storage' ,
439+ client ._connection .API_VERSION ,
440+ 'b' ,
441+ ])
442+ self .assertTrue (requested_url .startswith (expected_base_url ))
443+
444+ expected_query = {
445+ 'project' : [OTHER_PROJECT ],
446+ 'projection' : ['noAcl' ],
447+ }
448+ uri_parts = urlparse (requested_url )
449+ self .assertEqual (parse_qs (uri_parts .query ), expected_query )
450+
374451 def test_list_buckets_non_empty (self ):
375452 PROJECT = 'PROJECT'
376453 CREDENTIALS = _make_credentials ()
0 commit comments