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
def post(self, **kwargs): 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 error_message(EINVAL, 'Field script_type is required'), 400 script_type = request.form.get('script_type', None) if script_type is None: return error_message(EINVAL, 'Field script_type is required'), 400 if script_type == 'user_scripts': root = get_user_scripts_root(team=team, organization=organization) elif script_type == 'backing_scripts': root = get_back_scripts_root(team=team, organization=organization) else: return error_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 '..' in file.filename: return error_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 error_message(ENOENT, 'No files are found in the request'), 404 if script_type == 'user_scripts': for name, file in request.files.items(): ret = db_update_test(scripts_dir=root, script=file.filename, user=user.email, organization=organization, team=team) if ret: return error_message(UNKNOWN_ERROR, 'Failed to update test suite'), 401
def event_handler_update_user_script(event): script = event.message['script'] user = event.message['user'] db_update_test(script=script, user=user)
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)
def post(self, **kwargs): script = request.json.get('file', None) if script is None or script == '': return error_message(EINVAL, 'Field file is required'), 400 if '..' in script: return error_message( EINVAL, 'Referencing to Upper level directory is not allowed'), 401 new_name = request.json.get('new_name', None) if new_name: if '..' in new_name: return error_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 error_message(EINVAL, 'Field script_type is required'), 400 content = request.json.get('content', None) if content is None and new_name is None: return error_message(EINVAL, 'Field content is required'), 400 organization = kwargs['organization'] team = kwargs['team'] user = kwargs['user'] if script_type == 'user_scripts': root = get_user_scripts_root(team=team, organization=organization) elif script_type == 'backing_scripts': root = get_back_scripts_root(team=team, organization=organization) else: return error_message(EINVAL, 'Unsupported script type ' + script_type), 400 if content: if script_type == 'user_scripts': content = re.sub(r'\\([{}_])', r'\1', content) elif script_type == 'backing_scripts': content = re.sub(r'\r\n', '\n', content) dirname = os.path.dirname(script) try: os.makedirs(root / dirname) except FileExistsError: pass if dirname != script: with open(root / script, 'w') as f: f.write(content) if new_name: os.rename(root / script, root / os.path.dirname(script) / new_name) if script_type == 'user_scripts': _script = str(Path(os.path.dirname(script)) / new_name) if new_name else script ret = db_update_test(scripts_dir=root, script=_script, user=user.email, organization=organization, team=team) if ret: return error_message(UNKNOWN_ERROR, 'Failed to update test suite'), 401
def event_handler_update_user_script(app, event): # TODO: return script = event.message['script'] user = event.message['user'] db_update_test(script=script, user=user)