refactor(系统设置): 优化测试用例逻辑及补充覆盖率

This commit is contained in:
song-cc-rock 2023-09-15 17:59:45 +08:00 committed by fit2-zhao
parent e79d88ec70
commit 22446027e0
7 changed files with 25 additions and 28 deletions

View File

@ -26,9 +26,10 @@ public interface ExtProjectUserRoleMapper {
* 根据用户组ID获取用户组成员关系
*
* @param roleIds 用户组ID集合
* @param projectId 项目ID
* @return 用户组成员关系
*/
List<UserRoleRelation> getRelationByRoleIds(@Param("roleIds") List<String> roleIds);
List<UserRoleRelation> getRelationByRoleIds(@Param("projectId") String projectId, @Param("roleIds") List<String> roleIds);
/**
* 获取项目成员列表

View File

@ -17,7 +17,7 @@
select urr.*
from user_role_relation urr
left join user u on urr.user_id = u.id
where u.deleted = 0 and urr.role_id in
where u.deleted = 0 and urr.source_id = #{projectId} and urr.role_id in
<foreach collection="roleIds" item="id" open="(" separator="," close=")">
#{id}
</foreach>

View File

@ -15,17 +15,17 @@ import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.uid.UUID;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -353,7 +353,7 @@ public class ProjectMemberService {
private boolean isUserOrRoleNotExist(String userId, String roleId) {
UserExample example = new UserExample();
example.createCriteria().andIdEqualTo(userId).andDeletedEqualTo(false);
return userMapper.selectByExample(example) == null || userRoleMapper.selectByPrimaryKey(roleId) == null;
return CollectionUtils.isEmpty(userMapper.selectByExample(example)) || userRoleMapper.selectByPrimaryKey(roleId) == null;
}
/**

View File

@ -56,7 +56,7 @@ public class ProjectUserRoleService extends BaseUserRoleService {
return new ArrayList<>();
}
List<String> roleIds = roles.stream().map(ProjectUserRoleDTO::getId).toList();
List<UserRoleRelation> relations = extProjectUserRoleMapper.getRelationByRoleIds(roleIds);
List<UserRoleRelation> relations = extProjectUserRoleMapper.getRelationByRoleIds(request.getProjectId(), roleIds);
if (CollectionUtils.isNotEmpty(relations)) {
Map<String, Long> countMap = relations.stream().collect(Collectors.groupingBy(UserRoleRelation::getRoleId, Collectors.counting()));
roles.forEach(role -> {
@ -139,9 +139,6 @@ public class ProjectUserRoleService extends BaseUserRoleService {
public List<PermissionDefinitionItem> getPermissionSetting(String id) {
UserRole userRole = get(id);
if (userRole == null) {
throw new MSException(Translator.get("user_role_not_exist"));
}
checkProjectUserRole(userRole);
return getPermissionSetting(userRole);
}

View File

@ -8,18 +8,18 @@ import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.uid.UUID;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.utils.ServiceUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.*;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.service.OperationLogService;
import io.metersphere.system.mapper.*;
import io.metersphere.system.request.*;
import io.metersphere.system.uid.UUID;
import io.metersphere.system.utils.ServiceUtils;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -83,8 +83,8 @@ public class OrganizationService {
if (CollectionUtils.isEmpty(organizationDTOS)) {
return new ArrayList<>();
}
List<OrganizationDTO> organizations = buildExtraInfo(organizationDTOS);
return buildOrgAdminInfo(organizations);
List<OrganizationDTO> organizations = buildOrgAdminInfo(organizationDTOS);
return buildExtraInfo(organizations);
}
/**
@ -794,9 +794,6 @@ public class OrganizationService {
* @return 组织列表
*/
private List<OrganizationDTO> buildOrgAdminInfo(List<OrganizationDTO> organizationDTOS) {
if (CollectionUtils.isEmpty(organizationDTOS)) {
return organizationDTOS;
}
organizationDTOS.forEach(organizationDTO -> {
List<User> orgAdminList = extOrganizationMapper.getOrgAdminList(organizationDTO.getId());
organizationDTO.setOrgAdmins(orgAdminList);

View File

@ -1,15 +1,15 @@
package io.metersphere.system.controller;
import io.metersphere.system.base.BaseTest;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.OrganizationDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.request.OrganizationMemberRequest;
import io.metersphere.system.request.OrganizationRequest;
import io.metersphere.system.request.ProjectRequest;
@ -184,9 +184,9 @@ public class SystemOrganizationControllerTests extends BaseTest{
String sortData = sortResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class);
Pager<?> sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class);
// 返回值中取出第一条ID最大的数据, 并判断是否是default-admin
// 返回值中取出第一条ID最大的数据, 并判断是否是admin
UserExtend userExtend1 = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), UserExtend.class).get(0);
Assertions.assertTrue(StringUtils.contains(userExtend1.getId(), "default-admin"));
Assertions.assertTrue(StringUtils.contains(userExtend1.getId(), "admin"));
// 权限校验
requestPostPermissionsTest(List.of(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, PermissionConstants.SYSTEM_USER_READ),
ORGANIZATION_LIST_MEMBER, organizationRequest);

View File

@ -14,6 +14,8 @@ INSERT INTO organization(id, num, name, description, create_time, update_time, c
INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUE
('default-admin', 'default-Administrator', 'admin-default@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUE
(UUID(), 'default-admin', 'org_admin', 'default-organization-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
(UUID(), 'admin', 'org_admin', 'default-organization-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUE
(UUID(), 'default-admin', 'org_admin', 'default-organization-3', 'default-organization-3', UNIX_TIMESTAMP() * 1000, 'admin');
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUE
('default-project', null, 'default-organization-2', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);