feat(系统设置): 系统参数设置接口及测试用例调整

This commit is contained in:
WangXu10 2023-07-04 11:24:41 +08:00 committed by 刘瑞斌
parent a953f85056
commit 06211cc0b3
9 changed files with 295 additions and 163 deletions

View File

@ -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_already_exists=Authentication source name already exists
authsource_name_is_null=Authentication source name cannot be empty authsource_name_is_null=Authentication source name cannot be empty
authsource_configuration_is_null=Authentication source configuration 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 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: custom_field_already=A feild already exists under this organization:
template_already=A template already exists under this organization: template_already=A template already exists under this organization:

View File

@ -237,6 +237,7 @@ import_fail_custom_num_exists=导入失败自定义ID已存在
authsource_name_already_exists=认证源名称已经存在 authsource_name_already_exists=认证源名称已经存在
authsource_name_is_null=认证源名称不能为空 authsource_name_is_null=认证源名称不能为空
authsource_configuration_is_null=认证源配置不能为空 authsource_configuration_is_null=认证源配置不能为空
authsource_type_is_null=认证源类型不能为空
custom_field_already=工作空间下已存在该字段: custom_field_already=工作空间下已存在该字段:
template_already=工作空间下已存在该模板: template_already=工作空间下已存在该模板:
expect_name_exists=预期名称已存在 expect_name_exists=预期名称已存在

View File

@ -236,6 +236,7 @@ import_fail_custom_num_exists=導入失敗自定義ID已存在
authsource_name_already_exists=認證源名稱已經存在 authsource_name_already_exists=認證源名稱已經存在
authsource_name_is_null=認證源名稱不能為空 authsource_name_is_null=認證源名稱不能為空
authsource_configuration_is_null=認證源配置不能為空 authsource_configuration_is_null=認證源配置不能為空
authsource_type_is_null=認證源類型不能為空
custom_field_already=工作空間下已存在該字段: custom_field_already=工作空間下已存在該字段:
template_already=工作空間下已存在該模板: template_already=工作空間下已存在該模板:
expect_name_exists=預期名稱已存在 expect_name_exists=預期名稱已存在

View File

@ -3,14 +3,18 @@ package io.metersphere.system.controller;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.BasePageRequest;
import io.metersphere.sdk.log.annotation.Log; import io.metersphere.sdk.log.annotation.Log;
import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.AuthSource; import io.metersphere.system.domain.AuthSource;
import io.metersphere.system.request.AuthSourceRequest;
import io.metersphere.system.service.AuthSourceService; import io.metersphere.system.service.AuthSourceService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -23,40 +27,54 @@ public class AuthSourceController {
@Resource @Resource
private AuthSourceService authSourceService; private AuthSourceService authSourceService;
@PostMapping("/list/{goPage}/{pageSize}") @PostMapping("/list")
@Operation(summary = "认证设置列表查询")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
public Pager<List<AuthSource>> list(@PathVariable int goPage, @PathVariable int pageSize) { public Pager<List<AuthSource>> list(@Validated @RequestBody BasePageRequest request) {
Page<Object> page = PageHelper.startPage(goPage, pageSize, true); Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(),
StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc");
return PageUtils.setPageInfo(page, authSourceService.list()); return PageUtils.setPageInfo(page, authSourceService.list());
} }
@PostMapping("/add") @PostMapping("/add")
@Operation(summary = "新增认证设置")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_CREAT) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_CREAT)
@Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING,
details = "认证设置") details = "认证设置")
public void add(@Validated @RequestBody AuthSource authSource) { public void add(@Validated @RequestBody AuthSourceRequest authSource) {
authSourceService.addAuthSource(authSource); authSourceService.addAuthSource(authSource);
} }
@PostMapping("/update") @PostMapping("/update")
@Operation(summary = "更新认证设置")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING,
details = "认证设置", sourceId = "#authSource.id") details = "认证设置", sourceId = "#authSource.id")
public void update(@Validated @RequestBody AuthSource authSource) { public void update(@Validated @RequestBody AuthSourceRequest authSource) {
authSourceService.updateAuthSource(authSource); authSourceService.updateAuthSource(authSource);
} }
@GetMapping("/get/{id}") @GetMapping("/get/{id}")
@Operation(summary = "获取认证设置详细信息")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
public AuthSource get(@PathVariable(value = "id") String id) { public AuthSource get(@PathVariable(value = "id") String id) {
return authSourceService.getAuthSource(id); return authSourceService.getAuthSource(id);
} }
@GetMapping("/delete/{id}") @GetMapping("/delete/{id}")
@Operation(summary = "删除认证设置")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_DELETE) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_DELETE)
@Log(type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, @Log(type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING,
details = "认证设置", sourceId = "#id") details = "认证设置", sourceId = "#id")
public void delete(@PathVariable(value = "id") String id) { public void delete(@PathVariable(value = "id") String id) {
authSourceService.deleteAuthSource(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);
}
} }

View File

@ -9,6 +9,8 @@ import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.service.SystemParameterService; import io.metersphere.sdk.service.SystemParameterService;
import io.metersphere.system.domain.SystemParameter; 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 jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -18,6 +20,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
@RestController @RestController
@Tag(name = "系统参数")
@RequestMapping("/system/parameter") @RequestMapping("/system/parameter")
public class SystemParameterController { public class SystemParameterController {
@ -25,31 +28,25 @@ public class SystemParameterController {
SystemParameterService systemParameterService; SystemParameterService systemParameterService;
/**
* 基本配置
*
* @param systemParameter
*/
@PostMapping("/save/base-info") @PostMapping("/save/base-info")
@Operation(summary = "保存基本信息")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE)
@Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "基本配置", sourceId = "#systemParameter.get(0).paramKey") @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "基本配置", sourceId = "#systemParameter.get(0).paramKey")
public void saveBaseParameter(@Validated @RequestBody List<SystemParameter> systemParameter) { public void saveBaseParameter(@Validated @RequestBody List<SystemParameter> systemParameter) {
systemParameterService.saveBaseInfo(systemParameter); systemParameterService.saveBaseInfo(systemParameter);
} }
@GetMapping("/get/base-info") @GetMapping("/get/base-info")
@Operation(summary = "获取基本信息")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
public BaseSystemConfigDTO getBaseInfo() { public BaseSystemConfigDTO getBaseInfo() {
return systemParameterService.getBaseInfo(); return systemParameterService.getBaseInfo();
} }
/**
* 邮件设置
*
* @return
*/
@GetMapping("/get/email-info") @GetMapping("/get/email-info")
@Operation(summary = "获取邮件信息")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
public EMailInfoDto getEmailInfo() { public EMailInfoDto getEmailInfo() {
return systemParameterService.getEmailInfo(); return systemParameterService.getEmailInfo();
@ -57,6 +54,7 @@ public class SystemParameterController {
@PostMapping("/edit/email-info") @PostMapping("/edit/email-info")
@Operation(summary = "保存邮件信息")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置", sourceId = "#systemParameter.get(0).paramKey") @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置", sourceId = "#systemParameter.get(0).paramKey")
public void editEMailInfo(@Validated @RequestBody List<SystemParameter> systemParameter) { public void editEMailInfo(@Validated @RequestBody List<SystemParameter> systemParameter) {
@ -64,12 +62,8 @@ public class SystemParameterController {
} }
/**
* 邮件测试连接
*
* @param hashMap
*/
@PostMapping("/test/email") @PostMapping("/test/email")
@Operation(summary = "测试连接")
@RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ) @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ)
public void testEmailConnection(@RequestBody HashMap<String, String> hashMap) { public void testEmailConnection(@RequestBody HashMap<String, String> hashMap) {
systemParameterService.testEmailConnection(hashMap); systemParameterService.testEmailConnection(hashMap);

View File

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

View File

@ -6,6 +6,7 @@ import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.AuthSource; import io.metersphere.system.domain.AuthSource;
import io.metersphere.system.domain.AuthSourceExample; import io.metersphere.system.domain.AuthSourceExample;
import io.metersphere.system.mapper.AuthSourceMapper; import io.metersphere.system.mapper.AuthSourceMapper;
import io.metersphere.system.request.AuthSourceRequest;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -25,16 +26,26 @@ public class AuthSourceService {
return authSourceMapper.selectByExample(example); return authSourceMapper.selectByExample(example);
} }
public void addAuthSource(AuthSource authSource) { public void addAuthSource(AuthSourceRequest authSource) {
checkAuthSource(authSource); checkAuthSource(authSource);
long createTime = System.currentTimeMillis(); AuthSource source = delRequestToDB(authSource);
authSource.setCreateTime(createTime); authSourceMapper.insertSelective(source);
authSource.setUpdateTime(createTime);
authSource.setId(UUID.randomUUID().toString());
authSourceMapper.insertSelective(authSource);
} }
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(); String resourcePoolName = authSource.getName();
if (StringUtils.isBlank(resourcePoolName)) { if (StringUtils.isBlank(resourcePoolName)) {
throw new MSException(Translator.get("authsource_name_is_null")); throw new MSException(Translator.get("authsource_name_is_null"));
@ -63,11 +74,23 @@ public class AuthSourceService {
return authSourceMapper.selectByPrimaryKey(id); return authSourceMapper.selectByPrimaryKey(id);
} }
public void updateAuthSource(AuthSource authSource) { public void updateAuthSource(AuthSourceRequest authSource) {
checkAuthSource(authSource); checkAuthSource(authSource);
authSource.setCreateTime(null); AuthSource source = authSourceMapper.selectByPrimaryKey(authSource.getId());
authSource.setUpdateTime(System.currentTimeMillis()); if (source != null) {
authSourceMapper.updateByPrimaryKeySelective(authSource); 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);
}
} }

View File

@ -2,8 +2,13 @@ package io.metersphere.system.controller;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import io.metersphere.sdk.constants.SessionConstants; 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.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.AuthSource; import io.metersphere.system.domain.AuthSource;
import io.metersphere.system.request.AuthSourceRequest;
import io.metersphere.utils.JsonUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -28,6 +37,18 @@ public class AuthSourceControllerTest {
private static String sessionId; private static String sessionId;
private static String csrfToken; 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 @BeforeEach
public void login() throws Exception { public void login() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login")
@ -44,89 +65,107 @@ public class AuthSourceControllerTest {
@Test @Test
@Order(1) @Order(1)
public void testAddSource() throws Exception { public void testAddSource() throws Exception {
AuthSource authSource = new AuthSource(); AuthSourceRequest authSource = new AuthSourceRequest();
authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf"); authSource.setConfiguration("123");
authSource.setConfiguration("123".getBytes());
authSource.setName("测试CAS"); authSource.setName("测试CAS");
authSource.setCreateTime(System.currentTimeMillis()); authSource.setType("CAS");
authSource.setUpdateTime(System.currentTimeMillis()); this.requestPost(AUTH_SOURCE_ADD, authSource);
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());
} }
@Test @Test
@Order(2) @Order(2)
public void testGetSourceList() throws Exception { public void testGetSourceList() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/list/1/10") BasePageRequest basePageRequest = new BasePageRequest();
.header(SessionConstants.HEADER_TOKEN, sessionId) basePageRequest.setCurrent(1);
.header(SessionConstants.CSRF_TOKEN, csrfToken)) basePageRequest.setPageSize(10);
.andExpect(status().isOk()) this.requestPost(AUTH_SOURCE_List, basePageRequest);
.andDo(print());
} }
@Test @Test
@Order(3) @Order(3)
public void testUpdateSource() throws Exception { public void testUpdateSource() throws Exception {
AuthSource authSource = new AuthSource(); List<AuthSourceRequest> authSourceList = this.getAuthSourceList();
authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf"); AuthSourceRequest authSource = new AuthSourceRequest();
authSource.setConfiguration("123666".getBytes()); authSource.setId(authSourceList.get(0).getId());
authSource.setConfiguration("123666");
authSource.setName("更新"); authSource.setName("更新");
authSource.setUpdateTime(System.currentTimeMillis()); authSource.setType("CAS");
mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/update") this.requestPost(AUTH_SOURCE_UPDATE, authSource);
.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());
} }
@Test @Test
@Order(4) @Order(4)
public void testGetSourceById() throws Exception { public void testUpdateStatus() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/system/authsource/get/2b6a83d0-7c66-43ed-a1d9-5132d3167aaf") List<AuthSourceRequest> authSourceList = this.getAuthSourceList();
.header(SessionConstants.HEADER_TOKEN, sessionId) this.requestGet(AUTH_SOURCE_UPDATE + "/" + authSourceList.get(0).getId() + "/status/false");
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andDo(print());
} }
@Test @Test
@Order(5) @Order(5)
public void testDelSourceById() throws Exception { public void testGetSourceById() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/system/authsource/delete/2b6a83d0-7c66-43ed-a1d9-5132d3167aaf") List<AuthSourceRequest> authSourceList = this.getAuthSourceList();
.header(SessionConstants.HEADER_TOKEN, sessionId) this.requestGet(AUTH_SOURCE_GET + authSourceList.get(0).getId());
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andDo(print());
} }
@Test @Test
@Order(6) @Order(6)
public void testAddSourceByNullName() throws Exception { public void testDelSourceById() throws Exception {
List<AuthSourceRequest> 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 authSource = new AuthSource();
authSource.setId("2b6a83d0-7c66-43ed-a1d9-5132d3167aaf");
authSource.setConfiguration("123".getBytes()); authSource.setConfiguration("123".getBytes());
authSource.setCreateTime(System.currentTimeMillis()); this.requestPost(AUTH_SOURCE_ADD, authSource, CLIENT_ERROR_MATCHER);
authSource.setUpdateTime(System.currentTimeMillis()); }
mockMvc.perform(MockMvcRequestBuilders.post("/system/authsource/add")
private MvcResult requestPost(String url, Object param) throws Exception {
return mockMvc.perform(MockMvcRequestBuilders.post(url)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(authSource)) .content(JSON.toJSONString(param))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is5xxServerError()) .andExpect(status().isOk())
.andDo(print()); .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<AuthSourceRequest> 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<AuthSourceRequest> 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));
} }
} }

View File

@ -11,6 +11,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.ArrayList; import java.util.ArrayList;
@ -32,6 +33,19 @@ public class SystemParameterControllerTest {
private static String sessionId; private static String sessionId;
private static String csrfToken; 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 @BeforeEach
public void login() throws Exception { public void login() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login")
@ -48,26 +62,20 @@ public class SystemParameterControllerTest {
@Order(1) @Order(1)
public void testSaveBaseInfo() throws Exception { public void testSaveBaseInfo() throws Exception {
List<SystemParameter> systemParameters = new ArrayList<>(); List<SystemParameter> systemParameters = new ArrayList<>() {{
SystemParameter systemParameter = new SystemParameter(); add(new SystemParameter() {{
systemParameter.setParamKey("base.url"); setParamKey("base.url");
systemParameter.setParamValue("https://baidu.com"); setParamValue("https://baidu.com");
systemParameter.setType("text"); setType("text");
SystemParameter parameter = new SystemParameter(); }});
parameter.setParamKey("base.prometheus.host"); add(new SystemParameter() {{
parameter.setParamValue("http://127.0.0.1:1111"); setParamKey("base.prometheus.host");
parameter.setType("text"); setParamValue("http://127.0.0.1:1111");
systemParameters.add(systemParameter); setType("text");
systemParameters.add(parameter); }});
}};
mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/save/base-info") this.requestPost(BASE_INFO_SAVE_URL, systemParameters);
.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());
} }
@ -75,21 +83,13 @@ public class SystemParameterControllerTest {
@Test @Test
@Order(2) @Order(2)
public void testGetBaseInfo() throws Exception { public void testGetBaseInfo() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/base-info") this.requestGet(BASE_INFO_URL);
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andDo(print());
} }
@Test @Test
@Order(3) @Order(3)
public void testGetEmailInfo() throws Exception { public void testGetEmailInfo() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/system/parameter/get/email-info") this.requestGet(EMAIL_INFO_URL);
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk());
} }
@ -97,76 +97,93 @@ public class SystemParameterControllerTest {
@Order(4) @Order(4)
public void testEditEmailInfo() throws Exception { public void testEditEmailInfo() throws Exception {
List<SystemParameter> systemParameters = new ArrayList<>(); List<SystemParameter> systemParameters = new ArrayList<>() {{
SystemParameter systemParameter1 = new SystemParameter(); add(new SystemParameter() {{
systemParameter1.setParamKey("smtp.host"); setParamKey("smtp.host");
systemParameter1.setParamValue("xxx.xxx.com"); setParamValue("https://baidu.com");
systemParameter1.setType("text"); setType("text");
}});
SystemParameter systemParameter2 = new SystemParameter(); add(new SystemParameter() {{
systemParameter2.setParamKey("smtp.port"); setParamKey("smtp.port");
systemParameter2.setParamValue("xxx"); setParamValue("8080");
systemParameter2.setType("text"); setType("text");
}});
SystemParameter systemParameter3 = new SystemParameter(); add(new SystemParameter() {{
systemParameter3.setParamKey("smtp.account"); setParamKey("smtp.account");
systemParameter3.setParamValue("aaa@qq.com"); setParamValue("aaa@fit2cloud.com");
systemParameter3.setType("text"); setType("text");
}});
add(new SystemParameter() {{
systemParameters.add(systemParameter1); setParamKey("smtp.ssl");
systemParameters.add(systemParameter2); setParamValue("true");
systemParameters.add(systemParameter3); setType("text");
}});
mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/edit/email-info") }};
.header(SessionConstants.HEADER_TOKEN, sessionId) this.requestPost(EMAIL_INFO_SAVE_URL, systemParameters);
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(systemParameters))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andDo(print());
} }
@Test @Test
@Order(4) @Order(4)
public void testEmailConnect() throws Exception { public void testEmailConnect() throws Exception {
HashMap<String, String> hashMap = new HashMap<>(); HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("smtp.host", "xx"); hashMap.put("smtp.host", "https://baidu.com");
hashMap.put("smtp.port", "xx"); hashMap.put("smtp.port", "80");
hashMap.put("smtp.account", "xx"); hashMap.put("smtp.account", "aaa@fit2cloud.com");
hashMap.put("smtp.password", "xx"); hashMap.put("smtp.password", "test");
hashMap.put("smtp.from", "xx"); hashMap.put("smtp.from", "aaa@fit2cloud.com");
hashMap.put("smtp.recipient", "xx"); hashMap.put("smtp.recipient", "aaa@fit2cloud.com");
hashMap.put("smtp.ssl", "ture"); hashMap.put("smtp.ssl", "ture");
hashMap.put("smtp.tls", "false"); hashMap.put("smtp.tls", "false");
mockMvc.perform(MockMvcRequestBuilders.post("/system/parameter/test/email") this.requestPost(EMAIL_INFO_TEST_CONNECT_URL, hashMap, ERROR_REQUEST_MATCHER);
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(hashMap))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is5xxServerError());
} }
@Test @Test
@Order(5) @Order(5)
public void testSaveBaseInfoNullUrl() throws Exception { public void testSaveBaseInfoError() throws Exception {
List<SystemParameter> systemParameters = new ArrayList<>(); List<SystemParameter> systemParameters = new ArrayList<>() {{
SystemParameter parameter = new SystemParameter(); add(new SystemParameter() {{
parameter.setParamKey("base.prometheus.host"); setParamKey("base.url");
parameter.setParamValue("http://127.0.0.1:1111"); setParamValue("https://baidu.com");
parameter.setType("text"); setType("text");
systemParameters.add(parameter); }});
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.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(systemParameters)) .content(JSON.toJSONString(param))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) .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));
} }
} }