def _list_buckets_non_empty_helper(self, project, use_default=False): from six.moves.urllib.parse import parse_qs from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlparse from gcloud._testing import _monkey_defaults as _base_monkey_defaults from gcloud.storage._testing import _monkey_defaults from gcloud.storage.connection import Connection BUCKET_NAME = "bucket-name" conn = Connection() query_params = urlencode({"project": project, "projection": "noAcl"}) BASE_URI = "/".join([conn.API_BASE_URL, "storage", conn.API_VERSION]) URI = "/".join([BASE_URI, "b?%s" % (query_params,)]) http = conn._http = Http( {"status": "200", "content-type": "application/json"}, '{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME).encode("utf-8"), ) if use_default: with _base_monkey_defaults(project=project): with _monkey_defaults(connection=conn): buckets = list(self._callFUT()) else: buckets = list(self._callFUT(project=project, connection=conn)) self.assertEqual(len(buckets), 1) self.assertEqual(buckets[0].name, BUCKET_NAME) self.assertEqual(http._called_with["method"], "GET") self.assertTrue(http._called_with["uri"].startswith(BASE_URI)) self.assertEqual(parse_qs(urlparse(http._called_with["uri"]).query), parse_qs(urlparse(URI).query))
def _lookup_bucket_hit_helper(self, use_default=False): from gcloud.storage._testing import _monkey_defaults from gcloud.storage.bucket import Bucket from gcloud.storage.connection import Connection BLOB_NAME = 'blob-name' conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b', '%s?projection=noAcl' % (BLOB_NAME, ), ]) http = conn._http = Http( { 'status': '200', 'content-type': 'application/json' }, '{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'), ) if use_default: with _monkey_defaults(connection=conn): bucket = self._callFUT(BLOB_NAME) else: bucket = self._callFUT(BLOB_NAME, connection=conn) self.assertTrue(isinstance(bucket, Bucket)) self.assertTrue(bucket.connection is conn) self.assertEqual(bucket.name, BLOB_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI)
def _list_buckets_non_empty_helper(self, project, use_default=False): from six.moves.urllib.parse import parse_qs from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlparse from gcloud._testing import _monkey_defaults as _base_monkey_defaults from gcloud.storage._testing import _monkey_defaults from gcloud.storage.connection import Connection BUCKET_NAME = 'bucket-name' conn = Connection() query_params = urlencode({'project': project, 'projection': 'noAcl'}) BASE_URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, ]) URI = '/'.join([BASE_URI, 'b?%s' % (query_params,)]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, '{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME) .encode('utf-8'), ) if use_default: with _base_monkey_defaults(project=project): with _monkey_defaults(connection=conn): buckets = list(self._callFUT()) else: buckets = list(self._callFUT(project=project, connection=conn)) self.assertEqual(len(buckets), 1) self.assertEqual(buckets[0].name, BUCKET_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertTrue(http._called_with['uri'].startswith(BASE_URI)) self.assertEqual(parse_qs(urlparse(http._called_with['uri']).query), parse_qs(urlparse(URI).query))
def _get_all_buckets_non_empty_helper(self, project, use_default=False): from gcloud._testing import _monkey_defaults as _base_monkey_defaults from gcloud.storage._testing import _monkey_defaults from gcloud.storage.connection import Connection BUCKET_NAME = 'bucket-name' conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b?project=%s' % project, ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, '{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME) .encode('utf-8'), ) if use_default: with _base_monkey_defaults(project=project): with _monkey_defaults(connection=conn): buckets = list(self._callFUT()) else: buckets = list(self._callFUT(project, conn)) self.assertEqual(len(buckets), 1) self.assertEqual(buckets[0].name, BUCKET_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI)
def _lookup_bucket_hit_helper(self, use_default=False): from gcloud.storage._testing import _monkey_defaults from gcloud.storage.bucket import Bucket from gcloud.storage.connection import Connection BLOB_NAME = 'blob-name' conn = Connection() URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, 'b', '%s?projection=noAcl' % (BLOB_NAME,), ]) http = conn._http = Http( {'status': '200', 'content-type': 'application/json'}, '{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'), ) if use_default: with _monkey_defaults(connection=conn): bucket = self._callFUT(BLOB_NAME) else: bucket = self._callFUT(BLOB_NAME, connection=conn) self.assertTrue(isinstance(bucket, Bucket)) self.assertTrue(bucket.connection is conn) self.assertEqual(bucket.name, BLOB_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI)
def test_no_env_var_set(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ with self._monkeyEnviron(None): with _monkey_defaults(): self._callFUT() self.assertEqual(_implicit_environ.get_default_bucket(), None)
def test_set_explicit_None_wo_env_var_set(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ CONNECTION = object() with self._monkeyEnviron(None): with _monkey_defaults(connection=CONNECTION): self._callFUT(None) self.assertEqual(_implicit_environ.get_default_bucket(), None)
def test_set_explicit_None_wo_connection_set(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ IMPLICIT_BUCKET_NAME = 'IMPLICIT' with self._monkeyEnviron(IMPLICIT_BUCKET_NAME): with _monkey_defaults(): self._callFUT(None) self.assertEqual(_implicit_environ.get_default_bucket(), None)
def test_create_no_project(self): from gcloud._testing import _monkey_defaults BUCKET_NAME = 'bucket-name' bucket = self._makeOne(BUCKET_NAME) CONNECTION = object() with _monkey_defaults(project=None): self.assertRaises(EnvironmentError, bucket.create, connection=CONNECTION)
def test_make_public_recursive(self): from gcloud.storage.acl import _ACLEntity from gcloud.storage.bucket import _BlobIterator from gcloud.storage._testing import _monkey_defaults _saved = [] class _Blob(object): _granted = False def __init__(self, bucket, name): self._bucket = bucket self._name = name @property def acl(self): return self # Faux ACL methods def all(self): return self def grant_read(self): self._granted = True def save(self, connection=None): _saved.append( (self._bucket, self._name, self._granted, connection)) class _Iterator(_BlobIterator): def get_items_from_response(self, response): for item in response.get('items', []): yield _Blob(self.bucket, item['name']) NAME = 'name' BLOB_NAME = 'blob-name' permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after, {'items': [{'name': BLOB_NAME}]}) bucket = self._makeOne(NAME) bucket.acl.loaded = True bucket.default_object_acl.loaded = True bucket._iterator_class = _Iterator with _monkey_defaults(connection=connection): bucket.make_public(recursive=True) self.assertEqual(list(bucket.acl), permissive) self.assertEqual(list(bucket.default_object_acl), []) self.assertEqual(_saved, [(bucket, BLOB_NAME, True, connection)]) kw = connection._requested self.assertEqual(len(kw), 2) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/b/%s' % NAME) self.assertEqual(kw[0]['data'], {'acl': permissive}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'}) self.assertEqual(kw[1]['method'], 'GET') self.assertEqual(kw[1]['path'], '/b/%s/o' % NAME) max_results = bucket._MAX_OBJECTS_FOR_ITERATION + 1 self.assertEqual(kw[1]['query_params'], {'maxResults': max_results, 'projection': 'full'})
def test_get_items_from_response_empty(self): from gcloud.storage._testing import _monkey_defaults connection = _Connection() bucket = _Bucket() with _monkey_defaults(connection=connection): iterator = self._makeOne(bucket) blobs = list(iterator.get_items_from_response({})) self.assertEqual(blobs, []) self.assertEqual(iterator.prefixes, ())
def test_save_none_set_none_passed_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults connection = _Connection() acl = self._makeOne() acl._connection = connection acl.save_path = '/testing' with _monkey_defaults(connection=connection): acl.save() kw = connection._requested self.assertEqual(len(kw), 0)
def test_set_explicit_w_env_var_set(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ EXPLICIT_BUCKET = object() with self._monkeyEnviron(None): with _monkey_defaults(): self._callFUT(EXPLICIT_BUCKET) self.assertEqual(_implicit_environ.get_default_bucket(), EXPLICIT_BUCKET)
def test_exists_implicit(self): from gcloud.storage._testing import _monkey_defaults from six.moves.http_client import NOT_FOUND NONESUCH = 'nonesuch' not_found_response = {'status': NOT_FOUND} connection = _Connection(not_found_response) bucket = _Bucket() blob = self._makeOne(NONESUCH, bucket=bucket) with _monkey_defaults(connection=connection): self.assertFalse(blob.exists())
def test___contains___hit(self): from gcloud.storage._testing import _monkey_defaults NAME = 'name' BLOB_NAME = 'blob-name' connection = _Connection({'name': BLOB_NAME}) bucket = self._makeOne(NAME, None) with _monkey_defaults(connection=connection): self.assertTrue(BLOB_NAME in bucket) kw, = connection._requested self.assertEqual(kw['method'], 'GET') self.assertEqual(kw['path'], '/b/%s/o/%s' % (NAME, BLOB_NAME))
def test___contains___miss(self): from gcloud.storage._testing import _monkey_defaults NAME = 'name' NONESUCH = 'nonesuch' connection = _Connection() bucket = self._makeOne(NAME, None) with _monkey_defaults(connection=connection): self.assertFalse(NONESUCH in bucket) kw, = connection._requested self.assertEqual(kw['method'], 'GET') self.assertEqual(kw['path'], '/b/%s/o/%s' % (NAME, NONESUCH))
def test_make_public_recursive(self): from gcloud.storage.acl import _ACLEntity from gcloud.storage.bucket import _BlobIterator from gcloud.storage._testing import _monkey_defaults _saved = [] class _Blob(object): _granted = False def __init__(self, bucket, name): self._bucket = bucket self._name = name @property def acl(self): return self def all(self): return self def grant_read(self): self._granted = True def save_acl(self): _saved.append((self._bucket, self._name, self._granted)) class _Iterator(_BlobIterator): def get_items_from_response(self, response): for item in response.get('items', []): yield _Blob(self.bucket, item['name']) NAME = 'name' BLOB_NAME = 'blob-name' permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after, {'items': [{'name': BLOB_NAME}]}) bucket = self._makeOne(NAME, connection) bucket.acl.loaded = True bucket.default_object_acl.loaded = True bucket._iterator_class = _Iterator with _monkey_defaults(connection=connection): bucket.make_public(recursive=True) self.assertEqual(list(bucket.acl), permissive) self.assertEqual(list(bucket.default_object_acl), []) self.assertEqual(_saved, [(bucket, BLOB_NAME, True)]) kw = connection._requested self.assertEqual(len(kw), 2) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/b/%s' % NAME) self.assertEqual(kw[0]['data'], {'acl': permissive}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'}) self.assertEqual(kw[1]['method'], 'GET') self.assertEqual(kw[1]['path'], '/b/%s/o' % NAME) self.assertEqual(kw[1]['query_params'], {'projection': 'noAcl'})
def test_ctor_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults http = _HTTP() connection = _Connection(http=http) with _monkey_defaults(connection=connection): batch = self._makeOne() self.assertTrue(batch._connection is connection) self.assertEqual(len(batch._requests), 0) self.assertEqual(len(batch._target_objects), 0)
def test_set_from_env_var(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ IMPLICIT_BUCKET_NAME = 'IMPLICIT' CONNECTION = object() with self._monkeyEnviron(IMPLICIT_BUCKET_NAME): with _monkey_defaults(connection=CONNECTION): self._callFUT() default_bucket = _implicit_environ.get_default_bucket() self.assertEqual(default_bucket.name, IMPLICIT_BUCKET_NAME)
def test_set_from_env_var(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ IMPLICIT_BUCKET_NAME = 'IMPLICIT' CONNECTION = object() with self._monkeyEnviron(IMPLICIT_BUCKET_NAME): with _monkey_defaults(connection=CONNECTION): self._callFUT() default_bucket = _implicit_environ.get_default_bucket() self.assertEqual(default_bucket.name, IMPLICIT_BUCKET_NAME) self.assertEqual(default_bucket.connection, CONNECTION)
def test_set_explicit_no_env_var_set(self): from gcloud.storage._testing import _monkey_defaults from gcloud.storage import _implicit_environ IMPLICIT_BUCKET_NAME = 'IMPLICIT' CONNECTION = object() EXPLICIT_BUCKET = object() with self._monkeyEnviron(IMPLICIT_BUCKET_NAME): with _monkey_defaults(connection=CONNECTION): self._callFUT(EXPLICIT_BUCKET) self.assertEqual(_implicit_environ.get_default_bucket(), EXPLICIT_BUCKET)
def test_ctor_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults connection = _Connection() bucket = _Bucket() with _monkey_defaults(connection=connection): iterator = self._makeOne(bucket) self.assertTrue(iterator.bucket is bucket) self.assertTrue(iterator.connection is connection) self.assertEqual(iterator.path, '%s/o' % bucket.path) self.assertEqual(iterator.page_number, 0) self.assertEqual(iterator.next_page_token, None) self.assertEqual(iterator.prefixes, set())
def test_list_blobs_defaults(self): from gcloud.storage._testing import _monkey_defaults NAME = 'name' connection = _Connection({'items': []}) bucket = self._makeOne(NAME) with _monkey_defaults(connection=connection): iterator = bucket.list_blobs() blobs = list(iterator) self.assertEqual(blobs, []) kw, = connection._requested self.assertEqual(kw['method'], 'GET') self.assertEqual(kw['path'], '/b/%s/o' % NAME) self.assertEqual(kw['query_params'], {'projection': 'noAcl'})
def test_delete_w_implicit_connection(self): from six.moves.http_client import NOT_FOUND from gcloud.storage._testing import _monkey_defaults BLOB_NAME = 'blob-name' not_found_response = {'status': NOT_FOUND} connection = _Connection(not_found_response) bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 with _monkey_defaults(connection=connection): blob.delete() self.assertFalse(blob.exists(connection=connection)) self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)])
def test_create_hit_implicit_project(self): from gcloud._testing import _monkey_defaults BUCKET_NAME = 'bucket-name' DATA = {'name': BUCKET_NAME} connection = _Connection(DATA) PROJECT = 'PROJECT' bucket = self._makeOne(BUCKET_NAME) with _monkey_defaults(project=PROJECT): bucket.create(connection=connection) kw, = connection._requested self.assertEqual(kw['method'], 'POST') self.assertEqual(kw['path'], '/b') self.assertEqual(kw['query_params'], {'project': PROJECT}) self.assertEqual(kw['data'], DATA)
def test_save_existing_missing_none_passed_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults connection = _Connection({}) acl = self._makeOne() acl.save_path = '/testing' acl.loaded = True with _monkey_defaults(connection=connection): acl.save() self.assertEqual(list(acl), []) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/testing') self.assertEqual(kw[0]['data'], {'acl': []}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_get_items_from_response_non_empty(self): from gcloud.storage.blob import Blob from gcloud.storage._testing import _monkey_defaults BLOB_NAME = 'blob-name' response = {'items': [{'name': BLOB_NAME}], 'prefixes': ['foo']} connection = _Connection() bucket = _Bucket() with _monkey_defaults(connection=connection): iterator = self._makeOne(bucket) blobs = list(iterator.get_items_from_response(response)) self.assertEqual(len(blobs), 1) blob = blobs[0] self.assertTrue(isinstance(blob, Blob)) self.assertEqual(blob.name, BLOB_NAME) self.assertEqual(iterator.prefixes, ('foo',))
def test_rename_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults BLOB_NAME = 'blob-name' NEW_NAME = 'new-name' connection = _Connection() bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 with _monkey_defaults(connection=connection): new_blob = blob.rename(NEW_NAME) self.assertEqual(blob.name, BLOB_NAME) self.assertEqual(new_blob.name, NEW_NAME) self.assertFalse(BLOB_NAME in bucket._blobs) self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) self.assertTrue(NEW_NAME in bucket._blobs)
def test_reload_missing_w_implicit_connection(self): # https://github.com/GoogleCloudPlatform/gcloud-python/issues/652 from gcloud.storage._testing import _monkey_defaults ROLE = 'role' connection = _Connection({}) acl = self._makeOne() acl.reload_path = '/testing/acl' acl.loaded = True acl.entity('allUsers', ROLE) with _monkey_defaults(connection=connection): acl.reload() self.assertEqual(list(acl), []) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'GET') self.assertEqual(kw[0]['path'], '/testing/acl')
def test_reload_nonempty_result_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults ROLE = 'role' connection = _Connection( {'items': [{'entity': 'allUsers', 'role': ROLE}]}) acl = self._makeOne() acl.reload_path = '/testing/acl' acl.loaded = True with _monkey_defaults(connection=connection): acl.reload() self.assertTrue(acl.loaded) self.assertEqual(list(acl), [{'entity': 'allUsers', 'role': ROLE}]) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'GET') self.assertEqual(kw[0]['path'], '/testing/acl')
def test_reload_empty_result_clears_local_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults ROLE = 'role' connection = _Connection({'items': []}) acl = self._makeOne() acl.reload_path = '/testing/acl' acl.loaded = True acl.entity('allUsers', ROLE) with _monkey_defaults(connection=connection): acl.reload() self.assertTrue(acl.loaded) self.assertEqual(list(acl), []) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'GET') self.assertEqual(kw[0]['path'], '/testing/acl')
def test_save_no_arg_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults ROLE = 'role' AFTER = [{'entity': 'allUsers', 'role': ROLE}] connection = _Connection({'acl': AFTER}) acl = self._makeOne() acl.save_path = '/testing' acl.loaded = True acl.entity('allUsers').grant(ROLE) with _monkey_defaults(connection=connection): acl.save() self.assertEqual(list(acl), AFTER) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/testing') self.assertEqual(kw[0]['data'], {'acl': AFTER}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_clear_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults ROLE1 = 'role1' ROLE2 = 'role2' STICKY = {'entity': 'allUsers', 'role': ROLE2} connection = _Connection({'acl': [STICKY]}) acl = self._makeOne() acl.save_path = '/testing' acl.loaded = True acl.entity('allUsers', ROLE1) with _monkey_defaults(connection=connection): acl.clear() self.assertEqual(list(acl), [STICKY]) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/testing') self.assertEqual(kw[0]['data'], {'acl': []}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_make_public_w_implicit_ocnnection(self): from gcloud.storage.acl import _ACLEntity from gcloud.storage._testing import _monkey_defaults BLOB_NAME = 'blob-name' permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive} connection = _Connection(after) bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob.acl.loaded = True with _monkey_defaults(connection=connection): blob.make_public() self.assertEqual(list(blob.acl), permissive) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/b/name/o/%s' % BLOB_NAME) self.assertEqual(kw[0]['data'], {'acl': permissive}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_make_public_defaults(self): from gcloud.storage.acl import _ACLEntity from gcloud.storage._testing import _monkey_defaults NAME = 'name' permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after) bucket = self._makeOne(NAME) bucket.acl.loaded = True bucket.default_object_acl.loaded = True with _monkey_defaults(connection=connection): bucket.make_public() self.assertEqual(list(bucket.acl), permissive) self.assertEqual(list(bucket.default_object_acl), []) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/b/%s' % NAME) self.assertEqual(kw[0]['data'], {'acl': after['acl']}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_make_public_defaults(self): from gcloud.storage.acl import _ACLEntity from gcloud.storage._testing import _monkey_defaults NAME = 'name' permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after) bucket = self._makeOne(NAME, connection) bucket.acl.loaded = True bucket.default_object_acl.loaded = True with _monkey_defaults(connection=connection): bucket.make_public() self.assertEqual(list(bucket.acl), permissive) self.assertEqual(list(bucket.default_object_acl), []) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/b/%s' % NAME) self.assertEqual(kw[0]['data'], {'acl': after['acl']}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def test_save_w_arg_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults ROLE1 = 'role1' ROLE2 = 'role2' STICKY = {'entity': 'allUsers', 'role': ROLE2} new_acl = [{'entity': 'allUsers', 'role': ROLE1}] connection = _Connection({'acl': [STICKY] + new_acl}) acl = self._makeOne() acl.save_path = '/testing' acl.loaded = True with _monkey_defaults(connection=connection): acl.save(new_acl) entries = list(acl) self.assertEqual(len(entries), 2) self.assertTrue(STICKY in entries) self.assertTrue(new_acl[0] in entries) kw = connection._requested self.assertEqual(len(kw), 1) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/testing') self.assertEqual(kw[0]['data'], {'acl': new_acl}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'})
def _list_buckets_non_empty_helper(self, project, use_default=False): from six.moves.urllib.parse import parse_qs from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlparse from gcloud._testing import _monkey_defaults as _base_monkey_defaults from gcloud.storage._testing import _monkey_defaults from gcloud.storage.connection import Connection BUCKET_NAME = 'bucket-name' conn = Connection() query_params = urlencode({'project': project, 'projection': 'noAcl'}) BASE_URI = '/'.join([ conn.API_BASE_URL, 'storage', conn.API_VERSION, ]) URI = '/'.join([BASE_URI, 'b?%s' % (query_params, )]) http = conn._http = Http( { 'status': '200', 'content-type': 'application/json' }, '{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME).encode( 'utf-8'), ) if use_default: with _base_monkey_defaults(project=project): with _monkey_defaults(connection=conn): buckets = list(self._callFUT()) else: buckets = list(self._callFUT(project=project, connection=conn)) self.assertEqual(len(buckets), 1) self.assertEqual(buckets[0].name, BUCKET_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertTrue(http._called_with['uri'].startswith(BASE_URI)) self.assertEqual(parse_qs(urlparse(http._called_with['uri']).query), parse_qs(urlparse(URI).query))
def _make_public_w_future_helper(self, default_object_acl_loaded=True): from gcloud.storage.acl import _ACLEntity from gcloud.storage._testing import _monkey_defaults NAME = 'name' permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after1 = {'acl': permissive, 'defaultObjectAcl': []} after2 = {'acl': permissive, 'defaultObjectAcl': permissive} if default_object_acl_loaded: num_requests = 2 connection = _Connection(after1, after2) else: num_requests = 3 # We return the same value for default_object_acl.reload() # to consume. connection = _Connection(after1, after1, after2) bucket = self._makeOne(NAME, connection) bucket.acl.loaded = True bucket.default_object_acl.loaded = default_object_acl_loaded with _monkey_defaults(connection=connection): bucket.make_public(future=True) self.assertEqual(list(bucket.acl), permissive) self.assertEqual(list(bucket.default_object_acl), permissive) kw = connection._requested self.assertEqual(len(kw), num_requests) self.assertEqual(kw[0]['method'], 'PATCH') self.assertEqual(kw[0]['path'], '/b/%s' % NAME) self.assertEqual(kw[0]['data'], {'acl': permissive}) self.assertEqual(kw[0]['query_params'], {'projection': 'full'}) if not default_object_acl_loaded: self.assertEqual(kw[1]['method'], 'GET') self.assertEqual(kw[1]['path'], '/b/%s/defaultObjectAcl' % NAME) # Last could be 1 or 2 depending on `default_object_acl_loaded`. self.assertEqual(kw[-1]['method'], 'PATCH') self.assertEqual(kw[-1]['path'], '/b/%s' % NAME) self.assertEqual(kw[-1]['data'], {'defaultObjectAcl': permissive}) self.assertEqual(kw[-1]['query_params'], {'projection': 'full'})
def _monkey(self, connection): from gcloud.storage._testing import _monkey_defaults return _monkey_defaults(connection=connection)