Test cases for DeviceAccessAuthorizationService

4.x.x
GDLMadushanka 7 years ago
parent 4e49da00d7
commit 83a9ed533e

@ -346,6 +346,12 @@
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${power.mock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -89,6 +89,7 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori
}
}
//done
@Override
public boolean isUserAuthorized(DeviceIdentifier deviceIdentifier, String username)
throws DeviceAccessAuthorizationException {
@ -101,11 +102,13 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori
return isUserAuthorized(deviceIdentifier, this.getUserName(), groupPermissions);
}
//done
@Override
public boolean isUserAuthorized(DeviceIdentifier deviceIdentifier) throws DeviceAccessAuthorizationException {
return isUserAuthorized(deviceIdentifier, this.getUserName(), null);
}
//done
@Override
public boolean isDeviceAdminUser() throws DeviceAccessAuthorizationException {
String username = this.getUserName();
@ -165,12 +168,14 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori
return deviceAuthorizationResult;
}
//done
@Override
public DeviceAuthorizationResult isUserAuthorized(List<DeviceIdentifier> deviceIdentifiers, String username)
throws DeviceAccessAuthorizationException {
return isUserAuthorized(deviceIdentifiers, username, null);
}
//done
@Override
public DeviceAuthorizationResult isUserAuthorized(List<DeviceIdentifier> deviceIdentifiers)
throws DeviceAccessAuthorizationException {

@ -18,73 +18,137 @@
package org.wso2.carbon.device.mgt.core.authorization;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAuthorizationResult;
import org.wso2.carbon.device.mgt.common.permission.mgt.*;
import org.wso2.carbon.device.mgt.core.TestDeviceManagementService;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
import org.wso2.carbon.device.mgt.core.dao.*;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.user.api.UserStoreManager;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.registry.core.config.RegistryContext;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.internal.RegistryDataHolder;
import org.wso2.carbon.registry.core.jdbc.realm.InMemoryRealmService;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.api.*;
import org.wso2.carbon.user.api.Permission;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.JDBCTenantManager;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class DeviceAccessAuthorizationServiceTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceAccessAuthorizationServiceTest.class);
DeviceDAO deviceDAO;
DeviceTypeDAO deviceTypeDAO;
DeviceAccessAuthorizationServiceImpl deviceAccessAuthorizationService = new DeviceAccessAuthorizationServiceImpl();
private DeviceAccessAuthorizationServiceImpl deviceAccessAuthorizationService;
private static final String DEVICE_TYPE = "AUTH_SERVICE_TEST_TYPE";
private static final int NO_OF_DEVICES = 5;
private static final String ADMIN_USER = "admin";
private static final String ADMIN_ROLE = "adminRole";
private static final String DEVICE_ID_PREFIX = "AUTH-SERVICE-TEST-DEVICE-ID-";
private List<DeviceIdentifier> deviceIds = new ArrayList<>();
// UserStoreManager userStoreManager = mock(UserStoreManager.class);
@BeforeClass
@Override
public void init() throws Exception {
this.initDataSource();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
DeviceConfigurationManager.getInstance().initConfig();
log.info("Initializing");
for (int i = 0; i < NO_OF_DEVICES; i++) {
deviceIds.add(new DeviceIdentifier(DEVICE_ID_PREFIX + i, DEVICE_TYPE));
}
List<Device> devices = TestDataHolder.generateDummyDeviceData(this.deviceIds);
DeviceManagementProviderService deviceMgtService = new DeviceManagementProviderServiceImpl();
DeviceManagementServiceComponent.notifyStartupListeners();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceMgtService);
DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService());
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl());
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl());
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));
for (Device device : devices) {
deviceMgtService.enrollDevice(device);
}
List<Device> returnedDevices = deviceMgtService.getAllDevices(DEVICE_TYPE);
for (Device device : returnedDevices) {
if (!device.getDeviceIdentifier().startsWith(DEVICE_ID_PREFIX)) {
throw new Exception("Incorrect device with ID - " + device.getDeviceIdentifier() + " returned!");
}
}
deviceAccessAuthorizationService = Mockito.mock(DeviceAccessAuthorizationServiceImpl.class, Mockito.CALLS_REAL_METHODS);
}
@Test(groups = "device.mgt.test", description = "Testing the first test case with testng.")
public void setUp() throws Exception {
log.info("test start");
DeviceManagementDAOFactory.beginTransaction();
DeviceType deviceType = new DeviceType();
deviceType.setName("Sample");
deviceTypeDAO.addDeviceType(deviceType, -1234, true);
deviceType = deviceTypeDAO.getDeviceType("Sample", -1234);
log.info(deviceType.getId());
Assert.assertEquals(deviceType.getName(), "Sample");
Device device = new Device();
device.setId(1);
device.setDeviceIdentifier("device1");
device.setName("sample device");
device.setType("Sample");
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setOwner("Lahiru");
device.setEnrolmentInfo(enrolmentInfo);
deviceDAO.addDevice(1,device,-1234);
private RegistryService getRegistryService() throws RegistryException, UserStoreException {
RealmService realmService = new InMemoryRealmService();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ADMIN_USER);
BasicDataSource dataSource = new BasicDataSource();
String connectionUrl = "jdbc:h2:./target/databasetest/CARBON_TEST";
dataSource.setUrl(connectionUrl);
dataSource.setDriverClassName("org.h2.Driver");
JDBCTenantManager jdbcTenantManager = new JDBCTenantManager(dataSource, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
realmService.setTenantManager(jdbcTenantManager);
RegistryDataHolder.getInstance().setRealmService(realmService);
DeviceManagementDataHolder.getInstance().setRealmService(realmService);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
context.setSetup(true);
return context.getEmbeddedRegistryService();
}
DeviceManagementDAOFactory.closeConnection();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId("1");
deviceIdentifier.setType("Sample");
@Test
public void isUserAuthenticated() throws Exception {
for (DeviceIdentifier deviceId : deviceIds) {
Assert.assertTrue(deviceAccessAuthorizationService.isUserAuthorized(deviceId, ADMIN_USER));
}
}
// Assert.assertTrue(deviceAccessAuthorizationService.isUserAuthorized(deviceIdentifier,"Lahiru"));
@Test
public void isUserAuthenticatedList() throws Exception {
DeviceAuthorizationResult deviceAuthorizationResult = deviceAccessAuthorizationService.
isUserAuthorized(deviceIds, ADMIN_USER);
Assert.assertEquals(deviceAuthorizationResult.getAuthorizedDevices().size(), 5);
Assert.assertEquals(deviceAuthorizationResult.getUnauthorizedDevices().size(), 0);
}
@Test
public void isUserAuthenticatedListOnlyDevId() throws Exception {
DeviceAuthorizationResult deviceAuthorizationResult = deviceAccessAuthorizationService.isUserAuthorized(deviceIds);
Assert.assertEquals(deviceAuthorizationResult.getAuthorizedDevices().size(), 5);
Assert.assertEquals(deviceAuthorizationResult.getUnauthorizedDevices().size(), 0);
}
@Test
public void isUserAuthenticatedOnlyDevId() throws Exception {
for (DeviceIdentifier deviceId : deviceIds) {
Assert.assertTrue(deviceAccessAuthorizationService.isUserAuthorized(deviceId));
}
}
@Test
public void isDeviceAdminUser() throws DeviceAccessAuthorizationException, UserStoreException, PermissionManagementException {
Permission permission = new Permission("/permission/admin", CarbonConstants.UI_PERMISSION_ACTION);
DeviceManagementDataHolder.getInstance().getRealmService()
.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getUserStoreManager()
.addRole(ADMIN_ROLE, new String[]{ADMIN_USER}, new Permission[]{permission});
Assert.assertTrue(deviceAccessAuthorizationService.isDeviceAdminUser());
}
}

Loading…
Cancel
Save