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.");
}
}
Aggregations