Search in sources :

Example 1 with SessionFilter

use of com.jayway.restassured.filter.session.SessionFilter 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)

Aggregations

SessionFilter (com.jayway.restassured.filter.session.SessionFilter)1 Test (org.junit.Test)1 FrameworkException (org.structr.common.error.FrameworkException)1 Tx (org.structr.core.graph.Tx)1 StructrUiTest (org.structr.web.StructrUiTest)1 User (org.structr.web.entity.User)1