From 585701c17446c04da618f628e02686ee662adce7 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Fri, 16 Jun 2023 17:59:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0@Validated=E6=B3=A8=E8=A7=A3=E5=92=8C?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=8F=82=E6=95=B0=E8=AE=BE=E7=BD=AE=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthSourceController.java | 9 +++--- .../controller/SystemParameterController.java | 17 ++++++----- .../controller/AuthSourceControllerTest.java | 18 +++++++++++ .../SystemParameterControllerTest.java | 30 ++++++++++++++++--- 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java index 200a1d45bd..0cc23551d5 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java @@ -12,6 +12,7 @@ import io.metersphere.system.domain.AuthSource; import io.metersphere.system.service.AuthSourceService; import jakarta.annotation.Resource; import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -33,15 +34,15 @@ public class AuthSourceController { @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_CREAT) @RequestLog(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "认证设置") - public void add(@RequestBody AuthSource authSource) { + public void add(@Validated @RequestBody AuthSource authSource) { authSourceService.addAuthSource(authSource); } @PostMapping("/update") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @RequestLog(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, - details = "认证设置") - public void update(@RequestBody AuthSource authSource) { + details = "认证设置", sourceId = "#authSource.id") + public void update(@Validated @RequestBody AuthSource authSource) { authSourceService.updateAuthSource(authSource); } @@ -54,7 +55,7 @@ public class AuthSourceController { @GetMapping("/delete/{id}") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_DELETE) @RequestLog(type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, - details = "认证设置") + details = "认证设置", sourceId = "#id") public void delete(@PathVariable(value = "id") String id) { authSourceService.deleteAuthSource(id); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java index 3718879a21..d44f9ac358 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java @@ -11,6 +11,7 @@ import io.metersphere.sdk.service.SystemParameterService; import io.metersphere.system.domain.SystemParameter; import jakarta.annotation.Resource; import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.HashMap; @@ -29,14 +30,14 @@ public class SystemParameterController { * * @param systemParameter */ - @PostMapping("/save/baseInfo") + @PostMapping("/save/base-info") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) - @RequestLog(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "基本配置") - public void saveBaseParameter(@RequestBody List systemParameter) { + @RequestLog(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "基本配置", sourceId = "#systemParameter.get(0).paramKey") + public void saveBaseParameter(@Validated @RequestBody List systemParameter) { systemParameterService.saveBaseInfo(systemParameter); } - @GetMapping("/get/baseInfo") + @GetMapping("/get/base-info") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) public BaseSystemConfigDTO getBaseInfo() { return systemParameterService.getBaseInfo(); @@ -48,17 +49,17 @@ public class SystemParameterController { * * @return */ - @GetMapping("/get/emailInfo") + @GetMapping("/get/email-info") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) public EMailInfoDto getEmailInfo() { return systemParameterService.getEmailInfo(); } - @PostMapping("/edit/emailInfo") + @PostMapping("/edit/email-info") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) - @RequestLog(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置") - public void editEMailInfo(@RequestBody List systemParameter) { + @RequestLog(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置", sourceId = "#systemParameter.get(0).paramKey") + public void editEMailInfo(@Validated @RequestBody List systemParameter) { systemParameterService.editEMailInfo(systemParameter); } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTest.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTest.java index b0c3c0966f..a9e067bd74 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTest.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTest.java @@ -111,4 +111,22 @@ public class AuthSourceControllerTest { .andExpect(status().isOk()) .andDo(print()); } + + + @Test + @Order(6) + public void testAddSourceByNullName() throws Exception { + AuthSource authSource = new AuthSource(); + authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf"); + authSource.setConfiguration("123".getBytes()); + authSource.setCreateTime(System.currentTimeMillis()); + authSource.setUpdateTime(System.currentTimeMillis()); + mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/add") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(authSource)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is5xxServerError()) + .andDo(print()); + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemParameterControllerTest.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemParameterControllerTest.java index f4ed11c358..ddafa02314 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemParameterControllerTest.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemParameterControllerTest.java @@ -60,7 +60,7 @@ public class SystemParameterControllerTest { systemParameters.add(systemParameter); systemParameters.add(parameter); - mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/save/baseInfo") + mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/save/base-info") .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .content(JSON.toJSONString(systemParameters)) @@ -75,7 +75,7 @@ public class SystemParameterControllerTest { @Test @Order(2) public void testGetBaseInfo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/baseInfo") + mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/base-info") .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken)) .andExpect(status().isOk()) @@ -86,7 +86,7 @@ public class SystemParameterControllerTest { @Test @Order(3) public void testGetEmailInfo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/emailInfo") + mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/email-info") .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken)) .andExpect(status().isOk()); @@ -118,7 +118,7 @@ public class SystemParameterControllerTest { systemParameters.add(systemParameter2); systemParameters.add(systemParameter3); - mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/edit/emailInfo") + mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/edit/email-info") .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .content(JSON.toJSONString(systemParameters)) @@ -147,4 +147,26 @@ public class SystemParameterControllerTest { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().is5xxServerError()); } + + + @Test + @Order(5) + public void testSaveBaseInfoNullUrl() throws Exception { + List systemParameters = new ArrayList<>(); + SystemParameter parameter = new SystemParameter(); + parameter.setParamKey("base.prometheus.host"); + parameter.setParamValue("http://127.0.0.1:1111"); + parameter.setType("text"); + systemParameters.add(parameter); + + mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/save/base-info") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(systemParameters)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andDo(print()); + + } }