Search in sources :

Example 26 with User

use of org.structr.web.entity.User in project structr by structr.

the class UserSelfRegistrationTest method testUserSelfRegistration.

@Test
public void testUserSelfRegistration() {
    // since we cannot test the mail confirmation workflow, we just disable sending an e-mail
    Settings.SmtpTesting.setValue(true);
    // enable self-registration and auto-login
    Settings.RestUserAutocreate.setValue(true);
    Settings.RestUserAutologin.setValue(true);
    final String eMail = "test@structr.com";
    String id = null;
    String confKey = null;
    // switch to REST servlet
    RestAssured.basePath = restUrl;
    grant("_registration", UiAuthenticator.NON_AUTH_USER_POST, true);
    grant("_login", UiAuthenticator.NON_AUTH_USER_POST, false);
    // verify self registration
    RestAssured.given().body("{ name: '" + eMail + "',  eMail: '" + eMail + "' }").expect().statusCode(201).when().post("/registration");
    try (final Tx tx = app.tx()) {
        final User user = app.nodeQuery(User.class).getFirst();
        assertNotNull("User was not created", user);
        // store ID for later user
        id = user.getProperty(StructrApp.key(User.class, "id"));
        confKey = user.getProperty(StructrApp.key(User.class, "confirmationKey"));
        assertNotNull("Confirmation key was not set", confKey);
        tx.success();
    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }
    // switch to HTML servlet
    RestAssured.basePath = htmlUrl;
    // access the user confirmation page
    RestAssured.given().param(HtmlServlet.CONFIRM_KEY_KEY, confKey).expect().statusCode(200).when().get(HtmlServlet.CONFIRM_REGISTRATION_PAGE);
    // verify that the user has no confirmation key
    try (final Tx tx = app.tx()) {
        final User user = app.nodeQuery(User.class).getFirst();
        assertNotNull("User was not created", user);
        // store ID for later user
        id = user.getProperty(StructrApp.key(User.class, "id"));
        confKey = user.getProperty(StructrApp.key(User.class, "confirmationKey"));
        assertNull("Confirmation key was set after confirmation", confKey);
        tx.success();
    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }
}
Also used : User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 27 with User

use of org.structr.web.entity.User in project structr by structr.

the class FrontendTest method createAdminUser.

protected User createAdminUser() {
    final PropertyMap properties = new PropertyMap();
    properties.put(StructrApp.key(User.class, "name"), ADMIN_USERNAME);
    properties.put(StructrApp.key(User.class, "password"), ADMIN_PASSWORD);
    properties.put(StructrApp.key(User.class, "isAdmin"), true);
    properties.put(StructrApp.key(User.class, "backendUser"), true);
    User user = null;
    try (final Tx tx = app.tx()) {
        user = app.create(User.class, properties);
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
    }
    return user;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx)

Example 28 with User

use of org.structr.web.entity.User in project structr by structr.

the class FrontendTest method clearLocalStorage.

protected void clearLocalStorage() {
    final User user;
    try (final Tx tx = app.tx()) {
        user = app.nodeQuery(User.class).andName("admin").getFirst();
        user.setProperty(StructrApp.key(User.class, "localStorage"), null);
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
    }
}
Also used : User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx)

Example 29 with User

use of org.structr.web.entity.User 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", "&lt;a b=&quot;c&quot;&gt;&amp;d&lt;/a&gt;", Scripting.replaceVariables(ctx, testOne, "${escape_html(this.htmlString)}"));
        testOne.setProperty(TestOne.htmlString, "&lt;a b=&quot;c&quot;&gt;&amp;d&lt;/a&gt;");
        // 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");
    }
}
Also used : A(org.structr.web.entity.html.A) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeList(org.w3c.dom.NodeList) Page(org.structr.web.entity.dom.Page) DOMElement(org.structr.web.entity.dom.DOMElement) ActionContext(org.structr.schema.action.ActionContext) PropertyMap(org.structr.core.property.PropertyMap) TestOne(org.structr.web.entity.TestOne) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 30 with User

use of org.structr.web.entity.User in project structr by structr.

the class SchemaMethodsTest method test03SchemaMethodOnEntityOfBuiltinType.

@Test
public void test03SchemaMethodOnEntityOfBuiltinType() {
    final String builtinTypeName = "File";
    final String schemaMethodName = "testFileMethod";
    User admin = null;
    try (final Tx tx = app.tx()) {
        admin = createAdminUser();
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Add schema method "testFileMethod" to built-in File class
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName(builtinTypeName).getFirst();
        final PropertyMap testFileMethodProperties = new PropertyMap();
        testFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
        testFileMethodProperties.put(SchemaMethod.source, "()");
        testFileMethodProperties.put(SchemaMethod.schemaNode, fileNodeDef);
        SchemaMethod testFileMethod = app.create(SchemaMethod.class, testFileMethodProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    File testFile = null;
    try (final Tx tx = app.tx()) {
        // Create File instance
        testFile = app.create(File.class, "Test File");
        testFile.setProperty(File.owner, admin);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post(builtinTypeName + "/" + testFile.getUuid() + "/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) File(org.structr.web.entity.File) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Aggregations

User (org.structr.web.entity.User)32 Tx (org.structr.core.graph.Tx)27 FrameworkException (org.structr.common.error.FrameworkException)23 Test (org.junit.Test)21 StructrUiTest (org.structr.web.StructrUiTest)16 PropertyMap (org.structr.core.property.PropertyMap)14 Principal (org.structr.core.entity.Principal)10 App (org.structr.core.app.App)8 StructrApp (org.structr.core.app.StructrApp)8 NodeAttribute (org.structr.core.graph.NodeAttribute)7 SecurityContext (org.structr.common.SecurityContext)5 Folder (org.structr.web.entity.Folder)5 SchemaMethod (org.structr.core.entity.SchemaMethod)4 SchemaNode (org.structr.core.entity.SchemaNode)4 Page (org.structr.web.entity.dom.Page)4 GraphObject (org.structr.core.GraphObject)3 RenderContext (org.structr.web.common.RenderContext)3 File (org.structr.web.entity.File)3 Body (org.structr.web.entity.html.Body)3 Div (org.structr.web.entity.html.Div)3