use of org.structr.core.entity.Principal in project structr by structr.
the class SystemTest method testCallbacksWithNormalContext.
@Test
public void testCallbacksWithNormalContext() {
try {
Principal person = this.createTestNode(Principal.class);
final SecurityContext securityContext = SecurityContext.getInstance(person, null, AccessMode.Backend);
testCallbacks(securityContext);
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class ScriptingTest method testGrantViaScripting.
@Test
public void testGrantViaScripting() {
Settings.LogSchemaOutput.setValue(true);
// setup phase: create schema nodes
try (final Tx tx = app.tx()) {
// create two nodes and associate them with each other
final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
final SchemaMethod method = createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.grant(Structr.find('Principal')[0], 'read', 'write'); }"));
sourceNode.setProperty(SchemaNode.schemaMethods, Arrays.asList(new SchemaMethod[] { method }));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class sourceType = config.getNodeEntityClass("Source");
Principal testUser = null;
// create test node as superuser
try (final Tx tx = app.tx()) {
app.create(sourceType);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// create test user
try (final Tx tx = app.tx()) {
testUser = app.create(Principal.class, new NodeAttribute<>(Principal.name, "test"), new NodeAttribute<>(StructrApp.key(Principal.class, "password"), "test"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final App userApp = StructrApp.getInstance(SecurityContext.getInstance(testUser, AccessMode.Backend));
// first test without grant, expect no test object to be found using the user context
try (final Tx tx = userApp.tx()) {
assertEquals("Invalid grant() scripting result", 0, userApp.nodeQuery(sourceType).getAsList().size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// grant read access to test user
try (final Tx tx = app.tx()) {
app.nodeQuery(sourceType).getFirst().invokeMethod("doTest01", Collections.EMPTY_MAP, true);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
// first test without grant, expect no test object to be found using the user context
try (final Tx tx = userApp.tx()) {
assertEquals("Invalid grant() scripting result", 1, userApp.nodeQuery(sourceType).getAsList().size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class ScriptingTest method testSystemProperties.
@Test
public void testSystemProperties() {
try {
final Principal user = createTestNode(Principal.class);
// create new node
TestOne t1 = createTestNode(TestOne.class, user);
final SecurityContext userContext = SecurityContext.getInstance(user, AccessMode.Frontend);
final App userApp = StructrApp.getInstance(userContext);
try (final Tx tx = userApp.tx()) {
final ActionContext userActionContext = new ActionContext(userContext, null);
assertEquals("node should be of type TestOne", "TestOne", Scripting.replaceVariables(userActionContext, t1, "${(get(this, 'type'))}"));
try {
assertEquals("setting the type should fail", "TestTwo", Scripting.replaceVariables(userActionContext, t1, "${(set(this, 'type', 'TestThree'), get(this, 'type'))}"));
fail("setting a system property should fail");
} catch (FrameworkException fx) {
}
assertEquals("setting the type should work after setting it with unlock_system_properties_once", "TestFour", Scripting.replaceVariables(userActionContext, t1, "${(unlock_system_properties_once(this), set(this, 'type', 'TestFour'), get(this, 'type'))}"));
tx.success();
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.Principal in project structr by structr.
the class GraphQLTest method testBasics.
@Test
public void testBasics() {
RestAssured.basePath = "/structr/graphql";
Group group = null;
Principal tester = null;
try (final Tx tx = app.tx()) {
final PropertyKey<List> membersKey = StructrApp.key(Group.class, "members");
tester = app.create(Principal.class, new NodeAttribute<>(Principal.name, "tester"));
group = app.create(Group.class, new NodeAttribute<>(Group.name, "TestGroup"), new NodeAttribute<>(membersKey, Arrays.asList(tester)));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final String query1 = "{ Group { id, type, name, members { id, type, name } }, Principal(_pageSize: 1) { id, type name }}";
final String query2 = "{ Group { id, type, name, members { } }}";
final String query3 = "{ Group(id: \"" + group.getUuid() + "\") { id, type, name }}";
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query1).expect().statusCode(200).body("Group", hasSize(1)).body("Principal", hasSize(1)).body("Group[0].id", equalTo(group.getUuid())).body("Group[0].type", equalTo("Group")).body("Group[0].name", equalTo("TestGroup")).body("Group[0].members[0].id", equalTo(tester.getUuid())).body("Group[0].members[0].type", equalTo("Principal")).body("Group[0].members[0].name", equalTo("tester")).body("Principal[0].id", equalTo(group.getUuid())).body("Principal[0].type", equalTo("Group")).body("Principal[0].name", equalTo("TestGroup")).when().post("/");
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query2).expect().statusCode(422).body("message", equalTo("Parse error at } in line 1, column 36")).body("code", equalTo(422)).body("query", equalTo(query2)).when().post("/");
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).body(query3).expect().statusCode(200).body("Group", hasSize(1)).body("Group[0].id", equalTo(group.getUuid())).body("Group[0].type", equalTo("Group")).body("Group[0].name", equalTo("TestGroup")).when().post("/");
}
use of org.structr.core.entity.Principal in project structr by structr.
the class GraphQLTest method testAdvancedQueries.
@Test
public void testAdvancedQueries() {
final List<MailTemplate> templates = new LinkedList<>();
final List<Principal> team = new LinkedList<>();
Group group = null;
try (final Tx tx = app.tx()) {
final PropertyKey<List> membersKey = StructrApp.key(Group.class, "members");
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Axel")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Christian")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Christian")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Inès")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Kai")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Lukas")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Michael")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Susanne")));
team.add(app.create(Principal.class, new NodeAttribute<>(Principal.name, "Tobias")));
group = app.create(Group.class, new NodeAttribute<>(Group.name, "Structr Team"), new NodeAttribute<>(membersKey, team));
app.create(Group.class, new NodeAttribute<>(Group.name, "All teams"), new NodeAttribute<>(membersKey, Arrays.asList(group)));
templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate4"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "zrtsga"), new NodeAttribute<>(AbstractNode.owner, team.get(2))));
templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate2"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "lertdf"), new NodeAttribute<>(AbstractNode.owner, team.get(0))));
templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate5"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "tzegsg"), new NodeAttribute<>(AbstractNode.owner, team.get(3))));
templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate3"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "asgw"), new NodeAttribute<>(AbstractNode.owner, team.get(1))));
templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate6"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "dfjgr"), new NodeAttribute<>(AbstractNode.owner, team.get(4))));
templates.add(app.create(MailTemplate.class, new NodeAttribute<>(StructrApp.key(MailTemplate.class, "text"), "MailTemplate1"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "locale"), "de_DE"), new NodeAttribute<>(StructrApp.key(MailTemplate.class, "name"), "abcdef"), new NodeAttribute<>(AbstractNode.owner, team.get(0))));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
RestAssured.basePath = "/structr/graphql";
{
final Map<String, Object> result = fetchGraphQL("{ Principal(id: \"" + team.get(0).getUuid() + "\") { id, type, name } }");
assertMapPathValueIs(result, "Principal.#", 1);
assertMapPathValueIs(result, "Principal.0.id", team.get(0).getUuid());
assertMapPathValueIs(result, "Principal.0.type", "Principal");
assertMapPathValueIs(result, "Principal.0.name", "Axel");
}
{
final Map<String, Object> result = fetchGraphQL("{ Principal(name: \"Axel\") { name } }");
assertMapPathValueIs(result, "Principal.#", 1);
assertMapPathValueIs(result, "Principal.0.name", "Axel");
assertMapPathValueIs(result, "Principal.0.type", null);
assertMapPathValueIs(result, "Principal.0.id", null);
}
{
final Map<String, Object> result = fetchGraphQL("{ Principal { name(_equals: \"Axel\") } }");
assertMapPathValueIs(result, "Principal.#", 1);
assertMapPathValueIs(result, "Principal.0.name", "Axel");
assertMapPathValueIs(result, "Principal.0.type", null);
assertMapPathValueIs(result, "Principal.0.id", null);
}
{
final Map<String, Object> result = fetchGraphQL("{ Principal { name(_contains: \"a\", _contains: \"l\", _conj: \"and\") } }");
assertMapPathValueIs(result, "Principal.#", 4);
assertMapPathValueIs(result, "Principal.0.name", "All teams");
assertMapPathValueIs(result, "Principal.1.name", "Axel");
assertMapPathValueIs(result, "Principal.2.name", "Lukas");
assertMapPathValueIs(result, "Principal.3.name", "Michael");
}
{
final Map<String, Object> result = fetchGraphQL("{ Group(_pageSize: 1) { name }, Principal(_pageSize: 2, _page: 2) { name(_contains: \"i\") } }");
assertMapPathValueIs(result, "Group.#", 1);
assertMapPathValueIs(result, "Group.0.name", "All teams");
assertMapPathValueIs(result, "Principal.#", 2);
assertMapPathValueIs(result, "Principal.0.name", "Inès");
assertMapPathValueIs(result, "Principal.1.name", "Kai");
}
{
final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_pageSize: 2, _page: 2) { name }}}");
assertMapPathValueIs(result, "Group.#", 2);
assertMapPathValueIs(result, "Group.0.name", "All teams");
assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
assertMapPathValueIs(result, "Group.1.name", "Structr Team");
assertMapPathValueIs(result, "Group.1.members.#", 2);
assertMapPathValueIs(result, "Group.1.members.0.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.1.name", "Inès");
}
{
final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\") { name }}}");
assertMapPathValueIs(result, "Group.#", 2);
assertMapPathValueIs(result, "Group.0.name", "All teams");
assertMapPathValueIs(result, "Group.0.members.#", 1);
assertMapPathValueIs(result, "Group.1.name", "Structr Team");
assertMapPathValueIs(result, "Group.1.members.#", 9);
assertMapPathValueIs(result, "Group.1.members.0.name", "Axel");
assertMapPathValueIs(result, "Group.1.members.1.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.2.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.3.name", "Inès");
assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
assertMapPathValueIs(result, "Group.1.members.5.name", "Lukas");
assertMapPathValueIs(result, "Group.1.members.6.name", "Michael");
assertMapPathValueIs(result, "Group.1.members.7.name", "Susanne");
assertMapPathValueIs(result, "Group.1.members.8.name", "Tobias");
}
{
final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\", _desc: true) { name }}}");
assertMapPathValueIs(result, "Group.#", 2);
assertMapPathValueIs(result, "Group.0.name", "All teams");
assertMapPathValueIs(result, "Group.0.members.#", 1);
assertMapPathValueIs(result, "Group.1.name", "Structr Team");
assertMapPathValueIs(result, "Group.1.members.#", 9);
assertMapPathValueIs(result, "Group.1.members.8.name", "Axel");
assertMapPathValueIs(result, "Group.1.members.7.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.6.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.5.name", "Inès");
assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
assertMapPathValueIs(result, "Group.1.members.3.name", "Lukas");
assertMapPathValueIs(result, "Group.1.members.2.name", "Michael");
assertMapPathValueIs(result, "Group.1.members.1.name", "Susanne");
assertMapPathValueIs(result, "Group.1.members.0.name", "Tobias");
}
{
final Map<String, Object> result = fetchGraphQL("{ Group { members { name(_contains: \"k\", _contains: \"l\", _conj: \"and\") }}}");
assertMapPathValueIs(result, "Group.#", 2);
assertMapPathValueIs(result, "Group.0.name", null);
assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
assertMapPathValueIs(result, "Group.1.members.0.name", "Lukas");
assertMapPathValueIs(result, "Group.1.members.1", null);
}
{
final Map<String, Object> result = fetchGraphQL("{ Group { members { name(_contains: \"k\", _contains: \"l\", _conj: \"or\") }}}");
assertMapPathValueIs(result, "Group.#", 2);
assertMapPathValueIs(result, "Group.0.name", null);
assertMapPathValueIs(result, "Group.0.members", new LinkedList<>());
assertMapPathValueIs(result, "Group.1.members.#", 4);
assertMapPathValueIs(result, "Group.1.members.0.name", "Axel");
assertMapPathValueIs(result, "Group.1.members.1.name", "Kai");
assertMapPathValueIs(result, "Group.1.members.2.name", "Lukas");
assertMapPathValueIs(result, "Group.1.members.3.name", "Michael");
}
{
final Map<String, Object> result = fetchGraphQL("{ MailTemplate { id, type, text(_contains: \"2\"), owner(_equals: { name: \"Axel\"}) { name } }}");
assertMapPathValueIs(result, "MailTemplate.#", 1);
assertMapPathValueIs(result, "MailTemplate.0.id", templates.get(1).getUuid());
assertMapPathValueIs(result, "MailTemplate.0.type", "MailTemplate");
assertMapPathValueIs(result, "MailTemplate.0.text", "MailTemplate2");
assertMapPathValueIs(result, "MailTemplate.0.name", null);
assertMapPathValueIs(result, "MailTemplate.0.owner.name", "Axel");
}
{
final Map<String, Object> result = fetchGraphQL("{ MailTemplate(_sort: \"owner.name\") { name, owner { name }}}");
assertMapPathValueIs(result, "MailTemplate.#", 6);
assertMapPathValueIs(result, "MailTemplate.0.name", "abcdef");
assertMapPathValueIs(result, "MailTemplate.0.owner.name", "Axel");
assertMapPathValueIs(result, "MailTemplate.1.name", "lertdf");
assertMapPathValueIs(result, "MailTemplate.1.owner.name", "Axel");
assertMapPathValueIs(result, "MailTemplate.2.name", "asgw");
assertMapPathValueIs(result, "MailTemplate.2.owner.name", "Christian");
assertMapPathValueIs(result, "MailTemplate.3.name", "zrtsga");
assertMapPathValueIs(result, "MailTemplate.3.owner.name", "Christian");
assertMapPathValueIs(result, "MailTemplate.4.name", "tzegsg");
assertMapPathValueIs(result, "MailTemplate.4.owner.name", "Inès");
assertMapPathValueIs(result, "MailTemplate.5.name", "dfjgr");
assertMapPathValueIs(result, "MailTemplate.5.owner.name", "Kai");
}
{
final Map<String, Object> result = fetchGraphQL("{ Group { name, members(_sort: \"name\", _desc: true) { name }}}");
assertMapPathValueIs(result, "Group.#", 2);
assertMapPathValueIs(result, "Group.0.name", "All teams");
assertMapPathValueIs(result, "Group.0.members.#", 1);
assertMapPathValueIs(result, "Group.1.name", "Structr Team");
assertMapPathValueIs(result, "Group.1.members.#", 9);
assertMapPathValueIs(result, "Group.1.members.8.name", "Axel");
assertMapPathValueIs(result, "Group.1.members.7.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.6.name", "Christian");
assertMapPathValueIs(result, "Group.1.members.5.name", "Inès");
assertMapPathValueIs(result, "Group.1.members.4.name", "Kai");
assertMapPathValueIs(result, "Group.1.members.3.name", "Lukas");
assertMapPathValueIs(result, "Group.1.members.2.name", "Michael");
assertMapPathValueIs(result, "Group.1.members.1.name", "Susanne");
assertMapPathValueIs(result, "Group.1.members.0.name", "Tobias");
}
}
Aggregations