Search in sources :

Example 46 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class AbstractBasicVOServiceWithPermissionsTestBase method testAddEditRoleUserDoesNotHave.

/**
 * There will be 2 validation messages about this, must retain permission AND cannot add/remove a role you do not have
 */
@Test
public void testAddEditRoleUserDoesNotHave() {
    validation.expectValidationException(getEditPermissionContextKey());
    VO vo = newVO(editUser);
    setReadPermission(MangoPermission.requireAnyRole(roleService.getUserRole()), vo);
    setEditPermission(MangoPermission.requireAnyRole(roleService.getUserRole()), vo);
    service.insert(vo);
    runAs.runAs(readUser, () -> {
        VO fromDb = service.get(vo.getId());
        assertVoEqual(vo, fromDb);
        setEditPermission(MangoPermission.superadminOnly(), fromDb);
        service.update(fromDb.getId(), fromDb);
    });
}
Also used : AbstractBasicVO(com.serotonin.m2m2.vo.AbstractBasicVO) Test(org.junit.Test)

Example 47 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class AbstractBasicVOServiceWithPermissionsTestBase method testAddReadRoleUserDoesNotHave.

/**
 * There will be 2 validation messages about this, must retain permission AND cannot add/remove a role you do not have
 */
@Test
public void testAddReadRoleUserDoesNotHave() {
    validation.expectValidationException(getReadPermissionContextKey());
    VO vo = newVO(editUser);
    setReadPermission(MangoPermission.requireAnyRole(roleService.getUserRole()), vo);
    setEditPermission(MangoPermission.requireAnyRole(roleService.getUserRole()), vo);
    service.insert(vo);
    runAs.runAs(readUser, () -> {
        VO fromDb = service.get(vo.getId());
        assertVoEqual(vo, fromDb);
        setReadPermission(MangoPermission.superadminOnly(), fromDb);
        service.update(fromDb.getId(), fromDb);
    });
}
Also used : AbstractBasicVO(com.serotonin.m2m2.vo.AbstractBasicVO) Test(org.junit.Test)

Example 48 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class DataPointPermissionTest method testDeleteDataPoint.

/**
 * Test to ensure an un-used permission is deleted
 */
@Test
public void testDeleteDataPoint() {
    // Insert some data points
    Set<Role> readRoles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    List<IDataPoint> points = this.createMockDataPoints(1, false, MangoPermission.requireAnyRole(readRoles), new MangoPermission());
    DataPointService service = Common.getBean(DataPointService.class);
    runAs.runAs(new PermissionHolder() {

        @Override
        public String getPermissionHolderName() {
            return "Test";
        }

        @Override
        public boolean isPermissionHolderDisabled() {
            return false;
        }

        @Override
        public Set<Role> getRoles() {
            return readRoles;
        }
    }, () -> {
        List<Integer> ids = points.stream().map(dp -> dp.getId()).collect(Collectors.toList());
        QueryBuilder<DataPointVO> query = service.buildQuery().in("id", ids.toArray());
        List<DataPointVO> vos = query.query();
        assertEquals(points.size(), vos.size());
        for (DataPointVO vo : vos) {
            assertTrue(points.contains(vo));
        }
        List<Integer> existing = getPermissionIds(null);
        // Delete the source and point
        DataSourceDao.getInstance().delete(vos.get(0).getDataSourceId());
        // We should have removed 1 permission
        assertEquals(existing.size() - 1, getPermissionIds(null).size());
    });
}
Also used : RunAs(com.infiniteautomation.mango.spring.components.RunAs) Role(com.serotonin.m2m2.vo.role.Role) Common(com.serotonin.m2m2.Common) DataSourceDao(com.serotonin.m2m2.db.dao.DataSourceDao) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Permissions(com.infiniteautomation.mango.db.tables.Permissions) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Collectors(java.util.stream.Collectors) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) QueryBuilder(com.serotonin.m2m2.db.dao.QueryBuilder) List(java.util.List) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) Before(org.junit.Before) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Set(java.util.Set) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Role(com.serotonin.m2m2.vo.role.Role) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) IDataPoint(com.serotonin.m2m2.vo.IDataPoint) Test(org.junit.Test)

Example 49 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class PermissionPersistenceTest method testModifyPermission.

/**
 * Modify a permission and ensure that it is retrieved correctly from the database after
 */
@Test
public void testModifyPermission() {
    PermissionService service = Common.getBean(PermissionService.class);
    // insert some roles
    Set<Role> roles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    // insert the permission
    MangoPermission permission = service.findOrCreate(MangoPermission.requireAnyRole(roles));
    MangoPermission read = service.get(permission.getId());
    assertEquals(2, read.getRoles().size());
    Iterator<Set<Role>> it = read.getRoles().iterator();
    Role toKeep = it.next().iterator().next();
    MangoPermission keep = service.findOrCreate(MangoPermission.requireAnyRole(toKeep));
    read = service.get(keep.getId());
    assertEquals(1, read.getRoles().size());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Role(com.serotonin.m2m2.vo.role.Role) Iterator(java.util.Iterator) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Set(java.util.Set) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 50 with Role

use of com.serotonin.m2m2.vo.role.Role in project ma-core-public by infiniteautomation.

the class PermissionPersistenceTest method testOrPermission.

@Test
public void testOrPermission() {
    PermissionService service = Common.getBean(PermissionService.class);
    // Create some roles
    Set<Role> roles = this.createRoles(2).stream().map(r -> r.getRole()).collect(Collectors.toSet());
    // insert the permission
    MangoPermission permission = service.findOrCreate(MangoPermission.requireAnyRole(roles));
    MangoPermission read = service.get(permission.getId());
    assertEquals(2, read.getRoles().size());
    Iterator<Set<Role>> it = read.getRoles().iterator();
    assertEquals(1, it.next().size());
    assertEquals(1, it.next().size());
}
Also used : PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Role(com.serotonin.m2m2.vo.role.Role) Role(com.serotonin.m2m2.vo.role.Role) Iterator(java.util.Iterator) Common(com.serotonin.m2m2.Common) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RoleDao(com.serotonin.m2m2.db.dao.RoleDao) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) MangoTestBase(com.serotonin.m2m2.MangoTestBase) DSLContext(org.jooq.DSLContext) Assert.assertEquals(org.junit.Assert.assertEquals) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Set(java.util.Set) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Role (com.serotonin.m2m2.vo.role.Role)102 Test (org.junit.Test)59 HashSet (java.util.HashSet)40 Set (java.util.Set)38 User (com.serotonin.m2m2.vo.User)33 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)23 RoleVO (com.serotonin.m2m2.vo.role.RoleVO)22 Collectors (java.util.stream.Collectors)18 Common (com.serotonin.m2m2.Common)17 MangoTestBase (com.serotonin.m2m2.MangoTestBase)15 RoleDao (com.serotonin.m2m2.db.dao.RoleDao)15 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)15 List (java.util.List)15 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)14 Assert.assertEquals (org.junit.Assert.assertEquals)14 Assert.assertTrue (org.junit.Assert.assertTrue)14 DataPointService (com.infiniteautomation.mango.spring.service.DataPointService)12 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)12 IDataPoint (com.serotonin.m2m2.vo.IDataPoint)11 DSLContext (org.jooq.DSLContext)11