feat: add org member

This commit is contained in:
guoyuqi 2023-07-21 20:11:30 +08:00 committed by 刘瑞斌
parent bf38341b64
commit 546c35f1e5
4 changed files with 104 additions and 30 deletions

View File

@ -140,7 +140,7 @@ public class OrganizationService {
String projectId = userRoleRelationsByUser.getSourceId(); String projectId = userRoleRelationsByUser.getSourceId();
String roleId = userRoleRelationsByUser.getRoleId(); String roleId = userRoleRelationsByUser.getRoleId();
String userId = userRoleRelationsByUser.getUserId(); String userId = userRoleRelationsByUser.getUserId();
Map<String, String> pIdNameMap = userIdprojectIdMap.get(projectId); Map<String, String> pIdNameMap = userIdprojectIdMap.get(userId);
if (pIdNameMap == null || MapUtils.isEmpty(pIdNameMap)) { if (pIdNameMap == null || MapUtils.isEmpty(pIdNameMap)) {
pIdNameMap = new HashMap<>(); pIdNameMap = new HashMap<>();
} }
@ -213,9 +213,6 @@ public class OrganizationService {
} }
private Map<String, UserRole> checkUseRoleExist(List<String> userRoleIds) { private Map<String, UserRole> checkUseRoleExist(List<String> userRoleIds) {
if (CollectionUtils.isEmpty(userRoleIds)) {
throw new MSException(Translator.get("user_role_not_exist"));
}
UserRoleExample userRoleExample = new UserRoleExample(); UserRoleExample userRoleExample = new UserRoleExample();
userRoleExample.createCriteria().andIdIn(userRoleIds).andTypeEqualTo("ORGANIZATION"); userRoleExample.createCriteria().andIdIn(userRoleIds).andTypeEqualTo("ORGANIZATION");
List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample); List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
@ -227,9 +224,6 @@ public class OrganizationService {
} }
private Map<String, User> checkUserExist(List<String> memberIds) { private Map<String, User> checkUserExist(List<String> memberIds) {
if (CollectionUtils.isEmpty(memberIds)) {
throw new MSException(Translator.get("user.not.exist"));
}
UserExample userExample = new UserExample(); UserExample userExample = new UserExample();
userExample.createCriteria().andIdIn(memberIds); userExample.createCriteria().andIdIn(memberIds);
List<User> users = userMapper.selectByExample(userExample); List<User> users = userMapper.selectByExample(userExample);

View File

@ -1,6 +1,7 @@
package io.metersphere.system.controller; package io.metersphere.system.controller;
import base.BaseTest; import base.BaseTest;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
@ -9,6 +10,7 @@ import io.metersphere.system.dto.OrgUserExtend;
import io.metersphere.system.request.*; import io.metersphere.system.request.*;
import io.metersphere.utils.JsonUtils; import io.metersphere.utils.JsonUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
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;
@ -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_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_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"), 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()); addOrUpdateOrganizationMemberError(ORGANIZATION_UPDATE_MEMBER, "sys_default_organization_3", Collections.emptyList(), Collections.emptyList(), status().isBadRequest());
// 组织不存在 // 组织不存在
@ -194,6 +200,33 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(7) @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<OrgUserExtend> orgUserExtends = JSON.parseArray(JSON.toJSONString(pageData.getList()), OrgUserExtend.class);
Assertions.assertTrue(CollectionUtils.isEmpty(orgUserExtends));
}
@Test
@Order(8)
public void getOrgMemberListError() throws Exception { public void getOrgMemberListError() throws Exception {
// 页码有误 // 页码有误
OrganizationRequest organizationRequest = new OrganizationRequest(); OrganizationRequest organizationRequest = new OrganizationRequest();
@ -213,7 +246,7 @@ public class OrganizationControllerTests extends BaseTest {
@Test @Test
@Order(8) @Order(9)
@Sql(scripts = {"/dml/init_sys_org_project.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) @Sql(scripts = {"/dml/init_sys_org_project.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void addOrgMemberToProjectSuccess() throws Exception { public void addOrgMemberToProjectSuccess() throws Exception {
OrgMemberExtendProjectRequest organizationMemberRequest = new OrgMemberExtendProjectRequest(); OrgMemberExtendProjectRequest organizationMemberRequest = new OrgMemberExtendProjectRequest();
@ -222,11 +255,11 @@ public class OrganizationControllerTests extends BaseTest {
organizationMemberRequest.setProjectIds(Arrays.asList("sys_org_projectId", "sys_org_projectId1")); organizationMemberRequest.setProjectIds(Arrays.asList("sys_org_projectId", "sys_org_projectId1"));
this.requestPost(ORGANIZATION_PROJECT_ADD_MEMBER, organizationMemberRequest, status().isOk()); 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 @Test
@Order(9) @Order(10)
public void addOrgMemberToProjectError() throws Exception { public void addOrgMemberToProjectError() throws Exception {
// 成员选择为空 // 成员选择为空
addOrUpdateOrganizationProjectMemberError(ORGANIZATION_PROJECT_ADD_MEMBER, "sys_default_organization_3", Collections.emptyList(), Arrays.asList("projectId1", "projectId2"), status().isBadRequest()); 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 @Test
@Order(10) @Order(11)
public void removeOrgMemberSuccess() throws Exception { 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()); this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/sys_default_organization_3/sys_default_user", status().isOk());
} }
@Test @Test
@Order(11) @Order(13)
public void removeOrgMemberError() throws Exception { public void removeOrgMemberError() throws Exception {
// 项目不存在 // 项目不存在
this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError()); this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError());

View File

@ -113,7 +113,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(1) @Order(2)
public void testListOrganizationError() throws Exception { public void testListOrganizationError() throws Exception {
// 页码有误 // 页码有误
OrganizationRequest organizationRequest = new OrganizationRequest(); OrganizationRequest organizationRequest = new OrganizationRequest();
@ -128,7 +128,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(2) @Order(3)
public void testListAllOrganizationSuccess() throws Exception { public void testListAllOrganizationSuccess() throws Exception {
MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_ALL, null); MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_ALL, null);
// 获取返回值 // 获取返回值
@ -141,13 +141,13 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(3) @Order(4)
public void testListAllOrganizationError() throws Exception { public void testListAllOrganizationError() throws Exception {
this.requestGet(ORGANIZATION_LIST_ALL, status().isMethodNotAllowed()); this.requestGet(ORGANIZATION_LIST_ALL, status().isMethodNotAllowed());
} }
@Test @Test
@Order(4) @Order(5)
public void testListOrganizationMemberSuccess() throws Exception { public void testListOrganizationMemberSuccess() throws Exception {
OrganizationRequest organizationRequest = new OrganizationRequest(); OrganizationRequest organizationRequest = new OrganizationRequest();
organizationRequest.setCurrent(1); organizationRequest.setCurrent(1);
@ -187,7 +187,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(5) @Order(6)
public void testListOrganizationMemberError() throws Exception { public void testListOrganizationMemberError() throws Exception {
// 页码有误 // 页码有误
OrganizationRequest organizationRequest = new OrganizationRequest(); OrganizationRequest organizationRequest = new OrganizationRequest();
@ -207,7 +207,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
@Test @Test
@Order(6) @Order(7)
public void testAddOrganizationMemberSuccess() throws Exception { public void testAddOrganizationMemberSuccess() throws Exception {
OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest();
organizationMemberRequest.setOrganizationId("default-organization-3"); organizationMemberRequest.setOrganizationId("default-organization-3");
@ -240,13 +240,56 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @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 { public void testAddOrganizationMemberError() throws Exception {
// 成员选择为空 // 成员选择为空
OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest();
organizationMemberRequest.setOrganizationId("default-organization-3"); organizationMemberRequest.setOrganizationId("default-organization-3");
organizationMemberRequest.setMemberIds(Collections.emptyList()); organizationMemberRequest.setMemberIds(Collections.emptyList());
this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isBadRequest()); 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 = new OrganizationMemberRequest();
organizationMemberRequest.setOrganizationId("default-organization-x"); organizationMemberRequest.setOrganizationId("default-organization-x");
@ -255,22 +298,20 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(8) @Order(10)
public void testRemoveOrganizationMemberSuccess() throws Exception { public void testRemoveOrganizationMemberSuccess() throws Exception {
this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin", status().isOk()); this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin", status().isOk());
} }
@Test @Test
@Order(9) @Order(11)
public void testRemoveOrganizationMemberError() throws Exception { public void testRemoveOrganizationMemberError() throws Exception {
// 组织不存在 // 组织不存在
this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin-x", status().is5xxServerError()); 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 @Test
@Order(10) @Order(12)
public void testGetOrganizationProjectSuccess() throws Exception { public void testGetOrganizationProjectSuccess() throws Exception {
ProjectRequest projectRequest = new ProjectRequest(); ProjectRequest projectRequest = new ProjectRequest();
projectRequest.setCurrent(1); projectRequest.setCurrent(1);
@ -304,7 +345,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(11) @Order(13)
public void testGetOrganizationProjectError() throws Exception { public void testGetOrganizationProjectError() throws Exception {
// 页码有误 // 页码有误
ProjectRequest projectRequest = new ProjectRequest(); ProjectRequest projectRequest = new ProjectRequest();
@ -321,7 +362,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(12) @Order(14)
public void testGetDefaultOrganizationSuccess() throws Exception { public void testGetDefaultOrganizationSuccess() throws Exception {
MvcResult mvcResult = this.responseGet(SystemOrganizationControllerTests.ORGANIZATION_DEFAULT); MvcResult mvcResult = this.responseGet(SystemOrganizationControllerTests.ORGANIZATION_DEFAULT);
// 获取返回值 // 获取返回值
@ -337,7 +378,7 @@ public class SystemOrganizationControllerTests extends BaseTest{
} }
@Test @Test
@Order(13) @Order(15)
public void testGetDefaultOrganizationError() throws Exception { public void testGetDefaultOrganizationError() throws Exception {
this.requestPost(ORGANIZATION_DEFAULT, null, status().isMethodNotAllowed()); this.requestPost(ORGANIZATION_DEFAULT, null, status().isMethodNotAllowed());
} }

View File

@ -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_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, (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_projectId1', null, 'sys_default_organization_3', 'sys_org_projectId1', '系统默认创建的项目', 'test', 'test', unix_timestamp() * 1000, unix_timestamp() * 1000);