Search in sources :

Example 51 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class AccessControlTest method test01SetOwner.

@Test
public void test01SetOwner() {
    try {
        Principal user1 = null;
        Principal user2 = null;
        TestOne t1 = null;
        Class type = TestOne.class;
        try (final Tx tx = app.tx()) {
            List<Principal> users = createTestNodes(Principal.class, 2);
            user1 = (Principal) users.get(0);
            user1.setProperty(AbstractNode.name, "user1");
            user2 = (Principal) users.get(1);
            user2.setProperty(AbstractNode.name, "user2");
            t1 = createTestNode(TestOne.class);
            t1.setProperty(AbstractNode.owner, user1);
            tx.success();
        } catch (FrameworkException ex) {
            logger.error(ex.toString());
        }
        try (final Tx tx = app.tx()) {
            assertEquals(user1, t1.getProperty(AbstractNode.owner));
            // Switch user context to user1
            final App user1App = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));
            // Check if user1 can see t1
            assertEquals(t1, user1App.nodeQuery(type).getFirst());
        }
        try (final Tx tx = app.tx()) {
            // As superuser, make another user the owner
            t1.setProperty(AbstractNode.owner, user2);
            tx.success();
        } catch (FrameworkException ex) {
            logger.error(ex.toString());
        }
        try (final Tx tx = app.tx()) {
            // Switch user context to user2
            final App user2App = StructrApp.getInstance(SecurityContext.getInstance(user2, AccessMode.Backend));
            // Check if user2 can see t1
            assertEquals(t1, user2App.nodeQuery(type).getFirst());
            // Check if user2 is owner of t1
            assertEquals(user2, t1.getProperty(AbstractNode.owner));
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 52 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class AccessControlTest method test04BackendUserAccessToProtectedNode.

@Test
public void test04BackendUserAccessToProtectedNode() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        List<Principal> users = createTestNodes(Principal.class, 2);
        Principal user1 = (Principal) users.get(0);
        Principal user2 = (Principal) users.get(1);
        PropertyMap props = new PropertyMap();
        props.put(AbstractNode.visibleToPublicUsers, true);
        // Create two nodes with user context, one of them is visible to public users
        Class type = TestOne.class;
        TestOne t1 = createTestNode(TestOne.class, props, user1);
        props = new PropertyMap();
        props.put(AbstractNode.visibleToAuthenticatedUsers, true);
        TestOne t2 = createTestNode(TestOne.class, props, user1);
        // Let another user search
        SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Backend);
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
            assertEquals(2, result.size());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Result(org.structr.core.Result) Test(org.junit.Test)

Example 53 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class AccessControlTest method test02PublicAccessToPublicNode.

@Test
public void test02PublicAccessToPublicNode() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        List<Principal> users = createTestNodes(Principal.class, 1);
        Principal user = (Principal) users.get(0);
        PropertyMap props = new PropertyMap();
        props.put(AbstractNode.visibleToPublicUsers, true);
        // Create two nodes with user context, one of them is visible to public users
        Class type = TestOne.class;
        TestOne t1 = createTestNode(TestOne.class, props, user);
        TestOne t2 = createTestNode(TestOne.class, user);
        SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(publicContext).nodeQuery(type).getResult();
            assertEquals(1, result.size());
            assertEquals(t1.getUuid(), result.get(0).getUuid());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Result(org.structr.core.Result) Test(org.junit.Test)

Example 54 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class AccessControlTest method test06GrantReadPermission.

@Test
public void test06GrantReadPermission() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        List<Principal> users = createTestNodes(Principal.class, 2);
        Principal user1 = (Principal) users.get(0);
        Principal user2 = (Principal) users.get(1);
        Result result = null;
        // Let user 1 create a node
        Class type = TestOne.class;
        final TestOne t1 = createTestNode(TestOne.class, user1);
        try (final Tx tx = app.tx()) {
            // Grant read permission to user 2
            t1.grant(Permission.read, user2);
            tx.success();
        }
        // Let user 2 search
        SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Backend);
        try (final Tx tx = app.tx()) {
            result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
            assertEquals(1, result.size());
            assertEquals(t1.getUuid(), result.get(0).getUuid());
        }
        try (final Tx tx = app.tx()) {
            // Revoke permission again
            t1.revoke(Permission.read, user2);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
            assertTrue(result.isEmpty());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Result(org.structr.core.Result) Test(org.junit.Test)

Example 55 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class BasicTest method test02CreateTwoNodesWithSameUuidInTwoTx.

@Test
public void test02CreateTwoNodesWithSameUuidInTwoTx() {
    try {
        final PropertyMap props = new PropertyMap();
        TestOne node = null;
        final String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
        props.put(GraphObject.id, uuid);
        try (final Tx tx = app.tx()) {
            node = app.create(TestOne.class, props);
            assertTrue(node != null);
            assertTrue(node instanceof TestOne);
            assertEquals(node.getUuid(), uuid);
            tx.success();
        } catch (FrameworkException ex) {
            logger.error(ex.toString());
            fail("Unexpected exception");
        }
        try (final Tx tx = app.tx()) {
            node = app.create(TestOne.class, props);
            tx.success();
            fail("Validation failed!");
        }
    } catch (FrameworkException ex) {
        // validate exception
        ex.printStackTrace();
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)74 TestOne (org.structr.core.entity.TestOne)74 Tx (org.structr.core.graph.Tx)72 FrameworkException (org.structr.common.error.FrameworkException)70 StructrTest (org.structr.common.StructrTest)20 Result (org.structr.core.Result)15 Principal (org.structr.core.entity.Principal)15 TestSix (org.structr.core.entity.TestSix)15 NodeInterface (org.structr.core.graph.NodeInterface)15 Random (java.util.Random)12 ActionContext (org.structr.schema.action.ActionContext)12 LinkedList (java.util.LinkedList)10 TestFour (org.structr.core.entity.TestFour)9 PropertyMap (org.structr.core.property.PropertyMap)9 PropertyKey (org.structr.core.property.PropertyKey)8 Date (java.util.Date)7 App (org.structr.core.app.App)7 StructrApp (org.structr.core.app.StructrApp)7 NotFoundException (org.structr.api.NotFoundException)6 UnlicensedException (org.structr.common.error.UnlicensedException)6