use of org.structr.core.entity.TestOne in project structr by structr.
the class AccessControlTest method test01SetOwner.
@Test
public void test01SetOwner() {
try {
Principal user1 = null;
Principal user2 = null;
TestOne t1 = null;
Class type = TestOne.class;
try (final Tx tx = app.tx()) {
List<Principal> users = createTestNodes(Principal.class, 2);
user1 = (Principal) users.get(0);
user1.setProperty(AbstractNode.name, "user1");
user2 = (Principal) users.get(1);
user2.setProperty(AbstractNode.name, "user2");
t1 = createTestNode(TestOne.class);
t1.setProperty(AbstractNode.owner, user1);
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
}
try (final Tx tx = app.tx()) {
assertEquals(user1, t1.getProperty(AbstractNode.owner));
// Switch user context to user1
final App user1App = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));
// Check if user1 can see t1
assertEquals(t1, user1App.nodeQuery(type).getFirst());
}
try (final Tx tx = app.tx()) {
// As superuser, make another user the owner
t1.setProperty(AbstractNode.owner, user2);
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
}
try (final Tx tx = app.tx()) {
// Switch user context to user2
final App user2App = StructrApp.getInstance(SecurityContext.getInstance(user2, AccessMode.Backend));
// Check if user2 can see t1
assertEquals(t1, user2App.nodeQuery(type).getFirst());
// Check if user2 is owner of t1
assertEquals(user2, t1.getProperty(AbstractNode.owner));
}
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class AccessControlTest method test04BackendUserAccessToProtectedNode.
@Test
public void test04BackendUserAccessToProtectedNode() {
// remove auto-generated resource access objects
clearResourceAccess();
try {
List<Principal> users = createTestNodes(Principal.class, 2);
Principal user1 = (Principal) users.get(0);
Principal user2 = (Principal) users.get(1);
PropertyMap props = new PropertyMap();
props.put(AbstractNode.visibleToPublicUsers, true);
// Create two nodes with user context, one of them is visible to public users
Class type = TestOne.class;
TestOne t1 = createTestNode(TestOne.class, props, user1);
props = new PropertyMap();
props.put(AbstractNode.visibleToAuthenticatedUsers, true);
TestOne t2 = createTestNode(TestOne.class, props, user1);
// Let another user search
SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Backend);
try (final Tx tx = app.tx()) {
Result result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
assertEquals(2, result.size());
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class AccessControlTest method test02PublicAccessToPublicNode.
@Test
public void test02PublicAccessToPublicNode() {
// remove auto-generated resource access objects
clearResourceAccess();
try {
List<Principal> users = createTestNodes(Principal.class, 1);
Principal user = (Principal) users.get(0);
PropertyMap props = new PropertyMap();
props.put(AbstractNode.visibleToPublicUsers, true);
// Create two nodes with user context, one of them is visible to public users
Class type = TestOne.class;
TestOne t1 = createTestNode(TestOne.class, props, user);
TestOne t2 = createTestNode(TestOne.class, user);
SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
try (final Tx tx = app.tx()) {
Result result = StructrApp.getInstance(publicContext).nodeQuery(type).getResult();
assertEquals(1, result.size());
assertEquals(t1.getUuid(), result.get(0).getUuid());
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class AccessControlTest method test06GrantReadPermission.
@Test
public void test06GrantReadPermission() {
// remove auto-generated resource access objects
clearResourceAccess();
try {
List<Principal> users = createTestNodes(Principal.class, 2);
Principal user1 = (Principal) users.get(0);
Principal user2 = (Principal) users.get(1);
Result result = null;
// Let user 1 create a node
Class type = TestOne.class;
final TestOne t1 = createTestNode(TestOne.class, user1);
try (final Tx tx = app.tx()) {
// Grant read permission to user 2
t1.grant(Permission.read, user2);
tx.success();
}
// Let user 2 search
SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Backend);
try (final Tx tx = app.tx()) {
result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
assertEquals(1, result.size());
assertEquals(t1.getUuid(), result.get(0).getUuid());
}
try (final Tx tx = app.tx()) {
// Revoke permission again
t1.revoke(Permission.read, user2);
tx.success();
}
try (final Tx tx = app.tx()) {
result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
assertTrue(result.isEmpty());
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.TestOne in project structr by structr.
the class BasicTest method test02CreateTwoNodesWithSameUuidInTwoTx.
@Test
public void test02CreateTwoNodesWithSameUuidInTwoTx() {
try {
final PropertyMap props = new PropertyMap();
TestOne node = null;
final String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
props.put(GraphObject.id, uuid);
try (final Tx tx = app.tx()) {
node = app.create(TestOne.class, props);
assertTrue(node != null);
assertTrue(node instanceof TestOne);
assertEquals(node.getUuid(), uuid);
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
node = app.create(TestOne.class, props);
tx.success();
fail("Validation failed!");
}
} catch (FrameworkException ex) {
// validate exception
ex.printStackTrace();
}
}
Aggregations