refactor(系统设置): 优化用户管理的权限单元测试

This commit is contained in:
song-tianyang 2023-08-22 10:47:41 +08:00 committed by 建国
parent ca943d1a8a
commit 2e689901e2
9 changed files with 143 additions and 199 deletions

View File

@ -155,9 +155,8 @@ public class UserController {
@PostMapping("/add-project-member") @PostMapping("/add-project-member")
//todo 这里权限有更改 权限待定 @Operation(summary = "批量添加用户到项目")
@Operation(summary = "添加用户到项目") @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_READ_UPDATE, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_MEMBER_ADD}, logical = Logical.AND)
// @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE)
public void addProjectMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) { public void addProjectMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) {
ProjectAddMemberBatchRequest request = new ProjectAddMemberBatchRequest(); ProjectAddMemberBatchRequest request = new ProjectAddMemberBatchRequest();
request.setProjectIds(userRoleBatchRelationRequest.getRoleIds()); request.setProjectIds(userRoleBatchRelationRequest.getRoleIds());
@ -166,9 +165,8 @@ public class UserController {
} }
@PostMapping("/add-org-member") @PostMapping("/add-org-member")
@Operation(summary = "添加用户到组织") @Operation(summary = "批量添加用户到组织")
//todo 这里权限有更改 权限待定 @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_READ_UPDATE, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_MEMBER_ADD}, logical = Logical.AND)
// @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE)
public void addMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) { public void addMember(@Validated @RequestBody UserRoleBatchRelationRequest userRoleBatchRelationRequest) {
OrganizationMemberBatchRequest request = new OrganizationMemberBatchRequest(); OrganizationMemberBatchRequest request = new OrganizationMemberBatchRequest();
request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds()); request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds());

View File

@ -21,7 +21,7 @@ public class UserBaseBatchRequest {
> userIds; > userIds;
@Schema(description = "不处理的用户ID") @Schema(description = "不处理的用户ID")
List<String> skipIds; List<String> excludeIds;
@Schema(description = "是否选择所有数据") @Schema(description = "是否选择所有数据")
private boolean selectAll; private boolean selectAll;

View File

@ -468,8 +468,8 @@ public class UserService {
if (request.isSelectAll()) { if (request.isSelectAll()) {
List<User> userList = baseUserMapper.selectByKeyword(request.getCondition().getKeyword(), true); List<User> userList = baseUserMapper.selectByKeyword(request.getCondition().getKeyword(), true);
List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList()); List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(request.getSkipIds())) { if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
userIdList.removeAll(request.getSkipIds()); userIdList.removeAll(request.getExcludeIds());
} }
return userIdList; return userIdList;
} else { } else {

View File

@ -1,136 +0,0 @@
package io.metersphere.system.controller.user;
import com.jayway.jsonpath.JsonPath;
import io.metersphere.system.dto.UserCreateInfo;
import io.metersphere.system.request.user.UserAndRoleBatchRequest;
import io.metersphere.system.request.user.UserBaseBatchRequest;
import io.metersphere.system.request.user.UserChangeEnableRequest;
import io.metersphere.system.response.user.UserSelectOption;
import io.metersphere.system.utils.user.UserParamUtils;
import io.metersphere.system.utils.user.UserRequestUtils;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Sql(scripts = {"/dml/init_user_controller_none_permission_test.sql"},
config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED),
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public class UserControllerNonePermissionTests {
@Resource
private MockMvc mockMvc;
UserRequestUtils userRequestUtils = null;
private static final String NONE_ROLE_USERNAME = "tianyang.member@163.com";
private static final String NONE_ROLE_PASSWORD = "tianyang.member@163.com";
private static final ResultMatcher CHECK_RESULT_MATHER = status().isForbidden();
/**
* 用户创建接口判断无权限用户是否可以访问
* 接口参数校验优先级大于操作用户权限校验所以参数要保证合法化
*/
@Test
@Order(0)
public void testGetGlobalSystemUserRoleSuccess() throws Exception {
UserCreateInfo paramUserInfo = new UserCreateInfo() {{
setId("testId");
setName("tianyang.no.permission.email");
setEmail("tianyang.no.permission.email@126.com");
}};
List<UserSelectOption> paramRoleList = new ArrayList<>() {{
this.add(
new UserSelectOption() {{
this.setId("member");
this.setName("member");
}});
}};
userRequestUtils.requestGet(String.format(userRequestUtils.URL_USER_GET, NONE_ROLE_USERNAME), CHECK_RESULT_MATHER);
//校验权限系统全局用户组获取
userRequestUtils.requestGet(userRequestUtils.URL_GET_GLOBAL_SYSTEM, CHECK_RESULT_MATHER);
//校验权限用户创建
userRequestUtils.requestPost(userRequestUtils.URL_USER_CREATE,
UserParamUtils.getUserCreateDTO(
paramRoleList,
new ArrayList<>() {{
add(paramUserInfo);
}}
),
CHECK_RESULT_MATHER);
//校验权限分页查询用户列表
userRequestUtils.requestPost(userRequestUtils.URL_USER_PAGE, UserParamUtils.getDefaultPageRequest(), CHECK_RESULT_MATHER);
//校验权限修改用户
userRequestUtils.requestPost(userRequestUtils.URL_USER_UPDATE,
UserParamUtils.getUserUpdateDTO(paramUserInfo, paramRoleList), CHECK_RESULT_MATHER);
//校验权限启用/禁用用户
UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest();
userChangeEnableRequest.setEnable(false);
userChangeEnableRequest.setUserIds(new ArrayList<>() {{
this.add("testId");
}});
userRequestUtils.requestPost(userRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest, CHECK_RESULT_MATHER);
//用户导入
//导入正常文件
String filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_success.xlsx")).getPath();
MockMultipartFile file = new MockMultipartFile("file", "userImport.xlsx", MediaType.APPLICATION_OCTET_STREAM_VALUE, UserParamUtils.getFileBytes(filePath));
userRequestUtils.requestFile(userRequestUtils.URL_USER_IMPORT, file, CHECK_RESULT_MATHER);
//用户删除
UserBaseBatchRequest request = new UserBaseBatchRequest();
request.setUserIds(new ArrayList<>() {{
this.add("testId");
}});
userRequestUtils.requestPost(userRequestUtils.URL_USER_DELETE, request, CHECK_RESULT_MATHER);
//重置密码
request = new UserBaseBatchRequest();
request.setUserIds(Collections.singletonList("admin"));
userRequestUtils.requestPost(userRequestUtils.URL_USER_RESET_PASSWORD, request, CHECK_RESULT_MATHER);
//添加用户到用户组
UserAndRoleBatchRequest userRoleRelationRequest = new UserAndRoleBatchRequest();
userRoleRelationRequest.setUserIds(new ArrayList<>() {{
this.add(NONE_ROLE_USERNAME);
}});
userRoleRelationRequest.setRoleIds(new ArrayList<>() {{
this.add("member");
}});
userRequestUtils.requestPost(userRequestUtils.URL_USER_ROLE_RELATION, userRoleRelationRequest, CHECK_RESULT_MATHER);
}
@BeforeEach
public void login() throws Exception {
if (userRequestUtils == null) {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login")
.content("{\"username\":\"" + NONE_ROLE_USERNAME + "\",\"password\":\"" + NONE_ROLE_PASSWORD + "\"}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
String sessionId = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.sessionId");
String csrfToken = JsonPath.read(mvcResult.getResponse().getContentAsString(), "$.data.csrfToken");
userRequestUtils = new UserRequestUtils(mockMvc, sessionId, csrfToken);
}
}
}

View File

@ -0,0 +1,117 @@
package io.metersphere.system.controller.user;
import io.metersphere.sdk.base.BaseTest;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.UserCreateInfo;
import io.metersphere.system.request.user.UserAndRoleBatchRequest;
import io.metersphere.system.request.user.UserBaseBatchRequest;
import io.metersphere.system.request.user.UserChangeEnableRequest;
import io.metersphere.system.request.user.UserRoleBatchRelationRequest;
import io.metersphere.system.response.user.UserSelectOption;
import io.metersphere.system.utils.user.UserParamUtils;
import io.metersphere.system.utils.user.UserRequestUtils;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class UserControllerPermissionTests extends BaseTest {
@Test
public void permissionTest() throws Exception {
//获取用户
this.requestGetPermissionTest(PermissionConstants.SYSTEM_USER_READ, String.format(UserRequestUtils.URL_USER_GET, "admin"));
//校验权限用户创建
UserCreateInfo paramUserInfo = new UserCreateInfo() {{
setId("testId");
setName("tianyang.no.permission.email");
setEmail("tianyang.no.permission.email@126.com");
}};
List<UserSelectOption> paramRoleList = new ArrayList<>() {{
this.add(
new UserSelectOption() {{
this.setId("member");
this.setName("member");
}});
}};
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ_ADD, UserRequestUtils.URL_USER_CREATE, UserParamUtils.getUserCreateDTO(
paramRoleList,
new ArrayList<>() {{
add(paramUserInfo);
}}
));
//校验权限修改用户
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ_UPDATE, UserRequestUtils.URL_USER_UPDATE, UserParamUtils.getUserUpdateDTO(paramUserInfo, paramRoleList));
//校验权限分页查询用户列表
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ, UserRequestUtils.URL_USER_PAGE, UserParamUtils.getDefaultPageRequest());
//校验权限启用/禁用用户
UserChangeEnableRequest userChangeEnableRequest = new UserChangeEnableRequest();
userChangeEnableRequest.setEnable(false);
userChangeEnableRequest.setUserIds(new ArrayList<>() {{
this.add("admin");
}});
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ_UPDATE, UserRequestUtils.URL_USER_UPDATE_ENABLE, userChangeEnableRequest);
//用户导入
File jarFile = new File(
Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/user_import_permission.xlsx")).getPath());
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("file", jarFile);
this.requestMultipartPermissionTest(PermissionConstants.SYSTEM_USER_READ_IMPORT, UserRequestUtils.URL_USER_IMPORT, paramMap);
//用户删除
UserBaseBatchRequest request = new UserBaseBatchRequest();
request.setUserIds(new ArrayList<>() {{
this.add("testId");
}});
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ_DELETE, UserRequestUtils.URL_USER_DELETE, request);
//重置密码
request = new UserBaseBatchRequest();
request.setUserIds(Collections.singletonList("admin"));
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ_UPDATE, UserRequestUtils.URL_USER_RESET_PASSWORD, request);
//批量添加用户到用户组
UserAndRoleBatchRequest userAndRoleBatchRequest = new UserAndRoleBatchRequest();
userAndRoleBatchRequest.setUserIds(Collections.singletonList("admin"));
userAndRoleBatchRequest.setRoleIds(Collections.singletonList("member"));
this.requestPostPermissionTest(PermissionConstants.SYSTEM_USER_READ_UPDATE, UserRequestUtils.URL_USER_ROLE_RELATION, userAndRoleBatchRequest);
//校验权限系统全局用户组获取
this.requestGetPermissionTest(PermissionConstants.SYSTEM_USER_ROLE_READ, UserRequestUtils.URL_GET_GLOBAL_SYSTEM);
// 查看组织
this.requestGetPermissionsTest(List.of(PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ), UserRequestUtils.URL_GET_ORGANIZATION);
//查看项目
this.requestGetPermissionsTest(List.of(PermissionConstants.SYSTEM_USER_ROLE_READ, PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ), UserRequestUtils.URL_GET_PROJECT);
// 批量添加用户到项目
UserRoleBatchRelationRequest roleBatchRelationRequest = new UserRoleBatchRelationRequest();
roleBatchRelationRequest.setUserIds(Collections.singletonList("admin"));
roleBatchRelationRequest.setRoleIds(Collections.singletonList("member"));
List<String> addMemberPermissionList = new ArrayList<>();
addMemberPermissionList.add(PermissionConstants.SYSTEM_USER_READ_UPDATE);
addMemberPermissionList.add(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_MEMBER_ADD);
this.requestPostPermissionsTest(addMemberPermissionList, UserRequestUtils.URL_ADD_PROJECT_MEMBER, roleBatchRelationRequest);
// 批量添加用户到组织
this.requestPostPermissionsTest(addMemberPermissionList, UserRequestUtils.URL_ADD_ORGANIZATION_MEMBER, roleBatchRelationRequest);
}
}

View File

@ -58,6 +58,10 @@ public class UserControllerTests extends BaseTest {
private UserMapper userMapper; private UserMapper userMapper;
@Resource @Resource
private GlobalUserRoleRelationService globalUserRoleRelationService; private GlobalUserRoleRelationService globalUserRoleRelationService;
@Resource
ProjectMapper projectMapper;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
//失败请求返回编码 //失败请求返回编码
private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest(); private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest();
@ -470,14 +474,6 @@ public class UserControllerTests extends BaseTest {
} }
} }
public final String URL_GET_PROJECT = "/system/user/get/project";
public final String URL_ADD_PROJECT_MEMBER = "/system/user/add-project-member";
public final String URL_ADD_ORGANIZATION_MEMBER = "/system/user/add-org-member";
@Resource
ProjectMapper projectMapper;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
@Test @Test
@Order(8) @Order(8)
public void testUserResetPasswordError() throws Exception { public void testUserResetPasswordError() throws Exception {
@ -643,7 +639,7 @@ public class UserControllerTests extends BaseTest {
//重置非Admin用户的密码 //重置非Admin用户的密码
{ {
UserBaseBatchRequest request = new UserBaseBatchRequest(); UserBaseBatchRequest request = new UserBaseBatchRequest();
request.setSkipIds(Collections.singletonList("admin")); request.setExcludeIds(Collections.singletonList("admin"));
request.setSelectAll(true); request.setSelectAll(true);
BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult( BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(
this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request), this.requestPostAndReturn(userRequestUtils.URL_USER_RESET_PASSWORD, request),
@ -906,29 +902,6 @@ public class UserControllerTests extends BaseTest {
Assertions.assertTrue(user.getDeleted()); Assertions.assertTrue(user.getDeleted());
USER_LIST.remove(deleteUser); USER_LIST.remove(deleteUser);
} }
//删除已存的所有用户(不包括admin
{
UserBaseBatchRequest request = new UserBaseBatchRequest();
request.setUserIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList()));
request.setSkipIds(Collections.singletonList("admin"));
BatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), BatchProcessResponse.class);
Assertions.assertEquals(request.getUserIds().size(), response.getTotalCount(), response.getSuccessCount());
//检查数据库
UserExample example = new UserExample();
example.createCriteria().andIdIn(request.getUserIds());
List<User> userList = userMapper.selectByExample(example);
for (User user : userList) {
Assertions.assertTrue(user.getDeleted());
}
//记录已经删除了的用户用于反例
DELETED_USER_ID_LIST.clear();
USER_LIST.clear();
DELETED_USER_ID_LIST.addAll(request.getUserIds());
//检查删除了的用户可以用其邮箱继续注册
this.testAddSuccess();
}
} }
//删除失败的方法要放在删除成功方法后面执行 //删除失败的方法要放在删除成功方法后面执行

View File

@ -13,29 +13,28 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class UserRequestUtils { public class UserRequestUtils {
//用户管理URL //用户管理URL
public final String URL_USER_CREATE = "/system/user/add"; public static final String URL_USER_CREATE = "/system/user/add";
public final String URL_USER_UPDATE = "/system/user/update"; public static final String URL_USER_UPDATE = "/system/user/update";
public final String URL_USER_GET = "/system/user/get/%s"; public static final String URL_USER_GET = "/system/user/get/%s";
public final String URL_USER_PAGE = "/system/user/page"; public static final String URL_USER_PAGE = "/system/user/page";
public final String URL_GET_GLOBAL_SYSTEM = "/system/user/get/global/system/role"; public static final String URL_GET_GLOBAL_SYSTEM = "/system/user/get/global/system/role";
public final String URL_USER_UPDATE_ENABLE = "/system/user/update/enable"; public static final String URL_USER_UPDATE_ENABLE = "/system/user/update/enable";
public final String URL_USER_IMPORT = "/system/user/import"; public static final String URL_USER_IMPORT = "/system/user/import";
public final String URL_USER_DELETE = "/system/user/delete"; public static final String URL_USER_DELETE = "/system/user/delete";
public final String URL_USER_RESET_PASSWORD = "/system/user/reset/password"; public static final String URL_USER_RESET_PASSWORD = "/system/user/reset/password";
public final String URL_USER_ROLE_RELATION = "/system/user/add/batch/user-role"; public static final String URL_USER_ROLE_RELATION = "/system/user/add/batch/user-role";
//查找组织项目 //查找组织项目
public final String URL_GET_ORGANIZATION = "/system/user/get/organization"; public static final String URL_GET_ORGANIZATION = "/system/user/get/organization";
public final String URL_GET_PROJECT = "/system/user/get/project"; public static final String URL_GET_PROJECT = "/system/user/get/project";
public final String URL_ADD_PROJECT_MEMBER = "/system/user/add-project-member"; public static final String URL_ADD_PROJECT_MEMBER = "/system/user/add-project-member";
public final String URL_ADD_ORGANIZATION_MEMBER = "/system/user/add-org-member"; public static final String URL_ADD_ORGANIZATION_MEMBER = "/system/user/add-org-member";
private final MockMvc mockMvc; private final MockMvc mockMvc;
private final String sessionId; private final String sessionId;

View File

@ -1,7 +0,0 @@
-- 初始化一个没有任何权限的用户
insert into user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source,
last_project_id, create_user, update_user)
VALUES ('tianyang.member.id', 'tianyang.member', 'tianyang.member@163.com', MD5('tianyang.member@163.com'),
UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin');
INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user)
VALUES (uuid(), 'tianyang.member.id', 'member', 'system', 1684747668375, 'admin');