diff --git a/backend/framework/sdk/src/test/java/base/BaseTest.java b/backend/framework/sdk/src/test/java/base/BaseTest.java index ceec11a41b..517ff56a55 100644 --- a/backend/framework/sdk/src/test/java/base/BaseTest.java +++ b/backend/framework/sdk/src/test/java/base/BaseTest.java @@ -4,6 +4,8 @@ import com.jayway.jsonpath.JsonPath; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Pager; +import io.metersphere.system.domain.OperationLogExample; +import io.metersphere.system.mapper.OperationLogMapper; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.BeforeEach; @@ -32,6 +34,8 @@ public abstract class BaseTest { private MockMvc mockMvc; protected static String sessionId; protected static String csrfToken; + @Resource + private OperationLogMapper operationLogMapper; /** * 可以重写该方法定义 BASE_PATH @@ -69,8 +73,8 @@ public abstract class BaseTest { } protected ResultActions requestPost(String url, Object param, Object... uriVariables) throws Exception { - return mockMvc.perform(getPostRequestBuilder(url, param, uriVariables)) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + return mockMvc.perform(getPostRequestBuilder(url, param, uriVariables)) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } protected MvcResult requestPostAndReturn(String url, Object... uriVariables) throws Exception { @@ -102,7 +106,7 @@ public abstract class BaseTest { .andExpect(status().isOk()); } - protected MvcResult requestPostWithOkAndReturn(String url, Object param, Object... uriVariables) throws Exception { + protected MvcResult requestPostWithOkAndReturn(String url, Object param, Object... uriVariables) throws Exception { return this.requestPostWithOk(url, param, uriVariables).andReturn(); } @@ -126,4 +130,16 @@ public abstract class BaseTest { pager.setList(list); return pager; } + + protected void checkLog(String resourceId, String type) throws Exception { + OperationLogExample example = new OperationLogExample(); + example.createCriteria().andSourceIdEqualTo(resourceId).andTypeEqualTo(type); + operationLogMapper.selectByExample(example).stream() + .filter(operationLog -> operationLog.getSourceId().equals(resourceId)) + .filter(operationLog -> operationLog.getType().equals(type)) + .filter(operationLog -> StringUtils.isNotBlank(operationLog.getProjectId())) + .filter(operationLog -> StringUtils.isNotBlank(operationLog.getModule())) + .findFirst() + .orElseThrow(() -> new Exception("日志不存在,请补充操作日志")); + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java index edddd40a39..e0e14f0e61 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java @@ -10,6 +10,7 @@ import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.AddProjectRequest; import io.metersphere.sdk.dto.ProjectDTO; import io.metersphere.sdk.dto.UpdateProjectRequest; +import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Pager; import io.metersphere.system.domain.UserRoleRelation; @@ -167,6 +168,9 @@ public class SystemProjectControllerTests extends BaseTest { projectExample.createCriteria().andOrganizationIdEqualTo(project.getOrganizationId()).andNameEqualTo(project.getName()); List projects = projectMapper.selectByExample(projectExample); projectId = result.getId(); + // 校验日志 + checkLog(projectId, OperationLogType.ADD.name()); + this.compareProjectDTO(projects.get(0), result); UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample(); userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue()); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java index d2d4e0a8b3..0a4e1c7267 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java @@ -6,6 +6,7 @@ import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.ExcelParseDTO; import io.metersphere.sdk.dto.UserDTO; +import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Pager; @@ -90,8 +91,15 @@ public class UserControllerTests extends BaseTest { } //成功入库的用户保存内存中,其他用例会使用到 - private void addUser2List(MvcResult mvcResult) { + private void addUser2List(MvcResult mvcResult){ UserBatchCreateDTO userMaintainRequest = UserTestUtils.parseObjectFromMvcResult(mvcResult, UserBatchCreateDTO.class); + userMaintainRequest.getUserInfoList().forEach(item ->{ + try { + checkLog(item.getId(), OperationLogType.ADD.name()); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); //返回值不为空 Assertions.assertNotNull(userMaintainRequest); USER_LIST.addAll(userMaintainRequest.getUserInfoList());