mirror of
https://github.com/YMFE/yapi.git
synced 2025-01-18 13:04:46 +08:00
chore: getCaseLit
This commit is contained in:
parent
4008501d51
commit
5d11290b9e
@ -124,42 +124,16 @@ class interfaceColController extends baseController {
|
||||
if (!id || id == 0) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 407, 'col_id不能为空')
|
||||
}
|
||||
let resultList = await this.caseModel.list(id, 'all');
|
||||
|
||||
let colData = await this.colModel.get(id);
|
||||
let project = await this.projectModel.getBaseInfo(colData.project_id);
|
||||
|
||||
if (project.project_type === 'private') {
|
||||
if (await this.checkAuth(project._id, 'project', 'view') !== true) {
|
||||
return ctx.body = yapi.commons.resReturn(null, 406, '没有权限');
|
||||
}
|
||||
}
|
||||
|
||||
for (let index = 0; index < resultList.length; index++) {
|
||||
let result = resultList[index].toObject();
|
||||
let data = await this.interfaceModel.get(result.interface_id);
|
||||
if (!data) {
|
||||
await this.caseModel.del(result._id);
|
||||
continue;
|
||||
}
|
||||
let projectData = await this.projectModel.getBaseInfo(data.project_id);
|
||||
result.path = projectData.basepath + data.path;
|
||||
result.method = data.method;
|
||||
result.req_body_type = data.req_body_type;
|
||||
result.req_headers = this.handleParamsValue(data.req_headers, result.req_headers);
|
||||
|
||||
result.res_body_type = data.res_body_type;
|
||||
|
||||
result.req_body_form = this.handleParamsValue(data.req_body_form, result.req_body_form)
|
||||
result.req_query = this.handleParamsValue(data.req_query, result.req_query)
|
||||
result.req_params = this.handleParamsValue(data.req_params, result.req_params)
|
||||
resultList[index] = result;
|
||||
}
|
||||
resultList = resultList.sort((a, b) => {
|
||||
return a.index - b.index;
|
||||
});
|
||||
let ctxBody = yapi.commons.resReturn(resultList);
|
||||
ctxBody.colData = colData;
|
||||
ctx.body = ctxBody;
|
||||
ctx.body = await yapi.commons.getCaseList(id);
|
||||
} catch (e) {
|
||||
ctx.body = yapi.commons.resReturn(null, 402, e.message);
|
||||
}
|
||||
@ -577,12 +551,12 @@ class interfaceColController extends baseController {
|
||||
result.path = projectData.basepath + data.path;
|
||||
result.method = data.method;
|
||||
result.req_body_type = data.req_body_type;
|
||||
result.req_headers = this.handleParamsValue(data.req_headers, result.req_headers);
|
||||
result.req_headers = yapi.commons.handleParamsValue(data.req_headers, result.req_headers);
|
||||
result.res_body = data.res_body;
|
||||
result.res_body_type = data.res_body_type;
|
||||
result.req_body_form = this.handleParamsValue(data.req_body_form, result.req_body_form)
|
||||
result.req_query = this.handleParamsValue(data.req_query, result.req_query)
|
||||
result.req_params = this.handleParamsValue(data.req_params, result.req_params)
|
||||
result.req_body_form = yapi.commons.handleParamsValue(data.req_body_form, result.req_body_form)
|
||||
result.req_query = yapi.commons.handleParamsValue(data.req_query, result.req_query)
|
||||
result.req_params = yapi.commons.handleParamsValue(data.req_params, result.req_params)
|
||||
result.interface_up_time = data.up_time;
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
} catch (e) {
|
||||
@ -590,31 +564,7 @@ class interfaceColController extends baseController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} params 接口定义的参数
|
||||
* @param {*} val 接口case 定义的参数值
|
||||
*/
|
||||
handleParamsValue(params, val) {
|
||||
let value = {};
|
||||
try {
|
||||
params = params.toObject();
|
||||
} catch (e) { }
|
||||
if (params.length === 0 || val.length === 0) {
|
||||
return params;
|
||||
}
|
||||
val.forEach((item, index) => {
|
||||
value[item.name] = item;
|
||||
})
|
||||
params.forEach((item, index) => {
|
||||
if (!value[item.name] || typeof value[item.name] !== 'object') return null;
|
||||
params[index].value = value[item.name].value;
|
||||
if (!_.isUndefined(value[item.name].enable)) {
|
||||
params[index].enable = value[item.name].enable
|
||||
}
|
||||
})
|
||||
return params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新一个接口集name或描述
|
||||
|
@ -1,11 +1,25 @@
|
||||
const projectModel = require('../models/project.js');
|
||||
const interfaceColModel = require('../models/interfaceCol.js');
|
||||
const interfaceCaseModel = require('../models/interfaceCase.js');
|
||||
const interfaceModel = require('../models/interface.js');
|
||||
const yapi = require('../yapi.js');
|
||||
const baseController = require('./base.js');
|
||||
|
||||
|
||||
|
||||
class openController extends baseController{
|
||||
constructor(ctx){
|
||||
super(ctx)
|
||||
this.projectModel = yapi.getInst(projectModel)
|
||||
this.interfaceColModel = yapi.getInst(interfaceColModel)
|
||||
this.interfaceCaseModel = yapi.getInst(interfaceCaseModel)
|
||||
this.interfaceModel = yapi.getInst(interfaceModel)
|
||||
|
||||
this.schemaMap = {
|
||||
runAutoTest: {
|
||||
'*id': 'number'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async projectInterfaceData(ctx){
|
||||
@ -13,7 +27,12 @@ class openController extends baseController{
|
||||
}
|
||||
|
||||
async runAutoTest(ctx){
|
||||
ctx.body = 'running...'
|
||||
let id = ctx.params.id;
|
||||
let colData = await this.interfaceColModel.get(id);
|
||||
let projectId = colData.project_id;
|
||||
let projectData = await this.projectModel.get(projectId);
|
||||
|
||||
ctx.body = await yapi.commons.getCaseList(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -448,7 +448,7 @@ let routerConfig = {
|
||||
},{
|
||||
action: "runAutoTest",
|
||||
path: "run_auto_test",
|
||||
method: "post"
|
||||
method: "get"
|
||||
}]
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,10 @@ const path = require('path');
|
||||
const yapi = require('../yapi.js');
|
||||
const sha1 = require('sha1');
|
||||
const logModel = require('../models/log.js');
|
||||
const projectModel = require('../models/project.js');
|
||||
const interfaceColModel = require('../models/interfaceCol.js');
|
||||
const interfaceCaseModel = require('../models/interfaceCase.js');
|
||||
const interfaceModel = require('../models/interface.js');
|
||||
const json5 = require('json5');
|
||||
const _ = require('underscore');
|
||||
const Ajv = require('ajv');
|
||||
@ -376,4 +380,67 @@ exports.createAction = (router, baseurl, routerController, action, path, method,
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} params 接口定义的参数
|
||||
* @param {*} val 接口case 定义的参数值
|
||||
*/
|
||||
function handleParamsValue (params, val){
|
||||
let value = {};
|
||||
try {
|
||||
params = params.toObject();
|
||||
} catch (e) { }
|
||||
if (params.length === 0 || val.length === 0) {
|
||||
return params;
|
||||
}
|
||||
val.forEach((item) => {
|
||||
value[item.name] = item;
|
||||
})
|
||||
params.forEach((item, index) => {
|
||||
if (!value[item.name] || typeof value[item.name] !== 'object') return null;
|
||||
params[index].value = value[item.name].value;
|
||||
if (!_.isUndefined(value[item.name].enable)) {
|
||||
params[index].enable = value[item.name].enable
|
||||
}
|
||||
})
|
||||
return params;
|
||||
}
|
||||
|
||||
exports.handleParamsValue = handleParamsValue;
|
||||
|
||||
exports.getCaseList = async function getCaseList(id) {
|
||||
|
||||
const caseInst = yapi.getInst(interfaceCaseModel)
|
||||
const colInst = yapi.getInst(interfaceColModel)
|
||||
const projectInst = yapi.getInst(projectModel)
|
||||
const interfaceInst = yapi.getInst(interfaceModel)
|
||||
|
||||
let resultList = await caseInst.list(id, 'all');
|
||||
let colData = await colInst.get(id);
|
||||
for (let index = 0; index < resultList.length; index++) {
|
||||
let result = resultList[index].toObject();
|
||||
let data = await interfaceInst.get(result.interface_id);
|
||||
if (!data) {
|
||||
await caseInst.del(result._id);
|
||||
continue;
|
||||
}
|
||||
let projectData = await projectInst.getBaseInfo(data.project_id);
|
||||
result.path = projectData.basepath + data.path;
|
||||
result.method = data.method;
|
||||
result.req_body_type = data.req_body_type;
|
||||
result.req_headers = handleParamsValue(data.req_headers, result.req_headers);
|
||||
result.res_body_type = data.res_body_type;
|
||||
result.req_body_form = handleParamsValue(data.req_body_form, result.req_body_form)
|
||||
result.req_query = handleParamsValue(data.req_query, result.req_query)
|
||||
result.req_params = handleParamsValue(data.req_params, result.req_params)
|
||||
resultList[index] = result;
|
||||
}
|
||||
resultList = resultList.sort((a, b) => {
|
||||
return a.index - b.index;
|
||||
});
|
||||
let ctxBody = yapi.commons.resReturn(resultList);
|
||||
ctxBody.colData = colData;
|
||||
return ctxBody;
|
||||
}
|
Loading…
Reference in New Issue
Block a user