Merge remote-tracking branch 'origin/master'

This commit is contained in:
wenyann 2020-07-15 10:41:12 +08:00
commit b15d35558f
25 changed files with 187 additions and 109 deletions

View File

@ -10,6 +10,8 @@ MeterSphere 是一站式的开源企业级持续测试平台,涵盖测试跟
- 性能测试: 兼容 JMeter支持 Kubernetes 和云环境,轻松支持高并发、分布式的性能测试;
- 团队协作: 两级租户体系,天然支持团队协作。
![产品定位](https://metersphere.io/images/icon/ct-devops.png)
UI 展示:
![UI](https://metersphere.io/images/screenshot/ss07.png)

View File

@ -293,7 +293,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.itfsw</groupId>

View File

@ -2,17 +2,15 @@ package io.metersphere.api.service;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.APITestResult;
import io.metersphere.api.dto.parse.ApiImport;
import io.metersphere.api.dto.QueryAPITestRequest;
import io.metersphere.api.dto.SaveAPITestRequest;
import io.metersphere.api.dto.parse.ApiImport;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.parse.ApiImportParser;
import io.metersphere.api.parse.ApiImportParserFactory;
import io.metersphere.api.parse.MsParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiTestFileMapper;
import io.metersphere.base.mapper.ApiTestMapper;
import io.metersphere.base.mapper.TestCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiTestMapper;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.FileType;
@ -27,17 +25,22 @@ import io.metersphere.i18n.Translator;
import io.metersphere.job.sechedule.ApiTestJob;
import io.metersphere.service.FileService;
import io.metersphere.service.ScheduleService;
import io.metersphere.track.service.TestCaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.*;
import java.util.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.annotation.Resource;
@Service
@Transactional(rollbackFor = Exception.class)
public class APITestService {
@ -57,7 +60,7 @@ public class APITestService {
@Resource
private ScheduleService scheduleService;
@Resource
private TestCaseMapper testCaseMapper;
private TestCaseService testCaseService;
public List<APITestResult> list(QueryAPITestRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
@ -130,20 +133,7 @@ public class APITestService {
}
public void delete(String testId) {
// 是否关联测试用例
TestCaseExample testCaseExample = new TestCaseExample();
testCaseExample.createCriteria().andTestIdEqualTo(testId);
List<TestCase> testCases = testCaseMapper.selectByExample(testCaseExample);
if (testCases.size() > 0) {
String caseName = "";
for (int i = 0; i < testCases.size(); i++) {
caseName = caseName + testCases.get(i).getName() + ",";
}
caseName = caseName.substring(0, caseName.length() - 1);
MSException.throwException(Translator.get("related_case_del_fail_prefix") + caseName + Translator.get("related_case_del_fail_suffix"));
}
testCaseService.checkIsRelateTest(testId);
deleteFileByTestId(testId);
apiReportService.deleteByTestId(testId);
apiTestMapper.deleteByPrimaryKey(testId);
@ -186,10 +176,7 @@ public class APITestService {
private Boolean isNameExist(SaveAPITestRequest request) {
ApiTestExample example = new ApiTestExample();
example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()).andIdNotEqualTo(request.getId());
if (apiTestMapper.countByExample(example) > 0) {
return true;
}
return false;
return apiTestMapper.countByExample(example) > 0;
}
private ApiTest updateTest(SaveAPITestRequest request) {
@ -294,7 +281,7 @@ public class APITestService {
request.setName(file.getOriginalFilename());
request.setProjectId("");
request.setScenarioDefinition(apiImport.getScenarios());
request.setUserId(SessionUtils.getUser().getId());
request.setUserId(SessionUtils.getUserId());
request.setId(UUID.randomUUID().toString());
for (FileType fileType : FileType.values()) {
String suffix = fileType.suffix();
@ -303,7 +290,7 @@ public class APITestService {
request.setName(name.substring(0, name.length() - suffix.length()));
}
}
;
if (isNameExist(request)) {
request.setName(request.getName() + "_" + request.getId().substring(0, 5));
}

View File

@ -1,5 +1,5 @@
package io.metersphere.commons.constants;
public enum UserSource {
Local, Ldap
LOCAL, LDAP
}

View File

@ -1,23 +1,13 @@
package io.metersphere.controller;
import io.metersphere.base.domain.UserRole;
import io.metersphere.commons.constants.UserSource;
import io.metersphere.controller.request.LoginRequest;
import io.metersphere.dto.UserDTO;
import io.metersphere.i18n.Translator;
import io.metersphere.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import static io.metersphere.commons.constants.SessionConstants.ATTR_USER;
@RestController
@RequestMapping
@ -36,6 +26,7 @@ public class LoginController {
@PostMapping(value = "/signin")
public ResultHolder login(@RequestBody LoginRequest request) {
SecurityUtils.getSubject().getSession().setAttribute("authenticate", UserSource.LOCAL.name());
return userService.login(request);
}

View File

@ -9,12 +9,12 @@ import io.metersphere.controller.request.LoginRequest;
import io.metersphere.i18n.Translator;
import io.metersphere.ldap.domain.Person;
import io.metersphere.ldap.service.LdapService;
import io.metersphere.ldap.domain.LdapInfo;
import io.metersphere.service.SystemParameterService;
import io.metersphere.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@ -38,10 +38,9 @@ public class LdapController {
Person person = ldapService.authenticate(request);
SecurityUtils.getSubject().getSession().setAttribute("authenticate", "ldap");
SecurityUtils.getSubject().getSession().setAttribute("authenticate", UserSource.LDAP.name());
String username = request.getUsername();
String password = request.getPassword();
String email = person.getEmail();
@ -55,19 +54,15 @@ public class LdapController {
user.setId(username);
user.setName(username);
user.setEmail(email);
user.setPassword(password);
user.setSource(UserSource.Ldap.name());
userService.createUser(user);
} else {
request.setUsername(u.getId());
request.setPassword(u.getPassword());
user.setSource(UserSource.LDAP.name());
userService.addLdapUser(user);
}
return userService.login(request);
}
@PostMapping("/test/connect")
public void testConnect(@RequestBody LdapInfo ldapInfo) {
public void testConnect() {
ldapService.testConnect();
}

View File

@ -110,6 +110,8 @@ public class EngineFactory {
try (ByteArrayInputStream source = new ByteArrayInputStream(fileContent.getFile())) {
String content = engineSourceParser.parse(engineContext, source);
engineContext.setContent(content);
} catch (MSException e) {
throw e;
} catch (Exception e) {
MSException.throwException(e);
}

View File

@ -66,6 +66,8 @@ public class DockerTestEngine extends AbstractEngine {
EngineContext context = null;
try {
context = EngineFactory.createContext(loadTest, resource.getId(), realThreadNum, this.getStartTime(), this.getReportId(), resourceIndex);
} catch (MSException e) {
throw e;
} catch (Exception e) {
MSException.throwException(e);
}

View File

@ -2,8 +2,10 @@ package io.metersphere.performance.parse.xml.reader.jmx;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.config.KafkaProperties;
import io.metersphere.i18n.Translator;
import io.metersphere.performance.engine.EngineContext;
import io.metersphere.performance.parse.xml.reader.DocumentParser;
import org.apache.commons.lang3.StringUtils;
@ -709,6 +711,14 @@ public class JmeterDocumentParser implements DocumentParser {
}
private void processThreadGroup(Element threadGroup) {
// 检查 threadgroup 后面的hashtree是否为空
Node hashTree = threadGroup.getNextSibling();
while (!(hashTree instanceof Element)) {
hashTree = hashTree.getNextSibling();
}
if (!hashTree.hasChildNodes()) {
MSException.throwException(Translator.get("jmx_content_valid"));
}
// 重命名 tagName
Document document = threadGroup.getOwnerDocument();
document.renameNode(threadGroup, threadGroup.getNamespaceURI(), CONCURRENCY_THREAD_GROUP);

View File

@ -22,6 +22,7 @@ import io.metersphere.service.FileService;
import io.metersphere.service.ScheduleService;
import io.metersphere.service.TestResourceService;
import io.metersphere.track.request.testplan.*;
import io.metersphere.track.service.TestCaseService;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@ -73,6 +74,8 @@ public class PerformanceTestService {
private ScheduleService scheduleService;
@Resource
private TestCaseMapper testCaseMapper;
@Resource
private TestCaseService testCaseService;
public List<LoadTestDTO> list(QueryTestPlanRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
@ -82,18 +85,7 @@ public class PerformanceTestService {
public void delete(DeleteTestPlanRequest request) {
String testId = request.getId();
// 是否关联测试用例
TestCaseExample testCaseExample = new TestCaseExample();
testCaseExample.createCriteria().andTestIdEqualTo(testId);
List<TestCase> testCases = testCaseMapper.selectByExample(testCaseExample);
if (testCases.size() > 0) {
String caseName = "";
for (int i = 0; i < testCases.size(); i++) {
caseName = caseName + testCases.get(i).getName() + ",";
}
caseName = caseName.substring(0, caseName.length() - 1);
MSException.throwException(Translator.get("related_case_del_fail_prefix") + caseName + Translator.get("related_case_del_fail_suffix"));
}
testCaseService.checkIsRelateTest(testId);
LoadTestReportExample loadTestReportExample = new LoadTestReportExample();
loadTestReportExample.createCriteria().andTestIdEqualTo(testId);

View File

@ -2,6 +2,7 @@ package io.metersphere.security;
import io.metersphere.base.domain.Role;
import io.metersphere.commons.constants.UserSource;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.UserDTO;
@ -19,6 +20,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
@ -64,8 +67,36 @@ public class ShiroDBRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String login = (String) SecurityUtils.getSubject().getSession().getAttribute("authenticate");
String userId = token.getUsername();
String password = String.valueOf(token.getPassword());
if (StringUtils.equals("local", runMode)) {
UserDTO user = getUserWithOutAuthenticate(userId);
userId = user.getId();
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
if (StringUtils.equals(login, UserSource.LOCAL.name())) {
return loginLocalMode(userId, password);
}
if (StringUtils.equals(login, UserSource.LDAP.name())) {
return loginLdapMode(userId, password);
}
UserDTO user = getUserWithOutAuthenticate(userId);
userId = user.getId();
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
private UserDTO getUserWithOutAuthenticate(String userId) {
UserDTO user = userService.getUserDTO(userId);
String msg;
if (user == null) {
@ -75,29 +106,40 @@ public class ShiroDBRealm extends AuthorizingRealm {
logger.warn(msg);
throw new UnknownAccountException(Translator.get("user_not_exist") + userId);
}
}
return user;
}
private AuthenticationInfo loginLdapMode(String userId, String password) {
//
UserDTO user = userService.getLoginUser(userId, Arrays.asList(UserSource.LDAP.name(), UserSource.LOCAL.name()));
String msg;
if (user == null) {
msg = "The user does not exist: " + userId;
logger.warn(msg);
throw new UnknownAccountException(Translator.get("user_not_exist") + userId);
}
userId = user.getId();
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
private AuthenticationInfo loginLocalMode(String userId, String password) {
UserDTO user = userService.getLoginUser(userId, Collections.singletonList(UserSource.LOCAL.name()));
String msg;
if (user == null) {
user = userService.getLoginUserByEmail(userId, UserSource.LOCAL.name());
if (user == null) {
msg = "The user does not exist: " + userId;
logger.warn(msg);
throw new UnknownAccountException(Translator.get("user_not_exist") + userId);
}
userId = user.getId();
}
// local test
if (StringUtils.equals("local", runMode)) {
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
// apikey 校验不验证密码
if (ApiKeySessionHandler.random.equalsIgnoreCase(password)) {
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
String login = (String) SecurityUtils.getSubject().getSession().getAttribute("authenticate");
if (StringUtils.equals(login, "ldap")) {
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
// 密码验证
if (!userService.checkUserPassword(userId, password)) {
throw new IncorrectCredentialsException(Translator.get("password_is_incorrect"));

View File

@ -138,17 +138,29 @@ public class UserService {
user.setUpdateTime(System.currentTimeMillis());
// 默认1:启用状态
user.setStatus(UserStatus.NORMAL);
user.setSource(UserSource.Local.name());
user.setSource(UserSource.LOCAL.name());
// 密码使用 MD5
user.setPassword(CodingUtil.md5(user.getPassword()));
checkEmailIsExist(user.getEmail());
userMapper.insertSelective(user);
}
public void addLdapUser(User user) {
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
user.setStatus(UserStatus.NORMAL);
checkEmailIsExist(user.getEmail());
userMapper.insertSelective(user);
}
private void checkEmailIsExist(String email) {
UserExample userExample = new UserExample();
UserExample.Criteria criteria = userExample.createCriteria();
criteria.andEmailEqualTo(user.getEmail());
criteria.andEmailEqualTo(email);
List<User> userList = userMapper.selectByExample(userExample);
if (!CollectionUtils.isEmpty(userList)) {
MSException.throwException(Translator.get("user_email_already_exists"));
}
userMapper.insertSelective(user);
}
public UserDTO getUserDTO(String userId) {
@ -168,6 +180,15 @@ public class UserService {
return userDTO;
}
public UserDTO getLoginUser(String userId, List<String> list) {
UserExample example = new UserExample();
example.createCriteria().andIdEqualTo(userId).andSourceIn(list);
if (userMapper.countByExample(example) == 0) {
return null;
}
return getUserDTO(userId);
}
public UserDTO getUserDTOByEmail(String email) {
UserExample example = new UserExample();
example.createCriteria().andEmailEqualTo(email);
@ -178,6 +199,16 @@ public class UserService {
return getUserDTO(users.get(0).getId());
}
public UserDTO getLoginUserByEmail(String email, String source) {
UserExample example = new UserExample();
example.createCriteria().andEmailEqualTo(email).andSourceEqualTo(source);
List<User> users = userMapper.selectByExample(example);
if (users == null || users.size() <= 0) {
return null;
}
return getUserDTO(users.get(0).getId());
}
public UserRoleDTO getUserRole(String userId) {
UserRoleDTO userRoleDTO = new UserRoleDTO();
//
@ -475,11 +506,15 @@ public class UserService {
}
public ResultHolder login(LoginRequest request) {
String login = (String) SecurityUtils.getSubject().getSession().getAttribute("authenticate");
String msg;
String username = StringUtils.trim(request.getUsername());
String password = StringUtils.trim(request.getPassword());
if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
return ResultHolder.error("user or password can't be null");
String password = "";
if (!StringUtils.equals(login, UserSource.LDAP.name())) {
password = StringUtils.trim(request.getPassword());
if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
return ResultHolder.error("user or password can't be null");
}
}
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
@ -520,6 +555,7 @@ public class UserService {
} catch (UnauthorizedException e) {
msg = Translator.get("not_authorized") + e.getMessage();
}
return ResultHolder.error(msg);
MSException.throwException(msg);
return null;
}
}

View File

@ -410,4 +410,23 @@ public class TestCaseService {
example.createCriteria().andProjectIdEqualTo(projectId);
testCaseMapper.deleteByExample(example);
}
/**
* 是否关联测试
*
* @param testId
*/
public void checkIsRelateTest(String testId) {
TestCaseExample testCaseExample = new TestCaseExample();
testCaseExample.createCriteria().andTestIdEqualTo(testId);
List<TestCase> testCases = testCaseMapper.selectByExample(testCaseExample);
StringBuilder caseName = new StringBuilder();
if (testCases.size() > 0) {
for (TestCase testCase : testCases) {
caseName = caseName.append(testCase.getName()).append(",");
}
String str = caseName.toString().substring(0, caseName.length() - 1);
MSException.throwException(Translator.get("related_case_del_fail_prefix") + " " + str + " " + Translator.get("related_case_del_fail_suffix"));
}
}
}

View File

@ -1,3 +1,3 @@
alter table user add source varchar(50) null;
update user set source = 'Local' where source is null;
update user set source = 'LOCAL' where source is null;

View File

@ -45,6 +45,7 @@ duplicate_node_ip=Duplicate IPs
max_thread_insufficient=The number of concurrent users exceeds
related_case_del_fail_prefix=Connected to
related_case_del_fail_suffix=TestCase, please disassociate first
jmx_content_valid=JMX content is invalid
#workspace
workspace_name_is_null=Workspace name cannot be null
workspace_name_already_exists=The workspace name already exists

View File

@ -45,6 +45,7 @@ duplicate_node_ip=节点 IP 重复
max_thread_insufficient=并发用户数超额
related_case_del_fail_prefix=已关联到
related_case_del_fail_suffix=测试用例,请先解除关联
jmx_content_valid=JMX 内容无效,请检查
#workspace
workspace_name_is_null=工作空间名不能为空
workspace_name_already_exists=工作空间名已存在

View File

@ -45,6 +45,7 @@ duplicate_node_ip=節點 IP 重復
max_thread_insufficient=並發用戶數超額
related_case_del_fail_prefix=已關聯到
related_case_del_fail_suffix=測試用例,請先解除關聯
jmx_content_valid=JMX 內容無效,請檢查
#workspace
workspace_name_is_null=工作空間名不能為空
workspace_name_already_exists=工作空間名已存在

View File

@ -147,7 +147,6 @@
<logger name="io.metersphere" additivity="false">
<level value="${logger.level:INFO}" />
<appender-ref ref="debugAsyncAppender" />
<appender-ref ref="infoAsyncAppender" />
<appender-ref ref="warnAsyncAppender" />
<appender-ref ref="errorAsyncAppender" />

View File

@ -81,25 +81,25 @@
</script>
<style>
@media only screen and (min-width: 1800px) {
@media only screen and (min-width: 1870px) {
.el-dialog.adv-dialog {
width: 70%;
}
}
@media only screen and (min-width: 1560px) and (max-width: 1799px) {
@media only screen and (min-width: 1650px) and (max-width: 1869px) {
.el-dialog.adv-dialog {
width: 80%;
}
}
@media only screen and (min-width: 1400px) and (max-width: 1559px) {
@media only screen and (min-width: 1470px) and (max-width: 1649px) {
.el-dialog.adv-dialog {
width: 90%;
}
}
@media only screen and (max-width: 1399px) {
@media only screen and (max-width: 1469px) {
.el-dialog.adv-dialog {
width: 70%;
min-width: 695px;
@ -131,13 +131,13 @@
width: 100%;
}
@media only screen and (max-width: 1399px) {
@media only screen and (max-width: 1469px) {
.search-item {
width: 100%;
}
}
@media only screen and (min-width: 1400px) {
@media only screen and (min-width: 1470px) {
.search-item {
width: 50%;
}

View File

@ -71,7 +71,6 @@
display: inline-block;
padding: 0 5px 0 10px;
width: calc(100% - 240px);
min-width: 400px;
box-sizing: border-box;
}
</style>

View File

@ -41,7 +41,7 @@
if (this.component.options.showLabel) {
return this.component.options.showLabel(op);
}
return this.$t(op.label);
return op.label.indexOf(".") !== -1 ? this.$t(op.label) : op.label;
}
}
}

View File

@ -199,7 +199,7 @@
initTableData() {
this.result = this.$get("/user/info/" + this.currentUser().id, response => {
let data = response.data;
this.isLdapUser = response.data.source === 'Ldap' ? true : false;
this.isLdapUser = response.data.source === 'LDAP' ? true : false;
let dataList = [];
dataList[0] = data;
this.tableData = dataList;

View File

@ -320,7 +320,6 @@ export default {
},
api_test: {
creator: "Creator",
title: "Test",
save_and_run: "Save and Run",
run: "Run",
running: "Running",

View File

@ -317,7 +317,7 @@ export default {
pressure_config_params_is_empty: '壓力配置參數不能為空!'
},
api_test: {
title: "測試",
creator: "創建人",
save_and_run: "保存並執行",
run: "執行",
running: "正在執行",

View File

@ -17,8 +17,8 @@
<div class="form">
<el-form-item v-slot:default>
<el-radio-group v-model="form.authenticate">
<el-radio label="ldap" size="mini">LDAP</el-radio>
<el-radio label="normal" size="mini">普通登录</el-radio>
<el-radio label="LDAP" size="mini">LDAP</el-radio>
<el-radio label="LOCAL" size="mini">普通登录</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item prop="username">
@ -69,7 +69,7 @@
form: {
username: '',
password: '',
authenticate: 'normal'
authenticate: 'LOCAL'
},
rules: {
username: [
@ -115,10 +115,10 @@
this.$refs[form].validate((valid) => {
if (valid) {
switch (this.form.authenticate) {
case "normal":
case "LOCAL":
this.normalLogin();
break;
case "ldap":
case "LDAP":
this.ldapLogin();
break;
default: