Rick Cheng
2 years ago
committed by
GitHub
14 changed files with 1162 additions and 33 deletions
@ -0,0 +1,85 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.AccessToken; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.AccessTokenMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class AccessTokenResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AccessTokenResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.AccessTokenResourcePermissionCheck accessTokenResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private AccessTokenMapper accessTokenMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(accessTokenResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = accessTokenResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.ACCESS_TOKEN), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
AccessToken accessToken = new AccessToken(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(accessToken.getId()); |
||||
List<AccessToken> accessTokens = Arrays.asList(accessToken); |
||||
|
||||
Mockito.when(accessTokenMapper.listAuthorizedAccessToken(user.getId(), null)).thenReturn(accessTokens); |
||||
|
||||
Assertions.assertEquals(ids, |
||||
accessTokenResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,84 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.AlertGroup; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class AlertGroupResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AlertGroupResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.AlertGroupResourcePermissionCheck alertGroupResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private AlertGroupMapper alertGroupMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(alertGroupResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = alertGroupResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.ALERT_GROUP), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
AlertGroup alertGroup = new AlertGroup(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(alertGroup.getId()); |
||||
List<AlertGroup> alertGroups = Arrays.asList(alertGroup); |
||||
|
||||
Mockito.when(alertGroupMapper.queryAllGroupList()).thenReturn(alertGroups); |
||||
|
||||
Assertions.assertEquals(ids, alertGroupResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,69 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class AlertPluginInstanceResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AlertPluginInstanceResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.AlertPluginInstanceResourcePermissionCheck alertPluginInstanceResourcePermissionCheck; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(alertPluginInstanceResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = alertPluginInstanceResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.ALERT_PLUGIN_INSTANCE), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
|
||||
Assertions.assertEquals(0, |
||||
alertPluginInstanceResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger).size()); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,84 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.DataSource; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class DataSourceResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataSourceResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.DataSourceResourcePermissionCheck dataSourceResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private DataSourceMapper dataSourceMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertTrue(dataSourceResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = dataSourceResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.DATASOURCE), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
DataSource dataSource = new DataSource(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(dataSource.getId()); |
||||
List<DataSource> dataSources = Arrays.asList(dataSource); |
||||
|
||||
Mockito.when(dataSourceMapper.listAuthorizedDataSource(user.getId(), null)).thenReturn(dataSources); |
||||
|
||||
Assertions.assertEquals(ids, dataSourceResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,85 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.Environment; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.EnvironmentMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class EnvironmentResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EnvironmentResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.EnvironmentResourcePermissionCheck environmentResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private EnvironmentMapper environmentMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertTrue(environmentResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = environmentResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.ENVIRONMENT), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
Environment environment = new Environment(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(environment.getId()); |
||||
List<Environment> environments = Arrays.asList(environment); |
||||
|
||||
Mockito.when(environmentMapper.queryAllEnvironmentList()).thenReturn(environments); |
||||
|
||||
Assertions.assertEquals(ids, |
||||
environmentResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,106 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.Resource; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.ResourceMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.ResourceUserMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class FilePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FilePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.FilePermissionCheck filePermissionCheck; |
||||
|
||||
@Mock |
||||
private ResourceMapper resourceMapper; |
||||
|
||||
@Mock |
||||
private ResourceUserMapper resourceUserMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertTrue(filePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = filePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Arrays.asList(AuthorizationType.RESOURCE_FILE_ID, AuthorizationType.UDF_FILE), |
||||
authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
// ADMIN_USER
|
||||
User user = getAdminUser(); |
||||
Resource resource = new Resource(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(resource.getId()); |
||||
List<Resource> resources = Arrays.asList(resource); |
||||
|
||||
Mockito.when(resourceMapper.queryResourceListAuthored(user.getId(), -1)).thenReturn(resources); |
||||
Assertions.assertEquals(ids, filePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
|
||||
// GENERAL_USER
|
||||
user = getLoginUser(); |
||||
Resource resource1 = new Resource(); |
||||
ids.add(resource1.getId()); |
||||
Mockito.when(resourceMapper.queryResourceListAuthored(user.getId(), -1)).thenReturn(resources); |
||||
Mockito.when(resourceUserMapper.queryResourcesIdListByUserIdAndPerm(user.getId(), 0)) |
||||
.thenReturn(Arrays.asList(resource1.getId())); |
||||
|
||||
Assertions.assertEquals(ids, filePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
|
||||
private User getAdminUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(0); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,85 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.K8sNamespace; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class K8sNamespaceResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(K8sNamespaceResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.K8sNamespaceResourcePermissionCheck k8sNamespaceResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private K8sNamespaceMapper k8sNamespaceMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(k8sNamespaceResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = k8sNamespaceResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.K8S_NAMESPACE), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
K8sNamespace k8sNamespace = new K8sNamespace(); |
||||
Set<Integer> ids = new HashSet<>(); |
||||
ids.add(k8sNamespace.getId()); |
||||
List<K8sNamespace> k8sNamespaces = Arrays.asList(k8sNamespace); |
||||
|
||||
Mockito.when(k8sNamespaceMapper.queryAuthedNamespaceListByUserId(user.getId())).thenReturn(k8sNamespaces); |
||||
|
||||
Assertions.assertEquals(ids, |
||||
k8sNamespaceResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,93 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.Project; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class ProjectsResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProjectsResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.ProjectsResourcePermissionCheck projectsResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private ProjectMapper projectMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertTrue(projectsResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = projectsResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.PROJECTS), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
Project project = getProject(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(project.getId()); |
||||
List<Project> projects = Arrays.asList(project); |
||||
|
||||
Mockito.when(projectMapper.listAuthorizedProjects(user.getId(), null)).thenReturn(projects); |
||||
|
||||
Assertions.assertEquals(ids, projectsResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
|
||||
private Project getProject() { |
||||
Project project = new Project(); |
||||
project.setCode(1L); |
||||
project.setId(1); |
||||
project.setName("projectName"); |
||||
project.setUserId(1); |
||||
return project; |
||||
} |
||||
} |
@ -0,0 +1,90 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.Queue; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.QueueMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class QueueResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(QueueResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.QueueResourcePermissionCheck queueResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private QueueMapper queueMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(queueResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = queueResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.QUEUE), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
Queue queue = new Queue(); |
||||
Mockito.when(queueMapper.selectList(null)).thenReturn(Arrays.asList(queue)); |
||||
// GENERAL_USER
|
||||
User user = getLoginUser(); |
||||
Assertions.assertEquals(0, queueResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger).size()); |
||||
|
||||
// ADMIN_USER
|
||||
user = getAdminUser(); |
||||
Assertions.assertEquals(1, queueResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger).size()); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
|
||||
private User getAdminUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(0); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,84 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.TaskGroup; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.TaskGroupMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class TaskGroupPermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TaskGroupPermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.TaskGroupPermissionCheck taskGroupPermissionCheck; |
||||
|
||||
@Mock |
||||
private TaskGroupMapper taskGroupMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertTrue(taskGroupPermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = taskGroupPermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.TASK_GROUP), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
TaskGroup taskGroup = new TaskGroup(); |
||||
Set<Integer> ids = new HashSet<>(); |
||||
ids.add(taskGroup.getId()); |
||||
List<TaskGroup> taskGroups = Arrays.asList(taskGroup); |
||||
|
||||
Mockito.when(taskGroupMapper.listAuthorizedResource(user.getId())).thenReturn(taskGroups); |
||||
|
||||
Assertions.assertEquals(ids, taskGroupPermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,84 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.Tenant; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.TenantMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class TenantResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TenantResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.TenantResourcePermissionCheck tenantResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private TenantMapper tenantMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(tenantResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = tenantResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.TENANT), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
Tenant tenant = new Tenant(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(tenant.getId()); |
||||
List<Tenant> tenants = Arrays.asList(tenant); |
||||
|
||||
Mockito.when(tenantMapper.queryAll()).thenReturn(tenants); |
||||
|
||||
Assertions.assertEquals(ids, tenantResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
@ -0,0 +1,94 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.Project; |
||||
import org.apache.dolphinscheduler.dao.entity.UdfFunc; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.UdfFuncMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class UdfFuncPermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UdfFuncPermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.UdfFuncPermissionCheck udfFuncPermissionCheck; |
||||
|
||||
@Mock |
||||
private UdfFuncMapper udfFuncMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertTrue(udfFuncPermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = udfFuncPermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.UDF), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
UdfFunc udfFunc = new UdfFunc(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(udfFunc.getId()); |
||||
List<UdfFunc> udfFuncs = Arrays.asList(udfFunc); |
||||
|
||||
Mockito.when(udfFuncMapper.listAuthorizedUdfByUserId(user.getId())).thenReturn(udfFuncs); |
||||
|
||||
Assertions.assertEquals(ids, udfFuncPermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
|
||||
private Project getProject() { |
||||
Project project = new Project(); |
||||
project.setCode(1L); |
||||
project.setId(1); |
||||
project.setName("projectName"); |
||||
project.setUserId(1); |
||||
return project; |
||||
} |
||||
} |
@ -0,0 +1,85 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.apache.dolphinscheduler.api.permission; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.AuthorizationType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.entity.WorkerGroup; |
||||
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
public class WorkerGroupResourcePermissionCheckTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WorkerGroupResourcePermissionCheckTest.class); |
||||
@InjectMocks |
||||
private ResourcePermissionCheckServiceImpl.WorkerGroupResourcePermissionCheck workerGroupResourcePermissionCheck; |
||||
|
||||
@Mock |
||||
private WorkerGroupMapper workerGroupMapper; |
||||
|
||||
@Test |
||||
public void testPermissionCheck() { |
||||
User user = getLoginUser(); |
||||
Assertions.assertFalse(workerGroupResourcePermissionCheck.permissionCheck(user.getId(), null, logger)); |
||||
} |
||||
|
||||
@Test |
||||
public void testAuthorizationTypes() { |
||||
List<AuthorizationType> authorizationTypes = workerGroupResourcePermissionCheck.authorizationTypes(); |
||||
Assertions.assertEquals(Collections.singletonList(AuthorizationType.WORKER_GROUP), authorizationTypes); |
||||
} |
||||
|
||||
@Test |
||||
public void testListAuthorizedResourceIds() { |
||||
User user = getLoginUser(); |
||||
WorkerGroup workerGroup = new WorkerGroup(); |
||||
Set<Integer> ids = new HashSet(); |
||||
ids.add(workerGroup.getId()); |
||||
List<WorkerGroup> workerGroups = Arrays.asList(workerGroup); |
||||
|
||||
Mockito.when(workerGroupMapper.queryAllWorkerGroup()).thenReturn(workerGroups); |
||||
|
||||
Assertions.assertEquals(ids, |
||||
workerGroupResourcePermissionCheck.listAuthorizedResourceIds(user.getId(), logger)); |
||||
} |
||||
|
||||
private User getLoginUser() { |
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName("test"); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
} |
||||
} |
Loading…
Reference in new issue