Search in sources :

Example 41 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class UserSelfRegistrationTest method testUserSelfRegistrationWithRedirect.

@Test
public void testUserSelfRegistrationWithRedirect() {
    // 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 SessionFilter sessionFilter = new SessionFilter();
    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().filter(sessionFilter).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.");
    }
    // create redirect page
    try (final Tx tx = app.tx()) {
        makeVisible(Page.createSimplePage(securityContext, "error"), true);
        makeVisible(Page.createSimplePage(securityContext, "success"), false);
        tx.success();
    } catch (FrameworkException fex) {
    }
    // switch to HTML servlet
    RestAssured.basePath = htmlUrl;
    // expect 404 Not Found when logging in because Jetty or
    // RestAssured don't preserve the session ID
    RestAssured.given().filter(sessionFilter).param(HtmlServlet.CONFIRM_KEY_KEY, confKey).param(HtmlServlet.TARGET_PAGE_KEY, "success").expect().statusCode(200).body("html.head.title", Matchers.equalTo("Success")).body("html.body.h1", Matchers.equalTo("Success")).body("html.body.div", Matchers.equalTo("Initial body text")).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);
        assertNull("Confirmation key was set after confirmation", user.getProperty(StructrApp.key(User.class, "confirmationKey")));
        final String[] sessionIds = user.getProperty(StructrApp.key(User.class, "sessionIds"));
        Assert.assertEquals("Invalid number of sessions after user confirmation", 1, sessionIds.length);
        Assert.assertEquals("Invalid session ID after user confirmation", StringUtils.substringBeforeLast(sessionFilter.getSessionId(), "."), sessionIds[0]);
        tx.success();
    } catch (FrameworkException t) {
        fail("Unexpected exception.");
    }
}
Also used : User(org.structr.web.entity.User) SessionFilter(com.jayway.restassured.filter.session.SessionFilter) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 42 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class BasicAuthTest method test02BasicAuthOnPageWithErrorPage.

@Test
public void test02BasicAuthOnPageWithErrorPage() {
    RestAssured.basePath = "/";
    try (final Tx tx = app.tx()) {
        final Page error = makeVisible(Page.createSimplePage(securityContext, "error"));
        error.setProperty(StructrApp.key(Page.class, "showOnErrorCodes"), "401");
        final Page page1 = makeVisibleToAuth(Page.createSimplePage(securityContext, "test1"));
        final Page page2 = makeVisibleToAuth(Page.createSimplePage(securityContext, "test2"));
        page1.setProperty(StructrApp.key(Page.class, "visibleToAuthenticatedUsers"), true);
        page1.setProperty(StructrApp.key(Page.class, "enableBasicAuth"), true);
        page2.setProperty(StructrApp.key(Page.class, "visibleToAuthenticatedUsers"), true);
        page2.setProperty(StructrApp.key(Page.class, "basicAuthRealm"), "realm");
        page2.setProperty(StructrApp.key(Page.class, "enableBasicAuth"), true);
        createUser();
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    RestAssured.expect().statusCode(401).header("WWW-Authenticate", "BASIC realm=\"test1\"").body("html.head.title", Matchers.equalTo("Error")).body("html.body.h1", Matchers.equalTo("Error")).body("html.body.div", Matchers.equalTo("Initial body text")).when().get("/html/test1");
    RestAssured.expect().statusCode(401).header("WWW-Authenticate", "BASIC realm=\"realm\"").body("html.head.title", Matchers.equalTo("Error")).body("html.body.h1", Matchers.equalTo("Error")).body("html.body.div", Matchers.equalTo("Initial body text")).when().get("/html/test2");
    // test successful basic auth
    RestAssured.given().authentication().basic("tester", "test").expect().statusCode(200).body("html.head.title", Matchers.equalTo("Test1")).body("html.body.h1", Matchers.equalTo("Test1")).body("html.body.div", Matchers.equalTo("Initial body text")).when().get("/html/test1");
    // test successful basic auth
    RestAssured.given().authentication().basic("tester", "test").expect().statusCode(200).body("html.head.title", Matchers.equalTo("Test2")).body("html.body.h1", Matchers.equalTo("Test2")).body("html.body.div", Matchers.equalTo("Initial body text")).when().get("/html/test2");
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Page(org.structr.web.entity.dom.Page) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 43 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class BasicAuthTest method test01BasicAuthOnFile.

@Test
public void test01BasicAuthOnFile() {
    RestAssured.basePath = "/";
    try (final Tx tx = app.tx()) {
        final File file1 = FileHelper.createFile(securityContext, "test1".getBytes(), "text/plain", File.class, "test1.txt");
        final File file2 = FileHelper.createFile(securityContext, "test2".getBytes(), "text/plain", File.class, "test2.txt");
        file1.setProperty(StructrApp.key(Page.class, "visibleToAuthenticatedUsers"), true);
        file1.setProperty(StructrApp.key(Page.class, "enableBasicAuth"), true);
        file2.setProperty(StructrApp.key(Page.class, "visibleToAuthenticatedUsers"), true);
        file2.setProperty(StructrApp.key(Page.class, "basicAuthRealm"), "realm");
        file2.setProperty(StructrApp.key(Page.class, "enableBasicAuth"), true);
        createUser();
        tx.success();
    } catch (FrameworkException | IOException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    RestAssured.expect().statusCode(401).header("WWW-Authenticate", "BASIC realm=\"test1.txt\"").when().get("test1.txt");
    RestAssured.expect().statusCode(401).header("WWW-Authenticate", "BASIC realm=\"realm\"").when().get("test2.txt");
    // test successful basic auth
    RestAssured.given().authentication().basic("tester", "test").expect().statusCode(200).body(Matchers.equalTo("test1")).when().get("test1.txt");
    // test successful basic auth
    RestAssured.given().authentication().basic("tester", "test").expect().statusCode(200).body(Matchers.equalTo("test2")).when().get("test2.txt");
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Page(org.structr.web.entity.dom.Page) IOException(java.io.IOException) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 44 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class CustomHtmlAttributeTest method testCustomHtmlAttribute.

@Test
public void testCustomHtmlAttribute() {
    try (final Tx tx = app.tx()) {
        // create a page
        final Page newPage = Page.createNewPage(securityContext, "customAttributeTestPage");
        final Html html = createElement(newPage, newPage, "html");
        final Head head = createElement(newPage, html, "head");
        final Title title = createElement(newPage, head, "title", "Test Page for custom html attributes");
        final Body body = createElement(newPage, html, "body");
        final Div div1 = createElement(newPage, body, "div", "DIV with old-style data attribute");
        div1.setProperty(new GenericProperty<String>("data-test-attribute-old-style"), "old-style data attribute");
        final Div div2 = createElement(newPage, body, "div", "DIV with new-style custom html attribute");
        div2.setProperty(new GenericProperty<String>("_custom_html_test-attribute-new-style"), "new-style custom attribute");
        final Div div3 = createElement(newPage, body, "div", "DIV with data-attribute as new-style custom html attribute");
        div3.setProperty(new GenericProperty<String>("_custom_html_data-test-attribute-new-style"), "new-style custom data-attribute");
        final RenderContext renderContext = new RenderContext(securityContext);
        newPage.render(renderContext, 0);
        final String renderedHtml = StringUtils.join(renderContext.getBuffer().getQueue(), "");
        final String expectedHtml = "<!DOCTYPE html>\n" + "<html>\n" + "	<head>\n" + "		<title>Test Page for custom html attributes</title>\n" + "	</head>\n" + "	<body>\n" + "		<div data-test-attribute-old-style=\"old-style data attribute\">DIV with old-style data attribute</div>\n" + "		<div test-attribute-new-style=\"new-style custom attribute\">DIV with new-style custom html attribute</div>\n" + "		<div data-test-attribute-new-style=\"new-style custom data-attribute\">DIV with data-attribute as new-style custom html attribute</div>\n" + "	</body>\n" + "</html>";
        Assert.assertEquals(expectedHtml, renderedHtml);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
}
Also used : Div(org.structr.web.entity.html.Div) RenderContext(org.structr.web.common.RenderContext) Head(org.structr.web.entity.html.Head) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Title(org.structr.web.entity.html.Title) Page(org.structr.web.entity.dom.Page) Body(org.structr.web.entity.html.Body) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 45 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class HtmlServletObjectResolvingTest method testObjectResolvingInHtmlServlet.

@Test
public void testObjectResolvingInHtmlServlet() {
    final List<String> testObjectIDs = new LinkedList<>();
    try (final Tx tx = app.tx()) {
        // setup three different test objects to be found by HtmlServlet
        testObjectIDs.add(app.create(TestOne.class, new NodeAttribute<>(TestOne.anInt, 123)).getUuid());
        testObjectIDs.add(app.create(TestOne.class, new NodeAttribute<>(TestOne.aDouble, 0.345)).getUuid());
        testObjectIDs.add(app.create(TestOne.class, new NodeAttribute<>(TestOne.aString, "abcdefg")).getUuid());
        // create a page
        final Page newPage = Page.createNewPage(securityContext, "testPage");
        if (newPage != null) {
            Element html = newPage.createElement("html");
            Element head = newPage.createElement("head");
            Element body = newPage.createElement("body");
            Text textNode = newPage.createTextNode("${current.id}");
            try {
                // add HTML element to page
                newPage.appendChild(html);
                html.appendChild(head);
                html.appendChild(body);
                body.appendChild(textNode);
            } catch (DOMException dex) {
                logger.warn("", dex);
                throw new FrameworkException(422, dex.getMessage());
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    RestAssured.basePath = "/structr/html";
    RestAssured.given().header("X-User", "superadmin").header("X-Password", "sehrgeheim").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).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().response().contentType("text/html").statusCode(200).body(Matchers.containsString(testObjectIDs.get(0))).when().get("/testPage/123");
    RestAssured.given().header("X-User", "superadmin").header("X-Password", "sehrgeheim").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).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().response().statusCode(200).body(Matchers.containsString(testObjectIDs.get(1))).when().get("/testPage/0.345");
    RestAssured.given().header("X-User", "superadmin").header("X-Password", "sehrgeheim").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).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().response().statusCode(200).body(Matchers.containsString(testObjectIDs.get(2))).when().get("/testPage/abcdefg");
}
Also used : DOMException(org.w3c.dom.DOMException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Element(org.w3c.dom.Element) Page(org.structr.web.entity.dom.Page) Text(org.w3c.dom.Text) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)892 Tx (org.structr.core.graph.Tx)673 Test (org.junit.Test)450 App (org.structr.core.app.App)175 StructrApp (org.structr.core.app.StructrApp)174 StructrUiTest (org.structr.web.StructrUiTest)134 NodeInterface (org.structr.core.graph.NodeInterface)121 StructrTest (org.structr.common.StructrTest)118 PropertyKey (org.structr.core.property.PropertyKey)109 PropertyMap (org.structr.core.property.PropertyMap)105 IOException (java.io.IOException)96 GraphObject (org.structr.core.GraphObject)93 TestOne (org.structr.core.entity.TestOne)92 File (org.structr.web.entity.File)85 SecurityContext (org.structr.common.SecurityContext)78 Principal (org.structr.core.entity.Principal)69 Page (org.structr.web.entity.dom.Page)69 LinkedList (java.util.LinkedList)62 Folder (org.structr.web.entity.Folder)60 NodeAttribute (org.structr.core.graph.NodeAttribute)56