Search in sources :

Example 56 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class UiScriptingTest method testDoPrivileged.

@Test
public void testDoPrivileged() {
    User tester = null;
    try (final Tx tx = app.tx()) {
        // create admin user
        createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        // create test user
        tester = createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final String script1 = "${{ return Structr.find('User', 'name', 'admin'); }}\n";
    final String script2 = "${{ return Structr.doPrivileged(function() { return Structr.find('User', 'name', 'admin'); }); }}\n";
    final SecurityContext userContext = SecurityContext.getInstance(tester, AccessMode.Backend);
    final App app = StructrApp.getInstance(userContext);
    final RenderContext renderContext = new RenderContext(userContext, new RequestMockUp(), new ResponseMockUp(), RenderContext.EditMode.NONE);
    try (final Tx tx = app.tx()) {
        // unprivileged call
        final Object result = Scripting.evaluate(renderContext, null, script1, "test");
        assertEquals("Result is of invalid type", ArrayList.class, result.getClass());
        assertEquals("Script in user context should not see admin", 0, ((List) result).size());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        // doPrivileged call
        final Object result = Scripting.evaluate(renderContext, null, script2, "test");
        assertEquals("Result is of invalid type", ArrayList.class, result.getClass());
        assertEquals("Privileged script should not see admin", 1, ((List) result).size());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 57 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class UiTest method testAutoRenameFileWhenMovingToFolderWhereIdenticalFilenameExists.

@Test
public void testAutoRenameFileWhenMovingToFolderWhereIdenticalFilenameExists() {
    Settings.UniquePaths.setValue(Boolean.TRUE);
    Folder folder1 = null;
    Folder folder2 = null;
    File file1 = null;
    File file2 = null;
    try (final Tx tx = app.tx()) {
        folder1 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/my/test/folder");
        assertNotNull(folder1);
        assertEquals(folder1.getPath(), "/my/test/folder");
        folder2 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/another/directory");
        assertNotNull(folder2);
        assertEquals(folder2.getPath(), "/another/directory");
        tx.success();
        file1 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "parent"), folder1));
        assertNotNull(file1);
        assertEquals("Testfolder 1 should have exactly one child", 1, Iterables.count(folder1.getChildren()));
        file2 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "parent"), folder2));
        assertNotNull(file2);
        assertEquals("Testfolder 2 should have exactly one child", 1, Iterables.count(folder2.getChildren()));
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        folder2.treeRemoveChild(file2);
        folder1.treeAppendChild(file2);
        assertEquals("Testfolder 1 should have exactly two children", 2, Iterables.count(folder1.getChildren()));
        assertEquals("Testfolder 2 should have no children", 0, Iterables.count(folder2.getChildren()));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
    }
    assertNotEquals(file1.getName(), file2.getName());
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 58 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class UiTest method testAutoRenameFileWithIdenticalPathInRootFolder.

@Test
public void testAutoRenameFileWithIdenticalPathInRootFolder() {
    Settings.UniquePaths.setValue(Boolean.TRUE);
    File rootFile1 = null;
    File rootFile2 = null;
    try (final Tx tx = app.tx()) {
        rootFile1 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"));
        assertNotNull(rootFile1);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        rootFile2 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"));
        assertNotNull(rootFile2);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    assertNotEquals(rootFile1.getName(), rootFile2.getName());
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 59 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class UiTest method testCreateBase64File.

@Test
public void testCreateBase64File() {
    final String base64Data = "data:text/plain;base64,RGllcyBpc3QgZWluIFRlc3Q=";
    final String plaintext = "Dies ist ein Test";
    File file = null;
    try (final Tx tx = app.tx()) {
        file = app.create(File.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "base64Data"), base64Data));
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        assertEquals("Invalid base64 encoded file content creation result", plaintext, IOUtils.toString(file.getInputStream()));
        tx.success();
    } catch (FrameworkException | IOException ex) {
        logger.error("", ex);
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) IOException(java.io.IOException) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 60 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class RenderContextTest method testNotionTransformedPropertyAccess.

@Test
public void testNotionTransformedPropertyAccess() {
    NodeInterface project = null;
    NodeInterface task1 = null;
    NodeInterface task2 = null;
    NodeInterface task3 = null;
    try (final Tx tx = app.tx()) {
        final SchemaNode projectNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Project"), new NodeAttribute(new StringProperty("_taskList"), "Notion(tasks, id, name)"), new NodeAttribute(new StringProperty("_taskNames"), "Notion(tasks, name)"));
        final SchemaNode taskNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Task"));
        // create schema relationship
        final PropertyMap taskProperties = new PropertyMap();
        taskProperties.put(SchemaRelationshipNode.sourceNode, projectNode);
        taskProperties.put(SchemaRelationshipNode.targetNode, taskNode);
        taskProperties.put(SchemaRelationshipNode.relationshipType, "TASK");
        taskProperties.put(SchemaRelationshipNode.relationshipType, "TASK");
        taskProperties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
        taskProperties.put(SchemaRelationshipNode.targetMultiplicity, "*");
        taskProperties.put(SchemaRelationshipNode.sourceJsonName, "project");
        taskProperties.put(SchemaRelationshipNode.targetJsonName, "tasks");
        app.create(SchemaRelationshipNode.class, taskProperties);
        // create schema relationship
        final PropertyMap currentTaskProperties = new PropertyMap();
        currentTaskProperties.put(SchemaRelationshipNode.sourceNode, projectNode);
        currentTaskProperties.put(SchemaRelationshipNode.targetNode, taskNode);
        currentTaskProperties.put(SchemaRelationshipNode.relationshipType, "CURRENT");
        currentTaskProperties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
        currentTaskProperties.put(SchemaRelationshipNode.targetMultiplicity, "1");
        currentTaskProperties.put(SchemaRelationshipNode.sourceJsonName, "project");
        currentTaskProperties.put(SchemaRelationshipNode.targetJsonName, "currentTask");
        app.create(SchemaRelationshipNode.class, currentTaskProperties);
        // compile the stuff
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class projectClass = config.getNodeEntityClass("Project");
    final Class taskClass = config.getNodeEntityClass("Task");
    final PropertyKey currentTaskKey = StructrApp.key(projectClass, "currentTask");
    final PropertyKey tasksKey = StructrApp.key(projectClass, "tasks");
    // create parent/child relationship
    try (final Tx tx = app.tx()) {
        project = app.create(projectClass, new NodeAttribute(SchemaNode.name, "Project1"));
        task1 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task1"));
        task2 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task2"));
        task3 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task3"));
        // add task to project
        final List tasks = new LinkedList<>();
        tasks.add(task1);
        tasks.add(task2);
        tasks.add(task3);
        final PropertyMap projectProperties = new PropertyMap();
        projectProperties.put(tasksKey, tasks);
        projectProperties.put(currentTaskKey, task3);
        project.setProperties(project.getSecurityContext(), projectProperties);
        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("project", project);
        renderContext.putDataObject("task", task1);
        assertEquals("Invalid dot syntax result: ", "Project1", Scripting.replaceVariables(renderContext, project, "${project.name}"));
        assertEquals("Invalid dot syntax result: ", "Task1", Scripting.replaceVariables(renderContext, project, "${project.tasks[0].name}"));
        assertEquals("Invalid dot syntax result: ", "Task2", Scripting.replaceVariables(renderContext, project, "${project.tasks[1].name}"));
        assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.tasks[2].name}"));
        assertEquals("Invalid dot syntax result: ", "[Task1, Task2, Task3]", Scripting.replaceVariables(renderContext, project, "${project.taskNames}"));
        assertEquals("Invalid dot syntax result: ", "Task1", Scripting.replaceVariables(renderContext, project, "${project.taskNames[0]}"));
        assertEquals("Invalid dot syntax result: ", "Task2", Scripting.replaceVariables(renderContext, project, "${project.taskNames[1]}"));
        assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.taskNames[2]}"));
        assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.currentTask.name}"));
        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) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) List(java.util.List) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

NodeAttribute (org.structr.core.graph.NodeAttribute)73 Tx (org.structr.core.graph.Tx)55 FrameworkException (org.structr.common.error.FrameworkException)54 Test (org.junit.Test)45 App (org.structr.core.app.App)23 StructrApp (org.structr.core.app.StructrApp)23 StructrUiTest (org.structr.web.StructrUiTest)21 LinkedList (java.util.LinkedList)19 PropertyKey (org.structr.core.property.PropertyKey)17 SchemaNode (org.structr.core.entity.SchemaNode)14 NodeInterface (org.structr.core.graph.NodeInterface)13 Principal (org.structr.core.entity.Principal)12 PropertyMap (org.structr.core.property.PropertyMap)12 StringProperty (org.structr.core.property.StringProperty)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 File (org.structr.web.entity.File)10 Folder (org.structr.web.entity.Folder)9 AbstractFile (org.structr.web.entity.AbstractFile)8 List (java.util.List)7 StructrTest (org.structr.common.StructrTest)7