001: package com.flexive.tests.embedded;
002:
003: import com.flexive.shared.CacheAdmin;
004: import com.flexive.shared.EJBLookup;
005: import com.flexive.shared.FxContext;
006: import com.flexive.shared.interfaces.ACLEngine;
007: import com.flexive.shared.exceptions.FxApplicationException;
008: import com.flexive.shared.exceptions.FxLogoutFailedException;
009: import com.flexive.shared.security.ACL;
010: import com.flexive.shared.security.UserTicket;
011: import com.flexive.shared.security.ACLAssignment;
012: import static com.flexive.shared.security.ACL.Permission;
013: import com.flexive.shared.value.FxString;
014: import static com.flexive.tests.embedded.FxTestUtils.login;
015: import static com.flexive.tests.embedded.FxTestUtils.logout;
016: import static org.testng.Assert.assertEquals;
017: import org.testng.annotations.AfterClass;
018: import org.testng.annotations.BeforeClass;
019: import org.testng.annotations.Test;
020:
021: import java.util.List;
022: import java.util.Arrays;
023:
024: /**
025: * Basic ACL engine tests.
026: *
027: * @author Daniel Lichtenberger, UCS
028: * @version $Rev$
029: */
030: @Test(groups={"ejb","security"})
031: public class ACLEngineTest {
032: @BeforeClass
033: public void beforeClass() throws Exception {
034: login(TestUsers.SUPERVISOR);
035: }
036:
037: @AfterClass
038: public void afterClass() throws FxLogoutFailedException {
039: logout();
040: }
041:
042: @Test(groups={"ejb","security"})
043: public void createAclTest() throws FxApplicationException {
044: final long aclId = EJBLookup.getACLEngine().create(
045: "create-acl-test", new FxString("first label"),
046: TestUsers.getTestMandator(), "#000000", "",
047: ACL.Category.INSTANCE);
048: try {
049: EJBLookup.getACLEngine().create("create-acl-test",
050: new FxString("first label"),
051: TestUsers.getTestMandator(), "#000000", "",
052: ACL.Category.INSTANCE);
053: assert false : "ACL's must have unique names";
054: } catch (Exception e) {
055: //ok
056: }
057:
058: try {
059: final ACL acl = CacheAdmin.getFilteredEnvironment().getACL(
060: aclId);
061: assertEquals(acl.getName(), "create-acl-test");
062: assertEquals(acl.getDescription(), "");
063: assertEquals(acl.getColor(), "#000000");
064: assertEquals(acl.getLabel(), new FxString("first label"));
065:
066: assert FxContext.get().getTicket().getGroups().length > 0;
067: final long groupId = FxContext.get().getTicket()
068: .getGroups()[0];
069:
070: EJBLookup.getACLEngine().update(
071: aclId,
072: "new-acl-test",
073: new FxString("test"),
074: null,
075: "new description",
076: Arrays.asList(new ACLAssignment(aclId, groupId,
077: true, true, true, false, false, false,
078: ACL.Category.INSTANCE, null)));
079: final ACL updatedAcl = CacheAdmin.getFilteredEnvironment()
080: .getACL(aclId);
081: assertEquals(updatedAcl.getName(), "new-acl-test");
082: assertEquals(updatedAcl.getDescription(), "new description");
083: assertEquals(updatedAcl.getColor(), "#000000");
084: assertEquals(updatedAcl.getLabel(), new FxString("test"));
085: } finally {
086: EJBLookup.getACLEngine().remove(aclId);
087: }
088: }
089:
090: @Test(groups={"ejb","security"})
091: public void aclAssignmentsTest() throws FxApplicationException {
092: final ACLEngine aclEngine = EJBLookup.getACLEngine();
093: final long aclId = aclEngine.create("create-acl-test",
094: new FxString("first label"), TestUsers
095: .getTestMandator(), "#000000", "",
096: ACL.Category.INSTANCE);
097: try {
098: final UserTicket ticket = FxContext.get().getTicket();
099: assert ticket.getGroups().length > 0;
100: for (long group : ticket.getGroups()) {
101: aclEngine.assign(aclId, group, Permission.EDIT,
102: Permission.CREATE);
103: }
104: final List<ACLAssignment> assignments = aclEngine
105: .loadAssignments(aclId);
106: for (long group : ticket.getGroups()) {
107: boolean found = false;
108: for (ACLAssignment assignment : assignments) {
109: if (assignment.getGroupId() == group) {
110: assert assignment.getMayEdit() : "Expected edit permissions";
111: assert assignment.getMayCreate() : "Expected create permissions";
112: assert !assignment.getMayDelete();
113: assert !assignment.getMayRead();
114: assert !assignment.getMayExport();
115: assert !assignment.getMayRelate();
116: found = true;
117: }
118: }
119: assert found : "Group " + group
120: + " not found in assignments: " + assignments;
121:
122: final List<ACLAssignment> groupAssignments = aclEngine
123: .loadGroupAssignments(group);
124: boolean foundOurAcl = false;
125: for (ACLAssignment groupAssignment : groupAssignments) {
126: if (groupAssignment.getAclId() == aclId) {
127: foundOurAcl = true;
128: }
129: }
130: assert foundOurAcl : "Didn't find ACL " + aclId
131: + " in group assignments for group " + group;
132: }
133: } finally {
134: aclEngine.remove(aclId);
135: }
136:
137: }
138: }
|