diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java index a0078b1d0f..b0a5eb70d7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java @@ -140,7 +140,7 @@ public class OrganizationService { String projectId = userRoleRelationsByUser.getSourceId(); String roleId = userRoleRelationsByUser.getRoleId(); String userId = userRoleRelationsByUser.getUserId(); - Map pIdNameMap = userIdprojectIdMap.get(projectId); + Map pIdNameMap = userIdprojectIdMap.get(userId); if (pIdNameMap == null || MapUtils.isEmpty(pIdNameMap)) { pIdNameMap = new HashMap<>(); } @@ -213,9 +213,6 @@ public class OrganizationService { } private Map checkUseRoleExist(List userRoleIds) { - if (CollectionUtils.isEmpty(userRoleIds)) { - throw new MSException(Translator.get("user_role_not_exist")); - } UserRoleExample userRoleExample = new UserRoleExample(); userRoleExample.createCriteria().andIdIn(userRoleIds).andTypeEqualTo("ORGANIZATION"); List userRoles = userRoleMapper.selectByExample(userRoleExample); @@ -227,9 +224,6 @@ public class OrganizationService { } private Map checkUserExist(List memberIds) { - if (CollectionUtils.isEmpty(memberIds)) { - throw new MSException(Translator.get("user.not.exist")); - } UserExample userExample = new UserExample(); userExample.createCriteria().andIdIn(memberIds); List users = userMapper.selectByExample(userExample); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java index 08b9e64b5b..4208801650 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java @@ -1,6 +1,7 @@ package io.metersphere.system.controller; import base.BaseTest; +import io.metersphere.sdk.constants.InternalUserRole; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.util.JSON; @@ -9,6 +10,7 @@ import io.metersphere.system.dto.OrgUserExtend; import io.metersphere.system.request.*; import io.metersphere.utils.JsonUtils; import jakarta.annotation.Resource; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -127,8 +129,12 @@ public class OrganizationControllerTests extends BaseTest { addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Arrays.asList("sys_default_user3", "sys_default_user4"), Arrays.asList("default-org-role-id-2", "default-org-role-id-3"), status().is5xxServerError()); // 成员有一个不存在 addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Arrays.asList("sys_default_user", "sys_default_user4"), Arrays.asList("default-org-role-id-2", "default-org-role-id-3"), status().is5xxServerError()); - // 用户组不存在 + // 用户组为空 addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Arrays.asList("sys_default_user", "sys_default_user2"), Collections.emptyList(), status().isBadRequest()); + // 用户组都不存在 + addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Arrays.asList("sys_default_user", "sys_default_user2"), Arrays.asList("default-org-role-id-8", "default-org-role-id-9"), status().is5xxServerError()); + // 用户组有一个不存在 + addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Arrays.asList("sys_default_user", "sys_default_user2"), Arrays.asList("default-org-role-id-2", "default-org-role-id-9"), status().is5xxServerError()); //成员和用户组都为空 addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Collections.emptyList(), Collections.emptyList(), status().isBadRequest()); // 组织不存在 @@ -194,6 +200,33 @@ public class OrganizationControllerTests extends BaseTest { @Test @Order(7) + public void getOrgMemberListSuccessWidthEmpty() throws Exception { + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("testUserOne"); + organizationRequest.setOrganizationId("sys_default_organization_6"); + MvcResult mvcResult = this.responsePost(organizationRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); + //判断是否为空 + List orgUserExtends = JSON.parseArray(JSON.toJSONString(pageData.getList()), OrgUserExtend.class); + Assertions.assertTrue(CollectionUtils.isEmpty(orgUserExtends)); + + } + + @Test + @Order(8) public void getOrgMemberListError() throws Exception { // 页码有误 OrganizationRequest organizationRequest = new OrganizationRequest(); @@ -213,7 +246,7 @@ public class OrganizationControllerTests extends BaseTest { @Test - @Order(8) + @Order(9) @Sql(scripts = {"/dml/init_sys_org_project.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) public void addOrgMemberToProjectSuccess() throws Exception { OrgMemberExtendProjectRequest organizationMemberRequest = new OrgMemberExtendProjectRequest(); @@ -222,11 +255,11 @@ public class OrganizationControllerTests extends BaseTest { organizationMemberRequest.setProjectIds(Arrays.asList("sys_org_projectId", "sys_org_projectId1")); this.requestPost(ORGANIZATION_PROJECT_ADD_MEMBER, organizationMemberRequest, status().isOk()); // 批量添加成员成功后, 验证是否添加成功 - listByKeyWord("testUserOne","sys_default_organization_3", false,null); + listByKeyWord("testUserOne","sys_default_organization_3", true, InternalUserRole.PROJECT_MEMBER.getValue()); } @Test - @Order(9) + @Order(10) public void addOrgMemberToProjectError() throws Exception { // 成员选择为空 addOrUpdateOrganizationProjectMemberError(ORGANIZATION_PROJECT_ADD_MEMBER, "sys_default_organization_3", Collections.emptyList(), Arrays.asList("projectId1", "projectId2"), status().isBadRequest()); @@ -237,13 +270,19 @@ public class OrganizationControllerTests extends BaseTest { } @Test - @Order(10) + @Order(11) public void removeOrgMemberSuccess() throws Exception { + this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_6/sys_default_user", status().isOk()); + } + + @Test + @Order(12) + public void removeOrgMemberSuccessWithNoProject() throws Exception { this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_3/sys_default_user", status().isOk()); } @Test - @Order(11) + @Order(13) public void removeOrgMemberError() throws Exception { // 项目不存在 this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError()); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java index 8ab20e7808..a23a9fccaf 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java @@ -113,7 +113,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(1) + @Order(2) public void testListOrganizationError() throws Exception { // 页码有误 OrganizationRequest organizationRequest = new OrganizationRequest(); @@ -128,7 +128,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(2) + @Order(3) public void testListAllOrganizationSuccess() throws Exception { MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_ALL, null); // 获取返回值 @@ -141,13 +141,13 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(3) + @Order(4) public void testListAllOrganizationError() throws Exception { this.requestGet(ORGANIZATION_LIST_ALL, status().isMethodNotAllowed()); } @Test - @Order(4) + @Order(5) public void testListOrganizationMemberSuccess() throws Exception { OrganizationRequest organizationRequest = new OrganizationRequest(); organizationRequest.setCurrent(1); @@ -187,7 +187,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(5) + @Order(6) public void testListOrganizationMemberError() throws Exception { // 页码有误 OrganizationRequest organizationRequest = new OrganizationRequest(); @@ -207,7 +207,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ @Test - @Order(6) + @Order(7) public void testAddOrganizationMemberSuccess() throws Exception { OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); organizationMemberRequest.setOrganizationId("default-organization-3"); @@ -240,13 +240,56 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(7) + @Order(8) + public void testAddOrganizationMemberSuccessWithRepeatUser() throws Exception { + OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); + organizationMemberRequest.setOrganizationId("default-organization-3"); + organizationMemberRequest.setMemberIds(Arrays.asList("admin", "admin","default-admin")); + this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isOk()); + // 批量添加成员成功后, 验证是否添加成功 + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("admin"); + organizationRequest.setOrganizationId("default-organization-3"); + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_MEMBER, organizationRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); + // 返回值中取出第一条数据, 并判断是否包含关键字admin + UserExtend userExtend = JSON.parseArray(JSON.toJSONString(pageData.getList()), UserExtend.class).get(0); + Assertions.assertTrue(StringUtils.contains(userExtend.getName(), organizationRequest.getKeyword()) + || StringUtils.contains(userExtend.getEmail(), organizationRequest.getKeyword()) + || StringUtils.contains(userExtend.getPhone(), organizationRequest.getKeyword())); + } + + @Test + @Order(9) public void testAddOrganizationMemberError() throws Exception { // 成员选择为空 OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); organizationMemberRequest.setOrganizationId("default-organization-3"); organizationMemberRequest.setMemberIds(Collections.emptyList()); this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isBadRequest()); + // 成员都不存在 + organizationMemberRequest = new OrganizationMemberRequest(); + organizationMemberRequest.setOrganizationId("default-organization-3"); + organizationMemberRequest.setMemberIds(Arrays.asList("SccNotExistOne", "SccNotExistTwo")); + this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().is5xxServerError()); + // 成员有一个不存在 + organizationMemberRequest = new OrganizationMemberRequest(); + organizationMemberRequest.setOrganizationId("default-organization-3"); + organizationMemberRequest.setMemberIds(Arrays.asList("SccNotExistOne", "default-admin")); + this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().is5xxServerError()); // 组织不存在 organizationMemberRequest = new OrganizationMemberRequest(); organizationMemberRequest.setOrganizationId("default-organization-x"); @@ -255,22 +298,20 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(8) + @Order(10) public void testRemoveOrganizationMemberSuccess() throws Exception { this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin", status().isOk()); } @Test - @Order(9) + @Order(11) public void testRemoveOrganizationMemberError() throws Exception { // 组织不存在 this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError()); - // 用户不存在 - this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin-x", status().is5xxServerError()); } @Test - @Order(10) + @Order(12) public void testGetOrganizationProjectSuccess() throws Exception { ProjectRequest projectRequest = new ProjectRequest(); projectRequest.setCurrent(1); @@ -304,7 +345,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(11) + @Order(13) public void testGetOrganizationProjectError() throws Exception { // 页码有误 ProjectRequest projectRequest = new ProjectRequest(); @@ -321,7 +362,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(12) + @Order(14) public void testGetDefaultOrganizationSuccess() throws Exception { MvcResult mvcResult = this.responseGet(SystemOrganizationControllerTests.ORGANIZATION_DEFAULT); // 获取返回值 @@ -337,7 +378,7 @@ public class SystemOrganizationControllerTests extends BaseTest{ } @Test - @Order(13) + @Order(15) public void testGetDefaultOrganizationError() throws Exception { this.requestPost(ORGANIZATION_DEFAULT, null, status().isMethodNotAllowed()); } diff --git a/backend/services/system-setting/src/test/resources/dml/init_sys_org_project.sql b/backend/services/system-setting/src/test/resources/dml/init_sys_org_project.sql index 5e5d45b9fd..7d6e2753b5 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_sys_org_project.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_sys_org_project.sql @@ -1,6 +1,6 @@ # 插入测试数据 -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('sys_org_projectId', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), 'sys_org_projectId', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); -INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('sys_org_projectId1', null, (SELECT id FROM organization WHERE name LIKE '默认组织'), 'sys_org_projectId1', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('sys_org_projectId', null, 'sys_default_organization_3', 'sys_org_projectId', '系统默认创建的项目', 'admin', 'admin', unix_timestamp() * 1000, unix_timestamp() * 1000); +INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES ('sys_org_projectId1', null, 'sys_default_organization_3', 'sys_org_projectId1', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000);