2323import unittest
2424import urllib
2525
26-
2726from google .api_core import exceptions
28-
27+ from google . auth . credentials import AnonymousCredentials
2928from google .oauth2 .service_account import Credentials
30- from . import _read_local_json
3129
30+ from google .cloud .storage ._helpers import STORAGE_EMULATOR_ENV_VAR
3231from google .cloud .storage .retry import DEFAULT_RETRY
3332from google .cloud .storage .retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
3433
34+ from . import _read_local_json
3535
3636_SERVICE_ACCOUNT_JSON = _read_local_json ("url_signer_v4_test_account.json" )
3737_CONFORMANCE_TESTS = _read_local_json ("url_signer_v4_test_data.json" )[
@@ -237,9 +237,6 @@ def test_ctor_mtls(self):
237237 self .assertEqual (client ._connection .API_BASE_URL , "http://foo" )
238238
239239 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-
243240 # avoids authentication if STORAGE_EMULATOR_ENV_VAR is set
244241 host = "http://localhost:8080"
245242 environ = {STORAGE_EMULATOR_ENV_VAR : host }
@@ -259,9 +256,6 @@ def test_ctor_w_emulator_wo_project(self):
259256 self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
260257
261258 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-
265259 # avoids authentication and infers the project from the environment
266260 host = "http://localhost:8080"
267261 environ_project = "environ-project"
@@ -277,9 +271,6 @@ def test_ctor_w_emulator_w_environ_project(self):
277271 self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
278272
279273 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-
283274 # project argument overrides project set in the enviroment
284275 host = "http://localhost:8080"
285276 environ_project = "environ-project"
@@ -296,7 +287,6 @@ def test_ctor_w_emulator_w_project_arg(self):
296287 self .assertIsInstance (client ._connection .credentials , AnonymousCredentials )
297288
298289 def test_create_anonymous_client (self ):
299- from google .auth .credentials import AnonymousCredentials
300290 from google .cloud .storage ._http import Connection
301291
302292 klass = self ._get_target_class ()
@@ -1187,11 +1177,91 @@ def test_lookup_bucket_hit_w_retry(self):
11871177 )
11881178
11891179 def test_create_bucket_w_missing_client_project (self ):
1180+ from google .cloud .exceptions import BadRequest
1181+
11901182 credentials = _make_credentials ()
11911183 client = self ._make_one (project = None , credentials = credentials )
11921184
1193- with self .assertRaises (ValueError ):
1194- client .create_bucket ("bucket" )
1185+ client ._post_resource = mock .Mock ()
1186+ client ._post_resource .side_effect = BadRequest ("Required parameter: project" )
1187+
1188+ bucket_name = "bucket-name"
1189+
1190+ with self .assertRaises (BadRequest ):
1191+ client .create_bucket (bucket_name )
1192+
1193+ expected_path = "/b"
1194+ expected_data = {"name" : bucket_name }
1195+ # no required parameter: project
1196+ expected_query_params = {}
1197+ client ._post_resource .assert_called_once_with (
1198+ expected_path ,
1199+ expected_data ,
1200+ query_params = expected_query_params ,
1201+ timeout = self ._get_default_timeout (),
1202+ retry = DEFAULT_RETRY ,
1203+ _target_object = mock .ANY ,
1204+ )
1205+
1206+ def test_create_bucket_w_missing_client_project_w_emulator (self ):
1207+ # mock STORAGE_EMULATOR_ENV_VAR is set
1208+ host = "http://localhost:8080"
1209+ environ = {STORAGE_EMULATOR_ENV_VAR : host }
1210+ with mock .patch ("os.environ" , environ ):
1211+ client = self ._make_one ()
1212+
1213+ bucket_name = "bucket-name"
1214+ api_response = {"name" : bucket_name }
1215+ client ._post_resource = mock .Mock ()
1216+ client ._post_resource .return_value = api_response
1217+
1218+ # mock STORAGE_EMULATOR_ENV_VAR is set
1219+ with mock .patch ("os.environ" , environ ):
1220+ bucket = client .create_bucket (bucket_name )
1221+
1222+ expected_path = "/b"
1223+ expected_data = api_response
1224+ expected_query_params = {"project" : "<none>" }
1225+ client ._post_resource .assert_called_once_with (
1226+ expected_path ,
1227+ expected_data ,
1228+ query_params = expected_query_params ,
1229+ timeout = self ._get_default_timeout (),
1230+ retry = DEFAULT_RETRY ,
1231+ _target_object = bucket ,
1232+ )
1233+
1234+ def test_create_bucket_w_environ_project_w_emulator (self ):
1235+ # mock STORAGE_EMULATOR_ENV_VAR is set
1236+ host = "http://localhost:8080"
1237+ environ_project = "environ-project"
1238+ environ = {
1239+ STORAGE_EMULATOR_ENV_VAR : host ,
1240+ "GOOGLE_CLOUD_PROJECT" : environ_project ,
1241+ }
1242+ with mock .patch ("os.environ" , environ ):
1243+ client = self ._make_one ()
1244+
1245+ bucket_name = "bucket-name"
1246+ api_response = {"name" : bucket_name }
1247+ client ._post_resource = mock .Mock ()
1248+ client ._post_resource .return_value = api_response
1249+
1250+ # mock STORAGE_EMULATOR_ENV_VAR is set
1251+ with mock .patch ("os.environ" , environ ):
1252+ bucket = client .create_bucket (bucket_name )
1253+
1254+ expected_path = "/b"
1255+ expected_data = api_response
1256+ expected_query_params = {"project" : environ_project }
1257+ client ._post_resource .assert_called_once_with (
1258+ expected_path ,
1259+ expected_data ,
1260+ query_params = expected_query_params ,
1261+ timeout = self ._get_default_timeout (),
1262+ retry = DEFAULT_RETRY ,
1263+ _target_object = bucket ,
1264+ )
11951265
11961266 def test_create_bucket_w_conflict_w_user_project (self ):
11971267 from google .cloud .exceptions import Conflict
@@ -1787,12 +1857,112 @@ def test_list_blobs_w_explicit_w_user_project(self):
17871857 )
17881858
17891859 def test_list_buckets_wo_project (self ):
1860+ from google .cloud .exceptions import BadRequest
1861+ from google .cloud .storage .client import _item_to_bucket
1862+
17901863 credentials = _make_credentials ()
17911864 client = self ._make_one (project = None , credentials = credentials )
17921865
1793- with self .assertRaises (ValueError ):
1866+ client ._list_resource = mock .Mock ()
1867+ client ._list_resource .side_effect = BadRequest ("Required parameter: project" )
1868+
1869+ with self .assertRaises (BadRequest ):
17941870 client .list_buckets ()
17951871
1872+ expected_path = "/b"
1873+ expected_item_to_value = _item_to_bucket
1874+ expected_page_token = None
1875+ expected_max_results = None
1876+ expected_page_size = None
1877+ # no required parameter: project
1878+ expected_extra_params = {
1879+ "projection" : "noAcl" ,
1880+ }
1881+ client ._list_resource .assert_called_once_with (
1882+ expected_path ,
1883+ expected_item_to_value ,
1884+ page_token = expected_page_token ,
1885+ max_results = expected_max_results ,
1886+ extra_params = expected_extra_params ,
1887+ page_size = expected_page_size ,
1888+ timeout = self ._get_default_timeout (),
1889+ retry = DEFAULT_RETRY ,
1890+ )
1891+
1892+ def test_list_buckets_wo_project_w_emulator (self ):
1893+ from google .cloud .storage .client import _item_to_bucket
1894+
1895+ # mock STORAGE_EMULATOR_ENV_VAR is set
1896+ host = "http://localhost:8080"
1897+ environ = {STORAGE_EMULATOR_ENV_VAR : host }
1898+ with mock .patch ("os.environ" , environ ):
1899+ client = self ._make_one ()
1900+
1901+ client ._list_resource = mock .Mock (spec = [])
1902+
1903+ # mock STORAGE_EMULATOR_ENV_VAR is set
1904+ with mock .patch ("os.environ" , environ ):
1905+ client .list_buckets ()
1906+
1907+ expected_path = "/b"
1908+ expected_item_to_value = _item_to_bucket
1909+ expected_page_token = None
1910+ expected_max_results = None
1911+ expected_page_size = None
1912+ expected_extra_params = {
1913+ "project" : "<none>" ,
1914+ "projection" : "noAcl" ,
1915+ }
1916+ client ._list_resource .assert_called_once_with (
1917+ expected_path ,
1918+ expected_item_to_value ,
1919+ page_token = expected_page_token ,
1920+ max_results = expected_max_results ,
1921+ extra_params = expected_extra_params ,
1922+ page_size = expected_page_size ,
1923+ timeout = self ._get_default_timeout (),
1924+ retry = DEFAULT_RETRY ,
1925+ )
1926+
1927+ def test_list_buckets_w_environ_project_w_emulator (self ):
1928+ from google .cloud .storage .client import _item_to_bucket
1929+
1930+ # mock STORAGE_EMULATOR_ENV_VAR is set
1931+ host = "http://localhost:8080"
1932+ environ_project = "environ-project"
1933+ environ = {
1934+ STORAGE_EMULATOR_ENV_VAR : host ,
1935+ "GOOGLE_CLOUD_PROJECT" : environ_project ,
1936+ }
1937+ with mock .patch ("os.environ" , environ ):
1938+ client = self ._make_one ()
1939+
1940+ client ._list_resource = mock .Mock (spec = [])
1941+
1942+ # mock STORAGE_EMULATOR_ENV_VAR is set
1943+ with mock .patch ("os.environ" , environ ):
1944+ client .list_buckets ()
1945+
1946+ expected_path = "/b"
1947+ expected_item_to_value = _item_to_bucket
1948+ expected_page_token = None
1949+ expected_max_results = None
1950+ expected_page_size = None
1951+ expected_extra_params = {
1952+ "project" : environ_project ,
1953+ "projection" : "noAcl" ,
1954+ }
1955+ client ._list_resource .assert_called_once_with (
1956+ expected_path ,
1957+ expected_item_to_value ,
1958+ page_token = expected_page_token ,
1959+ max_results = expected_max_results ,
1960+ extra_params = expected_extra_params ,
1961+ page_size = expected_page_size ,
1962+ timeout = self ._get_default_timeout (),
1963+ retry = DEFAULT_RETRY ,
1964+ )
1965+
17961966 def test_list_buckets_w_defaults (self ):
17971967 from google .cloud .storage .client import _item_to_bucket
17981968
0 commit comments