use of org.structr.core.property.PropertyMap in project structr by structr.
the class RenderContextTest method testVariableReplacementInDynamicTypes.
@Test
public void testVariableReplacementInDynamicTypes() {
SchemaNode itemNode = null;
NodeInterface parent = null;
NodeInterface child1 = null;
NodeInterface child2 = null;
try (final Tx tx = app.tx()) {
itemNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"));
final PropertyMap properties = new PropertyMap();
properties.put(SchemaRelationshipNode.sourceId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.targetId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.relationshipType, "CHILD");
properties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
properties.put(SchemaRelationshipNode.targetMultiplicity, "*");
properties.put(SchemaRelationshipNode.sourceJsonName, "parentItem");
properties.put(SchemaRelationshipNode.targetJsonName, "children");
app.create(SchemaRelationshipNode.class, properties);
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
final PropertyKey childrenProperty = StructrApp.key(itemClass, "children");
// create parent/child relationship
try (final Tx tx = app.tx()) {
parent = app.create(itemClass);
child1 = app.create(itemClass);
child2 = app.create(itemClass);
final List<NodeInterface> children = new LinkedList<>();
children.add(child1);
children.add(child2);
parent.setProperties(parent.getSecurityContext(), new PropertyMap(childrenProperty, children));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// verify that parent has two children
try (final Tx tx = app.tx()) {
// verify that parentItem can be accessed....
final Object value = parent.getProperty(childrenProperty);
assertTrue(value instanceof Collection);
final Collection coll = (Collection) value;
assertEquals(2, coll.size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
assertEquals(parent.toString(), Scripting.replaceVariables(new ActionContext(securityContext), child1, "${this.parentItem}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class ResourceAccessTest method test03ResourceAccessPUT.
@Test
public void test03ResourceAccessPUT() {
// clear resource access objects that are created by the dynamic schema
clearResourceAccess();
final String name = "testuser-01";
final String password = "testpassword-01";
ResourceAccess folderGrant = null;
User testUser = null;
Folder testFolder = null;
try (final Tx tx = app.tx()) {
testUser = createTestNodes(User.class, 1).get(0);
testFolder = createTestNodes(Folder.class, 1).get(0);
assertNotNull(testFolder);
// no resource access node at all => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
folderGrant = createResourceAccess("Folder", UiAuthenticator.FORBIDDEN);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// resource access explicitly set to FORBIDDEN => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
// allow PUT for authenticated users => access without user/pass should be still forbidden
folderGrant.setFlag(UiAuthenticator.AUTH_USER_PUT);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
// allow PUT for non-authenticated users =>
folderGrant.setProperties(folderGrant.getSecurityContext(), new PropertyMap(GraphObject.visibleToPublicUsers, true));
folderGrant.setFlag(UiAuthenticator.NON_AUTH_USER_PUT);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// ownerless non-public node cannot be found by anonymous user
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(404).when().put("/folder/" + testFolder.getUuid());
// Prepare for next test
final PropertyMap testUserProperties = new PropertyMap();
testUserProperties.put(StructrApp.key(User.class, "name"), name);
testUserProperties.put(StructrApp.key(User.class, "password"), password);
testUser.setProperties(testUser.getSecurityContext(), testUserProperties);
// now we give the user ownership and expect a 200
testFolder.setProperties(testFolder.getSecurityContext(), new PropertyMap(AbstractNode.owner, testUser));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().headers("X-User", name, "X-Password", password).contentType("application/json; charset=UTF-8").expect().statusCode(200).when().put("/folder/" + testFolder.getUuid());
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class ResourceAccessTest method test01ResourceAccessGET.
@Test
public void test01ResourceAccessGET() {
// clear resource access objects that are created by the dynamic schema
clearResourceAccess();
Folder testFolder = null;
ResourceAccess folderGrant = null;
try (final Tx tx = app.tx()) {
testFolder = createTestNodes(Folder.class, 1).get(0);
assertNotNull(testFolder);
// no resource access node at all => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().get("/folders");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
folderGrant = createResourceAccess("Folder", UiAuthenticator.FORBIDDEN);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// resource access explicetly set to FORBIDDEN => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().get("/folders");
// allow GET for authenticated users => access without user/pass should be still forbidden
folderGrant.setProperties(folderGrant.getSecurityContext(), new PropertyMap(GraphObject.visibleToPublicUsers, true));
folderGrant.setFlag(UiAuthenticator.AUTH_USER_GET);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().get("/folders");
// allow GET for non-authenticated users => access without user/pass should be allowed
folderGrant.setFlag(UiAuthenticator.NON_AUTH_USER_GET);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(200).when().get("/folders");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class ResourceAccessTest method test04ResourceAccessDELETE.
@Test
public void test04ResourceAccessDELETE() {
// clear resource access objects that are created by the dynamic schema
clearResourceAccess();
final String name = "testuser-01";
final String password = "testpassword-01";
Folder testFolder = null;
User testUser = null;
ResourceAccess folderGrant = null;
try (final Tx tx = app.tx()) {
testFolder = createTestNodes(Folder.class, 1).get(0);
assertNotNull(testFolder);
testUser = createTestNodes(User.class, 1).get(0);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// no resource access node at all => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().delete("/folder/" + testFolder.getUuid());
folderGrant = createResourceAccess("Folder", UiAuthenticator.FORBIDDEN);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// resource access explicitly set to FORBIDDEN => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().delete("/folder/" + testFolder.getUuid());
folderGrant.setFlag(UiAuthenticator.AUTH_USER_DELETE);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().delete("/folder/" + testFolder.getUuid());
folderGrant.setProperties(folderGrant.getSecurityContext(), new PropertyMap(GraphObject.visibleToPublicUsers, true));
folderGrant.setFlag(UiAuthenticator.NON_AUTH_USER_DELETE);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(404).when().delete("/folder/" + testFolder.getUuid());
final PropertyMap changedProperties = new PropertyMap();
changedProperties.put(StructrApp.key(User.class, "name"), name);
changedProperties.put(StructrApp.key(User.class, "password"), password);
testUser.setProperties(testUser.getSecurityContext(), changedProperties);
// make user own folder
testFolder.setProperties(testFolder.getSecurityContext(), new PropertyMap(AbstractNode.owner, testUser));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// test user owns object now => 200
RestAssured.given().headers("X-User", name, "X-Password", password).contentType("application/json; charset=UTF-8").expect().statusCode(200).when().delete("/folder/" + testFolder.getUuid());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class SchemaMethodsTest method test05InheritedSchemaMethodOnBuildinType.
@Test
public void test05InheritedSchemaMethodOnBuildinType() {
final String builtinTypeName = "File";
final String schemaMethodName = "testFileMethod";
User admin = null;
try (final Tx tx = app.tx()) {
admin = createAdminUser();
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
// Add schema method "testFileMethod" to built-in File class
SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName(builtinTypeName).getFirst();
final PropertyMap testFileMethodProperties = new PropertyMap();
testFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
testFileMethodProperties.put(SchemaMethod.source, "()");
testFileMethodProperties.put(SchemaMethod.schemaNode, fileNodeDef);
SchemaMethod testFileMethod = app.create(SchemaMethod.class, testFileMethodProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post("/Image/" + schemaMethodName);
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
Aggregations