def test_it(self): from tempfile import NamedTemporaryFile from gcloud import storage from gcloud.test_credentials import _Monkey bucket = object() class _Connection(object): def get_bucket(self, bucket_name): self._called_With = bucket_name return bucket connection = _Connection() _called_With = [] def get_connection(*args, **kw): _called_With.append((args, kw)) return connection BUCKET = 'bucket' PROJECT = 'project' CLIENT_EMAIL = '*****@*****.**' PRIVATE_KEY = 'SEEkR1t' with _Monkey(storage, get_connection=get_connection): with NamedTemporaryFile() as f: f.write(PRIVATE_KEY) f.flush() found = self._callFUT(BUCKET, PROJECT, CLIENT_EMAIL, f.name) self.assertTrue(found is bucket) self.assertEqual(_called_With, [((PROJECT, CLIENT_EMAIL, f.name), {})]) self.assertEqual(connection._called_With, BUCKET)
def test_it(self): from tempfile import NamedTemporaryFile from gcloud import credentials from gcloud.datastore import SCOPE from gcloud.datastore.connection import Connection from gcloud.datastore.dataset import Dataset from gcloud.test_credentials import _Client from gcloud.test_credentials import _Monkey CLIENT_EMAIL = '*****@*****.**' PRIVATE_KEY = 'SEEkR1t' DATASET_ID = 'DATASET' client = _Client() with _Monkey(credentials, client=client): with NamedTemporaryFile() as f: f.write(PRIVATE_KEY) f.flush() found = self._callFUT(DATASET_ID, CLIENT_EMAIL, f.name) self.assertTrue(isinstance(found, Dataset)) self.assertTrue(isinstance(found.connection(), Connection)) self.assertEqual(found.id(), DATASET_ID) self.assertEqual(client._called_with, {'service_account_name': CLIENT_EMAIL, 'private_key': PRIVATE_KEY, 'scope': SCOPE, })
def test_get_contents_as_string(self): from gcloud.test_credentials import _Monkey from gcloud.storage import key as MUT _CHUNKS = ['abc', 'def'] KEY = 'key' connection = _Connection() bucket = _Bucket(connection) key = self._makeOne(bucket, KEY) with _Monkey(MUT, KeyDataIterator=lambda self: iter(_CHUNKS)): fetched = key.get_contents_as_string() self.assertEqual(fetched, ''.join(_CHUNKS))
def test_make_public_recursive(self): from gcloud.storage.acl import ACL from gcloud.test_credentials import _Monkey from gcloud.storage import iterator from gcloud.storage import bucket as MUT _saved = [] class _Key(object): _granted = False def __init__(self, bucket, name): self._bucket = bucket self._name = name def get_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 _KeyIterator(iterator.KeyIterator): def get_items_from_response(self, response): for item in response.get('items', []): yield _Key(self.bucket, item['name']) NAME = 'name' KEY = 'key' before = {'acl': [], 'defaultObjectAcl': []} permissive = [{'entity': 'allUsers', 'role': ACL.Role.Reader}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after, {'items': [{'name': KEY}]}) bucket = self._makeOne(connection, NAME, before) with _Monkey(MUT, KeyIterator=_KeyIterator): bucket.make_public(recursive=True) self.assertEqual(bucket.metadata, after) self.assertEqual(list(bucket.acl), after['acl']) self.assertEqual(bucket.default_object_acl, None) self.assertEqual(_saved, [(bucket, KEY, 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': after['acl']}) 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'], None)
def test_get_contents_to_file(self): from StringIO import StringIO from gcloud.test_credentials import _Monkey from gcloud.storage import key as MUT _CHUNKS = ['abc', 'def'] KEY = 'key' connection = _Connection() bucket = _Bucket(connection) key = self._makeOne(bucket, KEY) fh = StringIO() with _Monkey(MUT, KeyDataIterator=lambda self: iter(_CHUNKS)): key.get_contents_to_file(fh) self.assertEqual(fh.getvalue(), ''.join(_CHUNKS))
def test_get_contents_to_filename(self): from tempfile import NamedTemporaryFile from gcloud.test_credentials import _Monkey from gcloud.storage import key as MUT _CHUNKS = ['abc', 'def'] KEY = 'key' connection = _Connection() bucket = _Bucket(connection) key = self._makeOne(bucket, KEY) with _Monkey(MUT, KeyDataIterator=lambda self: iter(_CHUNKS)): with NamedTemporaryFile() as f: key.get_contents_to_filename(f.name) f.flush() with open(f.name) as g: wrote = g.read() self.assertEqual(wrote, ''.join(_CHUNKS))
def test_upload_file_object_no_key(self): from gcloud.test_credentials import _Monkey from gcloud.storage import bucket as MUT FILENAME = 'file.txt' FILEOBJECT = MockFile(FILENAME) _uploaded = [] class _Key(object): def __init__(self, bucket, name): self._bucket = bucket self._name = name def set_contents_from_file(self, fh): _uploaded.append((self._bucket, self._name, fh)) bucket = self._makeOne() with _Monkey(MUT, Key=_Key): bucket.upload_file_object(FILEOBJECT) self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT)])
def test_upload_file_explicit_key(self): from gcloud.test_credentials import _Monkey from gcloud.storage import bucket as MUT FILENAME = '/path/to/file' KEY = 'key' _uploaded = [] class _Key(object): def __init__(self, bucket, name): self._bucket = bucket self._name = name def set_contents_from_filename(self, filename): _uploaded.append((self._bucket, self._name, filename)) bucket = self._makeOne() with _Monkey(MUT, Key=_Key): bucket.upload_file(FILENAME, KEY) self.assertEqual(_uploaded, [(bucket, KEY, FILENAME)])
def test_it(self): from tempfile import NamedTemporaryFile from gcloud import credentials from gcloud.storage import SCOPE from gcloud.storage.connection import Connection from gcloud.test_credentials import _Client from gcloud.test_credentials import _Monkey PROJECT = 'project' CLIENT_EMAIL = '*****@*****.**' PRIVATE_KEY = 'SEEkR1t' client = _Client() with _Monkey(credentials, client=client): with NamedTemporaryFile() as f: f.write(PRIVATE_KEY) f.flush() found = self._callFUT(PROJECT, CLIENT_EMAIL, f.name) self.assertTrue(isinstance(found, Connection)) self.assertEqual(found.project, PROJECT) self.assertTrue(found._credentials is client._signed) self.assertEqual(client._called_with, {'service_account_name': CLIENT_EMAIL, 'private_key': PRIVATE_KEY, 'scope': SCOPE, })