diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java index cc2e5f9f1e..6b9cfc3da5 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java @@ -21,6 +21,7 @@ import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.StorageType; +import io.metersphere.sdk.file.FileRequest; import io.metersphere.sdk.util.FileAssociationSourceUtil; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.TempFileUtils; @@ -29,13 +30,14 @@ import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.dto.AddProjectRequest; import io.metersphere.system.dto.sdk.BaseTreeNode; import io.metersphere.system.dto.sdk.request.NodeMoveRequest; -import io.metersphere.sdk.file.FileRequest; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.service.CommonProjectService; import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; +import lombok.AllArgsConstructor; +import lombok.Data; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.junit.jupiter.api.*; @@ -95,6 +97,8 @@ public class FileManagementControllerTests extends BaseTest { @Resource private CommonProjectService commonProjectService; + List checkLogModelList = new ArrayList<>(); + @BeforeEach public void initTestData() { //文件管理专用项目 @@ -168,8 +172,9 @@ public class FileManagementControllerTests extends BaseTest { Assertions.assertNotNull(baseTreeNode.getParentId()); } Assertions.assertNotNull(a1Node); - checkLog(a1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD); - + checkLogModelList.add( + new CheckLogModel(a1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD) + ); //测试a1无法获取存储库详情 this.requestGet(String.format(FileManagementRequestUtils.URL_FILE_REPOSITORY_INFO, a1Node.getId())).andExpect(status().is5xxServerError()); @@ -206,8 +211,13 @@ public class FileManagementControllerTests extends BaseTest { } Assertions.assertNotNull(a2Node); Assertions.assertNotNull(a1b1Node); - checkLog(a2Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD); - checkLog(a1b1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD); + + checkLogModelList.add( + new CheckLogModel(a2Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD) + ); + checkLogModelList.add( + new CheckLogModel(a1b1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD) + ); //a1节点下可以继续添加a1节点 request = new FileModuleCreateRequest(); @@ -231,7 +241,9 @@ public class FileManagementControllerTests extends BaseTest { } } Assertions.assertNotNull(a1ChildNode); - checkLog(a1ChildNode.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD); + checkLogModelList.add( + new CheckLogModel(a1ChildNode.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD) + ); //a1的子节点a1下继续创建节点a1-a1-c1 request = new FileModuleCreateRequest(); @@ -258,8 +270,9 @@ public class FileManagementControllerTests extends BaseTest { } } Assertions.assertNotNull(a1a1c1Node); - checkLog(a1a1c1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD); - + checkLogModelList.add( + new CheckLogModel(a1a1c1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD) + ); //子节点a1-b1下继续创建节点a1-b1-c1 request = new FileModuleCreateRequest(); request.setProjectId(project.getId()); @@ -284,7 +297,9 @@ public class FileManagementControllerTests extends BaseTest { Assertions.assertNotNull(a1b1c1Node); preliminaryTreeNodes = treeNodes; - checkLog(a1b1c1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD); + checkLogModelList.add( + new CheckLogModel(a1b1c1Node.getId(), OperationLogType.ADD, FileManagementRequestUtils.URL_MODULE_ADD) + ); } @Test @@ -367,7 +382,9 @@ public class FileManagementControllerTests extends BaseTest { this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_MODULE_UPDATE, updateRequest); preliminaryTreeNodes = this.getFileModuleTreeNode(); - checkLog(a1Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_UPDATE); + checkLogModelList.add( + new CheckLogModel(a1Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_UPDATE) + ); } @Test @@ -411,7 +428,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); MvcResult mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); String returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); picFileId = returnId; uploadedFileTypes.add("JPG"); @@ -431,7 +450,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); jarFileId = returnId; uploadedFileTypes.add("jar"); @@ -445,7 +466,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); fileUploadRequest.setEnable(false); @@ -457,7 +480,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); //svg文件,用于测试预览图下载 @@ -468,7 +493,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); uploadedFileTypes.add("svg"); @@ -487,7 +514,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); uploadedFileTypes.add(FileMetadataUtils.FILE_TYPE_EMPTY); @@ -508,7 +537,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); uploadedFileTypes.add("txt"); @@ -527,7 +558,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/noSuffixFile.unknown")).getPath(); @@ -537,7 +570,9 @@ public class FileManagementControllerTests extends BaseTest { paramMap.add("request", JSON.toJSONString(fileUploadRequest)); mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD) + ); FILE_ID_PATH.put(returnId, filePath); //检查文件类型获取接口有没有获取到数据 @@ -664,7 +699,9 @@ public class FileManagementControllerTests extends BaseTest { //重新上传并修改文件版本 MvcResult mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_RE_UPLOAD, paramMap); String reUploadId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); - checkLog(reUploadId, OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_RE_UPLOAD); + checkLogModelList.add( + new CheckLogModel(reUploadId, OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_RE_UPLOAD) + ); FILE_ID_PATH.put(reUploadId, filePath); FILE_VERSIONS_ID_MAP.put(reUploadId, reUploadFileId); fileAssociationNewFileId = reUploadId; @@ -1032,7 +1069,9 @@ public class FileManagementControllerTests extends BaseTest { updateRequest.setModuleId(a1a1Node.getId()); this.requestPostWithOk(FileManagementRequestUtils.URL_FILE_UPDATE, updateRequest); this.checkFileInformation(updateFileId, oldFileMetadata, updateRequest); - checkLog(updateRequest.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_UPDATE); + checkLogModelList.add( + new CheckLogModel(updateRequest.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_UPDATE) + ); //只改描述 oldFileMetadata = fileMetadataMapper.selectByPrimaryKey(updateFileId); @@ -1150,7 +1189,9 @@ public class FileManagementControllerTests extends BaseTest { //测试启用 this.requestGetWithOk(String.format(FileManagementRequestUtils.URL_CHANGE_JAR_ENABLE, jarFileId, true)); this.checkFileEnable(jarFileId, true); - this.checkLog(jarFileId, OperationLogType.UPDATE, "/project/file/jar-file-status"); + checkLogModelList.add( + new CheckLogModel(jarFileId, OperationLogType.UPDATE, "/project/file/jar-file-status") + ); //测试禁用 this.requestGetWithOk(String.format(FileManagementRequestUtils.URL_CHANGE_JAR_ENABLE, jarFileId, false)); this.checkFileEnable(jarFileId, false); @@ -1285,7 +1326,9 @@ public class FileManagementControllerTests extends BaseTest { this.requestPostWithOk(FileManagementRequestUtils.URL_FILE_DELETE, fileBatchProcessRequest); this.checkFileIsDeleted(fileMetadataId, refId); - checkLog(fileMetadataId, OperationLogType.DELETE, FileManagementRequestUtils.URL_FILE_DELETE); + checkLogModelList.add( + new CheckLogModel(fileMetadataId, OperationLogType.DELETE, FileManagementRequestUtils.URL_FILE_DELETE) + ); } FILE_VERSIONS_ID_MAP.clear(); @@ -2029,8 +2072,12 @@ public class FileManagementControllerTests extends BaseTest { this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false); } - checkLog(a1Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_MOVE); - checkLog(a3Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_MOVE); + checkLogModelList.add( + new CheckLogModel(a1Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_MOVE) + ); + checkLogModelList.add( + new CheckLogModel(a3Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_MOVE) + ); } @Test @@ -2049,7 +2096,9 @@ public class FileManagementControllerTests extends BaseTest { }}); this.requestPostWithOk(FileManagementRequestUtils.URL_FILE_BATCH_UPDATE, moveRequest); this.checkFileModule(picFileId, a1a1c1Node.getId()); - checkLog(picFileId, OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_BATCH_UPDATE); + checkLogModelList.add( + new CheckLogModel(picFileId, OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_BATCH_UPDATE) + ); //所有文件批量移动 moveRequest = new FileBatchMoveRequest(); moveRequest.setMoveModuleId(a1a1c1Node.getId()); @@ -2154,13 +2203,17 @@ public class FileManagementControllerTests extends BaseTest { BaseTreeNode a1b1Node = FileManagementBaseUtils.getNodeByName(this.getFileModuleTreeNode(), "a1-b1"); this.requestGetWithOk(String.format(FileManagementRequestUtils.URL_MODULE_DELETE, a1b1Node.getId())); this.checkModuleIsEmpty(a1b1Node.getId()); - checkLog(a1b1Node.getId(), OperationLogType.DELETE, FileManagementRequestUtils.URL_MODULE_DELETE); + checkLogModelList.add( + new CheckLogModel(a1b1Node.getId(), OperationLogType.DELETE, FileManagementRequestUtils.URL_MODULE_DELETE) + ); // 删除有文件的节点 a1-a1 检查是否级联删除根节点 BaseTreeNode a1a1Node = FileManagementBaseUtils.getNodeByName(this.getFileModuleTreeNode(), "a1-a1"); this.requestGetWithOk(String.format(FileManagementRequestUtils.URL_MODULE_DELETE, a1a1Node.getId())); this.checkModuleIsEmpty(a1a1Node.getId()); - checkLog(a1a1Node.getId(), OperationLogType.DELETE, FileManagementRequestUtils.URL_MODULE_DELETE); + checkLogModelList.add( + new CheckLogModel(a1a1Node.getId(), OperationLogType.DELETE, FileManagementRequestUtils.URL_MODULE_DELETE) + ); //删除不存在的节点 this.requestGetWithOk(String.format(FileManagementRequestUtils.URL_MODULE_DELETE, IDGenerator.nextNum())); @@ -2358,4 +2411,25 @@ public class FileManagementControllerTests extends BaseTest { } Assertions.assertTrue(error); } + + @Test + @Order(100) + public void testLog() throws Exception { + Thread.sleep(5000); + for (CheckLogModel checkLogModel : checkLogModelList) { + if (org.apache.commons.lang3.StringUtils.isEmpty(checkLogModel.getUrl())) { + this.checkLog(checkLogModel.getResourceId(), checkLogModel.getOperationType()); + } else { + this.checkLog(checkLogModel.getResourceId(), checkLogModel.getOperationType(), checkLogModel.getUrl()); + } + } + } +} + +@Data +@AllArgsConstructor +class CheckLogModel { + private String resourceId; + private OperationLogType operationType; + private String url; } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileRepositoryControllerTest.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileRepositoryControllerTest.java index 1b3cda2eac..38578b2418 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileRepositoryControllerTest.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileRepositoryControllerTest.java @@ -24,6 +24,8 @@ import io.metersphere.system.service.CommonProjectService; import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.utils.Pager; import jakarta.annotation.Resource; +import lombok.AllArgsConstructor; +import lombok.Data; import org.apache.commons.collections4.CollectionUtils; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -66,6 +68,8 @@ public class FileRepositoryControllerTest extends BaseTest { @Resource private CommonProjectService commonProjectService; + List checkLogModelList = new ArrayList<>(); + @BeforeEach public void initTestData() { //文件管理专用项目 @@ -162,7 +166,9 @@ public class FileRepositoryControllerTest extends BaseTest { ResultHolder rh = JSON.parseObject(returnStr, ResultHolder.class); repositoryId = rh.getData().toString(); this.checkFileRepository(repositoryId, createRequest.getProjectId(), createRequest.getName(), createRequest.getPlatform(), createRequest.getUrl(), createRequest.getToken(), createRequest.getUserName()); - this.checkLog(repositoryId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_CREATE); + checkLogModelList.add( + new RepositoryCheckLogModel(repositoryId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_CREATE) + ); //测试获取详情 MvcResult mvcResult = this.requestGetWithOkAndReturn(String.format(FileManagementRequestUtils.URL_FILE_REPOSITORY_INFO, repositoryId)); @@ -190,7 +196,9 @@ public class FileRepositoryControllerTest extends BaseTest { returnStr = result.getResponse().getContentAsString(); rh = JSON.parseObject(returnStr, ResultHolder.class); this.checkFileRepository(rh.getData().toString(), createRequest.getProjectId(), createRequest.getName(), createRequest.getPlatform(), createRequest.getUrl(), createRequest.getToken(), createRequest.getUserName()); - this.checkLog(rh.getData().toString(), OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_CREATE); + checkLogModelList.add( + new RepositoryCheckLogModel(rh.getData().toString(), OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_CREATE) + ); //参数测试: 没有url createRequest = new FileRepositoryCreateRequest(); @@ -263,7 +271,9 @@ public class FileRepositoryControllerTest extends BaseTest { createRequest.setName("GITEA存储库改个名字"); this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_REPOSITORY_UPDATE, createRequest); this.checkFileRepository(repositoryId, project.getId(), "GITEA存储库改个名字", ModuleConstants.NODE_TYPE_GITEA, GITEA_URL, GITEA_TOKEN, null); - this.checkLog(repositoryId, OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_REPOSITORY_UPDATE); + checkLogModelList.add( + new RepositoryCheckLogModel(repositoryId, OperationLogType.UPDATE, FileManagementRequestUtils.URL_FILE_REPOSITORY_UPDATE) + ); //修改用户名 FileModuleRepository updateModel = new FileModuleRepository(); updateModel.setFileModuleId(repositoryId); @@ -317,7 +327,9 @@ public class FileRepositoryControllerTest extends BaseTest { this.requestGetWithOk(String.format(FileManagementRequestUtils.URL_MODULE_DELETE, repositoryId)); this.checkRepositoryDeleted(repositoryId); - checkLog(repositoryId, OperationLogType.DELETE, FileManagementRequestUtils.URL_MODULE_DELETE); + checkLogModelList.add( + new RepositoryCheckLogModel(repositoryId, OperationLogType.DELETE, FileManagementRequestUtils.URL_MODULE_DELETE) + ); //重新添加 FileRepositoryCreateRequest createRequest = new FileRepositoryCreateRequest(); @@ -331,7 +343,9 @@ public class FileRepositoryControllerTest extends BaseTest { ResultHolder rh = JSON.parseObject(returnStr, ResultHolder.class); repositoryId = rh.getData().toString(); this.checkFileRepository(repositoryId, createRequest.getProjectId(), createRequest.getName(), createRequest.getPlatform(), createRequest.getUrl(), createRequest.getToken(), createRequest.getUserName()); - this.checkLog(repositoryId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_CREATE); + checkLogModelList.add( + new RepositoryCheckLogModel(repositoryId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_CREATE) + ); } @@ -379,7 +393,9 @@ public class FileRepositoryControllerTest extends BaseTest { MvcResult result = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_REPOSITORY_FILE_ADD, request); String fileId = JSON.parseObject(result.getResponse().getContentAsString(), ResultHolder.class).getData().toString(); this.checkRepositoryFile(fileId, request); - this.checkLog(fileId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_FILE_ADD); + checkLogModelList.add( + new RepositoryCheckLogModel(fileId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_REPOSITORY_FILE_ADD) + ); getFileMessage(fileId); fileList.add(fileId); @@ -628,11 +644,25 @@ public class FileRepositoryControllerTest extends BaseTest { fileBatchProcessRequest.setSelectIds(fileList); this.requestPostWithOk(FileManagementRequestUtils.URL_FILE_DELETE, fileBatchProcessRequest); for (String fileId : fileList) { - this.checkLog(fileId, OperationLogType.DELETE, FileManagementRequestUtils.URL_FILE_DELETE); + checkLogModelList.add( + new RepositoryCheckLogModel(fileId, OperationLogType.DELETE, FileManagementRequestUtils.URL_FILE_DELETE) + ); this.checkRepositoryFileDeleted(fileId); } } + @Test + @Order(100) + public void testLog() throws Exception { + Thread.sleep(5000); + for (RepositoryCheckLogModel checkLogModel : checkLogModelList) { + if (org.apache.commons.lang3.StringUtils.isEmpty(checkLogModel.getUrl())) { + this.checkLog(checkLogModel.getResourceId(), checkLogModel.getOperationType()); + } else { + this.checkLog(checkLogModel.getResourceId(), checkLogModel.getOperationType(), checkLogModel.getUrl()); + } + } + } private void checkFileRepository(String repositoryId, String projectId, String name, String platform, String url, String token, String userName) { FileModule module = fileModuleMapper.selectByPrimaryKey(repositoryId); @@ -672,3 +702,11 @@ public class FileRepositoryControllerTest extends BaseTest { return JSON.parseArray(JSON.toJSONString(resultHolder.getData()), String.class); } } + +@Data +@AllArgsConstructor +class RepositoryCheckLogModel { + private String resourceId; + private OperationLogType operationType; + private String url; +} \ No newline at end of file diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java index 8258b3027e..a33b94029d 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/user/UserControllerTests.java @@ -42,6 +42,8 @@ import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.user.UserParamUtils; import io.metersphere.system.utils.user.UserRequestUtils; import jakarta.annotation.Resource; +import lombok.AllArgsConstructor; +import lombok.Data; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.*; @@ -100,6 +102,8 @@ public class UserControllerTests extends BaseTest { UserRequestUtils userRequestUtils = null; + List checkLogModelList = new ArrayList<>(); + @Override @BeforeEach public void login() throws Exception { @@ -336,21 +340,27 @@ public class UserControllerTests extends BaseTest { user.setName("TEST-UPDATE"); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); - checkLog(response.getId(), OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(response.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE) + ); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); //更改邮箱 user.setEmail("songtianyang-test-email@12138.com"); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); - checkLog(response.getId(), OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(response.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE) + ); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); //更改手机号 user.setPhone("18511112222"); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); - checkLog(response.getId(), OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(response.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE) + ); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); //更改用户组(这里只改成用户成员权限) @@ -359,12 +369,16 @@ public class UserControllerTests extends BaseTest { ); response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); checkDTO = this.getUserByEmail(user.getEmail()); - checkLog(response.getId(), OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(response.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE) + ); UserParamUtils.compareUserDTO(response, checkDTO); //更改用户组(把上面的情况添加别的权限) userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); - checkLog(response.getId(), OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(response.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE) + ); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); //用户信息复原 @@ -372,7 +386,9 @@ public class UserControllerTests extends BaseTest { BeanUtils.copyBean(user, USER_LIST.get(0)); userMaintainRequest = UserParamUtils.getUserUpdateDTO(user, USER_ROLE_LIST); response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(UserRequestUtils.URL_USER_UPDATE, userMaintainRequest), UserEditRequest.class); - checkLog(response.getId(), OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(response.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE) + ); checkDTO = this.getUserByEmail(user.getEmail()); UserParamUtils.compareUserDTO(response, checkDTO); } @@ -432,7 +448,9 @@ public class UserControllerTests extends BaseTest { userChangeEnableRequest.setEnable(false); this.requestPost(UserRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, status().isOk()); for (String item : userChangeEnableRequest.getSelectIds()) { - checkLog(item, OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(item, OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE_ENABLE) + ); } UserDTO userDTO = this.getUserByEmail(userInfo.getEmail()); @@ -442,7 +460,9 @@ public class UserControllerTests extends BaseTest { userChangeEnableRequest.setEnable(true); this.requestPost(UserRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, status().isOk()); for (String item : userChangeEnableRequest.getSelectIds()) { - checkLog(item, OperationLogType.UPDATE); + checkLogModelList.add( + new CheckLogModel(item, OperationLogType.UPDATE, UserRequestUtils.URL_USER_UPDATE_ENABLE) + ); } userDTO = this.getUserByEmail(userInfo.getEmail()); @@ -486,7 +506,9 @@ public class UserControllerTests extends BaseTest { UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值 List userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 for (UserDTO item : userDTOList) { - checkLog(item.getId(), OperationLogType.ADD); + checkLogModelList.add( + new CheckLogModel(item.getId(), OperationLogType.ADD, UserRequestUtils.URL_USER_IMPORT) + ); } //导入空文件. 应当导入成功的数据为0 @@ -514,7 +536,9 @@ public class UserControllerTests extends BaseTest { UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 for (UserDTO item : userDTOList) { - checkLog(item.getId(), OperationLogType.ADD); + checkLogModelList.add( + new CheckLogModel(item.getId(), OperationLogType.ADD, UserRequestUtils.URL_USER_IMPORT) + ); } //文件内邮箱重复 应当导入成功的数据为8 @@ -526,7 +550,9 @@ public class UserControllerTests extends BaseTest { UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex); userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 for (UserDTO item : userDTOList) { - checkLog(item.getId(), OperationLogType.ADD); + checkLogModelList.add( + new CheckLogModel(item.getId(), OperationLogType.ADD, UserRequestUtils.URL_USER_IMPORT) + ); } //文件不符合规范 应当导入成功的数据为0 @@ -547,7 +573,9 @@ public class UserControllerTests extends BaseTest { UserParamUtils.checkImportResponse(response, importSuccessData, errorDataIndex);//检查返回值 userDTOList = this.checkImportUserInDb(userImportReportDTOByFile);//检查数据已入库 for (UserDTO item : userDTOList) { - checkLog(item.getId(), OperationLogType.ADD); + checkLogModelList.add( + new CheckLogModel(item.getId(), OperationLogType.ADD, UserRequestUtils.URL_USER_IMPORT) + ); } } @@ -686,7 +714,9 @@ public class UserControllerTests extends BaseTest { UserExample userExample = new UserExample(); userExample.createCriteria().andIdEqualTo("admin").andPasswordEqualTo(CodingUtils.md5("metersphere")); Assertions.assertEquals(1, userMapper.countByExample(userExample)); - this.checkLog("admin", OperationLogType.UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD); + checkLogModelList.add( + new CheckLogModel("admin", OperationLogType.UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD) + ); } //重置普通用户密码 { @@ -707,7 +737,9 @@ public class UserControllerTests extends BaseTest { UserExample userExample = new UserExample(); userExample.createCriteria().andIdEqualTo(checkUser.getId()).andPasswordEqualTo(CodingUtils.md5(checkUser.getEmail())); Assertions.assertEquals(1, userMapper.countByExample(userExample)); - this.checkLog(checkUser.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD); + checkLogModelList.add( + new CheckLogModel(checkUser.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD) + ); } } //重置非Admin用户的密码 @@ -731,7 +763,9 @@ public class UserControllerTests extends BaseTest { UserExample userExample = new UserExample(); userExample.createCriteria().andIdEqualTo(checkUser.getId()).andPasswordEqualTo(CodingUtils.md5(checkUser.getEmail())); Assertions.assertEquals(1, userMapper.countByExample(userExample)); - this.checkLog(checkUser.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD); + checkLogModelList.add( + new CheckLogModel(checkUser.getId(), OperationLogType.UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD) + ); } } } @@ -756,7 +790,9 @@ public class UserControllerTests extends BaseTest { ); //检查日志 for (String userID : request.getSelectIds()) { - this.checkLog(userID, OperationLogType.UPDATE, UserRequestUtils.URL_USER_ROLE_RELATION); + checkLogModelList.add( + new CheckLogModel(userID, OperationLogType.UPDATE, UserRequestUtils.URL_USER_ROLE_RELATION) + ); } //测试重复添加用户权限。预期结果:不会额外增加数据 @@ -890,7 +926,9 @@ public class UserControllerTests extends BaseTest { } //检查日志 for (String userID : request.getSelectIds()) { - this.checkLog(userID, OperationLogType.UPDATE, UserRequestUtils.URL_ADD_PROJECT_MEMBER); + checkLogModelList.add( + new CheckLogModel(userID, OperationLogType.UPDATE, UserRequestUtils.URL_ADD_PROJECT_MEMBER) + ); } //获取用户信息 for (String userID : request.getSelectIds()) { @@ -932,7 +970,9 @@ public class UserControllerTests extends BaseTest { } //检查日志 for (String userID : request.getSelectIds()) { - this.checkLog(userID, OperationLogType.UPDATE, UserRequestUtils.URL_ADD_ORGANIZATION_MEMBER); + checkLogModelList.add( + new CheckLogModel(userID, OperationLogType.UPDATE, UserRequestUtils.URL_ADD_ORGANIZATION_MEMBER) + ); } //获取用户信息 @@ -1037,7 +1077,9 @@ public class UserControllerTests extends BaseTest { User user = userMapper.selectByPrimaryKey(deleteUser.getId()); Assertions.assertTrue(user.getDeleted()); //检查日志 - this.checkLog(deleteUser.getId(), OperationLogType.DELETE, UserRequestUtils.URL_USER_DELETE); + checkLogModelList.add( + new CheckLogModel(deleteUser.getId(), OperationLogType.DELETE, UserRequestUtils.URL_USER_DELETE) + ); removeList.add(deleteUser); } USER_LIST.removeAll(removeList); @@ -1067,6 +1109,19 @@ public class UserControllerTests extends BaseTest { } + @Test + @Order(101) + public void testLog() throws Exception { + Thread.sleep(5000); + for (CheckLogModel checkLogModel : checkLogModelList) { + if (StringUtils.isEmpty(checkLogModel.getUrl())) { + this.checkLog(checkLogModel.getResourceId(), checkLogModel.getOperationType()); + } else { + this.checkLog(checkLogModel.getResourceId(), checkLogModel.getOperationType(), checkLogModel.getUrl()); + } + } + } + //记录查询到的组织信息 private void setDefaultUserRoleList(MvcResult mvcResult) throws Exception { String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); @@ -1093,7 +1148,9 @@ public class UserControllerTests extends BaseTest { private void addUser2List(MvcResult mvcResult) throws Exception { UserBatchCreateDTO userMaintainRequest = userRequestUtils.parseObjectFromMvcResult(mvcResult, UserBatchCreateDTO.class); for (UserCreateInfo item : userMaintainRequest.getUserInfoList()) { - checkLog(item.getId(), OperationLogType.ADD); + checkLogModelList.add( + new CheckLogModel(item.getId(), OperationLogType.ADD, null) + ); } //返回值不为空 Assertions.assertNotNull(userMaintainRequest); @@ -1131,7 +1188,9 @@ public class UserControllerTests extends BaseTest { UserInviteResponse response = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), UserInviteResponse.class); Assertions.assertEquals(2, response.getInviteIds().size()); //检查日志 此处日志的资源是邀请的用户,即admin - this.checkLog("admin", OperationLogType.ADD, UserRequestUtils.URL_INVITE); + checkLogModelList.add( + new CheckLogModel("admin", OperationLogType.ADD, UserRequestUtils.URL_INVITE) + ); INVITE_RECORD_ID_LIST.addAll(response.getInviteIds()); } @@ -1204,7 +1263,9 @@ public class UserControllerTests extends BaseTest { ResultHolder resultHolder = JSON.parseObject(resultHolderStr, ResultHolder.class); //检查日志 此处日志的资源是邀请的用户,即admin - this.checkLog(resultHolder.getData().toString(), OperationLogType.ADD, UserRequestUtils.URL_INVITE_REGISTER); + checkLogModelList.add( + new CheckLogModel(resultHolder.getData().toString(), OperationLogType.ADD, UserRequestUtils.URL_INVITE_REGISTER) + ); } private void testUserRegisterError() throws Exception { @@ -1279,3 +1340,11 @@ public class UserControllerTests extends BaseTest { userRequestUtils.requestPost(UserRequestUtils.URL_INVITE_REGISTER, request, ERROR_REQUEST_MATCHER); } } + +@Data +@AllArgsConstructor +class CheckLogModel { + private String resourceId; + private OperationLogType operationType; + private String url; +}