diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java index 104ab96f40..4e6511d484 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java @@ -6,6 +6,7 @@ import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.ApiTestService; import io.metersphere.jmeter.mock.Mock; import io.metersphere.plugin.api.dto.ApiPluginSelectOption; +import io.metersphere.project.dto.CommonScriptInfo; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.sdk.constants.PermissionConstants; @@ -151,4 +152,16 @@ public class ApiTestController { } apiTestService.download(path.asText(), response); } + + @GetMapping("/common-script/{scriptId}") + @Operation(summary = "获取最新的公共脚本信息") + @RequiresPermissions(value = { + PermissionConstants.PROJECT_API_DEFINITION_READ, + PermissionConstants.PROJECT_API_DEFINITION_CASE_READ, + PermissionConstants.PROJECT_API_DEBUG_READ, + PermissionConstants.PROJECT_API_SCENARIO_READ + }, logical = Logical.OR) + public CommonScriptInfo getCommonScriptInfo(@PathVariable String scriptId) { + return apiTestService.getCommonScriptInfo(scriptId); + } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java index d26c7f9518..8c34e6ea70 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiTestService.java @@ -7,9 +7,13 @@ import io.metersphere.plugin.api.dto.ApiPluginOptionsRequest; import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.plugin.api.spi.AbstractApiPlugin; import io.metersphere.plugin.api.spi.AbstractProtocolPlugin; +import io.metersphere.project.api.KeyValueParam; +import io.metersphere.project.dto.CommonScriptInfo; +import io.metersphere.project.dto.customfunction.CustomFunctionDTO; import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.mapper.ExtEnvironmentMapper; import io.metersphere.project.mapper.ExtProjectMapper; +import io.metersphere.project.service.CustomFunctionService; import io.metersphere.project.service.EnvironmentService; import io.metersphere.project.service.ProjectApplicationService; import io.metersphere.sdk.constants.ProjectApplicationType; @@ -17,6 +21,7 @@ import io.metersphere.sdk.constants.StorageType; import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.domain.TestResourcePool; import io.metersphere.system.dto.ProtocolDTO; @@ -59,6 +64,8 @@ public class ApiTestService { private ProjectApplicationService projectApplicationService; @Resource private FileService fileService; + @Resource + private CustomFunctionService customFunctionService; public List getProtocols(String orgId) { List protocols = apiPluginService.getProtocols(orgId); @@ -158,4 +165,20 @@ public class ApiTestService { response.getOutputStream().write(content); response.getOutputStream().flush(); } + + public CommonScriptInfo getCommonScriptInfo(String scriptId) { + CustomFunctionDTO customFunctionDTO = customFunctionService.get(scriptId); + if (customFunctionDTO == null) { + return null; + } + CommonScriptInfo commonScriptInfo = new CommonScriptInfo(); + commonScriptInfo.setScriptLanguage(customFunctionDTO.getType()); + commonScriptInfo.setScript(customFunctionDTO.getScript()); + commonScriptInfo.setName(customFunctionDTO.getName()); + commonScriptInfo.setId(customFunctionDTO.getId()); + if (StringUtils.isNotBlank(customFunctionDTO.getParams())) { + commonScriptInfo.setParams(JSON.parseArray(customFunctionDTO.getParams(), KeyValueParam.class)); + } + return commonScriptInfo; + } } \ No newline at end of file diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java index 5ec1e81366..55ec6a34cc 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java @@ -6,11 +6,16 @@ import io.metersphere.api.service.BaseResourcePoolTestService; import io.metersphere.plugin.api.dto.ApiPluginSelectOption; import io.metersphere.project.api.KeyValueParam; import io.metersphere.project.constants.ScriptLanguageType; +import io.metersphere.project.domain.CustomFunction; import io.metersphere.project.domain.ProjectTestResourcePool; import io.metersphere.project.domain.ProjectTestResourcePoolExample; +import io.metersphere.project.dto.CommonScriptInfo; +import io.metersphere.project.dto.customfunction.request.CustomFunctionRequest; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.mapper.ProjectTestResourcePoolMapper; +import io.metersphere.project.service.CustomFunctionService; +import io.metersphere.sdk.constants.InternalUser; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.domain.Environment; @@ -35,6 +40,7 @@ import java.io.FileInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.UUID; import static io.metersphere.sdk.constants.InternalUserRole.ADMIN; import static io.metersphere.system.controller.handler.result.MsHttpResultCode.NOT_FOUND; @@ -58,6 +64,7 @@ public class ApiTestControllerTests extends BaseTest { protected static final String PLUGIN_SCRIPT = "plugin/script/{0}"; protected static final String ENV_LIST = "env-list/{0}"; protected static final String ENVIRONMENT = "environment/{0}"; + protected static final String COMMON_SCRIPT = "common-script/{scriptId}"; @Resource private BaseResourcePoolTestService baseResourcePoolTestService; @@ -69,6 +76,8 @@ public class ApiTestControllerTests extends BaseTest { private BaseEnvTestService baseEnvTestService; @Resource private ProjectTestResourcePoolMapper projectTestResourcePoolMapper; + @Resource + private CustomFunctionService customFunctionService; @Override protected String getBasePath() { @@ -227,6 +236,35 @@ public class ApiTestControllerTests extends BaseTest { Assertions.assertNull(environmentConfig.getAssertionConfig()); } + @Test + public void getCommonScriptInfo() throws Exception { + MvcResult mvcResult = this.requestGetAndReturn(COMMON_SCRIPT, "111"); + Assertions.assertNull(parseResponse(mvcResult).get("data")); + + // 创建测试数据 + CustomFunctionRequest request = new CustomFunctionRequest(); + request.setProjectId(DEFAULT_PROJECT_ID); + request.setName(UUID.randomUUID().toString()); + request.setStatus("UNDERWAY"); + request.setScript("script"); + // 执行方法调用 + request.setName(UUID.randomUUID().toString()); + CustomFunction customFunction = customFunctionService.add(request, InternalUser.ADMIN.getValue()); + mvcResult = this.requestGetAndReturn(COMMON_SCRIPT, customFunction.getId()); + CommonScriptInfo resultData = getResultData(mvcResult, CommonScriptInfo.class); + + Assertions.assertEquals(resultData.getScript(), request.getScript()); + Assertions.assertEquals(resultData.getName(), request.getName()); + + // @@校验权限 + requestGetPermissionsTest(new ArrayList<>() {{ + add(PermissionConstants.PROJECT_API_DEFINITION_READ); + add(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ); + add(PermissionConstants.PROJECT_API_DEBUG_READ); + add(PermissionConstants.PROJECT_API_SCENARIO_READ); + }}, COMMON_SCRIPT, "11"); + } + @Test public void getPoolOption() throws Exception { // @@请求成功 diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/CustomFunctionController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/CustomFunctionController.java index 6da58ed575..357548a0eb 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/CustomFunctionController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/CustomFunctionController.java @@ -65,7 +65,7 @@ public class CustomFunctionController { @RequiresPermissions(PermissionConstants.PROJECT_CUSTOM_FUNCTION_READ) @CheckOwner(resourceId = "#id", resourceType = "custom_function") public CustomFunctionDTO get(@PathVariable String id) { - return customFunctionService.get(id); + return customFunctionService.getWithCheck(id); } @PostMapping("/add") diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/CustomFunctionService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/CustomFunctionService.java index da770836a7..f1d09b6c50 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/CustomFunctionService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/CustomFunctionService.java @@ -83,7 +83,7 @@ public class CustomFunctionService { list.forEach(item -> handleCustomFunctionBlob(item.getId(), item)); } - public CustomFunctionDTO get(String id) { + public CustomFunctionDTO getWithCheck(String id) { CustomFunction customFunction = checkCustomFunction(id); CustomFunctionDTO customFunctionDTO = new CustomFunctionDTO(); handleCustomFunctionBlob(id, customFunctionDTO); @@ -91,6 +91,17 @@ public class CustomFunctionService { return customFunctionDTO; } + public CustomFunctionDTO get(String id) { + CustomFunction customFunction = customFunctionMapper.selectByPrimaryKey(id); + if (customFunction == null) { + return null; + } + CustomFunctionDTO customFunctionDTO = new CustomFunctionDTO(); + handleCustomFunctionBlob(id, customFunctionDTO); + BeanUtils.copyBean(customFunctionDTO, customFunction); + return customFunctionDTO; + } + public void handleCustomFunctionBlob(String id, CustomFunctionDTO customFunctionDTO) { Optional customFunctionBlobOptional = Optional.ofNullable(customFunctionBlobMapper.selectByPrimaryKey(id)); customFunctionBlobOptional.ifPresent(blob -> {