def test_download(self): url = self.get_api_path('/books/%s/download' % str(ObjectId())) response = self.client.get(url, headers=self.jwt_headers) self.assert404(response) self.assertResponseCode(response, 104040) data = self.get_data('book') playbook = self.get_data('playbook') data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) playbook_file = self.get_data('playbook_file') playbook_file['book_id'] = playbook['book_id'] Playbook.insert_one(playbook_file) self.trash += [[Book, book_id], [Playbook, playbook['_id']], [Playbook, playbook_file['_id']]] url = self.get_api_path('/books/%s/download' % str(book_id)) response = self.client.get(url, headers=self.jwt_headers) # Playbook().collection.delete_one({'_id': playbook['_id']}) # Book().collection.delete_one({'_id': book_id}) self.assert200(response) headers = response.headers self.assertEqual(headers['Content-Type'], 'application/zip') assert len(response.get_data()) > 0 response.close()
def check_playbook(book_id): record = book.find_by_id(book_id) if not record: raise Exception('invalid playbook') records = Playbook.find({'book_id': book_id}) for item in records: parent = item.get('parent') if not item.get('parent'): continue p_item = Playbook.find_one({'book_id': book_id, 'path': parent}) if not p_item: p_path = os.path.dirname(parent) p_path = p_path if p_path != '/' else None data = { 'path': parent, 'parent': p_path, 'is_dir': True, 'is_edit': False, 'book_id': book_id, 'role': os.path.basename(parent), 'created_at': time.time(), } meta = Workspace.get_meta(parent) data.update(meta) data['additions'] = meta Playbook.insert_one(data) return True
def test_rename(self): data = self.get_data('book') playbook = self.get_data('playbook') data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [ [Book, book_id], [Playbook, playbook['_id']], ] path = '/playbook/%s/rename' url = self.get_api_path(path % str(ObjectId())) response = self.client.patch(url, data=self.body({'path': ''}), headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 104001) file_path = os.path.join('newpath', playbook.get('path')) response = self.client.patch(url, data=self.body({'path': file_path}), headers=self.jwt_headers) self.assert404(response) self.assertResponseCode(response, 104040) url = self.get_api_path(path % str(playbook['_id'])) response = self.client.patch(url, data=self.body({'path': file_path}), headers=self.jwt_headers) self.assert200(response)
def test_delete_book(self): data = self.get_data('book') playbook = self.get_data('playbook') data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [ [Book, book_id], [Playbook, playbook['_id']], ] url = self.get_api_path('/books/' + str(book_id)) not_found_url = self.get_api_path('/books/' + str(ObjectId())) response = self.client.delete(not_found_url, headers=self.jwt_headers) self.assert404(response) self.assertResponseCode(response, 154041) response = self.client.delete(url, headers=self.jwt_headers) self.assert200(response) record = Book.find_by_id(book_id) self.assertEqual(record, None) record = Book().collection.find_one({'_id': book_id}) self.assertIsNotNone(record) self.assertEqual(record['_id'], book_id) self.assertEqual(record['status'], -1) playbook_record = Playbook().collection.find_one( {'_id': playbook['_id']}) self.assertIsNotNone(playbook_record) self.assertEqual(playbook_record['status'], -1)
def test_delete_configuration(self): configuration = self.get_data('configuration') playbook = self.get_data('playbook') configuration['name'] = str(uuid.uuid4()) Configuration.insert_one(configuration) playbook['register'] = [str(configuration['_id'])] Playbook.insert_one(playbook) self.trash += [ [Configuration, configuration['_id']], [Playbook, playbook['_id']], ] path = '/configurations/%s' % str(ObjectId()) url = self.get_api_path(path) response = self.client.delete(url, headers=self.jwt_headers) self.assert404(response) self.assertResponseCode(response, 104040) path = '/configurations/%s' % str(configuration['_id']) url = self.get_api_path(path) response = self.client.delete(url, headers=self.jwt_headers) self.assert403(response) self.assertResponseCode(response, 104038) Playbook().collection.delete_one({'_id': playbook['_id']}) path = '/configurations/%s' % str(configuration['_id']) url = self.get_api_path(path) response = self.client.delete(url, headers=self.jwt_headers) self.assert200(response) Configuration().collection.delete_one({'_id': configuration['_id']})
def add_folder(): params = request.get_json() if not params or not params.get('id') or not params.get('folder'): return jsonify({ 'message': 'illegal param', 'code': 104000, }), 400 record_id = params.get('id') folder = params.get('folder') parent = params.get('parent') book_id = params.get('book_id') parent = parent if parent != '.' else '/' parent_path = None if parent != '/': parent_record = Playbook.find_one({ '_id': ObjectId(record_id), 'is_dir': True }) if not parent_record: return jsonify({ 'message': 'invalid params', 'code': 104001, }), 400 parent_path = parent_record.get('path') file_path = os.path.join(parent, folder) record = { 'path': file_path, 'book_id': book_id, 'parent': parent_path, 'is_dir': True, 'content': '', 'is_edit': False, 'add_by': login_user.get('username'), 'created_at': int(time.time()), 'updated_at': datetime.datetime.now().isoformat(), } meta = get_meta(file_path) record.update(meta) record['additions'] = meta check = Playbook.find_one({'book_id': book_id, 'path': record['path']}) if check: additions = check.get('additions') or {} additions.update(meta) record['additions'] = additions Playbook.update_one({'_id': check['_id']}, {'$set': record}) else: Playbook.insert_one(record) return jsonify({ 'message': 'ok', 'code': 0, })
def test_edit_file(self): data = self.get_data('book') playbook = self.get_data('playbook') data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [ [Book, book_id], [Playbook, playbook['_id']], ] path = '/playbook/%s/file' url = self.get_api_path(path % str(ObjectId())) response = self.client.put(url, data=self.body({}), headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 154000) url = self.get_api_path(path % str(ObjectId())) params = playbook.copy() params.pop('_id') register_id = str(ObjectId()) update = { 'description': 'big jet plane', 'is_edit': False, 'register': [register_id] } params.update(update) response = self.client.put(url, data=self.body(params), headers=self.jwt_headers) self.assert404(response) url = self.get_api_path(path % str(playbook['_id'])) with patch('eclogue.api.playbook.Configuration') as config_mock: config_mock.find.return_value = None response = self.client.put(url, data=self.body(params), headers=self.jwt_headers) self.assert404(response) self.assertResponseCode(response, 154042) config_mock.find.return_value = {'_id': register_id} with patch('eclogue.api.playbook.Workspace') as wk_mock: response = self.client.put(url, data=self.body(params), headers=self.jwt_headers) self.assert200(response) wk_mock.assert_called() wk_mock.return_value.write_book_file.assert_called()
def test_add_folder(self): data = self.get_data('book') playbook = self.get_data('playbook') playbook['is_dir'] = True data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [ [Book, book_id], [Playbook, playbook['_id']], ] path = '/playbook/folder' url = self.get_api_path(path) response = self.client.post(url, data=self.body({ 'id': '', 'folder': '' }), headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 104000) params = { 'id': str(ObjectId()), 'folder': 'test', 'parent': playbook['path'], 'book_id': str(book_id), } response = self.client.post(url, data=self.body(params), headers=self.jwt_headers) self.assert400(response) params['id'] = str(playbook['_id']) response = self.client.post(url, data=self.body(params), headers=self.jwt_headers) self.assert200(response) response = self.client.post(url, data=self.body(params), headers=self.jwt_headers) self.assert200(response)
def install_book_from_dir(dirname, book_id): wk = Workspace() bucket = wk.import_book_from_dir(dirname, book_id) playbooks = Playbook.find({'book_id': book_id}) files = map(lambda i: i['path'], bucket) paths = map(lambda i: i['path'], playbooks) diff = list(set(paths) - set(files)) for item in diff: Playbook.delete_one({'book_id': book_id, 'path': item}) mapping = {} map(lambda i: {mapping['path']: i}, playbooks) for item in bucket: record = mapping.get(item['path']) if not record: Playbook.insert_one(item) continue if record['additions']: item['additions'].update(record['additions']) Playbook.update_one({'_id': record['_id']}, {'$set': item})
def test_get_playbook(self): book = self.get_data('book') playbook = self.get_data('playbook') result = Book.insert_one(book.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [[Book, book_id], [Playbook, playbook['_id']]] url = self.get_api_path('/books/%s/playbook' % str(ObjectId())) response = self.client.get(url, headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 154001) Book.update_one({'_id': book_id}, {'$set': {'status': -1}}) url = self.get_api_path('/books/%s/playbook' % str(book_id)) response = self.client.get(url, headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 154001) Book.update_one({'_id': book_id}, {'$set': {'status': 1}}) url = self.get_api_path('/books/%s/playbook' % str(book_id)) response = self.client.get(url, headers=self.jwt_headers) self.assert200(response)
def check_playbook_node(node): """ loop check playbook parent node @param {dict} node """ parent_path = os.path.dirname(node['path']) record = Playbook.find_one({ 'book_id': node['book_id'], 'path': parent_path }) if not record: current = node.copy() current['path'] = parent_path current['parent'] = None if parent_path == '/' else os.path.dirname( parent_path) Playbook.insert_one(current) return check_playbook_node(current) where = { 'book_id': node['book_id'], 'path': node['path'], } return Playbook.update_one(where, node, upsert=True)
def test_get_playbook(self): data = self.get_data('book') playbook = self.get_data('playbook') data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [ [Book, book_id], [Playbook, playbook['_id']], ] url = self.get_api_path('/books/%s/playbook' % str(ObjectId())) query = {'current': str(playbook['_id'])} response = self.client.get(url, query_string=query, headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 154001) url = self.get_api_path('/books/%s/playbook' % playbook['book_id']) Book.update_one({'_id': book_id}, {'$set': {'status': -1}}) response = self.client.get(url, query_string=query, headers=self.jwt_headers) self.assert400(response) self.assertResponseCode(response, 154001) Book.update_one({'_id': book_id}, {'$set': {'status': 1}}) url = self.get_api_path('/books/%s/playbook' % playbook['book_id']) response = self.client.get(url, query_string=query, headers=self.jwt_headers) self.assert200(response) result = response.json data = result.get('data') check = map(lambda i: str(i['_id']), data) check = list(check) assert str(playbook['_id']) in check
def upload_playbook(_id): files = request.files record = Book.find_by_id(_id) if not record: return jsonify({ "message": "book not found", "code": 104004, }), 400 if not files: return jsonify({ 'message': 'invalid files params', 'code': 104001 }), 400 file = files['file'] filename = file.filename.lstrip('/') path_list = filename.split('/') filename = '/'.join(path_list[1:]) filename = '/' + filename home_path, basename = os.path.split(filename) file_list = set(_make_path(filename)) for dirname in file_list: check = Playbook.find_one({ 'book_id': _id, 'path': dirname, }) if not check: parent_path, name = os.path.split(dirname) parent_path = parent_path if parent_path != '/' else None parent = { 'path': dirname, 'is_dir': True, 'is_edit': False, 'book_id': _id, 'parent': parent_path, 'name': name, 'created_at': time.time(), } meta = get_meta(dirname) parent.update(meta) parent['additions'] = meta Playbook.insert_one(parent) data = { 'path': filename, 'is_dir': False, 'parent': home_path or None, 'book_id': _id } can_edit = is_edit(file) if not can_edit: file_id = db.save_file(filename=filename, fileobj=file) data['file_id'] = file_id else: content = file.stream.read() content = content.decode('utf-8') data['is_encrypt'] = Vault.is_encrypted(content) if data['is_encrypt']: # @todo vault password vault = Vault() data['content'] = vault.encrypt_string(content) data['md5'] = md5(content) else: data['content'] = content data['md5'] = md5(content) meta = get_meta(data['path']) data.update(meta) data['additions'] = meta data['is_edit'] = can_edit data['created_at'] = time.time() data['updated_at'] = time.time() Playbook.update_one({ 'path': filename, 'book_id': _id }, {'$set': data}, upsert=True) return jsonify({ "message": "ok", "code": 0, })
def test_upload(self): data = self.get_data('book') playbook = self.get_data('playbook') data['name'] = str(uuid.uuid4()) result = Book.insert_one(data.copy()) book_id = result.inserted_id playbook['book_id'] = str(book_id) Playbook.insert_one(playbook) self.trash += [ [Book, book_id], [Playbook, playbook['_id']], ] headers = self.jwt_headers.copy() headers.update({'Content-Type': 'multipart/form-data'}) url = self.get_api_path('/playbook/upload') response = self.client.post(url, headers=headers) self.assert400(response) self.assertResponseCode(response, 104000) url = self.get_api_path('/playbook/upload') form = {'parent': '/', 'bookId': str(ObjectId())} response = self.client.post(url, data=form, headers=headers) self.assert400(response) self.assertResponseCode(response, 104001) stream = BytesIO(bytes('mock test', 'utf-8')) form['files'] = (stream, 'test.yaml') response = self.client.post(url, data=form, headers=headers) self.assert404(response) self.assertResponseCode(response, 104040) params = form.copy() stream = BytesIO(bytes('mock test', 'utf-8')) params['files'] = (stream, 'test.yaml') params['parent'] = ObjectId() response = self.client.post(url, data=params, headers=headers, content_type='multipart/form-data') self.assert400(response) self.assertResponseCode(response, 104004) stream = BytesIO(bytes('mock test', 'utf-8')) form['files'] = (stream, 'test.yaml') form['bookId'] = book_id response = self.client.post(url, data=form, headers=headers, content_type='multipart/form-data') self.assert200(response) record = Playbook.find_one({ 'book_id': str(book_id), '_id': { '$ne': playbook['_id'] } }) assert record is not None db.fs().delete(record.get('file_id')) with patch('eclogue.api.playbook.is_edit') as build_mock: build_mock.return_value = False stream = BytesIO(bytes('mock test', 'utf-8')) form['files'] = (stream, 'binary.mock') response = self.client.post(url, data=form, headers=headers, content_type='multipart/form-data') self.assert200(response) record = Playbook.find_one({ 'book_id': str(book_id), 'file_id': { '$exists': True } }) assert record is not None db.fs().delete(record.get('file_id')) Playbook().collection.delete_many({'book_id': str(book_id)})