feat(系统设置): 添加@Validated注解和系统参数设置用例

This commit is contained in:
WangXu10 2023-06-16 17:59:06 +08:00 committed by 刘瑞斌
parent bc2989bce0
commit 585701c174
4 changed files with 58 additions and 16 deletions

View File

@ -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);
}

View File

@ -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> systemParameter) {
@RequestLog(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "基本配置", sourceId = "#systemParameter.get(0).paramKey")
public void saveBaseParameter(@Validated @RequestBody List<SystemParameter> 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> systemParameter) {
@RequestLog(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置", sourceId = "#systemParameter.get(0).paramKey")
public void editEMailInfo(@Validated @RequestBody List<SystemParameter> systemParameter) {
systemParameterService.editEMailInfo(systemParameter);
}

View File

@ -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());
}
}

View File

@ -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<SystemParameter> 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());
}
}