build: 项目管理模块-单元测试-初始化权限认证数据

This commit is contained in:
jianxing 2023-08-25 13:54:07 +08:00 committed by 刘瑞斌
parent 896a58a5eb
commit 0e8d916097
2 changed files with 27 additions and 30 deletions

View File

@ -12,8 +12,10 @@ import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.mapper.OperationLogMapper;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRolePermission;
import io.metersphere.system.domain.UserRolePermissionExample;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRolePermissionMapper;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
@ -43,10 +45,7 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.function.Supplier;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ -65,12 +64,17 @@ public abstract class BaseTest {
private OperationLogMapper operationLogMapper;
@Resource
private UserRolePermissionMapper userRolePermissionMapper;
@Resource
private UserMapper userMapper;
protected static final String DEFAULT_LIST = "list";
protected static final String DEFAULT_GET = "get/{0}";
protected static final String DEFAULT_ADD = "add";
protected static final String DEFAULT_UPDATE = "update";
protected static final String DEFAULT_DELETE = "delete/{0}";
protected static final String DEFAULT_USER_PASSWORD = "metersphere";
protected static final String DEFAULT_PROJECT_ID = "default_project";
protected static final String DEFAULT_ORGANIZATION_ID = "default_organization";
/**
* 可以重写该方法定义 BASE_PATH
@ -82,16 +86,20 @@ public abstract class BaseTest {
@BeforeEach
public void login() throws Exception {
if (this.adminAuthInfo == null) {
this.adminAuthInfo = initAuthInfo("admin", "metersphere");
this.adminAuthInfo = initAuthInfo("admin", DEFAULT_USER_PASSWORD);
this.sessionId = this.adminAuthInfo.getSessionId();
this.csrfToken = this.adminAuthInfo.getCsrfToken();
}
if (permissionAuthInfoMap.isEmpty()) {
// 获取系统组织项目对应的权限测试用户的认证信息
// 暂时只支持 SYSTEM, ORGANIZATION
// todo 补充 PROJECT
permissionAuthInfoMap.put(UserRoleType.SYSTEM.name(), initAuthInfo(UserRoleType.SYSTEM.name(), "metersphere"));
permissionAuthInfoMap.put(UserRoleType.ORGANIZATION.name(), initAuthInfo(UserRoleType.ORGANIZATION.name(), "metersphere"));
List<String> permissionUserNames = Arrays.asList(UserRoleType.SYSTEM.name(), UserRoleType.ORGANIZATION.name(), UserRoleType.PROJECT.name());
for (String permissionUserName : permissionUserNames) {
User permissionUser = userMapper.selectByPrimaryKey(permissionUserName);
// 有对应用户才初始化认证信息
if (permissionUser != null) {
permissionAuthInfoMap.put(permissionUserName, initAuthInfo(permissionUserName, DEFAULT_USER_PASSWORD));
}
}
}
}
@ -448,7 +456,7 @@ public abstract class BaseTest {
}
private void refreshUserPermissionByRoleId(String roleId) throws Exception {
AuthInfo authInfo = permissionAuthInfoMap.get(roleId);
AuthInfo authInfo = getPermissionAuthInfo(roleId);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/is-login")
.header(SessionConstants.HEADER_TOKEN, authInfo.getSessionId())
.header(SessionConstants.CSRF_TOKEN, authInfo.getCsrfToken());
@ -505,7 +513,11 @@ public abstract class BaseTest {
}
private AuthInfo getPermissionAuthInfo(String roleId) {
return permissionAuthInfoMap.get(roleId);
AuthInfo authInfo = permissionAuthInfoMap.get(roleId);
if (authInfo == null) {
throw new MSException("没有初始化权限认证用户信息!");
}
return authInfo;
}
private MockHttpServletRequestBuilder getPermissionRequestBuilder(String roleId, String url, Object... uriVariables) {

View File

@ -1,29 +1,14 @@
-- 初始化用于权限测试的用户
INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source,
last_project_id, create_user, update_user, deleted)
VALUES ('SYSTEM', 'SYSTEM', 'SYSTEM@fit2cloud.com', MD5('metersphere'),
UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', false);
-- 初始化一个用于权限测试的用户组,这里默认使用 SYSTEM 作为ID如果是组织和项目级别类似便于根据权限的前缀找到对应测试的用户组
INSERT INTO user_role (id, name, description, internal, type, create_time, update_time, create_user, scope_id)
VALUES ('SYSTEM', '系统级别权限校验', '', 1, 'SYSTEM', 1620674220005, 1620674220000, 'admin', 'global');
-- 初始化用户和组的关系
INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user)
VALUES ('SYSTEM', 'SYSTEM', 'SYSTEM', 'system', 1684747668375, 'admin');
-- 初始化用于权限测试的组织用户
INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source,
last_project_id, create_user, update_user, deleted)
VALUES ('ORGANIZATION', 'ORGANIZATION', 'ORGANIZATION@fit2cloud.com', MD5('metersphere'),
VALUES ('PROJECT', 'PROJECT', 'PROJECT@fit2cloud.com', MD5('metersphere'),
UNIX_TIMESTAMP() * 1000,
UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', false);
-- 初始化一个用于权限测试的用户组,这里默认使用 ORGANIZATION 作为ID如果是组织和项目级别类似便于根据权限的前缀找到对应测试的用户组
-- 初始化一个用于权限测试的用户组,这里默认使用 PROJECT 作为ID如果是组织和项目级别类似便于根据权限的前缀找到对应测试的用户组
INSERT INTO user_role (id, name, description, internal, type, create_time, update_time, create_user, scope_id)
VALUES ('ORGANIZATION', '组织级别权限校验', '', 1, 'ORGANIZATION', 1620674220005, 1620674220000, 'admin', 'global');
VALUES ('PROJECT', '项目级别权限校验', '', 1, 'PROJECT', 1620674220005, 1620674220000, 'admin', 'global');
-- 初始化用户和组的关系
INSERT INTO user_role_relation (id, user_id, role_id, source_id, create_time, create_user)
SELECT 'ORGANIZATION', 'ORGANIZATION', 'ORGANIZATION', id, 1684747668375, 'admin' FROM organization WHERE num = 100001;
SELECT 'PROJECT', 'PROJECT', 'PROJECT', id, 1684747668375, 'admin' FROM project WHERE num = 100001;