def test_unauthorized_upload(self): """Test an upload to the wrong token ID.""" token_id = 'url:user:name' cache_id = str(uuid4()) file_storage = self.make_test_file_storage(cache_id, token_id) minio.create_placeholder(cache_id, token_id) with self.assertRaises(exceptions.UnauthorizedAccess): minio.upload_cache(cache_id, token_id + 'x', file_storage) file_storage.stream.close()
def test_missing_cache_download(self): """Test a download to a missing cache ID.""" token_id = 'url:user:name' cache_id = str(uuid4()) minio.create_placeholder(cache_id, token_id) tmp_dir = tempfile.mkdtemp() with self.assertRaises(NoSuchKey): minio.download_cache(cache_id + 'x', token_id, tmp_dir) shutil.rmtree(tmp_dir)
def test_unauthorized_download(self): """Test a download to the wrong token ID.""" token_id = 'url:user:name' cache_id = str(uuid4()) minio.create_placeholder(cache_id, token_id) tmp_dir = tempfile.mkdtemp() with self.assertRaises(exceptions.UnauthorizedAccess): minio.download_cache(cache_id, token_id + 'x', tmp_dir) shutil.rmtree(tmp_dir)
def test_missing_cache_upload(self): """Test an upload to a missing cache ID.""" token_id = 'url:user:name' cache_id = str(uuid4()) file_storage = self.make_test_file_storage(cache_id, token_id) minio.create_placeholder(cache_id, token_id) with self.assertRaises(NoSuchKey): minio.upload_cache(cache_id + 'x', token_id, file_storage) file_storage.stream.close()
def test_cache_delete(self): """Test a valid file deletion.""" token_id = 'url:user:name' cache_id = str(uuid4()) minio.create_placeholder(cache_id, token_id) minio.delete_cache(cache_id, token_id) tmp_dir = tempfile.mkdtemp() with self.assertRaises(NoSuchKey): minio.download_cache(cache_id, token_id, tmp_dir) shutil.rmtree(tmp_dir)
def make_test_file_storage(self, cache_id, token_id): """ Create a FileStorage object for use in minio.cache_upload. FileStorage docs: http://werkzeug.pocoo.org/docs/0.14/datastructures/#others """ filename = 'test.json' minio.create_placeholder(cache_id, token_id) contents = io.BytesIO(b'contents') return FileStorage(filename=filename, stream=contents)
def test_delete_unauthorized_cache(self): """ Test a deletion of a cache entry with a cache created by a different token ID. DELETE /cache/<cache_id> """ cache_id = str(uuid4()) minio.create_placeholder(cache_id, 'test_user') resp = requests.delete(url + '/cache/' + cache_id, headers={'Authorization': 'non_admin_token'}) json = resp.json() self.assertEqual(resp.status_code, 403, 'Status code is 403') self.assertEqual(json['status'], 'error', 'Status is set to "error"') self.assertTrue('You do not have access' in json['error'])
def test_download_cache_file_unauthorized_cache(self): """ Test a call to download a cache file that was made by a different token ID GET /cache/<cache_id> """ cache_id = str(uuid4()) minio.create_placeholder(cache_id, 'test_user') resp = requests.get(url + '/cache/' + cache_id, headers={'Authorization': 'non_admin_token'}) json = resp.json() self.assertEqual(resp.status_code, 403, 'Status code is 403') self.assertEqual(json['status'], 'error', 'Status is set to "error"') self.assertTrue('You do not have access' in json['error'])
def test_upload_cache_file_unauthorized_cache(self): """ Test a call to upload a cache file successfully. POST /cache/<cache_id> """ cache_id = str(uuid4()) minio.create_placeholder(cache_id, 'test_user') resp = requests.post(url + '/cache/' + cache_id, headers={'Authorization': 'non_admin_token'}, files={'file': ('test.json', b'{"x": 1}')}) json = resp.json() self.assertEqual(resp.status_code, 403, 'Status code is 403') self.assertEqual(json['status'], 'error', 'Status is set to "error"') self.assertTrue('You do not have access' in json['error'])
def test_placeholder_creation(self): """Test the creation of a cache file placeholder.""" token_id = 'url:user:name' cache_id = str(uuid4()) minio.create_placeholder(cache_id, token_id) minio.authorize_access(cache_id, token_id) tmp_dir = tempfile.mkdtemp() with self.assertRaises(exceptions.MissingCache): minio.download_cache(cache_id, token_id, tmp_dir) save_path = os.path.join(tmp_dir, 'x') minio.minio_client.fget_object(minio.Config.minio_bucket_name, cache_id, save_path) with open(save_path, 'rb') as fd: contents = fd.read() self.assertEqual(contents, b'') shutil.rmtree(tmp_dir) metadata = minio.get_metadata(cache_id) self.assertTrue(int(metadata['expiration']) > time.time()) self.assertEqual(metadata['filename'], 'placeholder') self.assertEqual(metadata['token_id'], token_id)
def test_cache_upload(self): """Test a valid file upload to a cache ID.""" token_id = 'url:user:name' cache_id = str(uuid4()) minio.create_placeholder(cache_id, token_id) file_storage = self.make_test_file_storage(cache_id, token_id) minio.upload_cache(cache_id, token_id, file_storage) file_storage.stream.close() metadata = minio.get_metadata(cache_id) self.assertTrue( int(metadata['expiration']) > time.time(), 'Expiration is in the future') self.assertEqual(metadata['filename'], file_storage.filename, 'Correct filename is saved in the metadata') self.assertEqual(metadata['token_id'], token_id, 'Correct token ID is saved in the metadata') tmp_dir = tempfile.mkdtemp() save_path = minio.download_cache(cache_id, token_id, tmp_dir) with open(save_path, 'rb') as fd: saved_contents = fd.read().decode('utf-8') self.assertEqual(saved_contents, 'contents', 'Correct file contents uploaded')