use of org.structr.web.entity.User in project structr by structr.
the class DeploymentTest method test21ExportGrants.
@Test
public void test21ExportGrants() {
Principal user1 = null;
Principal user2 = null;
try (final Tx tx = app.tx()) {
user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
tx.success();
} catch (FrameworkException ex) {
fail("Unexpected exception.");
}
Assert.assertNotNull("User was not created, test cannot continue", user1);
Assert.assertNotNull("User was not created, test cannot continue", user2);
// setup
final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
final App app1 = StructrApp.getInstance(context1);
try (final Tx tx = app1.tx()) {
final Page page = Page.createNewPage(context1, "test21");
final Html html = createElement(page, page, "html");
final Head head = createElement(page, html, "head");
createElement(page, head, "title", "test21");
final Body body = createElement(page, html, "body");
final Div div1 = createElement(page, body, "div");
final Content content = createContent(page, div1, "<b>Test</b>");
content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
// create grants
page.grant(Permission.read, user2);
div1.grant(Permission.read, user2);
content.grant(Permission.read, user2);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// test
compare(calculateHash(), true, false);
}
use of org.structr.web.entity.User in project structr by structr.
the class DeploymentTest method test23FileOwnershipAndGrants.
@Test
public void test23FileOwnershipAndGrants() {
Principal user1 = null;
Principal user2 = null;
try (final Tx tx = app.tx()) {
user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
tx.success();
} catch (FrameworkException ex) {
fail("Unexpected exception.");
}
Assert.assertNotNull("User was not created, test cannot continue", user1);
Assert.assertNotNull("User was not created, test cannot continue", user2);
// setup
try (final Tx tx = app.tx()) {
// create some files and folders
final Folder folder1 = app.create(Folder.class, new NodeAttribute<>(Folder.name, "Folder1"), new NodeAttribute<>(StructrApp.key(Folder.class, "includeInFrontendExport"), true));
final Folder folder2 = app.create(Folder.class, new NodeAttribute<>(Folder.name, "Folder2"), new NodeAttribute<>(StructrApp.key(Folder.class, "parent"), folder1));
final File file1 = FileHelper.createFile(securityContext, "test".getBytes(), "text/plain", File.class, "test1.txt");
final File file2 = FileHelper.createFile(securityContext, "test".getBytes(), "text/plain", File.class, "test2.txt");
file1.setParent(folder2);
file2.setParent(folder2);
folder1.setProperty(Folder.owner, user1);
folder1.grant(Permission.read, user2);
folder2.setProperty(Folder.owner, user2);
folder2.grant(Permission.write, user1);
file1.setProperty(File.owner, user1);
file2.setProperty(File.owner, user2);
file1.setProperty(Folder.owner, user1);
file1.grant(Permission.read, user2);
file2.setProperty(Folder.owner, user2);
file2.grant(Permission.write, user1);
tx.success();
} catch (IOException | FrameworkException fex) {
fail("Unexpected exception.");
}
// test
doImportExportRoundtrip(true, true, new Function() {
@Override
public Object apply(Object t) {
try (final Tx tx = app.tx()) {
createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
tx.success();
} catch (FrameworkException ex) {
fail("Unexpected exception.");
}
return null;
}
});
}
use of org.structr.web.entity.User in project structr by structr.
the class DeploymentTest method test20ExportOwnership.
@Test
public void test20ExportOwnership() {
Principal user1 = null;
Principal user2 = null;
try (final Tx tx = app.tx()) {
user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
tx.success();
} catch (FrameworkException ex) {
fail("Unexpected exception.");
}
Assert.assertNotNull("User was not created, test cannot continue", user1);
Assert.assertNotNull("User was not created, test cannot continue", user2);
// setup
final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
final App app1 = StructrApp.getInstance(context1);
try (final Tx tx = app1.tx()) {
final Page page = Page.createNewPage(context1, "test20");
final Html html = createElement(page, page, "html");
final Head head = createElement(page, html, "head");
createElement(page, head, "title", "test20");
final Body body = createElement(page, html, "body");
final Div div1 = createElement(page, body, "div");
final Content content = createContent(page, div1, "<b>Test</b>");
content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
// set owner to different user
div1.setProperty(AbstractNode.owner, user2);
content.setProperty(AbstractNode.owner, user2);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// test
compare(calculateHash(), true, false);
}
use of org.structr.web.entity.User 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.");
}
}
use of org.structr.web.entity.User in project structr by structr.
the class RenderContextTest method testAnyAllAndNoneFunctions1.
@Test
public void testAnyAllAndNoneFunctions1() {
final ActionContext ctx = new ActionContext(securityContext, null);
Principal user = null;
TestOne test = null;
try (final Tx tx = app.tx()) {
user = app.create(User.class, "user1");
test = app.create(TestOne.class, "test1");
app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group1"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
final Group group2 = app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group2"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group3"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
test.setProperty(AbstractNode.owner, group2);
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
ctx.setConstant("user", user);
ctx.setConstant("test", test);
assertEquals("Invalid any() result", "true", Scripting.replaceVariables(ctx, null, "${any(user.groups, is_allowed(data, test, 'read'))}"));
assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(user.groups, is_allowed(data, test, 'read'))}"));
assertEquals("Invalid none() result", "false", Scripting.replaceVariables(ctx, null, "${none(user.groups, is_allowed(data, test, 'read'))}"));
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
Aggregations