Search in sources :

Example 46 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class RenderContextTest method testAnyAllAndNoneFunctions1.

@Test
public void testAnyAllAndNoneFunctions1() {
    final ActionContext ctx = new ActionContext(securityContext, null);
    Principal user = null;
    TestOne test = null;
    try (final Tx tx = app.tx()) {
        user = app.create(User.class, "user1");
        test = app.create(TestOne.class, "test1");
        app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group1"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
        final Group group2 = app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group2"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
        app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group3"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
        test.setProperty(AbstractNode.owner, group2);
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        ctx.setConstant("user", user);
        ctx.setConstant("test", test);
        assertEquals("Invalid any() result", "true", Scripting.replaceVariables(ctx, null, "${any(user.groups, is_allowed(data, test, 'read'))}"));
        assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(user.groups, is_allowed(data, test, 'read'))}"));
        assertEquals("Invalid none() result", "false", Scripting.replaceVariables(ctx, null, "${none(user.groups, is_allowed(data, test, 'read'))}"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Group(org.structr.core.entity.Group) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.web.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 47 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class RenderContextTest method testVariableReplacementInDynamicTypes.

@Test
public void testVariableReplacementInDynamicTypes() {
    SchemaNode itemNode = null;
    NodeInterface parent = null;
    NodeInterface child1 = null;
    NodeInterface child2 = null;
    try (final Tx tx = app.tx()) {
        itemNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"));
        final PropertyMap properties = new PropertyMap();
        properties.put(SchemaRelationshipNode.sourceId, itemNode.getUuid());
        properties.put(SchemaRelationshipNode.targetId, itemNode.getUuid());
        properties.put(SchemaRelationshipNode.relationshipType, "CHILD");
        properties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
        properties.put(SchemaRelationshipNode.targetMultiplicity, "*");
        properties.put(SchemaRelationshipNode.sourceJsonName, "parentItem");
        properties.put(SchemaRelationshipNode.targetJsonName, "children");
        app.create(SchemaRelationshipNode.class, properties);
        // compile the stuff
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class itemClass = config.getNodeEntityClass("Item");
    final PropertyKey childrenProperty = StructrApp.key(itemClass, "children");
    // create parent/child relationship
    try (final Tx tx = app.tx()) {
        parent = app.create(itemClass);
        child1 = app.create(itemClass);
        child2 = app.create(itemClass);
        final List<NodeInterface> children = new LinkedList<>();
        children.add(child1);
        children.add(child2);
        parent.setProperties(parent.getSecurityContext(), new PropertyMap(childrenProperty, children));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // verify that parent has two children
    try (final Tx tx = app.tx()) {
        // verify that parentItem can be accessed....
        final Object value = parent.getProperty(childrenProperty);
        assertTrue(value instanceof Collection);
        final Collection coll = (Collection) value;
        assertEquals(2, coll.size());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // check property access in template expressions
    try (final Tx tx = app.tx()) {
        assertEquals(parent.toString(), Scripting.replaceVariables(new ActionContext(securityContext), child1, "${this.parentItem}"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) ActionContext(org.structr.schema.action.ActionContext) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) Collection(java.util.Collection) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 48 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class RenderContextTest method testFunctionEvaluationInDynamicTypes.

@Test
public void testFunctionEvaluationInDynamicTypes() {
    NodeInterface item = null;
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testMethodCalled"), "Boolean"), new NodeAttribute(new StringProperty("___testMethod"), "set(this, 'testMethodCalled', true)"));
        // compile the stuff
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class itemClass = config.getNodeEntityClass("Item");
    // create parent/child relationship
    try (final Tx tx = app.tx()) {
        item = app.create(itemClass, new NodeAttribute(SchemaNode.name, "Item"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // check property access in template expressions
    try (final Tx tx = app.tx()) {
        final RenderContext renderContext = new RenderContext(securityContext);
        renderContext.putDataObject("item", item);
        assertEquals("Invalid combined array dot syntax result: ", "Item", Scripting.replaceVariables(renderContext, item, "${find('Item')[0].name}"));
        Scripting.replaceVariables(renderContext, item, "${item.testMethod()}");
        assertEquals("Invalid method evaluation result: ", "true", Scripting.replaceVariables(renderContext, item, "${item.testMethodCalled}"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) StringProperty(org.structr.core.property.StringProperty) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 49 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class RenderContextTest method testScriptEvaluation.

@Test
public void testScriptEvaluation() {
    Settings.CypherDebugLogging.setValue(true);
    try (final Tx tx = app.tx()) {
        // create a Project type
        final SchemaNode projectNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Project"));
        // create a Task type with a string property "task"
        final SchemaNode taskNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task"), new NodeAttribute<>(new StringProperty("_task"), "String"));
        // create a schema relationship between them
        createTestNode(SchemaRelationshipNode.class, new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceNode"), projectNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetNode"), taskNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "relationshipType"), "has"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceMultiplicity"), "1"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetMultiplicity"), "*"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceJsonName"), "project"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetJsonName"), "tasks"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        // obtain class objects to create instances of the above types
        final Class projectType = StructrApp.getConfiguration().getNodeEntityClass("Project");
        final Class taskType = StructrApp.getConfiguration().getNodeEntityClass("Task");
        final PropertyKey taskKey = StructrApp.key(taskType, "task");
        final PropertyKey tasksKey = StructrApp.key(projectType, "tasks");
        final List<NodeInterface> tasks = new LinkedList<>();
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 1"), new NodeAttribute<>(taskKey, "Task 1")));
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 2"), new NodeAttribute<>(taskKey, "Task 2")));
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 3"), new NodeAttribute<>(taskKey, "Task 3")));
        // create a project and a task
        final NodeInterface project = app.create(projectType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "project"), new NodeAttribute<>(tasksKey, tasks));
        // create an additional test task without a project
        final NodeInterface testTask = app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "test task"), new NodeAttribute<>(taskKey, "test task"));
        final RenderContext renderContext = new RenderContext(securityContext);
        renderContext.putDataObject("project", project);
        renderContext.putDataObject("task", testTask);
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${foo.page}"));
        assertEquals("Invalid scripting evaluation result", testTask.getUuid(), Scripting.replaceVariables(renderContext, null, "${task}"));
        assertEquals("Invalid scripting evaluation result", "test task", Scripting.replaceVariables(renderContext, null, "${task.task}"));
        assertEquals("Invalid scripting evaluation result", tasks.toString(), Scripting.replaceVariables(renderContext, null, "${project.tasks}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(0).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[0]}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(1).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[1]}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(2).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[2]}"));
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3]}"));
        assertEquals("Invalid scripting evaluation result", "Task 1", Scripting.replaceVariables(renderContext, null, "${project.tasks[0].task}"));
        assertEquals("Invalid scripting evaluation result", "Task 2", Scripting.replaceVariables(renderContext, null, "${project.tasks[1].task}"));
        assertEquals("Invalid scripting evaluation result", "Task 3", Scripting.replaceVariables(renderContext, null, "${project.tasks[2].task}"));
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3].task}"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 50 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class ResourceAccessTest method test03ResourceAccessPUT.

@Test
public void test03ResourceAccessPUT() {
    // clear resource access objects that are created by the dynamic schema
    clearResourceAccess();
    final String name = "testuser-01";
    final String password = "testpassword-01";
    ResourceAccess folderGrant = null;
    User testUser = null;
    Folder testFolder = null;
    try (final Tx tx = app.tx()) {
        testUser = createTestNodes(User.class, 1).get(0);
        testFolder = createTestNodes(Folder.class, 1).get(0);
        assertNotNull(testFolder);
        // no resource access node at all => forbidden
        RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
        folderGrant = createResourceAccess("Folder", UiAuthenticator.FORBIDDEN);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        logger.error(fex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        // resource access explicitly set to FORBIDDEN => forbidden
        RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
        // allow PUT for authenticated users => access without user/pass should be still forbidden
        folderGrant.setFlag(UiAuthenticator.AUTH_USER_PUT);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        logger.error(fex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
        // allow PUT for non-authenticated users =>
        folderGrant.setProperties(folderGrant.getSecurityContext(), new PropertyMap(GraphObject.visibleToPublicUsers, true));
        folderGrant.setFlag(UiAuthenticator.NON_AUTH_USER_PUT);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        logger.error(fex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        // ownerless non-public node cannot be found by anonymous user
        RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(404).when().put("/folder/" + testFolder.getUuid());
        // Prepare for next test
        final PropertyMap testUserProperties = new PropertyMap();
        testUserProperties.put(StructrApp.key(User.class, "name"), name);
        testUserProperties.put(StructrApp.key(User.class, "password"), password);
        testUser.setProperties(testUser.getSecurityContext(), testUserProperties);
        // now we give the user ownership and expect a 200
        testFolder.setProperties(testFolder.getSecurityContext(), new PropertyMap(AbstractNode.owner, testUser));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        logger.error(fex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().headers("X-User", name, "X-Password", password).contentType("application/json; charset=UTF-8").expect().statusCode(200).when().put("/folder/" + testFolder.getUuid());
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : ResourceAccess(org.structr.core.entity.ResourceAccess) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)892 Tx (org.structr.core.graph.Tx)673 Test (org.junit.Test)450 App (org.structr.core.app.App)175 StructrApp (org.structr.core.app.StructrApp)174 StructrUiTest (org.structr.web.StructrUiTest)134 NodeInterface (org.structr.core.graph.NodeInterface)121 StructrTest (org.structr.common.StructrTest)118 PropertyKey (org.structr.core.property.PropertyKey)109 PropertyMap (org.structr.core.property.PropertyMap)105 IOException (java.io.IOException)96 GraphObject (org.structr.core.GraphObject)93 TestOne (org.structr.core.entity.TestOne)92 File (org.structr.web.entity.File)85 SecurityContext (org.structr.common.SecurityContext)78 Principal (org.structr.core.entity.Principal)69 Page (org.structr.web.entity.dom.Page)69 LinkedList (java.util.LinkedList)62 Folder (org.structr.web.entity.Folder)60 NodeAttribute (org.structr.core.graph.NodeAttribute)56