Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some typos and unit tests. #1462

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import ai.chat2db.server.domain.core.converter.ConfigConverter;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.domain.repository.entity.SystemConfigDO;
import ai.chat2db.server.domain.repository.mapper.ChartMapper;
import ai.chat2db.server.domain.repository.mapper.SystemConfigMapper;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.model.DataSource;
import ai.chat2db.server.domain.api.service.DataSourceAccessBusinessService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class DataSourceAccessBusinessServiceTest extends TestApplication {

@Autowired
private DataSourceAccessBusinessService dataSourceAccessBusinessService;

/**
* 1. First, determine whether it is a private data source (PRIVATE) based on the type of the data source.
* If it is a private data source, determine whether the currently logged-in user is the owner of the data source.
* If so, allow the operation, otherwise throw a permission exception.
* <p>
* 2. If the currently logged-in user is an administrator userLoginIdentity(true, **), the operation is allowed.
* If the currently logged-in user is a common user, determine whether the user has permission to access the data source.
* If so, the operation is allowed.
* <p>
* 3. If the team to which the currently logged-in user belongs has permission to access the data source, the operation is allowed.
* <p>
* 4. If none of the above conditions are met, a permission exception is thrown.
*/
@Test
public void testCheckPermission() {
// userLoginIdentity(false, 3L);
userLoginIdentity(true, 2L);

DataSource source = new DataSource();
// source.setKind("PRIVATE");
source.setKind("SHARED");
source.setUserId(5L);
source.setId(3L);

ActionResult actionResult = dataSourceAccessBusinessService.checkPermission(source);
assertNotNull(actionResult);
}

/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.model.DataSourceAccess;
import ai.chat2db.server.domain.api.param.datasource.DataSourceSelector;
import ai.chat2db.server.domain.api.param.datasource.access.DataSourceAccessComprehensivePageQueryParam;
import ai.chat2db.server.domain.api.param.datasource.access.DataSourceAccessCreatParam;
import ai.chat2db.server.domain.api.param.datasource.access.DataSourceAccessPageQueryParam;
import ai.chat2db.server.domain.api.param.datasource.access.DataSourceAccessSelector;
import ai.chat2db.server.domain.api.service.DataSourceAccessService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.start.test.dialect.TestUtils;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.PageResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class DataSourceAccessServiceTest extends TestApplication {

@Autowired
private DataSourceAccessService dataSourceAccessService;

@Test
public void testPageQuery() {
// userLoginIdentity(true,5L);
userLoginIdentity(false, 2L);


DataSourceAccessPageQueryParam queryParam = new DataSourceAccessPageQueryParam();
queryParam.setDataSourceId(TestUtils.nextLong());
// queryParam.setAccessObjectType("TEAM");
queryParam.setAccessObjectType("USER");
queryParam.setAccessObjectId(TestUtils.nextLong());
queryParam.setPageNo(3);
queryParam.setPageSize(5);

// Returns false by default
queryParam.setEnableReturnCount(true);


DataSourceAccessSelector accessSelector = new DataSourceAccessSelector();
accessSelector.setAccessObject(true);
accessSelector.setDataSource(true);
accessSelector.setDataSourceSelector(new DataSourceSelector(true));

PageResult<DataSourceAccess> result = dataSourceAccessService.pageQuery(queryParam, accessSelector);
assertNotNull(result);

}

@Test
public void testComprehensivePageQuery() {

userLoginIdentity(false, 2L);
// userLoginIdentity(true,5L);

DataSourceAccessComprehensivePageQueryParam param = new DataSourceAccessComprehensivePageQueryParam();
param.setPageNo(1);
param.setPageSize(10);
param.setEnableReturnCount(true);
param.setDataSourceId(TestUtils.nextLong());
param.setAccessObjectType("USER");
// param.setAccessObjectType("TEAM");
param.setAccessObjectId(TestUtils.nextLong());
param.setUserOrTeamSearchKey("test");
param.setDataSourceSearchKey("m");

DataSourceAccessSelector selector = new DataSourceAccessSelector();
selector.setAccessObject(true);
selector.setDataSource(true);
selector.setDataSourceSelector(new DataSourceSelector(true));

PageResult<DataSourceAccess> result = dataSourceAccessService.comprehensivePageQuery(param, selector);
assertNotNull(result);
}

@Test
public void testCreateAndDelete() {

userLoginIdentity(false, 8L);
// userLoginIdentity(true,6L);

DataSourceAccessCreatParam creatParam = new DataSourceAccessCreatParam();
creatParam.setDataSourceId(TestUtils.nextLong());
creatParam.setAccessObjectId(TestUtils.nextLong());
creatParam.setAccessObjectType("USER");
// creatParam.setAccessObjectType("TEAM");

DataResult<Long> result = dataSourceAccessService.create(creatParam);
assertNotNull(result);
ActionResult delete = dataSourceAccessService.delete(result.getData());
assertNotNull(delete);

}

/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package ai.chat2db.server.start.test.core;

import ai.chat2db.server.domain.api.model.DataSource;
import ai.chat2db.server.domain.api.param.datasource.*;
import ai.chat2db.server.domain.api.service.DataSourceService;
import ai.chat2db.server.domain.repository.Dbutils;
import ai.chat2db.server.start.test.TestApplication;
import ai.chat2db.server.start.test.dialect.DialectProperties;
import ai.chat2db.server.start.test.dialect.TestUtils;
import ai.chat2db.server.tools.base.wrapper.param.OrderBy;
import ai.chat2db.server.tools.base.wrapper.result.ActionResult;
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
import ai.chat2db.server.tools.base.wrapper.result.PageResult;
import ai.chat2db.server.tools.common.model.Context;
import ai.chat2db.server.tools.common.model.LoginUser;
import ai.chat2db.server.tools.common.util.ContextUtils;
import ai.chat2db.spi.config.DriverConfig;
import ai.chat2db.spi.model.Database;
import ai.chat2db.spi.model.KeyValue;
import ai.chat2db.spi.model.SSHInfo;
import ai.chat2db.spi.model.SSLInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class DataSourceServiceTest extends TestApplication {

@Autowired
private DataSourceService dataSourceService;

@Autowired
private List<DialectProperties> dialectPropertiesList;

@Test
public void testCreateWithPermission() {
// userLoginIdentity(true, 1L);
userLoginIdentity(false, 2L);

DataSourceCreateParam createParam = new DataSourceCreateParam();
createParam.setKind("PRIVATE");
// createParam.setKind("SHARED");
createParam.setDriverConfig(new DriverConfig());

DataResult<Long> withPermission = dataSourceService.createWithPermission(createParam);
assertNotNull(withPermission.getData());

}

@Test
public void testUpdateWithPermission() {
// userLoginIdentity(true, 7L);
userLoginIdentity(false, 2L);

DataSourceUpdateParam updateParam = new DataSourceUpdateParam();
updateParam.setId(4L);
updateParam.setDriverConfig(new DriverConfig());
updateParam.setPassword("123456");

DataResult<Long> result = dataSourceService.updateWithPermission(updateParam);
ActionResult delete = dataSourceService.deleteWithPermission(4L);
assertNotNull(result.getData());
assertNotNull(delete);

}

@Test
public void testQueryById() {
userLoginIdentity(false, 2L);
// userLoginIdentity(true, 7L);

DataResult<DataSource> result = dataSourceService.queryById(3L);
ListResult<DataSource> dataSourceListResult = dataSourceService.listQuery(new ArrayList<>(), null);
assertNotNull(result.getData());
assertNotNull(dataSourceListResult.getData());
}

@Test
public void testQueryExistent() {
userLoginIdentity(false, 2L);
// userLoginIdentity(true, 7L);

DataSourceSelector selector = new DataSourceSelector();
selector.setEnvironment(true);
// selector.setEnvironment(false);

DataResult<DataSource> result = dataSourceService.queryExistent(3L, null);
assertNotNull( result.getData(),"Data should not be null");
}

@Test
public void testCopyByIdWithPermission() {
userLoginIdentity(false, 2L);
// userLoginIdentity(true, 7L);

DataResult<Long> longDataResult = dataSourceService.copyByIdWithPermission(3L);
assertNotNull(longDataResult.getData());

}

@Test
public void testQueryPage() {
userLoginIdentity(false,6L);
// userLoginIdentity(true,9L);

DataSourcePageQueryParam queryParam = new DataSourcePageQueryParam();
queryParam.setSearchKey("test");
queryParam.setPageNo(1);
queryParam.setPageSize(10);

DataSourceSelector selector = new DataSourceSelector();
selector.setEnvironment(true);
// selector.setEnvironment(false);

PageResult<DataSource> result = dataSourceService.queryPage(queryParam, selector);
assertNotNull(result.getData());
}

@Test
public void testQueryPageWithPermission() {
// userLoginIdentity(false,3L);
userLoginIdentity(true,9L);

DataSourcePageQueryParam queryParam = new DataSourcePageQueryParam();
queryParam.setSearchKey("test");
queryParam.setKind("PRIVATE");
// queryParam.setKind("SHARED");
queryParam.setPageNo(1);
queryParam.setPageSize(10);
queryParam.setOrderByList(new ArrayList<OrderBy>());

DataSourceSelector selector = new DataSourceSelector();
selector.setEnvironment(true);

PageResult<DataSource> result = dataSourceService.queryPageWithPermission(queryParam, selector);
assertNotNull(result.getData());

}

@Test
public void testPreConnect() {

for (DialectProperties dialectProperties : dialectPropertiesList) {

DataSourcePreConnectParam param = new DataSourcePreConnectParam();
param.setType(dialectProperties.getDbType());
param.setUser(dialectProperties.getUsername());
param.setUrl(dialectProperties.getUrl());
param.setPassword(dialectProperties.getPassword());
param.setPort(String.valueOf(dialectProperties.getPort()));
param.setHost("183.247.151.185");
param.setSsh(new SSHInfo());
param.setSsl(new SSLInfo());
param.setExtendInfo(new ArrayList<KeyValue>());

ActionResult result = dataSourceService.preConnect(param);
assertNotNull(result);

Long consoleId= TestUtils.nextLong();
Long dataSourceId= TestUtils.nextLong();
TestUtils.buildContext(dialectProperties,dataSourceId,consoleId);
ListResult<Database> connect = dataSourceService.connect(dataSourceId);
assertNotNull(connect.getData());

dataSourceService.close(dataSourceId);

}
}


/**
* Save the current user identity (administrator or normal user) and user ID to the context and database session for subsequent use.
*
* @param isAdmin
* @param userId
*/
private static void userLoginIdentity(boolean isAdmin, Long userId) {
Context context = Context.builder().loginUser(
LoginUser.builder().admin(isAdmin).id(userId).build()
).build();
ContextUtils.setContext(context);
Dbutils.setSession();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DatabaseServiceTest extends TestApplication {
@Test
public void testQueryAll() {

// MYSQL ORACLE POSTGRESQL
// MYSQL ORACLE POSTGRESQL MONGODB MARIADB
for (DialectProperties dialectProperties : dialectPropertiesList) {
String dbType = dialectProperties.getDbType();
Long dataSourceId = TestUtils.nextLong();
Expand Down
Loading