From 251e666e4b0ddf6de6f8ac60b2fb51f6e7973de2 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Thu, 3 Aug 2023 16:33:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E8=AE=A4=E8=AF=81=E8=AE=BE=E7=BD=AE=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthSourceController.java | 17 ++++++------ .../request/AuthSourceStatusRequest.java | 18 +++++++++++++ .../system/service/AuthSourceService.java | 23 ++++++++-------- .../controller/AuthSourceControllerTests.java | 27 ++++++++++++++----- .../OperationLogControllerTests.java | 4 +-- 5 files changed, 62 insertions(+), 27 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceStatusRequest.java 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 3eb8cd5e37..42247adaa9 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 @@ -10,6 +10,7 @@ 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.request.AuthSourceStatusRequest; import io.metersphere.system.service.AuthSourceLogService; import io.metersphere.system.service.AuthSourceService; import io.swagger.v3.oas.annotations.Operation; @@ -42,16 +43,16 @@ public class AuthSourceController { @Operation(summary = "新增认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ_CREAT) @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#authSource)", msClass = AuthSourceLogService.class) - public void add(@Validated @RequestBody AuthSourceRequest authSource) { - authSourceService.addAuthSource(authSource); + public AuthSource add(@Validated @RequestBody AuthSourceRequest authSource) { + return authSourceService.addAuthSource(authSource); } @PostMapping("/update") @Operation(summary = "更新认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ_UPDATE) @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#authSource)", msClass = AuthSourceLogService.class) - public void update(@Validated @RequestBody AuthSourceRequest authSource) { - authSourceService.updateAuthSource(authSource); + public AuthSourceRequest update(@Validated @RequestBody AuthSourceRequest authSource) { + return authSourceService.updateAuthSource(authSource); } @GetMapping("/get/{id}") @@ -70,11 +71,11 @@ public class AuthSourceController { } - @GetMapping("/update/{authId}/status/{status}") + @PostMapping("/update/status") @Operation(summary = "更新状态") @RequiresPermissions(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ_UPDATE) - @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#authId)", msClass = AuthSourceLogService.class) - public void updateStatus(@PathVariable(value = "authId") String authId, @PathVariable("status") String status) { - authSourceService.updateStatus(authId, status); + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request.getId())", msClass = AuthSourceLogService.class) + public void updateStatus(@Validated @RequestBody AuthSourceStatusRequest request ) { + authSourceService.updateStatus(request.getId(), request.getEnable()); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceStatusRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceStatusRequest.java new file mode 100644 index 0000000000..0bf7384ce6 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/request/AuthSourceStatusRequest.java @@ -0,0 +1,18 @@ +package io.metersphere.system.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; + +@Data +@EqualsAndHashCode(callSuper = false) +public class AuthSourceStatusRequest implements Serializable { + private static final long serialVersionUID = 1L; + @Schema(title = "是否禁用") + private Boolean enable; + + @Schema(title = "id") + private String id; +} 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 6d6d32c9fe..0acdf43106 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 @@ -26,10 +26,11 @@ public class AuthSourceService { return authSourceMapper.selectByExample(example); } - public void addAuthSource(AuthSourceRequest authSource) { + public AuthSource addAuthSource(AuthSourceRequest authSource) { checkAuthSource(authSource); AuthSource source = delRequestToDB(authSource); authSourceMapper.insertSelective(source); + return source; } private AuthSource delRequestToDB(AuthSourceRequest authSource) { @@ -47,9 +48,6 @@ public class AuthSourceService { public void checkAuthSource(AuthSourceRequest authSource) { String resourcePoolName = authSource.getName(); - if (StringUtils.isBlank(resourcePoolName)) { - throw new MSException(Translator.get("authsource_name_is_null")); - } AuthSourceExample example = new AuthSourceExample(); AuthSourceExample.Criteria criteria = example.createCriteria(); @@ -74,7 +72,7 @@ public class AuthSourceService { return authSourceMapper.selectByPrimaryKey(id); } - public void updateAuthSource(AuthSourceRequest authSource) { + public AuthSourceRequest updateAuthSource(AuthSourceRequest authSource) { checkAuthSource(authSource); AuthSource source = authSourceMapper.selectByPrimaryKey(authSource.getId()); if (source != null) { @@ -84,13 +82,16 @@ public class AuthSourceService { source.setUpdateTime(System.currentTimeMillis()); authSourceMapper.updateByPrimaryKeySelective(source); } + return authSource; } - 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); + public void updateStatus(String id, Boolean status) { + if (status != null) { + AuthSource record = new AuthSource(); + record.setId(id); + record.setEnable(status); + record.setUpdateTime(System.currentTimeMillis()); + authSourceMapper.updateByPrimaryKeySelective(record); + } } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTests.java index 59bd5f6166..f8951b14f1 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/AuthSourceControllerTests.java @@ -9,6 +9,7 @@ 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.system.request.AuthSourceStatusRequest; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -21,6 +22,7 @@ import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.List; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; @@ -44,13 +46,18 @@ public class AuthSourceControllerTests extends BaseTest { private static final ResultMatcher CLIENT_ERROR_MATCHER = status().is4xxClientError(); + public static final String AUTH_SOURCE_UPDATE_STATUS = "/system/authsource/update/status"; + + private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); + @Test @Order(1) public void testAddSource() throws Exception { AuthSourceRequest authSource = new AuthSourceRequest(); - authSource.setConfiguration("123"); authSource.setName("测试CAS"); authSource.setType("CAS"); + this.requestPost(AUTH_SOURCE_ADD, authSource,ERROR_REQUEST_MATCHER); + authSource.setConfiguration("123"); this.requestPost(AUTH_SOURCE_ADD, authSource); // @@校验权限 @@ -64,6 +71,10 @@ public class AuthSourceControllerTests extends BaseTest { basePageRequest.setCurrent(1); basePageRequest.setPageSize(10); this.requestPost(AUTH_SOURCE_LIST, basePageRequest); + basePageRequest.setSort(new HashMap<>() {{ + put("createTime", "desc"); + }}); + this.requestPost(AUTH_SOURCE_LIST, basePageRequest); requestPostPermissionTest(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ, AUTH_SOURCE_LIST, basePageRequest); } @@ -74,11 +85,12 @@ public class AuthSourceControllerTests extends BaseTest { public void testUpdateSource() throws Exception { List authSourceList = this.getAuthSourceList(); AuthSourceRequest authSource = new AuthSourceRequest(); - authSource.setId(authSourceList.get(0).getId()); + authSource.setId("authsource_id"); authSource.setConfiguration("123666"); authSource.setName("更新"); authSource.setType("CAS"); this.requestPost(AUTH_SOURCE_UPDATE, authSource); + authSource.setId(authSourceList.get(0).getId()); requestPostPermissionTest(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ_UPDATE, AUTH_SOURCE_UPDATE, authSource); } @@ -86,11 +98,14 @@ public class AuthSourceControllerTests extends BaseTest { @Test @Order(4) public void testUpdateStatus() throws Exception { - List authSourceList = this.getAuthSourceList(); - String url = AUTH_SOURCE_UPDATE + "/" + authSourceList.get(0).getId() + "/status/false"; - this.requestGet(url); - requestGetPermissionTest(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ_UPDATE, url); + List authSourceList = this.getAuthSourceList(); + AuthSourceStatusRequest request = new AuthSourceStatusRequest(); + request.setId(authSourceList.get(0).getId()); + this.requestPost(AUTH_SOURCE_UPDATE_STATUS, request); + request.setEnable(false); + this.requestPost(AUTH_SOURCE_UPDATE_STATUS, request); + requestPostPermissionTest(PermissionConstants.SYSTEM_PARAMETER_SETTING_AUTH_READ_UPDATE, AUTH_SOURCE_UPDATE_STATUS, request); } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java index 1bb29c8140..f6692c9e5b 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OperationLogControllerTests.java @@ -132,7 +132,7 @@ public class OperationLogControllerTests extends BaseTest { this.requestGetWithOkAndReturn(OPTIONS_LIST); // @@校验权限 - //requestGetPermissionTest(PermissionConstants.SYSTEM_OPERATING_LOG_READ, OPTIONS_LIST); + requestGetPermissionTest(PermissionConstants.SYSTEM_OPERATING_LOG_READ, OPTIONS_LIST); } @Test @@ -141,7 +141,7 @@ public class OperationLogControllerTests extends BaseTest { this.requestGetWithOkAndReturn(USER_LIST); // @@校验权限 - // requestGetPermissionTest(PermissionConstants.SYSTEM_OPERATING_LOG_READ, USER_LIST); + requestGetPermissionTest(PermissionConstants.SYSTEM_OPERATING_LOG_READ, USER_LIST); } @Test