fix(接口测试): 解决环境配置报错&更改scope后类型异常

This commit is contained in:
RubyLiu 2023-12-22 18:29:50 +08:00 committed by Craftsman
parent 4f542b3d47
commit d3a74dc68d
3 changed files with 238 additions and 131 deletions

View File

@ -1,17 +1,41 @@
<template> <template>
<div> <div>
<el-dialog :close-on-click-modal="false" :title="$t('api_test.environment.environment_config')" <el-dialog
:visible.sync="visible" class="environment-dialog" width="80%" :close-on-click-modal="false"
@close="close" append-to-body destroy-on-close ref="environmentConfig" top="2vh"> :title="$t('api_test.environment.environment_config')"
:visible.sync="visible"
class="environment-dialog"
width="80%"
@close="close"
append-to-body
destroy-on-close
ref="environmentConfig"
top="2vh"
>
<el-container v-loading="result"> <el-container v-loading="result">
<ms-aside-item :enable-aside-hidden="false" :title="$t('api_test.environment.environment_list')" <ms-aside-item
:data="environments" :item-operators="environmentOperators" :add-fuc="addEnvironment" :enable-aside-hidden="false"
:env-add-permission="ENV_CREATE" :title="$t('api_test.environment.environment_list')"
:delete-fuc="openDelEnv" @itemSelected="environmentSelected" ref="environmentItems"/> :data="environments"
<environment-edit :if-create="ifCreate" :environment="currentEnvironment" v-if="visible" :item-operators="environmentOperators"
ref="environmentEdit" :is-read-only="isReadOnly" :add-fuc="addEnvironment"
@confirm="save" :is-project="true" :key="currentEnvironment.id" :env-add-permission="ENV_CREATE"
@close="close" @refreshAfterSave="refresh"> :delete-fuc="openDelEnv"
@itemSelected="environmentSelected"
ref="environmentItems"
/>
<environment-edit
:if-create="ifCreate"
:environment="currentEnvironment"
v-if="visible"
ref="environmentEdit"
:is-read-only="isReadOnly"
@confirm="save"
:is-project="true"
:key="currentEnvironment.id"
@close="close"
@refreshAfterSave="refresh"
>
</environment-edit> </environment-edit>
</el-container> </el-container>
</el-dialog> </el-dialog>
@ -19,14 +43,17 @@
</template> </template>
<script> <script>
import draggable from 'vuedraggable'; import draggable from "vuedraggable";
import MsAsideItem from "../../components/MsAsideItem"; import MsAsideItem from "../../components/MsAsideItem";
import EnvironmentEdit from "../../components/environment/EnvironmentEdit"; import EnvironmentEdit from "../../components/environment/EnvironmentEdit";
import {listenGoBack, removeGoBackListener, getUUID} from "../../utils"; import { listenGoBack, removeGoBackListener, getUUID } from "../../utils";
import {hasPermission} from "../../utils/permission"; import { hasPermission } from "../../utils/permission";
import {Environment, parseEnvironment} from "../../model/EnvironmentModel"; import { Environment, parseEnvironment } from "../../model/EnvironmentModel";
import MsDialogHeader from "../../components/MsDialogHeader"; import MsDialogHeader from "../../components/MsDialogHeader";
import {delApiEnvironment, getEnvironmentByProjectId} from "../../api/environment"; import {
delApiEnvironment,
getEnvironmentByProjectId,
} from "../../api/environment";
import EnvironmentGlobalScript from "./EnvironmentGlobalScript"; import EnvironmentGlobalScript from "./EnvironmentGlobalScript";
import GlobalAssertions from "./assertion/GlobalAssertions"; import GlobalAssertions from "./assertion/GlobalAssertions";
@ -38,75 +65,88 @@ export default {
EnvironmentGlobalScript, EnvironmentGlobalScript,
GlobalAssertions, GlobalAssertions,
draggable, draggable,
MsDialogHeader MsDialogHeader,
}, },
data() { data() {
return { return {
result: false, result: false,
visible: false, visible: false,
projectId: '', projectId: "",
environments: [], environments: [],
currentEnvironment: new Environment(), currentEnvironment: new Environment(),
environmentOperators: [ environmentOperators: [
{ {
icon: 'el-icon-document-copy', icon: "el-icon-document-copy",
func: this.copyEnvironment, func: this.copyEnvironment,
permissions: this.type === 'project' ? permissions:
['PROJECT_ENVIRONMENT:READ+COPY'] : ['WORKSPACE_PROJECT_ENVIRONMENT:READ+COPY'] this.type === "project"
? ["PROJECT_ENVIRONMENT:READ+COPY"]
: ["WORKSPACE_PROJECT_ENVIRONMENT:READ+COPY"],
}, },
{ {
icon: 'el-icon-delete', icon: "el-icon-delete",
func: this.deleteEnvironment, func: this.deleteEnvironment,
permissions: this.type === 'project' ? permissions:
['PROJECT_ENVIRONMENT:READ+DELETE'] : ['WORKSPACE_PROJECT_ENVIRONMENT:READ+DELETE'] this.type === "project"
} ? ["PROJECT_ENVIRONMENT:READ+DELETE"]
: ["WORKSPACE_PROJECT_ENVIRONMENT:READ+DELETE"],
},
], ],
selectEnvironmentId: '', selectEnvironmentId: "",
ifCreate: false, // ifCreate: false, //
currentIndex: -1, currentIndex: -1,
isCopy: false isCopy: false,
} };
}, },
props: { props: {
type: { type: {
type: String, type: String,
default() { default() {
return "project"; return "project";
} },
} },
}, },
computed: { computed: {
ENV_CREATE() { ENV_CREATE() {
return this.type === 'project' ? return this.type === "project"
['PROJECT_ENVIRONMENT:READ+CREATE'] : ['WORKSPACE_PROJECT_ENVIRONMENT:READ+CREATE']; ? ["PROJECT_ENVIRONMENT:READ+CREATE"]
: ["WORKSPACE_PROJECT_ENVIRONMENT:READ+CREATE"];
}, },
ENV_EDIT() { ENV_EDIT() {
return this.type === 'project' ? return this.type === "project"
['PROJECT_ENVIRONMENT:READ+EDIT'] : ['WORKSPACE_PROJECT_ENVIRONMENT:READ+EDIT']; ? ["PROJECT_ENVIRONMENT:READ+EDIT"]
: ["WORKSPACE_PROJECT_ENVIRONMENT:READ+EDIT"];
}, },
isReadOnly() { isReadOnly() {
// / // /
return this.type === 'project' ? return this.type === "project"
!hasPermission('PROJECT_ENVIRONMENT:READ+EDIT') : !hasPermission('WORKSPACE_PROJECT_ENVIRONMENT:READ+EDIT'); ? !hasPermission("PROJECT_ENVIRONMENT:READ+EDIT")
} : !hasPermission("WORKSPACE_PROJECT_ENVIRONMENT:READ+EDIT");
},
}, },
methods: { methods: {
updateGlobalScript(currentEnvironment, isPreScript, filedName, value) { updateGlobalScript(currentEnvironment, isPreScript, filedName, value) {
if (isPreScript) { if (isPreScript) {
if (filedName === "connScenario") { if (filedName === "connScenario") {
currentEnvironment.config.globalScriptConfig.connScenarioPreScript = value; currentEnvironment.config.globalScriptConfig.connScenarioPreScript =
value;
} else if (filedName === "execAfterPrivateScript") { } else if (filedName === "execAfterPrivateScript") {
currentEnvironment.config.globalScriptConfig.isPreScriptExecAfterPrivateScript = value; currentEnvironment.config.globalScriptConfig.isPreScriptExecAfterPrivateScript =
value;
} else if (filedName === "filterRequest") { } else if (filedName === "filterRequest") {
currentEnvironment.config.globalScriptConfig.filterRequestPreScript = value; currentEnvironment.config.globalScriptConfig.filterRequestPreScript =
value;
} }
} else { } else {
if (filedName === "connScenario") { if (filedName === "connScenario") {
currentEnvironment.config.globalScriptConfig.connScenarioPostScript = value; currentEnvironment.config.globalScriptConfig.connScenarioPostScript =
value;
} else if (filedName === "execAfterPrivateScript") { } else if (filedName === "execAfterPrivateScript") {
currentEnvironment.config.globalScriptConfig.isPostScriptExecAfterPrivateScript = value; currentEnvironment.config.globalScriptConfig.isPostScriptExecAfterPrivateScript =
value;
} else if (filedName === "filterRequest") { } else if (filedName === "filterRequest") {
currentEnvironment.config.globalScriptConfig.filterRequestPostScript = value; currentEnvironment.config.globalScriptConfig.filterRequestPostScript =
value;
} }
} }
}, },
@ -118,23 +158,30 @@ export default {
listenGoBack(this.close); listenGoBack(this.close);
}, },
deleteEnvironment(environment, index) { deleteEnvironment(environment, index) {
this.$alert(this.$t('commons.delete') + "(" + environment.name + ")" + this.$t('project.del_env_tip'), '', { this.$alert(
confirmButtonText: this.$t('commons.confirm'), this.$t("commons.delete") +
cancelButtonText: this.$t('commons.cancel'), "(" +
callback: (action) => { environment.name +
if (action === 'confirm') { ")" +
if (environment.id) { this.$t("project.del_env_tip"),
this.result = delApiEnvironment(environment.id).then(() => { "",
this.$success(this.$t('commons.delete_success')); {
this.getEnvironments(); confirmButtonText: this.$t("commons.confirm"),
}); cancelButtonText: this.$t("commons.cancel"),
} else { callback: (action) => {
this.environments.splice(index, 1); if (action === "confirm") {
if (environment.id) {
this.result = delApiEnvironment(environment.id).then(() => {
this.$success(this.$t("commons.delete_success"));
this.getEnvironments();
});
} else {
this.environments.splice(index, 1);
}
} }
} },
} }
}); );
}, },
copyEnvironment(environment) { copyEnvironment(environment) {
this.ifCreate = false; this.ifCreate = false;
@ -143,32 +190,32 @@ export default {
this.environmentSelected(environment); this.environmentSelected(environment);
this.currentEnvironment = environment; this.currentEnvironment = environment;
if (!environment.id) { if (!environment.id) {
this.$warning(this.$t('commons.please_save')); this.$warning(this.$t("commons.please_save"));
return; return;
} }
let newEnvironment = {}; let newEnvironment = {};
newEnvironment = new Environment(environment); newEnvironment = new Environment(environment);
newEnvironment.id = null; newEnvironment.id = null;
newEnvironment.config.databaseConfigs.forEach(dataSource => { newEnvironment.config.databaseConfigs.forEach((dataSource) => {
if (dataSource.id) { if (dataSource.id) {
dataSource.id = getUUID(); dataSource.id = getUUID();
} }
}) });
newEnvironment.name = this.getNoRepeatName(newEnvironment.name); newEnvironment.name = this.getNoRepeatName(newEnvironment.name);
if (!this.validateEnvironment(newEnvironment)) { if (!this.validateEnvironment(newEnvironment)) {
return; return;
} }
newEnvironment.config.databaseConfigs.forEach(dataSource => { newEnvironment.config.databaseConfigs.forEach((dataSource) => {
if (dataSource.id) { if (dataSource.id) {
dataSource.id = getUUID(); dataSource.id = getUUID();
} }
}) });
this.$refs.environmentEdit._save(newEnvironment); this.$refs.environmentEdit._save(newEnvironment);
this.getEnvironments(); this.getEnvironments();
}, },
validateEnvironment(environment) { validateEnvironment(environment) {
if (!this.$refs.environmentEdit.validate()) { if (!this.$refs.environmentEdit.validate()) {
this.$error(this.$t('commons.formatErr')); this.$error(this.$t("commons.formatErr"));
return false; return false;
} }
return true; return true;
@ -176,7 +223,7 @@ export default {
getNoRepeatName(name) { getNoRepeatName(name) {
for (let i in this.environments) { for (let i in this.environments) {
if (this.environments[i].name === name) { if (this.environments[i].name === name) {
return this.getNoRepeatName(name + ' copy'); return this.getNoRepeatName(name + " copy");
} }
} }
return name; return name;
@ -184,10 +231,13 @@ export default {
addEnvironment() { addEnvironment() {
this.ifCreate = true; this.ifCreate = true;
let newEnvironment = new Environment({ let newEnvironment = new Environment({
projectId: this.projectId projectId: this.projectId,
}); });
this.environments.push(newEnvironment); this.environments.push(newEnvironment);
this.$refs.environmentItems.itemSelected(this.environments.length - 1, newEnvironment); this.$refs.environmentItems.itemSelected(
this.environments.length - 1,
newEnvironment
);
}, },
environmentSelected(environment) { environmentSelected(environment) {
if (this.$refs.environmentEdit) { if (this.$refs.environmentEdit) {
@ -197,27 +247,40 @@ export default {
}, },
getEnvironments() { getEnvironments() {
if (this.projectId) { if (this.projectId) {
this.result = getEnvironmentByProjectId(this.projectId).then(response => { this.result = getEnvironmentByProjectId(this.projectId).then(
this.environments = response.data; (response) => {
if (this.environments.length > 0) { this.environments = response.data;
if (this.selectEnvironmentId) { if (this.environments.length > 0) {
const index = this.environments.findIndex(e => e.id === this.selectEnvironmentId); if (this.selectEnvironmentId) {
if (index !== -1) { const index = this.environments.findIndex(
this.$refs.environmentItems.itemSelected(index, this.environments[index]); (e) => e.id === this.selectEnvironmentId
);
if (index !== -1) {
this.$refs.environmentItems.itemSelected(
index,
this.environments[index]
);
} else {
this.$refs.environmentItems.itemSelected(
0,
this.environments[0]
);
}
} else { } else {
this.$refs.environmentItems.itemSelected(0, this.environments[0]); this.$refs.environmentItems.itemSelected(
0,
this.environments[0]
);
} }
} else { } else {
this.$refs.environmentItems.itemSelected(0, this.environments[0]); let item = new Environment({
projectId: this.projectId,
});
this.environments.push(item);
this.$refs.environmentItems.itemSelected(0, item);
} }
} else {
let item = new Environment({
projectId: this.projectId
});
this.environments.push(item);
this.$refs.environmentItems.itemSelected(0, item);
} }
}); );
} }
}, },
getEnvironment(environment) { getEnvironment(environment) {
@ -229,10 +292,9 @@ export default {
}, },
save() { save() {
this.$refs.environmentEdit.save(); this.$refs.environmentEdit.save();
this.currentEnvironment = null;
}, },
close() { close() {
this.$emit('close'); this.$emit("close");
if (!this.isCopy) { if (!this.isCopy) {
this.visible = false; this.visible = false;
} }
@ -246,18 +308,17 @@ export default {
this.currentIndex = index; this.currentIndex = index;
}, },
delEnvironment() { delEnvironment() {
this.deleteEnvironment(this.currentEnvironment, this.currentIndex) this.deleteEnvironment(this.currentEnvironment, this.currentIndex);
}, },
refresh() { refresh() {
this.getEnvironments(); this.getEnvironments();
this.$emit('saveRefresh'); this.$emit("saveRefresh");
}, },
} },
} };
</script> </script>
<style scoped> <style scoped>
.environment-dialog :deep(.el-dialog__body) { .environment-dialog :deep(.el-dialog__body) {
padding-top: 20px; padding-top: 20px;
} }
@ -270,5 +331,4 @@ export default {
height: 100%; height: 100%;
position: absolute; position: absolute;
} }
</style> </style>

View File

@ -1,49 +1,79 @@
<template> <template>
<div> <div>
<el-form :model="commonConfig" :rules="rules" ref="commonConfig" :disabled="isReadOnly" label-width="30px"> <el-form
<ms-api-scenario-variables :show-copy="false" :items="commonConfig.variables" :is-read-only="isReadOnly" ref="apiScenario"/> :model="commonConfig"
:rules="rules"
ref="commonConfig"
:disabled="isReadOnly"
label-width="30px"
>
<ms-api-scenario-variables
:show-copy="false"
:items="commonConfig.variables"
:is-read-only="isReadOnly"
ref="apiScenario"
/>
<el-form-item> <el-form-item>
<el-switch v-model="commonConfig.enableHost" active-text="Hosts"/> <el-switch v-model="commonConfig.enableHost" active-text="Hosts" />
<el-tooltip class="hostItem" effect="dark" :content="$t('api_test.home_page.host_config')" placement="right"> <el-tooltip
class="hostItem"
effect="dark"
:content="$t('api_test.home_page.host_config')"
placement="right"
>
<i class="el-icon-info"></i> <i class="el-icon-info"></i>
</el-tooltip> </el-tooltip>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<span>{{ $t('api_test.environment.request_timeout') }}:</span> <span>{{ $t("api_test.environment.request_timeout") }}:</span>
<el-input-number style="margin-left: 20px" controls-position="right" size="small" <el-input-number
v-model="commonConfig.requestTimeout" style="margin-left: 20px"
onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" controls-position="right"
:precision="0"> size="small"
{{ $t('api_test.environment.globalVariable') }} v-model="commonConfig.requestTimeout"
onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
:precision="0"
>
{{ $t("api_test.environment.globalVariable") }}
</el-input-number> </el-input-number>
<span style="margin-left: 30px">{{ $t('api_test.environment.response_timeout') }}:</span> <span style="margin-left: 30px"
<el-input-number style="margin-left: 20px" controls-position="right" size="small" >{{ $t("api_test.environment.response_timeout") }}:</span
v-model="commonConfig.responseTimeout" >
onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))" <el-input-number
:precision="0"> style="margin-left: 20px"
{{ $t('api_test.environment.globalVariable') }} controls-position="right"
size="small"
v-model="commonConfig.responseTimeout"
onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"
:precision="0"
>
{{ $t("api_test.environment.globalVariable") }}
</el-input-number> </el-input-number>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<ms-api-host-table v-if="commonConfig.enableHost" :hostTable="commonConfig.hosts" ref="refHostTable"/> <ms-api-host-table
v-if="commonConfig.enableHost"
:hostTable="commonConfig.hosts"
ref="refHostTable"
/>
</el-form-item> </el-form-item>
</el-form> </el-form>
</div> </div>
</template> </template>
<script> <script>
import {CommonConfig} from "../../model/EnvironmentModel"; import { CommonConfig } from "../../model/EnvironmentModel";
import MsApiScenarioVariables from "./commons/ApiScenarioVariables"; import MsApiScenarioVariables from "./commons/ApiScenarioVariables";
import MsApiHostTable from "./commons/ApiHostTable"; import MsApiHostTable from "./commons/ApiHostTable";
export default { export default {
name: "MsEnvironmentCommonConfig", name: "MsEnvironmentCommonConfig",
components: {MsApiHostTable, MsApiScenarioVariables}, components: { MsApiHostTable, MsApiScenarioVariables },
props: { props: {
commonConfig: new CommonConfig(), commonConfig: new CommonConfig(),
isReadOnly: { isReadOnly: {
type: Boolean, type: Boolean,
default: false default: false,
}, },
}, },
data() { data() {
@ -53,37 +83,39 @@ export default {
}, },
created() { created() {
if (!this.commonConfig.requestTimeout) { if (!this.commonConfig.requestTimeout) {
this.$set(this.commonConfig, 'requestTimeout', 60000); this.$set(this.commonConfig, "requestTimeout", 60000);
} }
if (!this.commonConfig.responseTimeout) { if (!this.commonConfig.responseTimeout) {
this.$set(this.commonConfig, 'responseTimeout', 60000); this.$set(this.commonConfig, "responseTimeout", 60000);
} }
}, },
watch: { watch: {
commonConfig() { commonConfig() {
if (!this.commonConfig.requestTimeout) { if (!this.commonConfig.requestTimeout) {
this.$set(this.commonConfig, 'requestTimeout', 60000); this.$set(this.commonConfig, "requestTimeout", 60000);
} }
if (!this.commonConfig.responseTimeout) { if (!this.commonConfig.responseTimeout) {
this.$set(this.commonConfig, 'responseTimeout', 60000); this.$set(this.commonConfig, "responseTimeout", 60000);
} }
} },
}, },
methods: { methods: {
mergeData() { mergeData() {
if (this.$refs.apiScenario) { if (this.$refs.apiScenario) {
this.commonConfig.variables = this.$refs['apiScenario'].allData; this.commonConfig.variables = this.$refs["apiScenario"].allData;
} }
}, },
validate() { validate() {
let isValidate = false; let isValidate = false;
this.$refs['commonConfig'].validate((valid) => { this.$refs["commonConfig"].validate((valid) => {
if (valid) { if (valid) {
// host // host
let valHost = true; let valHost = true;
if (this.commonConfig.enableHost) { if (this.commonConfig.enableHost) {
for (let i = 0; i < this.commonConfig.hosts.length; i++) { for (let i = 0; i < this.commonConfig.hosts.length; i++) {
valHost = this.$refs['refHostTable'].confirm(this.commonConfig.hosts[i]); valHost = this.$refs["refHostTable"].confirm(
this.commonConfig.hosts[i]
);
} }
} }
if (valHost) { if (valHost) {
@ -96,8 +128,8 @@ export default {
} }
}); });
return isValidate; return isValidate;
} },
} },
}; };
</script> </script>

View File

@ -85,7 +85,7 @@
v-model="scope.row.scope" v-model="scope.row.scope"
:placeholder="$t('commons.please_select')" :placeholder="$t('commons.please_select')"
size="mini" size="mini"
@change="changeType($event, scope.row)" @change="changeScope($event, scope.row)"
> >
<el-option <el-option
v-for="item in scopeTypeFilters" v-for="item in scopeTypeFilters"
@ -424,26 +424,41 @@ export default {
// //
this.$emit("change", this.items); this.$emit("change", this.items);
}, },
// /UI change
changeScope: function(value,row) {
if (value === "ui") {
row.type = "STRING";
} else {
row.type = "CONSTANT";
}
this.items.forEach((item) => {
if (item.id === row.id) {
item.scope = value;
}
});
this.changeType(row.type, row);
},
changeType(value, data) { changeType(value, data) {
data.value = ""; data.value = ''
if ( const notDelimiters = !data.delimiter ||
!data.delimiter ||
(!data.files && data.files.length === 0) || (!data.files && data.files.length === 0) ||
!data.quotedData !data.quotedData
if (
notDelimiters
) { ) {
data.delimiter = ","; data.delimiter = ",";
data.files = []; data.files = [];
data.quotedData = "false"; data.quotedData = "false";
} }
if (!data.scope || data.scope == "ui") {
data.type = "STRING";
}
this.items.forEach((item) => { this.items.forEach((item) => {
if (item.id === data.id) { if (item.id === data.id) {
item.scope = value; item.type = value;
item.type = data.type;
item.value = data.value; item.value = data.value;
if(notDelimiters) {
item.delimiter = data.delimiter;
item.files = data.files;
item.quotedData = data.quotedData;
}
} }
}); });
}, },