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