Exemplo n.º 1
0
async def get_workstation_projects(token_user: TokenUser = Depends(
    auth_token)) -> BaseRes:
    try:
        project_list: List[AtpProject] = Db.select_by_condition(
            AtpProject, [AtpProject.type == 1, AtpProject.is_delete == 2],
            AtpProject.create_time.desc())
        parted_project_list = []
        for project in project_list:
            is_part_in = Db.select_by_condition(
                AtpProjectMember,
                [
                    AtpProjectMember.is_delete == 2,
                    AtpProjectMember.project_id == project.id,
                    AtpProjectMember.member_id.in_([token_user.user_id])
                ],
            )
            if is_part_in:
                parted_project_list.append(project)
            if project.img:
                file: AtpFileSystemFile = Db.select_by_primary_key(
                    AtpFileSystemFile, project.img)
                project.img = {
                    'id': file.id,
                    'url': get_settings().archive_host + file.name
                }
            else:
                project.img = {
                    'id':
                    0,
                    'url':
                    'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
                }
        session = Db.get_session()
        report_list: List[AtpProjectTestReport] = session.query(
            AtpProjectTestReport).join(
                AtpProject,
                AtpProject.id == AtpProjectTestReport.project_id).filter(
                    AtpProjectTestReport.is_delete == 2,
                    AtpProjectTestReport.creator == token_user.user_id,
                    AtpProject.is_delete == 2).limit(10).all()
        total_projects_num = Db.select_count_by_condition(
            AtpProject.id, [AtpProject.is_delete == 2])
        return BaseRes(
            data={
                'underway_projects': project_list[:6],
                'parted_projects': parted_project_list[:6],
                'my_reports': report_list,
                'total_projects_num': total_projects_num,
                'total_underway_projects_num': len(project_list)
            })
    except Exception as e:
        logger.error(e)
        return BaseRes(status=0, error=str(e))
Exemplo n.º 2
0
async def get_task_by_condition(project_id: int = Query(...),
                                keyword: str = Query(None),
                                relation_type: int = Query(...),
                                filter_type: int = Query(...),
                                token_user: TokenUser = Depends(
                                    auth_token)) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    try:
        task_list_condition: list = [
            AtpOverviewList.is_delete == 2,
            AtpOverviewList.project_id == project_id
        ]
        task_list: List[AtpOverviewList] = Db.select_by_condition(
            AtpOverviewList, task_list_condition, AtpOverviewList.sort)
        for l in task_list:
            task_condition: list = [
                AtpOverviewTask.is_delete == 2,
                AtpOverviewTask.project_id == project_id,
                AtpOverviewTask.list_id == l.id
            ]
            if keyword:
                task_condition.append(
                    AtpOverviewTask.title.like(f'%{keyword}%'))
            if filter_type in [1, 2]:
                task_condition.append(AtpOverviewTask.status == filter_type)
            if relation_type == 2:
                task_condition.append(
                    AtpOverviewTask.creator == token_user.user_id)
            tasks: List[AtpOverviewTask] = Db.select_by_condition(
                AtpOverviewTask, task_condition, AtpOverviewTask.sort)
            # 根据条件找出所有符合条件的, 再筛选我关注的
            if relation_type == 3:
                target_tasks = []
                for t in tasks:
                    if token_user.user_id in json.loads(t.follower):
                        target_tasks.append(t)
                tasks = target_tasks
            l.taskList = tasks
            for t in tasks:
                user: SysUser = Db.select_by_primary_key(
                    SysUser, int(str(t.creator)))
                t.creator = {
                    'id': user.id if user else 0,
                    'cname': user.cname if user else ''
                }
            l.taskList = tasks
        return BaseRes(data=task_list)
    except Exception as e:
        logger.error(traceback.format_exc())
        return BaseRes(status=0, error=str(e))
Exemplo n.º 3
0
async def get_case_by_module_id(project_id: int = Body(..., embed=True),
                                module_id: int = Body(None, embed=True),
                                keyword: str = Body(None,
                                                    embed=True)) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    condition = [
        AtpProjectCase.is_delete == 2, AtpProjectCase.project_id == project_id
    ]
    if module_id:
        condition.append(AtpProjectCase.module_id == module_id)
    if keyword:
        condition.append(AtpProjectCase.name.like(f'%{keyword}%'))
    try:
        case_list: List[AtpProjectCase] = Db.select_by_condition(
            AtpProjectCase, condition, AtpProjectCase.id.desc())
        index = 1
        for case in case_list:
            case.index = index
            index += 1
            case.steps = Db.select_by_condition(AtpProjectCaseStep, [
                AtpProjectCaseStep.is_delete == 2, AtpProjectCaseStep.case_id
                == case.id
            ])
            case.cname = Db.select_by_primary_key(SysUser, case.creator).cname
            case.module_name = Db.select_by_primary_key(
                AtpProjectModule, case.module_id).name
        return BaseRes(data=case_list)
Exemplo n.º 4
0
async def get_project_id(id: int = Query(...)) -> BaseRes:
    _, error = verify_project_deleted(id)
    if error:
        return error
    try:
        project: AtpProject = Db.select_by_primary_key(AtpProject, id)
        member_relation_list: List[AtpProjectMember] = Db.select_by_condition(
            AtpProjectMember,
            [
                AtpProjectMember.project_id == id,
                AtpProjectMember.is_delete == 2
            ]
        )
        if project.img:
            file: AtpFileSystemFile = Db.select_by_primary_key(AtpFileSystemFile, project.img)
            project.img = {
                'id': file.id,
                'url': get_settings().archive_host + file.name
            }
        else:
            project.img = {
                'id': 0,
                'url': 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
            }
        project.member = []
        for member_relation in member_relation_list:
            user: SysUser = Db.select_by_primary_key(SysUser, member_relation.member_id)
            project.member.append({
                'id': user.id,
                'cname': user.cname,
            })
        return BaseRes(data=project)
    except Exception as e:
        logger.error(traceback.format_exc())
        return BaseRes(status=0, error=str(e))
Exemplo n.º 5
0
async def execute_suite(
        request: ExecuteSuite,
        background_tasks: BackgroundTasks,
        token_user: TokenUser = Depends(auth_token)
) -> BaseRes:
    _, error = verify_project_deleted(request.project_id)
    if error:
        return error
    _, error = verify_project_member(token_user.user_id, request.project_id)
    if error:
        return error
    if request.is_use_env and request.env_host:
        env_db_res: AtpProjectEnv = Db.select_by_primary_key(AtpProjectEnv, request.env_host)
        global_host = env_db_res.host
    else:
        if request.request_host:
            global_host = request.request_host
        else:
            global_host = ''
    case_relation: List[AtpProjectApiSuiteCaseRelation] = Db.select_by_condition(
        AtpProjectApiSuiteCaseRelation,
        [
            AtpProjectApiSuiteCaseRelation.project_id == request.project_id,
            AtpProjectApiSuiteCaseRelation.suite_id == request.suite_id,
            AtpProjectApiSuiteCaseRelation.is_delete == 2,
        ],
        AtpProjectApiSuiteCaseRelation.sort
    )
    request_list = []
    for item in case_relation:
        case: AtpProjectApiCase = Db.select_by_primary_key(AtpProjectApiCase, item.case_id)
        case_item = {
            'case_id': case.id,
            'suite_id': request.suite_id,
            'path': case.request_path,
            'params': json.loads(case.request_query),
            'body': json.loads(case.request_body),
            'method': 'Get' if case.method == 1 else 'Post'
        }
        if global_host:
            case_item.setdefault('host', global_host)
        else:
            if case.is_use_env:
                request_host_item: AtpProjectEnv = Db.select_by_primary_key(
                    AtpProjectEnv,
                    case.env_host)
                case_item.setdefault('host', request_host_item.host)
            else:
                case_item.setdefault('host', case.request_host)
        request_headers = json.loads(deepcopy(case.request_headers))
        if request_headers is None:
            request_headers = {}
        if request.global_headers:
            request_headers.update(request.global_headers)
            case_item.setdefault('headers', request_headers)
        else:
            case_item.setdefault('headers', request_headers)
        request_list.append(case_item)
Exemplo n.º 6
0
async def get_env_by_project_id(project_id: int = Query(...)) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    try:
        env_list: List[AtpProjectEnv] = Db.select_by_condition(AtpProjectEnv,
                                                               [AtpProjectEnv.is_delete == 2,
                                                                AtpProjectEnv.project_id == project_id])
        return BaseRes(data=env_list)
    except Exception as e:
        logger.error(traceback.format_exc())
        return BaseRes(status=0, error=str(e))
Exemplo n.º 7
0
async def login(login_user: LoginUser) -> BaseRes:
    user_list = Db.select_by_condition(SysUser, [SysUser.name == login_user.user_name, SysUser.is_delete == '2'])
    if (len(user_list)) == 0:
        return BaseRes(status=0, error='用户名或密码错误')
    else:
        user: SysUser = user_list[0]
        if verify_password(login_user.password, user.password):
            token_data = {'id': user.id, 'cname': user.cname}
            token = create_access_token(data=token_data)
            data = {'token': token, 'type': 'Bearer'}
            return BaseRes(data=data)
        else:
            return BaseRes(status=0, error='用户名或密码错误')
Exemplo n.º 8
0
async def get_all_module(project_id: int = Query(...)) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    try:
        return_data: List[AtpProjectModule] = Db.select_by_condition(
            AtpProjectModule, [
                AtpProjectModule.is_delete == 2, AtpProjectModule.project_id
                == project_id
            ])
        return BaseRes(data=return_data)
    except Exception as e:
        logger.error(e)
        return BaseRes(status=0, error=str(e))
Exemplo n.º 9
0
async def get_api_case_by_condition(
        project_id: int = Query(...),
) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    suite: List[AtpProjectApiSuite] = Db.select_by_condition(
        AtpProjectApiSuite,
        [
            AtpProjectApiSuite.is_delete == 2,
            AtpProjectApiSuite.project_id == project_id
        ]
    )
    return BaseRes(data=suite)
Exemplo n.º 10
0
async def get_all_user() -> BaseRes:
    try:
        user_list: List[SysUser] = Db.select_by_condition(SysUser, [SysUser.is_delete == '2'])
        res_user_list = []
        for user in user_list:
            res_user_list.append({
                'id': user.id,
                'cname': user.cname,
                # 'name':user.name,
                # 'email':user.email,
                # 'phone':user.phone
            })
        data = {'userList': res_user_list}
        return BaseRes(data=data)
    except Exception as e:
        logger.error(traceback.format_exc())
        return BaseRes(status=0, error=str(e))
Exemplo n.º 11
0
async def get_all_report(project_id: int = Query(...)) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    try:
        report_list: List[AtpProjectTestReport] = Db.select_by_condition(
            AtpProjectTestReport, [
                AtpProjectTestReport.is_delete == '2',
                AtpProjectTestReport.project_id == project_id
            ], AtpProjectTestReport.create_time.desc())
        for report in report_list:
            report.cname = Db.select_by_primary_key(SysUser,
                                                    report.creator).cname
        return BaseRes(data=report_list)
    except Exception as e:
        logger.error(e)
        return BaseRes(status=0, error=str(e))
Exemplo n.º 12
0
def verify_project_member(user_id: int, project_id: int):
    user: SysUser = Db.select_by_primary_key(SysUser, user_id)
    if not user:
        return None, BaseRes(status=0, error='用户不存在')
    else:
        member_list: typing.List[AtpProjectMember] = Db.select_by_condition(
            AtpProjectMember, [
                AtpProjectMember.project_id == project_id,
                AtpProjectMember.is_delete == 2
            ])
        is_memeber = False
        for member_item in member_list:
            if member_item.member_id == user.id:
                is_memeber = True
                break
        if is_memeber:
            return True, None
        else:
            return None, BaseRes(status=0, error='非项目成员无权操作')
Exemplo n.º 13
0
async def get_all_project(type: int = Query(...)) -> BaseRes:
    try:
        project_list: List[AtpProject] = Db.select_by_condition(
            AtpProject,
            [AtpProject.is_delete == 2, AtpProject.type == type],
            AtpProject.create_time.desc()
        )
        for project in project_list:
            if project.img:
                file: AtpFileSystemFile = Db.select_by_primary_key(AtpFileSystemFile, project.img)
                project.img = {
                    'id': file.id,
                    'url': get_settings().archive_host + file.name
                }
            else:
                project.img = {
                    'id': 0,
                    'url': 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
                }
        return BaseRes(data=project_list)
    except Exception as e:
        logger.error(traceback.format_exc())
        return BaseRes(status=0, error=str(e))
Exemplo n.º 14
0
async def get_api_case_by_condition(
        project_id: int = Query(...),
        suite_id: int = Query(...),
        page_num: int = Query(...),
        page_size: int = Query(...),
        type: int = Query(...),
        keyword: Optional[str] = Query(None)
) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    try:
        if page_num == 0 and page_size == 0:
            offset = None
            limit = None
        else:
            offset = page_size * (page_num - 1)
            limit = page_size
        condition_list: list = [
            AtpProjectApiCase.is_delete == 2,
            AtpProjectApiCase.project_id == project_id,
            AtpProjectApiCase.suite_id == suite_id
        ]
        if keyword and keyword != '':
            condition_list.append(AtpProjectApiCase.name.like(f'%{keyword}%'))
        if type in [1, 2]:
            condition_list.append(AtpProjectApiCase.method == type)
        api_case_list: List[AtpProjectApiCase] = Db.select_by_condition(
            AtpProjectApiCase,
            condition_list,
            AtpProjectApiCase.id,
            offset,
            limit
        )
        total: int = Db.select_count_by_condition(AtpProjectApiCase.id, condition_list)
        res_list: list = []
        order_id = page_size * (page_num - 1)
        for item in api_case_list:
            item_dict: dict = {}
            order_id += 1
            item_dict.setdefault('id', item.id)
            item_dict.setdefault('order_id', order_id)
            item_dict.setdefault('name', item.name)
            item_dict.setdefault('method', item.method)
            item_dict.setdefault('is_use_env', item.is_use_env)
            item_dict.setdefault('env_host', item.env_host)
            item_dict.setdefault('request_url', item.request_url)
            item_dict.setdefault('request_headers', json.loads(item.request_headers))
            item_dict.setdefault('request_query', json.loads(item.request_query))
            item_dict.setdefault('request_body', item.request_body)
            item_dict.setdefault('suite_id', item.suite_id)
            if item.is_use_env:
                if item.env_host:
                    env: AtpProjectEnv = Db.select_by_primary_key(AtpProjectEnv, item.env_host)
                    item_dict.setdefault('real_host', env.host)
                else:
                    item_dict.setdefault('real_host', None)
            else:
                item_dict.setdefault('real_host', None)
            res_list.append(item_dict)
        return BaseRes(data={'data': res_list, 'total': total})
    except Exception as e:
        logger.error(traceback.format_exc())
        return BaseRes(status=0, error=str(e))
Exemplo n.º 15
0
async def get_report_detail(
        report_id: int = Query(...),
        project_id: int = Query(...),
) -> BaseRes:
    _, error = verify_project_deleted(project_id)
    if error:
        return error
    try:
        report_detail_list: List[
            AtpProjectTestReportDetail] = Db.select_by_condition(
                AtpProjectTestReportDetail, [
                    AtpProjectTestReportDetail.is_delete == '2',
                    AtpProjectTestReportDetail.report_id == report_id
                ], AtpProjectTestReportDetail.id)
        report: AtpProjectTestReport = Db.select_by_primary_key(
            AtpProjectTestReport, report_id)
        suite: AtpProjectApiSuite = Db.select_by_primary_key(
            AtpProjectApiSuite, report.suite_id)
        project: AtpProject = Db.select_by_primary_key(AtpProject,
                                                       report.project_id)
        user: SysUser = Db.select_by_primary_key(SysUser, report.creator)
        status_code_distribution = {}
        for report_detail in report_detail_list:
            case: AtpProjectApiCase = Db.select_by_primary_key(
                AtpProjectApiCase, report_detail.case_id)
            report_detail.case_name = case.name
            if report.global_host:
                report_detail.host = report.global_host
            else:
                if case.is_use_env:
                    report_detail.host = Db.select_by_primary_key(
                        AtpProjectEnv, case.env_host).host
                else:
                    report_detail.host = case.request_host
            report_detail.path = case.request_path
            if report.global_headers:
                init_header = json.loads(deepcopy(case.request_headers))
                if init_header is None:
                    report_detail.headers = json.loads(report.global_headers)
                else:
                    init_header.update(json.loads(report.global_headers))
                    report_detail.headers = init_header
            else:
                report_detail.headers = json.loads(case.request_headers)
            report_detail.params = json.loads(case.request_query)
            report_detail.body = json.loads(case.request_body)
            if status_code_distribution.get(report_detail.status, None):
                status_code_distribution[
                    report_detail.status] = status_code_distribution[
                        report_detail.status] + 1
            else:
                status_code_distribution.setdefault(report_detail.status, 1)
        res = {
            'report_id': report_id,
            'report_name': report.report_name,
            'suite_id': report.suite_id,
            'suite_name': suite.name,
            'project_id': report.project_id,
            'project_name': project.name,
            'global_host': report.global_host,
            'global_headers': report.global_headers,
            'is_save_cookie': report.is_save_cookie,
            'total_case_num': report.total_case_num,
            'success_case_num': report.success_case_num,
            'failed_case_num': report.failed_case_num,
            'status_code_distribution': status_code_distribution,
            'cname': user.cname,
            'detail': report_detail_list
        }
        return BaseRes(data=res)