def _bucket_with_unauth_locations(self, access_control): log = logging.getLogger("_bucket_with_unauth_locations") access_control_json = json.dumps(access_control) s3_connection = motoboto.S3Emulator() # create the bucket bucket = s3_connection.create_unique_bucket( access_control=access_control_json) self.assertTrue(bucket is not None) # the bucket's authenticated connection should be able to list keys _ = bucket.get_all_keys() # in location an unauthenticated connection should be denied list_access with self.assertRaises(LumberyardHTTPError) as context_manager: _ = _list_keys(bucket.name) self.assertEqual(context_manager.exception.status, 401) # the bucket's authenticated connection should be able to write auth_key_name = "authenticated_key" auth_test_string = "authenticated test string" write_key = Key(bucket) write_key.name = auth_key_name write_key.set_contents_from_string(auth_test_string) self.assertTrue(write_key.exists()) # an unauthenticated connection should also be able to write unauth_key_name = "unauthenticated_key" unauth_test_string = "unauth test string" archive_result = _archive_key_from_string(bucket.name, unauth_key_name, unauth_test_string) self.assertTrue("version_identifier" in archive_result) head_result = _head_key(bucket.name, unauth_key_name) log.info("head_result = {0}".format(head_result)) # the bucket's authenticated connection should be able to read read_key = Key(bucket, auth_key_name) returned_string = read_key.get_contents_as_string() self.assertEqual(returned_string.decode("utf-8"), auth_test_string) # an unauthenticated connection should also be able to read returned_string = _retrieve_key_to_string(bucket.name, unauth_key_name) self.assertEqual(returned_string.decode("utf-8"), unauth_test_string) # the bucket's authenticated connection should be able to delete read_key.delete() # an unauthenticated connection should also be able to delete delete_result = _delete_key(bucket.name, unauth_key_name) self.assertTrue(delete_result["success"]) # delete the bucket s3_connection.delete_bucket(bucket.name) s3_connection.close()
def _bucket_without_unauth_access(self, access_control): if access_control is None: access_control_json = None else: access_control_json = json.dumps(access_control) s3_connection = motoboto.S3Emulator() # create the bucket bucket = s3_connection.create_unique_bucket( access_control=access_control_json) # the bucket's authenticated connection should be able to list keys _ = bucket.get_all_keys() # an unauthenticated connection should be denied list_access with self.assertRaises(LumberyardHTTPError) as context_manager: _ = _list_keys(bucket.name) self.assertEqual(context_manager.exception.status, 401) # the bucket's authenticated connection should be able to write auth_key_name = "authenticated_key" auth_test_string = "authenticated test string" write_key = Key(bucket) write_key.name = auth_key_name write_key.set_contents_from_string(auth_test_string) self.assertTrue(write_key.exists()) # an unauthenticated connection should be denied write_access unauth_key_name = "unauthenticated_key" unauth_test_string = "unauth test string" with self.assertRaises(LumberyardHTTPError) as context_manager: _ = _archive_key_from_string(bucket.name, unauth_key_name, unauth_test_string) self.assertEqual(context_manager.exception.status, 401) # the bucket's authenticated connection should be able to read read_key = Key(bucket, auth_key_name) returned_string = read_key.get_contents_as_string() self.assertEqual(returned_string.decode("utf-8"), auth_test_string) # an unauthenticated connection should be denied read_access with self.assertRaises(LumberyardHTTPError) as context_manager: _ = _retrieve_key_to_string(bucket.name, unauth_key_name) self.assertEqual(context_manager.exception.status, 401) # the bucket's authenticated connection should be able to delete read_key.delete() # an unauthenticated connection should be denied delete_access with self.assertRaises(LumberyardHTTPError) as context_manager: _ = _delete_key(bucket.name, unauth_key_name) self.assertEqual(context_manager.exception.status, 401) # delete the bucket s3_connection.delete_bucket(bucket.name) s3_connection.close()
def xxxtest_key_with_meta(self): """ test simple key with metadata added """ bucket_name = "com-dougfort-test-key-with-meta" key_name = u"test-key" test_string = _random_string(1024) meta_key = u"meta_key" meta_value = "pork" # create the bucket bucket = self._s3_connection.create_bucket(bucket_name) self.assertTrue(bucket is not None) self.assertEqual(bucket.name, bucket_name) # create an empty key write_key = Key(bucket) # set the name write_key.name = key_name # self.assertFalse(write_key.exists()) # set some metadata write_key.set_metadata(meta_key, meta_value) # upload some data write_key.set_contents_from_string(test_string) self.assertTrue(write_key.exists()) # create another key with the same name read_key = Key(bucket, key_name) # read back the data returned_string = read_key.get_contents_as_string() self.assertEqual(returned_string, test_string) # get the metadata returned_meta_value = read_key.get_metadata(meta_key) self.assertEqual(returned_meta_value, meta_value) # delete the key read_key.delete() self.assertFalse(write_key.exists()) # delete the bucket self._s3_connection.delete_bucket(bucket_name)
def test_key_with_strings(self): """ test simple key 'from_string' and 'as_string' functions """ bucket_name = "com-dougfort-test-key-with-strings" key_name = u"test-key" test_string = _random_string(1024) # create the bucket bucket = self._s3_connection.create_bucket(bucket_name) self.assertTrue(bucket is not None) self.assertEqual(bucket.name, bucket_name) # create an empty key write_key = Key(bucket) # set the name write_key.name = key_name # self.assertFalse(write_key.exists()) # upload some data write_key.set_contents_from_string(test_string) self.assertTrue(write_key.exists()) # create another key with the same name read_key = Key(bucket, key_name) # read back the data returned_string = read_key.get_contents_as_string() self.assertEqual(returned_string, test_string) # delete the key read_key.delete() self.assertFalse(write_key.exists()) # delete the bucket self._s3_connection.delete_bucket(bucket_name)