use of org.structr.core.graph.Tx in project structr by structr.
the class UiScriptingTest method testRestQueryRepeater.
@Test
public void testRestQueryRepeater() {
String uuid = null;
try (final Tx tx = app.tx()) {
final Page page = Page.createSimplePage(securityContext, "test");
final Div div = (Div) page.getElementsByTagName("div").item(0);
final Content content = (Content) div.getFirstChild();
// setup scripting repeater
content.setProperty(StructrApp.key(Content.class, "restQuery"), "/Page/${current.id}");
content.setProperty(StructrApp.key(Content.class, "dataKey"), "test");
content.setProperty(StructrApp.key(Content.class, "content"), "${test.id}");
// store UUID for later use
uuid = page.getUuid();
// 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));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
RestAssured.basePath = "/";
// test successful basic auth
RestAssured.given().headers("X-User", "admin", "X-Password", "admin").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("html.head.title", Matchers.equalTo("Test")).body("html.body.h1", Matchers.equalTo("Test")).body("html.body.div", Matchers.equalTo(uuid)).when().get("/html/test/" + uuid);
}
use of org.structr.core.graph.Tx in project structr by structr.
the class UiScriptingTest method testSingleRequestParameters.
@Test
public void testSingleRequestParameters() {
try (final Tx tx = app.tx()) {
Page page = (Page) app.create(Page.class, new NodeAttribute(Page.name, "test"), new NodeAttribute(Page.visibleToPublicUsers, true));
Template template = (Template) app.create(Template.class, new NodeAttribute(Page.visibleToPublicUsers, true));
template.setContent("${request.param}");
page.appendChild(template);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body(equalTo("a")).when().get("http://localhost:8875/test?param=a");
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
}
use of org.structr.core.graph.Tx in project structr by structr.
the class UiScriptingTest method testGroupFunctions.
@Test
public void testGroupFunctions() {
Group group = null;
User tester = null;
try (final Tx tx = app.tx()) {
// create test user
tester = createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "test"));
// create test group
group = createTestNode(Group.class, new NodeAttribute<>(StructrApp.key(Group.class, "name"), "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final RenderContext renderContext = new RenderContext(securityContext, new RequestMockUp(), new ResponseMockUp(), RenderContext.EditMode.NONE);
try (final Tx tx = app.tx()) {
// check that the user is not in the group at first
assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
// add user to group
Scripting.evaluate(renderContext, null, "${add_to_group(first(find('Group')), first(find('User')))}", "test");
// check that the user is in the group after the call to add_to_group
final List<Principal> members = group.getMembers();
assertTrue("User should be in the test group now", members.contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return true.", true, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
// remove user from group
Scripting.evaluate(renderContext, null, "${remove_from_group(first(find('Group')), first(find('User')))}", "test");
// check that the user is not in the group any more after the call to remove_from_group
assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.core.graph.Tx in project structr by structr.
the class UiTest method testExtensionBasedMimeTypeDetection.
@Test
public void testExtensionBasedMimeTypeDetection() {
final Map<String, Map<String, byte[]>> testMap = new LinkedHashMap<>();
testMap.put("text/html", toMap(new Pair("test.html", "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Test</h1></body></html>".getBytes()), new Pair("test.htm", "<!DOCTYPE html>".getBytes())));
testMap.put("text/plain", toMap(new Pair("test.txt", "Hello world!".getBytes())));
testMap.put("text/css", toMap(new Pair("test.css", "body { background-color: #ffffff; }".getBytes())));
testMap.put("application/javascript", toMap(new Pair("test.js", "function() { alert('Test'); }".getBytes())));
testMap.put("application/zip", toMap(new Pair("test.zip", "".getBytes())));
testMap.put("image/jpeg", toMap(new Pair("test.jpg", "".getBytes()), new Pair("test.jpeg", "".getBytes())));
testMap.put("image/png", toMap(new Pair("test.png", "".getBytes())));
try (final Tx tx = app.tx()) {
for (final Entry<String, Map<String, byte[]>> entry : testMap.entrySet()) {
final String mimeType = entry.getKey();
for (final Entry<String, byte[]> fileEntry : entry.getValue().entrySet()) {
final String fileName = fileEntry.getKey();
final byte[] content = fileEntry.getValue();
try {
final File file = FileHelper.createFile(securityContext, content, null, File.class, fileName);
assertEquals("MIME type detection failed", mimeType, file.getContentType());
} catch (IOException ioex) {
logger.warn("", ioex);
fail("Unexpected exception");
}
}
}
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
}
use of org.structr.core.graph.Tx in project structr by structr.
the class UiTest method testCreateFolder.
@Test
public void testCreateFolder() {
Folder folder1 = null;
try (final Tx tx = app.tx()) {
folder1 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/folder1");
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
File file1 = (File) app.create(File.class, "file1");
assertNotNull(file1);
assertEquals(file1.getPath(), "/file1");
file1.setProperty(StructrApp.key(File.class, "parent"), folder1);
assertEquals(file1.getPath(), "/folder1/file1");
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
Image image1 = (Image) app.create(Image.class, "image1");
assertNotNull(image1);
assertEquals(image1.getPath(), "/image1");
image1.setProperty(StructrApp.key(File.class, "parent"), folder1);
assertEquals(image1.getPath(), "/folder1/image1");
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
assertEquals(2, Iterables.toList(folder1.getFiles()).size());
assertEquals(1, Iterables.toList(folder1.getImages()).size());
} catch (FrameworkException ex) {
logger.error("", ex);
}
}
Aggregations