Search in sources :

Example 51 with FrameworkException

use of org.structr.common.error.FrameworkException 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");
    }
}
Also used : ResourceAccess(org.structr.core.entity.ResourceAccess) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 52 with FrameworkException

use of org.structr.common.error.FrameworkException 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");
    }
}
Also used : ResourceAccess(org.structr.core.entity.ResourceAccess) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 53 with FrameworkException

use of org.structr.common.error.FrameworkException 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");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Example 54 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class SchemaMethodsTest method test06InheritedSchemaMethodOnEntityOfBuiltinType.

@Test
public void test06InheritedSchemaMethodOnEntityOfBuiltinType() {
    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);
    }
    String id = createEntityAsAdmin("Image", "{'name': 'Test Image'}");
    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/" + id + "/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Example 55 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class SchemaMethodsTest method test01SchemaMethodOnBuiltinType.

@Test
public void test01SchemaMethodOnBuiltinType() {
    final String builtinTypeName = "File";
    final String schemaMethodName = "testFileMethod";
    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);
    }
    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()) {
        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(builtinTypeName + "/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)892 Tx (org.structr.core.graph.Tx)673 Test (org.junit.Test)450 App (org.structr.core.app.App)175 StructrApp (org.structr.core.app.StructrApp)174 StructrUiTest (org.structr.web.StructrUiTest)134 NodeInterface (org.structr.core.graph.NodeInterface)121 StructrTest (org.structr.common.StructrTest)118 PropertyKey (org.structr.core.property.PropertyKey)109 PropertyMap (org.structr.core.property.PropertyMap)105 IOException (java.io.IOException)96 GraphObject (org.structr.core.GraphObject)93 TestOne (org.structr.core.entity.TestOne)92 File (org.structr.web.entity.File)85 SecurityContext (org.structr.common.SecurityContext)78 Principal (org.structr.core.entity.Principal)69 Page (org.structr.web.entity.dom.Page)69 LinkedList (java.util.LinkedList)62 Folder (org.structr.web.entity.Folder)60 NodeAttribute (org.structr.core.graph.NodeAttribute)56