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.");
}
}
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());
}
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());
}
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);
}
}
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");
}
}
Aggregations