def delete(self, **kwargs): """Delete the script file""" organization = kwargs['organization'] team = kwargs['team'] script = request.json.get('file', None) if not script: return response_message(EINVAL, 'Field file is required'), 400 if not is_path_secure(script): return response_message( EINVAL, 'Referencing to Upper level directory is not allowed'), 401 dirname = os.path.dirname(script) basename = os.path.basename(script) script_type = request.json.get('script_type', None) if script_type is None: return response_message(EINVAL, 'Field script_type is required'), 400 if script_type == 'test_scripts': root = get_user_scripts_root(team=team, organization=organization) elif script_type == 'test_libraries': root = get_back_scripts_root(team=team, organization=organization) else: return response_message(EINVAL, 'Unsupported script type ' + script_type), 400 if not os.path.exists(root / script): return response_message(ENOENT, 'File/directory doesn\'t exist'), 404 if os.path.isfile(root / script): try: os.remove(root / script) except OSError as err: current_app.logger.exception(err) return response_message( EIO, 'Error happened while deleting the file'), 401 if script_type == 'test_scripts': Test.objects(test_suite=os.path.splitext(basename)[0], path=dirname).delete() else: if script_type == 'test_scripts': cnt = Test.objects(path__startswith=dirname).delete() if cnt == 0: current_app.logger.error( f'Test suite not found in the database under the path {dirname}' ) try: shutil.rmtree(root / script) except OSError as err: current_app.logger.exception(err) return response_message( EIO, 'Error happened while deleting the directory'), 401 return response_message(SUCCESS)
async def delete(self, request): organization = request.ctx.organization team = request.ctx.team script = request.json.get('file', None) if not script: return json(response_message(EINVAL, 'Field file is required')) if not is_path_secure(script): return json(response_message(EINVAL, 'Referencing to Upper level directory is not allowed')) dirname = os.path.dirname(script) basename = os.path.basename(script) script_type = request.json.get('script_type', None) if script_type is None: return json(response_message(EINVAL, 'Field script_type is required')) if script_type == 'test_scripts': root = await get_user_scripts_root(team=team, organization=organization) elif script_type == 'test_libraries': root = await get_back_scripts_root(team=team, organization=organization) else: return json(response_message(EINVAL, 'Unsupported script type ' + script_type)) if not await async_exists(root / script): return json(response_message(ENOENT, 'File/directory doesn\'t exist')) if await async_isfile(root / script): try: await aiofiles.os.remove(root / script) except OSError as err: logger.exception(err) return json(response_message(EIO, 'Error happened while deleting the file')) if script_type == 'test_scripts': cnt = 0 async for test in Test.find({'test_suite': os.path.splitext(basename)[0], 'path': dirname, 'organization': organization.pk, 'team': team.pk if team else None}): await test.delete() cnt += 1 if cnt == 0: return json(response_message(ENOENT, 'Test suite not found in the database')) else: if script_type == 'test_scripts': cnt = 0 async for test in Test.find({'organization': organization.pk, 'team': team.pk if team else None}): if test.path.startswith(dirname): await test.delete() cnt += 1 if cnt == 0: logger.error(f'Test suite not found in the database under the path {dirname}') try: await async_rmtree(root / script) except OSError as err: logger.exception(err) return json(response_message(EIO, 'Error happened while deleting the directory')) return json(response_message(SUCCESS))
def post(self, **kwargs): """ Upload the scripts Note: Files in the form request payload are tuples of file name and file content which can't be explicitly listed here. Please check out the form data format on the web. Usually browser will take care of it. """ found = False organization = kwargs['organization'] team = kwargs['team'] user = kwargs['user'] script_type = request.form.get('script_type', None) if script_type is None: return response_message(EINVAL, 'Field script_type is required'), 400 if script_type == 'test_scripts': root = get_user_scripts_root(team=team, organization=organization) elif script_type == 'test_libraries': root = get_back_scripts_root(team=team, organization=organization) else: return response_message(EINVAL, 'Unsupported script type ' + script_type), 400 if not os.path.exists(root): os.mkdir(root) for name, file in request.files.items(): if not is_path_secure(file.filename): return response_message( EINVAL, 'Referencing to Upper level directory is not allowed'), 401 found = True filename = root / file.filename file.save(str(filename)) if not found: return response_message(EINVAL, 'No files are found in the request'), 401 if script_type == 'test_scripts': for name, file in request.files.items(): if not file.filename.endswith('.md'): continue ret = db_update_test(root, file.filename, user, organization, team) if not ret: return response_message(UNKNOWN_ERROR, 'Failed to update test suite'), 401
async def handler(request): found = False organization = request.ctx.organization team = request.ctx.team user = request.ctx.user script_type = request.form.get('script_type', None) if script_type is None: return json(response_message(EINVAL, 'Field script_type is required')) if script_type == 'test_scripts': root = await get_user_scripts_root(team=team, organization=organization) elif script_type == 'test_libraries': root = await get_back_scripts_root(team=team, organization=organization) else: return json(response_message(EINVAL, 'Unsupported script type ' + script_type)) if not await async_exists(root): await aiofiles.os.mkdir(root) for name, file in request.files.items(): if not is_path_secure(file.name): return json(response_message(EINVAL, 'saving file with an illegal file name')) found = True async with aiofiles.open(root / file.name, 'wb') as f: await f.write(file.body) if not found: return json(response_message(EINVAL, 'No files are found in the request')) if script_type == 'test_scripts': for name, file in request.files.items(): if not file.name.endswith('.md'): continue ret = await db_update_test(root, file.name, user, organization, team) if not ret: return json(response_message(UNKNOWN_ERROR, 'Failed to update test suite')) return json(response_message(SUCCESS))
def post(self, **kwargs): """Update the script file content""" script = request.json.get('file', None) if not script: return response_message(EINVAL, 'Field file is required'), 400 if not is_path_secure(script): return response_message(EINVAL, 'Illegal file path'), 401 new_name = request.json.get('new_name', None) if new_name: if not is_path_secure(new_name): return response_message( EINVAL, 'Referencing to Upper level directory is not allowed'), 401 script_type = request.json.get('script_type', None) if script_type is None: return response_message(EINVAL, 'Field script_type is required'), 400 content = request.json.get('content', None) if content is None and new_name is None: return response_message(EINVAL, 'Field content is required'), 400 organization = kwargs['organization'] team = kwargs['team'] user = kwargs['user'] package = None if script_type == 'test_scripts': root = get_user_scripts_root(team=team, organization=organization) elif script_type == 'test_libraries': root = get_back_scripts_root(team=team, organization=organization) else: return response_message(EINVAL, 'Unsupported script type ' + script_type), 400 dirname = os.path.dirname(script) basename = os.path.basename(script) if script_type == 'test_scripts' and basename.endswith('.md'): test = Test.objects(test_suite=os.path.splitext(basename)[0], path=dirname).first() elif script_type == 'test_libraries' and dirname: package = Package.objects(py_packages=dirname).first() if not package: return response_message(ENOENT, 'package not found'), 404 try: os.makedirs(root / dirname) except FileExistsError: pass if content or content == '': if script_type == 'test_scripts': content = re.sub(r'\\([{}*_\.])', r'\1', content) elif script_type == 'test_libraries': content = re.sub(r'\r\n', '\n', content) if basename: with open(root / script, 'w', encoding='utf-8') as f: f.write(content) if new_name: if basename: new_path = os.path.join(dirname, new_name) os.rename(root / script, root / new_path) if script_type == 'test_scripts' and test: test.modify(test_suite=os.path.splitext(new_name)[0]) else: os.rename(root / script, root / os.path.dirname(dirname) / new_name) if basename and script_type == 'test_scripts': _script = str(Path(dirname) / new_name) if new_name else script if _script.endswith('.md'): ret = db_update_test(root, _script, user, organization, team) if not ret: return response_message(UNKNOWN_ERROR, 'Failed to update test suite'), 401 if script_type == 'test_libraries' and package: package.modify(modified=True) return response_message(SUCCESS)
async def post(self, request): organization = request.ctx.organization team = request.ctx.team found = False temp_id = request.form.get('resource_id', None) if not temp_id: temp_id = str(ObjectId()) await aiofiles.os.mkdir(request.app.config.UPLOAD_ROOT / temp_id) upload_root = request.app.config.UPLOAD_ROOT / temp_id for name, file in request.files.items(): if not is_path_secure(file.name): return json( response_message(EINVAL, 'saving file with an illegal file name')) found = True async with aiofiles.open(upload_root / file.name, 'wb') as f: await f.write(file.body) files = request.form.getlist('file') if len(files) > 0: retrigger_task_id = request.form.get('retrigger_task', None) if not retrigger_task_id: return json( response_message(EINVAL, 'Field retrigger_task is required')) retrigger_task = await Task.find_one( {'_id': ObjectId(retrigger_task_id)}) if not retrigger_task: return json( response_message(ENOENT, 'Re-trigger task not found')) test = await retrigger_task.test.fetch() if test.organization != organization or test.team != team: return json( response_message( EINVAL, 'Re-triggering a task not belonging to your organization/team is not allowed' )) retrigger_task_upload_root = get_upload_files_root(retrigger_task) if not await async_exists(retrigger_task_upload_root): return json( response_message( ENOENT, 'Re-trigger task upload directory does not exist')) for f in files: try: await async_copy(retrigger_task_upload_root / f, upload_root) found = True except FileNotFoundError: await async_rmtree(upload_root) return json( response_message( ENOENT, 'File {} used in the re-triggered task not found'. format(f))) if not found: return json( response_message(ENOENT, 'No files are found in the request')) return json(response_message(SUCCESS, resource_id=temp_id))
async def post(self, request): script = request.json.get('file', None) if not script: return json(response_message(EINVAL, 'Field file is required')) if not is_path_secure(script): return json(response_message(EINVAL, 'Illegal file path')) new_name = request.json.get('new_name', None) if new_name: if not is_path_secure(new_name): return json(response_message(EINVAL, 'Illegal new name')) script_type = request.json.get('script_type', None) if script_type is None: return json(response_message(EINVAL, 'Field script_type is required')) content = request.json.get('content', None) if content is None and new_name is None: return json(response_message(EINVAL, 'Field content is required')) organization = request.ctx.organization team = request.ctx.team user = request.ctx.user package = None if script_type == 'test_scripts': root = await get_user_scripts_root(team=team, organization=organization) elif script_type == 'test_libraries': root = await get_back_scripts_root(team=team, organization=organization) else: return json(response_message(EINVAL, 'Unsupported script type ' + script_type)) dirname = os.path.dirname(script).split('/')[0] basename = os.path.basename(script) if script_type == 'test_scripts' and basename.endswith('.md'): test = await Test.find_one({'test_suite': os.path.splitext(basename)[0], 'path': dirname}) if not test: return json(response_message(ENOENT, 'test suite not found')) elif script_type == 'test_libraries' and dirname: package = await Package.find_one({'py_packages': dirname}) if not package: return json(response_message(ENOENT, 'package not found')) if not await async_exists(root / dirname): await async_makedirs(root / dirname) if content or content == '': if script_type == 'test_scripts': content = re.sub(r'\\([{}*_\.])', r'\1', content) elif script_type == 'test_libraries': content = re.sub(r'\r\n', '\n', content) if basename: async with aiofiles.open(root / script, 'w', encoding='utf-8') as f: await f.write(content) if new_name: if basename: new_path = os.path.join(dirname, new_name) await aiofiles.os.rename(root / script, root / new_path) if script_type == 'test_scripts' and test: # not md files test.test_suite = os.path.splitext(new_name)[0] await test.commit() else: await aiofiles.os.rename(root / script, root / os.path.dirname(dirname) / new_name) if basename and script_type == 'test_scripts': _script = str(Path(dirname) / new_name) if new_name else script if _script.endswith('.md'): ret = await db_update_test(root, _script, user, organization, team) if not ret: return json(response_message(UNKNOWN_ERROR, 'Failed to update test suite')) if script_type == 'test_libraries' and package: package.modified = True await package.commit() return json(response_message(SUCCESS))