Test cases for DeviceAccessAuthorizationService

revert-70aa11f8
GDLMadushanka 7 years ago
parent 4e49da00d7
commit 83a9ed533e

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

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

@ -18,73 +18,137 @@
package org.wso2.carbon.device.mgt.core.authorization; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.mockito.Mock; import org.mockito.Mockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.common.BaseDeviceManagementTest;
import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.common.TestDataHolder;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import static org.mockito.Matchers.anyString; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import static org.mockito.Mockito.mock; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import static org.mockito.Mockito.when; 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 { public class DeviceAccessAuthorizationServiceTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceAccessAuthorizationServiceTest.class); private static final Log log = LogFactory.getLog(DeviceAccessAuthorizationServiceTest.class);
DeviceDAO deviceDAO; private DeviceAccessAuthorizationServiceImpl deviceAccessAuthorizationService;
DeviceTypeDAO deviceTypeDAO; private static final String DEVICE_TYPE = "AUTH_SERVICE_TEST_TYPE";
DeviceAccessAuthorizationServiceImpl deviceAccessAuthorizationService = new DeviceAccessAuthorizationServiceImpl(); 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 @BeforeClass
@Override
public void init() throws Exception { public void init() throws Exception {
this.initDataSource(); DeviceConfigurationManager.getInstance().initConfig();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); log.info("Initializing");
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); 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.") private RegistryService getRegistryService() throws RegistryException, UserStoreException {
public void setUp() throws Exception { RealmService realmService = new InMemoryRealmService();
log.info("test start"); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ADMIN_USER);
BasicDataSource dataSource = new BasicDataSource();
DeviceManagementDAOFactory.beginTransaction(); String connectionUrl = "jdbc:h2:./target/databasetest/CARBON_TEST";
DeviceType deviceType = new DeviceType(); dataSource.setUrl(connectionUrl);
deviceType.setName("Sample"); dataSource.setDriverClassName("org.h2.Driver");
JDBCTenantManager jdbcTenantManager = new JDBCTenantManager(dataSource, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
deviceTypeDAO.addDeviceType(deviceType, -1234, true); realmService.setTenantManager(jdbcTenantManager);
deviceType = deviceTypeDAO.getDeviceType("Sample", -1234); RegistryDataHolder.getInstance().setRealmService(realmService);
log.info(deviceType.getId()); DeviceManagementDataHolder.getInstance().setRealmService(realmService);
Assert.assertEquals(deviceType.getName(), "Sample"); InputStream is = this.getClass().getClassLoader().getResourceAsStream("carbon-home/repository/conf/registry.xml");
RegistryContext context = RegistryContext.getBaseInstance(is, realmService);
Device device = new Device(); context.setSetup(true);
device.setId(1); return context.getEmbeddedRegistryService();
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);
DeviceManagementDAOFactory.closeConnection(); @Test
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); public void isUserAuthenticated() throws Exception {
deviceIdentifier.setId("1"); for (DeviceIdentifier deviceId : deviceIds) {
deviceIdentifier.setType("Sample"); 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