From 06211cc0b3f4eaaf0a228fccac91443a4029dcc6 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Tue, 4 Jul 2023 11:24:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=8F=82=E6=95=B0=E8=AE=BE=E7=BD=AE=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=8F=8A=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/i18n/commons_en_US.properties | 1 + .../resources/i18n/commons_zh_CN.properties | 1 + .../resources/i18n/commons_zh_TW.properties | 1 + .../controller/AuthSourceController.java | 28 ++- .../controller/SystemParameterController.java | 24 +-- .../system/request/AuthSourceRequest.java | 38 ++++ .../system/service/AuthSourceService.java | 45 +++-- .../controller/AuthSourceControllerTest.java | 143 +++++++++----- .../SystemParameterControllerTest.java | 177 ++++++++++-------- 9 files changed, 295 insertions(+), 163 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceRequest.java diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties index 3535f4ccf6..61f10ebb22 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_en_US.properties @@ -238,6 +238,7 @@ import_fail_custom_num_exists=import fail, custom num is exists authsource_name_already_exists=Authentication source name already exists authsource_name_is_null=Authentication source name cannot be empty authsource_configuration_is_null=Authentication source configuration cannot be empty +authsource_type_is_null=Authentication source type cannot be empty mobile_phone_number_cannot_be_empty=When the receiving mode is pin and enterprise wechat: the user's mobile phone number cannot be empty custom_field_already=A feild already exists under this organization: template_already=A template already exists under this organization: diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties index f328ad5d77..57e7a5a969 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_CN.properties @@ -237,6 +237,7 @@ import_fail_custom_num_exists=导入失败,自定义ID已存在 authsource_name_already_exists=认证源名称已经存在 authsource_name_is_null=认证源名称不能为空 authsource_configuration_is_null=认证源配置不能为空 +authsource_type_is_null=认证源类型不能为空 custom_field_already=工作空间下已存在该字段: template_already=工作空间下已存在该模板: expect_name_exists=预期名称已存在 diff --git a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties index 459bc1f3ca..39fa95713a 100644 --- a/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties +++ b/backend/framework/sdk/src/main/resources/i18n/commons_zh_TW.properties @@ -236,6 +236,7 @@ import_fail_custom_num_exists=導入失敗,自定義ID已存在 authsource_name_already_exists=認證源名稱已經存在 authsource_name_is_null=認證源名稱不能為空 authsource_configuration_is_null=認證源配置不能為空 +authsource_type_is_null=認證源類型不能為空 custom_field_already=工作空間下已存在該字段: template_already=工作空間下已存在該模板: expect_name_exists=預期名稱已存在 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 38192dd9f1..333f4ea805 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 @@ -3,14 +3,18 @@ package io.metersphere.system.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.log.annotation.Log; import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; import io.metersphere.system.domain.AuthSource; +import io.metersphere.system.request.AuthSourceRequest; import io.metersphere.system.service.AuthSourceService; +import io.swagger.v3.oas.annotations.Operation; import jakarta.annotation.Resource; +import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -23,40 +27,54 @@ public class AuthSourceController { @Resource private AuthSourceService authSourceService; - @PostMapping("/list/{goPage}/{pageSize}") + @PostMapping("/list") + @Operation(summary = "认证设置列表查询") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) - public Pager> list(@PathVariable int goPage, @PathVariable int pageSize) { - Page page = PageHelper.startPage(goPage, pageSize, true); + public Pager> list(@Validated @RequestBody BasePageRequest request) { + Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), + StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc"); return PageUtils.setPageInfo(page, authSourceService.list()); } @PostMapping("/add") + @Operation(summary = "新增认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_CREAT) @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "认证设置") - public void add(@Validated @RequestBody AuthSource authSource) { + public void add(@Validated @RequestBody AuthSourceRequest authSource) { authSourceService.addAuthSource(authSource); } @PostMapping("/update") + @Operation(summary = "更新认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "认证设置", sourceId = "#authSource.id") - public void update(@Validated @RequestBody AuthSource authSource) { + public void update(@Validated @RequestBody AuthSourceRequest authSource) { authSourceService.updateAuthSource(authSource); } @GetMapping("/get/{id}") + @Operation(summary = "获取认证设置详细信息") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) public AuthSource get(@PathVariable(value = "id") String id) { return authSourceService.getAuthSource(id); } @GetMapping("/delete/{id}") + @Operation(summary = "删除认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_DELETE) @Log(type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "认证设置", sourceId = "#id") public void delete(@PathVariable(value = "id") String id) { authSourceService.deleteAuthSource(id); } + + + @GetMapping("/update/{authId}/status/{status}") + @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, + details = "认证设置", sourceId = "#authSource.authId") + public void updateStatus(@PathVariable(value = "authId") String authId, @PathVariable("status") String status) { + authSourceService.updateStatus(authId, status); + } } 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 e570bf2f8d..73dba6f7c9 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 @@ -9,6 +9,8 @@ import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.service.SystemParameterService; import io.metersphere.system.domain.SystemParameter; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.validation.annotation.Validated; @@ -18,6 +20,7 @@ import java.util.HashMap; import java.util.List; @RestController +@Tag(name = "系统参数") @RequestMapping("/system/parameter") public class SystemParameterController { @@ -25,31 +28,25 @@ public class SystemParameterController { SystemParameterService systemParameterService; - /** - * 基本配置 - * - * @param systemParameter - */ @PostMapping("/save/base-info") + @Operation(summary = "保存基本信息") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @Log(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/base-info") + @Operation(summary = "获取基本信息") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) public BaseSystemConfigDTO getBaseInfo() { return systemParameterService.getBaseInfo(); } - /** - * 邮件设置 - * - * @return - */ @GetMapping("/get/email-info") + @Operation(summary = "获取邮件信息") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) public EMailInfoDto getEmailInfo() { return systemParameterService.getEmailInfo(); @@ -57,6 +54,7 @@ public class SystemParameterController { @PostMapping("/edit/email-info") + @Operation(summary = "保存邮件信息") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置", sourceId = "#systemParameter.get(0).paramKey") public void editEMailInfo(@Validated @RequestBody List systemParameter) { @@ -64,12 +62,8 @@ public class SystemParameterController { } - /** - * 邮件测试连接 - * - * @param hashMap - */ @PostMapping("/test/email") + @Operation(summary = "测试连接") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) public void testEmailConnection(@RequestBody HashMap hashMap) { systemParameterService.testEmailConnection(hashMap); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceRequest.java new file mode 100644 index 0000000000..d3bef2fad7 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceRequest.java @@ -0,0 +1,38 @@ +package io.metersphere.system.request; + +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; + +@Data +@EqualsAndHashCode(callSuper = false) +public class AuthSourceRequest implements Serializable { + + private static final long serialVersionUID = 1L; + + @Schema(title = "认证源ID", requiredMode = Schema.RequiredMode.REQUIRED) + @Size(min = 1, max = 50, message = "{auth_source.id.length_range}", groups = {Created.class, Updated.class}) + private String id; + + + @Schema(title = "描述") + private String description; + + @Schema(title = "名称", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{authsource_name_is_null}") + private String name; + + @Schema(title = "类型") + @NotBlank(message = "{authsource_type_is_null}") + private String type; + + @Schema(title = "认证源配置", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{authsource_configuration_is_null}", groups = {Created.class}) + private String configuration; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceService.java index 0f6e774d11..6d6d32c9fe 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceService.java @@ -6,6 +6,7 @@ import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.AuthSource; import io.metersphere.system.domain.AuthSourceExample; import io.metersphere.system.mapper.AuthSourceMapper; +import io.metersphere.system.request.AuthSourceRequest; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -25,16 +26,26 @@ public class AuthSourceService { return authSourceMapper.selectByExample(example); } - public void addAuthSource(AuthSource authSource) { + public void addAuthSource(AuthSourceRequest authSource) { checkAuthSource(authSource); - long createTime = System.currentTimeMillis(); - authSource.setCreateTime(createTime); - authSource.setUpdateTime(createTime); - authSource.setId(UUID.randomUUID().toString()); - authSourceMapper.insertSelective(authSource); + AuthSource source = delRequestToDB(authSource); + authSourceMapper.insertSelective(source); } - public void checkAuthSource(AuthSource authSource) { + private AuthSource delRequestToDB(AuthSourceRequest authSource) { + long createTime = System.currentTimeMillis(); + AuthSource source = new AuthSource(); + source.setName(authSource.getName()); + source.setConfiguration(authSource.getConfiguration().getBytes()); + source.setDescription(authSource.getDescription()); + source.setType(authSource.getType()); + source.setCreateTime(createTime); + source.setUpdateTime(createTime); + source.setId(UUID.randomUUID().toString()); + return source; + } + + public void checkAuthSource(AuthSourceRequest authSource) { String resourcePoolName = authSource.getName(); if (StringUtils.isBlank(resourcePoolName)) { throw new MSException(Translator.get("authsource_name_is_null")); @@ -63,11 +74,23 @@ public class AuthSourceService { return authSourceMapper.selectByPrimaryKey(id); } - public void updateAuthSource(AuthSource authSource) { + public void updateAuthSource(AuthSourceRequest authSource) { checkAuthSource(authSource); - authSource.setCreateTime(null); - authSource.setUpdateTime(System.currentTimeMillis()); - authSourceMapper.updateByPrimaryKeySelective(authSource); + AuthSource source = authSourceMapper.selectByPrimaryKey(authSource.getId()); + if (source != null) { + source.setName(authSource.getName()); + source.setDescription(authSource.getDescription()); + source.setConfiguration(authSource.getConfiguration().getBytes()); + source.setUpdateTime(System.currentTimeMillis()); + authSourceMapper.updateByPrimaryKeySelective(source); + } } + public void updateStatus(String id, String status) { + AuthSource record = new AuthSource(); + record.setId(id); + record.setEnable(Boolean.parseBoolean(status)); + record.setUpdateTime(System.currentTimeMillis()); + authSourceMapper.updateByPrimaryKeySelective(record); + } } 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 a9e067bd74..fb932cfc22 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 @@ -2,8 +2,13 @@ package io.metersphere.system.controller; import com.jayway.jsonpath.JsonPath; import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.controller.handler.ResultHolder; +import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.Pager; import io.metersphere.system.domain.AuthSource; +import io.metersphere.system.request.AuthSourceRequest; +import io.metersphere.utils.JsonUtils; import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -11,8 +16,12 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import java.nio.charset.StandardCharsets; +import java.util.List; + import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -28,6 +37,18 @@ public class AuthSourceControllerTest { private static String sessionId; private static String csrfToken; + public static final String AUTH_SOURCE_ADD = "/system/authsource/add"; + + public static final String AUTH_SOURCE_List = "/system/authsource/list"; + + public static final String AUTH_SOURCE_UPDATE = "/system/authsource/update"; + + public static final String AUTH_SOURCE_GET = "/system/authsource/get/"; + + public static final String AUTH_SOURCE_DELETE = "/system/authsource/delete/"; + + private static final ResultMatcher CLIENT_ERROR_MATCHER = status().is4xxClientError(); + @BeforeEach public void login() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login") @@ -44,89 +65,107 @@ public class AuthSourceControllerTest { @Test @Order(1) public void testAddSource() throws Exception { - AuthSource authSource = new AuthSource(); - authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf"); - authSource.setConfiguration("123".getBytes()); + AuthSourceRequest authSource = new AuthSourceRequest(); + authSource.setConfiguration("123"); authSource.setName("测试CAS"); - 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().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andDo(print()); + authSource.setType("CAS"); + this.requestPost(AUTH_SOURCE_ADD, authSource); + } @Test @Order(2) public void testGetSourceList() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/list/1/10") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) - .andDo(print()); + BasePageRequest basePageRequest = new BasePageRequest(); + basePageRequest.setCurrent(1); + basePageRequest.setPageSize(10); + this.requestPost(AUTH_SOURCE_List, basePageRequest); } @Test @Order(3) public void testUpdateSource() throws Exception { - AuthSource authSource = new AuthSource(); - authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf"); - authSource.setConfiguration("123666".getBytes()); + List authSourceList = this.getAuthSourceList(); + AuthSourceRequest authSource = new AuthSourceRequest(); + authSource.setId(authSourceList.get(0).getId()); + authSource.setConfiguration("123666"); authSource.setName("更新"); - authSource.setUpdateTime(System.currentTimeMillis()); - mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/update") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken) - .content(JSON.toJSONString(authSource)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andDo(print()); + authSource.setType("CAS"); + this.requestPost(AUTH_SOURCE_UPDATE, authSource); } - - @Test @Order(4) - public void testGetSourceById() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/system/authsource/get/2b6a83d0-7c66-43ed-a1d9-5132d3167aaf") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) - .andDo(print()); + public void testUpdateStatus() throws Exception { + List authSourceList = this.getAuthSourceList(); + this.requestGet(AUTH_SOURCE_UPDATE + "/" + authSourceList.get(0).getId() + "/status/false"); } @Test @Order(5) - public void testDelSourceById() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/system/authsource/delete/2b6a83d0-7c66-43ed-a1d9-5132d3167aaf") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) - .andDo(print()); + public void testGetSourceById() throws Exception { + List authSourceList = this.getAuthSourceList(); + this.requestGet(AUTH_SOURCE_GET + authSourceList.get(0).getId()); } @Test @Order(6) - public void testAddSourceByNullName() throws Exception { + public void testDelSourceById() throws Exception { + List authSourceList = this.getAuthSourceList(); + this.requestGet(AUTH_SOURCE_DELETE + authSourceList.get(0).getId()); + } + + + @Test + @Order(7) + public void testAddSourceError() 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") + this.requestPost(AUTH_SOURCE_ADD, authSource, CLIENT_ERROR_MATCHER); + } + + + private MvcResult requestPost(String url, Object param) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.post(url) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) - .content(JSON.toJSONString(authSource)) + .content(JSON.toJSONString(param)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().is5xxServerError()) - .andDo(print()); + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andReturn(); + } + + private MvcResult requestGet(String url) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)) + .andExpect(status().isOk()).andDo(print()).andReturn(); + } + + private List getAuthSourceList() throws Exception { + BasePageRequest basePageRequest = new BasePageRequest(); + basePageRequest.setCurrent(1); + basePageRequest.setPageSize(10); + MvcResult mvcResult = this.requestPost(AUTH_SOURCE_List, basePageRequest); + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + Pager returnPager = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + List authSourceRequests = JSON.parseArray(JSON.toJSONString(returnPager.getList()), AuthSourceRequest.class); + return authSourceRequests; + } + + private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(param)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(resultMatcher).andDo(print()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } } 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 ddafa02314..7c7b30aec2 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 @@ -11,6 +11,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.util.ArrayList; @@ -32,6 +33,19 @@ public class SystemParameterControllerTest { private static String sessionId; private static String csrfToken; + public static final String BASE_INFO_SAVE_URL = "/system/parameter/save/base-info"; + + public static final String BASE_INFO_URL = "/system/parameter/get/base-info"; + + public static final String EMAIL_INFO_URL = "/system/parameter/get/email-info"; + + public static final String EMAIL_INFO_SAVE_URL = "/system/parameter/edit/email-info"; + + + public static final String EMAIL_INFO_TEST_CONNECT_URL = "/system/parameter/test/email"; + + private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); + @BeforeEach public void login() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login") @@ -48,26 +62,20 @@ public class SystemParameterControllerTest { @Order(1) public void testSaveBaseInfo() throws Exception { - List systemParameters = new ArrayList<>(); - SystemParameter systemParameter = new SystemParameter(); - systemParameter.setParamKey("base.url"); - systemParameter.setParamValue("https://baidu.com"); - systemParameter.setType("text"); - SystemParameter parameter = new SystemParameter(); - parameter.setParamKey("base.prometheus.host"); - parameter.setParamValue("http://127.0.0.1:1111"); - parameter.setType("text"); - systemParameters.add(systemParameter); - systemParameters.add(parameter); + List systemParameters = new ArrayList<>() {{ + add(new SystemParameter() {{ + setParamKey("base.url"); + setParamValue("https://baidu.com"); + setType("text"); + }}); + add(new SystemParameter() {{ + setParamKey("base.prometheus.host"); + setParamValue("http://127.0.0.1:1111"); + setType("text"); + }}); + }}; - 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()); + this.requestPost(BASE_INFO_SAVE_URL, systemParameters); } @@ -75,21 +83,13 @@ public class SystemParameterControllerTest { @Test @Order(2) public void testGetBaseInfo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/base-info") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) - .andDo(print()); - + this.requestGet(BASE_INFO_URL); } @Test @Order(3) public void testGetEmailInfo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/email-info") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()); + this.requestGet(EMAIL_INFO_URL); } @@ -97,76 +97,93 @@ public class SystemParameterControllerTest { @Order(4) public void testEditEmailInfo() throws Exception { - List systemParameters = new ArrayList<>(); - SystemParameter systemParameter1 = new SystemParameter(); - systemParameter1.setParamKey("smtp.host"); - systemParameter1.setParamValue("xxx.xxx.com"); - systemParameter1.setType("text"); - - SystemParameter systemParameter2 = new SystemParameter(); - systemParameter2.setParamKey("smtp.port"); - systemParameter2.setParamValue("xxx"); - systemParameter2.setType("text"); - - SystemParameter systemParameter3 = new SystemParameter(); - systemParameter3.setParamKey("smtp.account"); - systemParameter3.setParamValue("aaa@qq.com"); - systemParameter3.setType("text"); - - - systemParameters.add(systemParameter1); - systemParameters.add(systemParameter2); - systemParameters.add(systemParameter3); - - mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/edit/email-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()); + List systemParameters = new ArrayList<>() {{ + add(new SystemParameter() {{ + setParamKey("smtp.host"); + setParamValue("https://baidu.com"); + setType("text"); + }}); + add(new SystemParameter() {{ + setParamKey("smtp.port"); + setParamValue("8080"); + setType("text"); + }}); + add(new SystemParameter() {{ + setParamKey("smtp.account"); + setParamValue("aaa@fit2cloud.com"); + setType("text"); + }}); + add(new SystemParameter() {{ + setParamKey("smtp.ssl"); + setParamValue("true"); + setType("text"); + }}); + }}; + this.requestPost(EMAIL_INFO_SAVE_URL, systemParameters); } @Test @Order(4) public void testEmailConnect() throws Exception { HashMap hashMap = new HashMap<>(); - hashMap.put("smtp.host", "xx"); - hashMap.put("smtp.port", "xx"); - hashMap.put("smtp.account", "xx"); - hashMap.put("smtp.password", "xx"); - hashMap.put("smtp.from", "xx"); - hashMap.put("smtp.recipient", "xx"); + hashMap.put("smtp.host", "https://baidu.com"); + hashMap.put("smtp.port", "80"); + hashMap.put("smtp.account", "aaa@fit2cloud.com"); + hashMap.put("smtp.password", "test"); + hashMap.put("smtp.from", "aaa@fit2cloud.com"); + hashMap.put("smtp.recipient", "aaa@fit2cloud.com"); hashMap.put("smtp.ssl", "ture"); hashMap.put("smtp.tls", "false"); - mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/test/email") - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken) - .content(JSON.toJSONString(hashMap)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().is5xxServerError()); + this.requestPost(EMAIL_INFO_TEST_CONNECT_URL, hashMap, ERROR_REQUEST_MATCHER); } @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); + public void testSaveBaseInfoError() throws Exception { + List systemParameters = new ArrayList<>() {{ + add(new SystemParameter() {{ + setParamKey("base.url"); + setParamValue("https://baidu.com"); + setType("text"); + }}); + add(new SystemParameter() {{ + setParamKey(""); + setParamValue(""); + setType("text"); + }}); + }}; + this.requestPost(BASE_INFO_SAVE_URL, systemParameters); - mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/save/base-info") + } + + + private MvcResult requestPost(String url, Object param) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.post(url) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) - .content(JSON.toJSONString(systemParameters)) + .content(JSON.toJSONString(param)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andDo(print()); + .andDo(print()) + .andReturn(); + } + private MvcResult requestGet(String url) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)) + .andExpect(status().isOk()).andDo(print()).andReturn(); + } + + private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(param)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(resultMatcher).andDo(print()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } }