(improvement)(headless) Add data permission integrating test (#721)

Co-authored-by: jolunoluo
This commit is contained in:
LXW
2024-02-05 18:37:50 +08:00
committed by GitHub
parent 8a342eb32a
commit 74d0ec2b23
9 changed files with 358 additions and 300 deletions

View File

@@ -528,14 +528,9 @@ public class ModelDemoDataLoader {
public void addAuthGroup_2() {
AuthGroup authGroupReq = new AuthGroup();
authGroupReq.setModelId(3L);
authGroupReq.setName("tom_sales_permission");
authGroupReq.setName("tom_row_permission");
List<AuthRule> authRules = new ArrayList<>();
AuthRule authRule = new AuthRule();
authRule.setMetrics(Collections.singletonList("stay_hours"));
authRule.setDimensions(Collections.singletonList("page"));
authRules.add(authRule);
authGroupReq.setAuthRules(authRules);
authGroupReq.setDimensionFilters(Collections.singletonList("user_name = 'tom'"));
authGroupReq.setDimensionFilterDescription("用户名='tom'");

View File

@@ -1,7 +1,5 @@
package com.tencent.supersonic.headless;
import static java.time.LocalDate.now;
import com.tencent.supersonic.BaseApplication;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.common.pojo.Aggregator;
@@ -16,11 +14,14 @@ import com.tencent.supersonic.headless.api.pojo.request.SemanticQueryReq;
import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp;
import com.tencent.supersonic.headless.server.service.QueryService;
import com.tencent.supersonic.util.DataUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import static java.time.LocalDate.now;
public class BaseTest extends BaseApplication {
@@ -74,4 +75,26 @@ public class BaseTest extends BaseApplication {
queryStructReq.setOrders(orders);
return queryStructReq;
}
protected QueryStructReq buildQueryStructReq(List<String> groups,
Aggregator aggregator) {
QueryStructReq queryStructReq = new QueryStructReq();
for (Long modelId : DataUtils.getMetricAgentIModelIds()) {
queryStructReq.addModelId(modelId);
}
queryStructReq.setQueryType(QueryType.METRIC);
queryStructReq.setAggregators(Arrays.asList(aggregator));
if (CollectionUtils.isNotEmpty(groups)) {
queryStructReq.setGroups(groups);
}
DateConf dateConf = new DateConf();
dateConf.setDateMode(DateMode.BETWEEN);
dateConf.setEndDate(now().plusDays(0).toString());
dateConf.setStartDate(now().plusDays(-365).toString());
queryStructReq.setDateInfo(dateConf);
return queryStructReq;
}
}

View File

@@ -1,16 +1,17 @@
package com.tencent.supersonic.headless;
import static java.time.LocalDate.now;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.common.pojo.QueryColumn;
import com.tencent.supersonic.common.pojo.exception.InvalidPermissionException;
import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp;
import com.tencent.supersonic.util.DataUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static java.time.LocalDate.now;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class QueryBySqlTest extends BaseTest {
@Test
@@ -89,10 +90,29 @@ public class QueryBySqlTest extends BaseTest {
}
@Test
public void testAuthorization() {
User alice = new User(2L, "alice", "alice", "alice@email", 0);
public void testAuthorization_model() {
User alice = DataUtils.getUserAlice();
assertThrows(InvalidPermissionException.class,
() -> queryBySql("SELECT SUM(pv) FROM 超音数PVUV统计 WHERE department ='HR'", alice));
}
@Test
public void testAuthorization_sensitive_metric() throws Exception {
User tom = DataUtils.getUserTom();
SemanticQueryResp semanticQueryResp =
queryBySql("SELECT SUM(stay_hours) FROM 停留时长统计 WHERE department ='HR'", tom);
Assertions.assertEquals(false, semanticQueryResp.getColumns().get(0).getAuthorized());
Assertions.assertEquals("******",
semanticQueryResp.getResultList().get(0).get("SUM(stay_hours)"));
}
@Test
public void testAuthorization_row_permission() throws Exception {
User tom = DataUtils.getUserTom();
SemanticQueryResp semanticQueryResp =
queryBySql("SELECT SUM(stay_hours) FROM 停留时长统计 WHERE department ='HR'", tom);
Assertions.assertNotNull(semanticQueryResp.getQueryAuthorization().getMessage());
Assertions.assertTrue(semanticQueryResp.getSql().contains("`user_name` = 'tom'"));
}
}

View File

@@ -1,22 +1,26 @@
package com.tencent.supersonic.headless;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import com.tencent.supersonic.auth.api.authentication.pojo.User;
import com.tencent.supersonic.common.pojo.Aggregator;
import com.tencent.supersonic.common.pojo.Filter;
import com.tencent.supersonic.common.pojo.QueryColumn;
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
import com.tencent.supersonic.common.pojo.enums.FilterOperatorEnum;
import com.tencent.supersonic.common.pojo.enums.QueryType;
import com.tencent.supersonic.common.pojo.exception.InvalidPermissionException;
import com.tencent.supersonic.headless.api.pojo.request.QueryStructReq;
import com.tencent.supersonic.headless.api.pojo.response.SemanticQueryResp;
import com.tencent.supersonic.util.DataUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class QueryByStructTest extends BaseTest {
@@ -89,10 +93,35 @@ public class QueryByStructTest extends BaseTest {
}
@Test
public void testAuthorization() {
public void testAuthorization_model() {
User alice = new User(2L, "alice", "alice", "alice@email", 0);
QueryStructReq queryStructReq1 = buildQueryStructReq(Arrays.asList("department"));
assertThrows(InvalidPermissionException.class,
() -> queryService.queryByReq(queryStructReq1, alice));
}
@Test
public void testAuthorization_sensitive_metric() throws Exception {
User tom = DataUtils.getUserTom();
Aggregator aggregator = new Aggregator();
aggregator.setFunc(AggOperatorEnum.SUM);
aggregator.setColumn("stay_hours");
QueryStructReq queryStructReq1 = buildQueryStructReq(Arrays.asList("department"), aggregator);
SemanticQueryResp semanticQueryResp = queryService.queryByReq(queryStructReq1, tom);
Assertions.assertEquals(false, semanticQueryResp.getColumns().get(1).getAuthorized());
Assertions.assertEquals("******", semanticQueryResp.getResultList().get(0).get("stay_hours"));
}
@Test
public void testAuthorization_row_permission() throws Exception {
User tom = DataUtils.getUserTom();
Aggregator aggregator = new Aggregator();
aggregator.setFunc(AggOperatorEnum.SUM);
aggregator.setColumn("stay_hours");
QueryStructReq queryStructReq1 = buildQueryStructReq(Arrays.asList("department"), aggregator);
SemanticQueryResp semanticQueryResp = queryService.queryByReq(queryStructReq1, tom);
Assertions.assertNotNull(semanticQueryResp.getQueryAuthorization().getMessage());
Assertions.assertTrue(semanticQueryResp.getSql().contains("`user_name` = 'tom'"));
}
}

View File

@@ -45,6 +45,15 @@ public class SchemaAuthTest extends BaseTest {
modelResps.stream().map(ModelResp::getId).collect(Collectors.toList()));
}
@Test
public void test_getVisibleModelList_alice() {
User user = DataUtils.getUserAlice();
List<ModelResp> modelResps = modelService.getModelListWithAuth(user, 0L, AuthType.VISIBLE);
List<Long> expectedModelIds = Lists.newArrayList(1L, 4L);
Assertions.assertEquals(expectedModelIds,
modelResps.stream().map(ModelResp::getId).collect(Collectors.toList()));
}
@Test
public void test_getViewList_alice() {
User user = DataUtils.getUserAlice();

View File

@@ -37,6 +37,10 @@ public class DataUtils {
return User.get(2L, "jack");
}
public static User getUserTom() {
return User.get(3L, "tom");
}
public static QueryReq getQueryContextReq(Integer id, String query) {
QueryReq queryContextReq = new QueryReq();
queryContextReq.setQueryText(query);