use of org.structr.web.entity.TestOne in project structr by structr.
the class RenderContextTest method testFiltering.
@Test
public void testFiltering() {
final List<TestOne> testOnes = new LinkedList<>();
try (final Tx tx = app.tx()) {
testOnes.addAll(createTestNodes(TestOne.class, 20));
final RenderContext ctx = new RenderContext(securityContext);
final TestOne testOne = testOnes.get(0);
// test filtering
ctx.setDetailsDataObject(testOnes.get(5));
final Object value = Scripting.evaluate(ctx, testOne, "${filter(find('TestOne'), not(equal(data.id, current.id)))}", "test");
assertNotNull("Invalid filter result", value);
assertTrue("Invalid filter result", value instanceof List);
assertEquals("Invalid filter result", 19, ((List) value).size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.web.entity.TestOne in project structr by structr.
the class RenderContextTest method testVariableReplacement.
@Test
public void testVariableReplacement() {
NodeInterface detailsDataObject = null;
Page page = null;
DOMNode html = null;
DOMNode head = null;
DOMNode body = null;
DOMNode title = null;
DOMNode h1 = null;
DOMNode div1 = null;
DOMNode p1 = null;
DOMNode div2 = null;
DOMNode p2 = null;
DOMNode div3 = null;
DOMNode p3 = null;
A a = null;
DOMNode div4 = null;
DOMNode p4 = null;
TestOne testOne = null;
try (final Tx tx = app.tx()) {
detailsDataObject = app.create(TestOne.class, "TestOne");
page = Page.createNewPage(securityContext, "testpage");
page.setProperties(page.getSecurityContext(), new PropertyMap(Page.visibleToPublicUsers, true));
assertTrue(page != null);
assertTrue(page instanceof Page);
html = (DOMNode) page.createElement("html");
head = (DOMNode) page.createElement("head");
body = (DOMNode) page.createElement("body");
title = (DOMNode) page.createElement("title");
h1 = (DOMNode) page.createElement("h1");
div1 = (DOMNode) page.createElement("div");
p1 = (DOMNode) page.createElement("p");
div2 = (DOMNode) page.createElement("div");
p2 = (DOMNode) page.createElement("p");
div3 = (DOMNode) page.createElement("div");
p3 = (DOMNode) page.createElement("p");
a = (A) page.createElement("a");
div4 = (DOMNode) page.createElement("div");
p4 = (DOMNode) page.createElement("p");
// add HTML element to page
page.appendChild(html);
// add HEAD and BODY elements to HTML
html.appendChild(head);
html.appendChild(body);
// add TITLE element to HEAD
head.appendChild(title);
// add H1 element to BODY
body.appendChild(h1);
// add DIV element 1 to BODY
body.appendChild(div1);
div1.appendChild(p1);
// add DIV element 2 to DIV
div1.appendChild(div2);
div2.appendChild(p2);
// add DIV element 3 to DIV
div2.appendChild(div3);
div3.appendChild(p3);
// add link to p3
p3.appendChild(a);
a.setLinkable(page);
body.appendChild(div4);
div4.appendChild(p4);
final PropertyMap p4Properties = new PropertyMap();
p4Properties.put(StructrApp.key(DOMElement.class, "restQuery"), "/divs");
p4Properties.put(StructrApp.key(DOMElement.class, "dataKey"), "div");
p4.setProperties(p4.getSecurityContext(), p4Properties);
NodeList paragraphs = page.getElementsByTagName("p");
assertEquals(p1, paragraphs.item(0));
assertEquals(p2, paragraphs.item(1));
assertEquals(p3, paragraphs.item(2));
assertEquals(p4, paragraphs.item(3));
// create users
final User tester1 = app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester1"), new NodeAttribute<>(StructrApp.key(User.class, "eMail"), "tester1@test.com"));
final User tester2 = app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester2"), new NodeAttribute<>(StructrApp.key(User.class, "eMail"), "tester2@test.com"));
assertNotNull("User tester1 should exist.", tester1);
assertNotNull("User tester2 should exist.", tester2);
// create admin user for later use
final PropertyMap adminProperties = new PropertyMap();
adminProperties.put(StructrApp.key(User.class, "name"), "admin");
adminProperties.put(StructrApp.key(User.class, "password"), "admin");
adminProperties.put(StructrApp.key(User.class, "isAdmin"), true);
app.create(User.class, adminProperties);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
final RenderContext ctx = new RenderContext(securityContext);
ctx.setDetailsDataObject(detailsDataObject);
ctx.setPage(page);
// test for "empty" return value
assertEquals("", Scripting.replaceVariables(ctx, p1, "${err}"));
assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.error}"));
assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.this.this.error}"));
assertEquals("", Scripting.replaceVariables(ctx, p1, "${parent.error}"));
assertEquals("", Scripting.replaceVariables(ctx, p1, "${this.owner}"));
assertEquals("", Scripting.replaceVariables(ctx, p1, "${parent.owner}"));
// other functions are tested in the ActionContextTest in structr-core, see there.
assertEquals("true", Scripting.replaceVariables(ctx, p1, "${true}"));
assertEquals("false", Scripting.replaceVariables(ctx, p1, "${false}"));
assertEquals("yes", Scripting.replaceVariables(ctx, p1, "${if(true, \"yes\", \"no\")}"));
assertEquals("no", Scripting.replaceVariables(ctx, p1, "${if(false, \"yes\", \"no\")}"));
assertEquals("true", Scripting.replaceVariables(ctx, p1, "${if(true, true, false)}"));
assertEquals("false", Scripting.replaceVariables(ctx, p1, "${if(false, true, false)}"));
// test keywords
assertEquals("${id} should evaluate to the ID if the current details object", detailsDataObject.getUuid(), Scripting.replaceVariables(ctx, p1, "${id}"));
ctx.setDetailsDataObject(null);
assertEquals("${id} should evaluate to the ID if the current details object", "abc12345", Scripting.replaceVariables(ctx, p1, "${id!abc12345}"));
ctx.setDetailsDataObject(detailsDataObject);
assertEquals("${id} should be equal to ${current.id}", "true", Scripting.replaceVariables(ctx, p1, "${equal(id, current.id)}"));
assertEquals("${element} should evaluate to the current DOM node", p1.toString(), Scripting.replaceVariables(ctx, p1, "${element}"));
assertNull(Scripting.replaceVariables(ctx, p1, "${if(true, null, \"no\")}"));
assertNull(Scripting.replaceVariables(ctx, p1, "${null}"));
assertEquals("Invalid replacement result", "/testpage?" + page.getUuid(), Scripting.replaceVariables(ctx, p1, "/${page.name}?${page.id}"));
assertEquals("Invalid replacement result", "/testpage?" + page.getUuid(), Scripting.replaceVariables(ctx, a, "/${link.name}?${link.id}"));
// these tests find single element => success
assertEquals("Invalid replacement result", page.getUuid(), Scripting.replaceVariables(ctx, a, "${get(find('Page', 'name', 'testpage'), 'id')}"));
assertEquals("Invalid replacement result", a.getUuid(), Scripting.replaceVariables(ctx, a, "${get(find('A'), 'id')}"));
// this test finds multiple <p> elements => error
assertEquals("Invalid replacement result", GetFunction.ERROR_MESSAGE_GET_ENTITY, Scripting.replaceVariables(ctx, a, "${get(find('P'), 'id')}"));
// more complex replacement
// assertEquals("Invalid replacement result", "", a.replaceVariables(ctx, securityContext, "${get(find('P'), 'id')}"));
// String default value
assertEquals("bar", Scripting.replaceVariables(ctx, p1, "${request.foo!bar}"));
// Number default value (will be evaluated to a string)
assertEquals("1", Scripting.replaceVariables(ctx, p1, "${page.position!1}"));
// Number default value
assertEquals("true", Scripting.replaceVariables(ctx, p1, "${equal(42, this.null!42)}"));
final User tester1 = app.nodeQuery(User.class).andName("tester1").getFirst();
final User tester2 = app.nodeQuery(User.class).andName("tester2").getFirst();
assertNotNull("User tester1 should exist.", tester1);
assertNotNull("User tester2 should exist.", tester2);
final ActionContext tester1Context = new ActionContext(SecurityContext.getInstance(tester1, AccessMode.Backend));
final ActionContext tester2Context = new ActionContext(SecurityContext.getInstance(tester2, AccessMode.Backend));
// users
assertEquals("tester1", Scripting.replaceVariables(tester1Context, p1, "${me.name}"));
assertEquals("tester2", Scripting.replaceVariables(tester2Context, p2, "${me.name}"));
// allow unauthenticated GET on /pages
grant("Page/_Ui", 16, true);
// test GET REST access
assertEquals("Invalid GET notation result", page.getName(), Scripting.replaceVariables(ctx, p1, "${from_json(GET('http://localhost:" + httpPort + "/structr/rest/pages/ui')).result[0].name}"));
grant("Folder", 64, true);
grant("_login", 64, false);
assertEquals("Invalid POST result", "201", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:status}').status}"));
assertEquals("Invalid POST result", "1.0", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:result_count}').body.result_count}"));
assertEquals("Invalid POST result", "application/json;charset=utf-8", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:content-type}').headers.Content-Type}"));
// test POST with invalid name containing curly braces to provoke 422
assertEquals("Invalid POST result", "422", Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/folders', '{name:\"ShouldFail/xyz\"}').status}"));
// test login and sessions
final String sessionIdCookie = Scripting.replaceVariables(ctx, page, "${POST('http://localhost:" + httpPort + "/structr/rest/login', '{name:admin,password:admin}').headers.Set-Cookie}");
final String sessionId = HttpCookie.parse(sessionIdCookie).get(0).getValue();
// test authenticated GET request using session ID cookie
assertEquals("Invalid authenticated GET result", "admin", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[0].name}"));
assertEquals("Invalid authenticated GET result", "tester1", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[1].name}"));
assertEquals("Invalid authenticated GET result", "tester2", Scripting.replaceVariables(ctx, page, "${add_header('Cookie', 'JSESSIONID=" + sessionId + ";Path=/')}${from_json(GET('http://localhost:" + httpPort + "/structr/rest/users')).result[2].name}"));
// locale
final String localeString = ctx.getLocale().toString();
assertEquals("Invalid locale result", localeString, Scripting.replaceVariables(ctx, page, "${locale}"));
// set new details object
final TestOne detailsDataObject2 = app.create(TestOne.class, "TestOne");
Scripting.replaceVariables(ctx, p1, "${set_details_object(first(find('TestOne', 'id', '" + detailsDataObject2.getUuid() + "')))}");
assertEquals("${current.id} should resolve to new details object", detailsDataObject2.getUuid(), Scripting.replaceVariables(ctx, p1, "${current.id}"));
// test values() with single parameter
assertEquals("Invalid values() result", "[test]", Scripting.replaceVariables(ctx, page, "${values(from_json('{name:test}'))}"));
testOne = createTestNode(TestOne.class);
testOne.setProperty(TestOne.htmlString, "<a b=\"c\">&d</a>");
// escape_html
assertEquals("Invalid escape_html() result", "<a b="c">&d</a>", Scripting.replaceVariables(ctx, testOne, "${escape_html(this.htmlString)}"));
testOne.setProperty(TestOne.htmlString, "<a b="c">&d</a>");
// unescape_html
assertEquals("Invalid unescape_html() result", "<a b=\"c\">&d</a>", Scripting.replaceVariables(ctx, testOne, "${unescape_html(this.htmlString)}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
Aggregations