001: package com.ecyrd.jspwiki.auth.acl;
002:
003: import java.security.Principal;
004: import java.util.Properties;
005: import java.util.regex.Matcher;
006:
007: import org.apache.commons.lang.ArrayUtils;
008:
009: import junit.framework.Test;
010: import junit.framework.TestCase;
011: import junit.framework.TestSuite;
012:
013: import com.ecyrd.jspwiki.TestEngine;
014: import com.ecyrd.jspwiki.WikiPage;
015: import com.ecyrd.jspwiki.auth.WikiPrincipal;
016: import com.ecyrd.jspwiki.auth.permissions.PermissionFactory;
017: import com.ecyrd.jspwiki.providers.ProviderException;
018:
019: public class DefaultAclManagerTest extends TestCase {
020: TestEngine m_engine;
021:
022: public DefaultAclManagerTest(String s) {
023: super (s);
024: }
025:
026: public void setUp() throws Exception {
027: Properties props = new Properties();
028: props.load(TestEngine.findTestProperties());
029: m_engine = new TestEngine(props);
030:
031: String text = "Foo";
032: m_engine.saveText("TestDefaultPage", text);
033:
034: text = "Bar. [{ALLOW edit Charlie, Herman}] ";
035: m_engine.saveText("TestAclPage", text);
036: }
037:
038: public void tearDown() {
039: try {
040: m_engine.deletePage("TestDefaultPage");
041: m_engine.deletePage("TestAclPage");
042: } catch (ProviderException e) {
043: }
044: }
045:
046: public void testGetPermissions() {
047: WikiPage page = m_engine.getPage("TestDefaultPage");
048: Acl acl = m_engine.getAclManager().getPermissions(page);
049: assertNotNull(page.getAcl());
050: assertTrue(page.getAcl().isEmpty());
051:
052: page = m_engine.getPage("TestAclPage");
053: acl = m_engine.getAclManager().getPermissions(page);
054: assertNotNull(page.getAcl());
055: assertFalse(page.getAcl().isEmpty());
056:
057: Principal[] p;
058:
059: // Charlie is an editor; reading is therefore implied
060: p = acl.findPrincipals(PermissionFactory.getPagePermission(
061: page, "view"));
062: assertEquals(2, p.length);
063: assertTrue(ArrayUtils.contains(p, new WikiPrincipal("Charlie")));
064:
065: // Charlie should be in the ACL as an editor
066: p = acl.findPrincipals(PermissionFactory.getPagePermission(
067: page, "edit"));
068: assertEquals(2, p.length);
069: assertTrue(ArrayUtils.contains(p, new WikiPrincipal("Charlie")));
070:
071: // Charlie should not be able to delete this page
072: p = acl.findPrincipals(PermissionFactory.getPagePermission(
073: page, "delete"));
074: assertEquals(0, p.length);
075:
076: // Herman is an unregistered user and editor; reading is implied
077: p = acl.findPrincipals(PermissionFactory.getPagePermission(
078: page, "view"));
079: assertEquals(2, p.length);
080: assertTrue(ArrayUtils.contains(p, new UnresolvedPrincipal(
081: "Herman")));
082:
083: // Herman should be in the ACL as an editor
084: p = acl.findPrincipals(PermissionFactory.getPagePermission(
085: page, "edit"));
086: assertEquals(2, p.length);
087: assertTrue(ArrayUtils.contains(p, new UnresolvedPrincipal(
088: "Herman")));
089:
090: // Herman should not be able to delete this page
091: p = acl.findPrincipals(PermissionFactory.getPagePermission(
092: page, "delete"));
093: assertEquals(0, p.length);
094: }
095:
096: public void testAclRegex() {
097: String acl;
098: Matcher m;
099:
100: acl = "[{ALLOW view Bob, Alice, Betty}] Test text.";
101: m = DefaultAclManager.ACL_PATTERN.matcher(acl);
102: assertTrue(m.find());
103: assertEquals(2, m.groupCount());
104: assertEquals("[{ALLOW view Bob, Alice, Betty}]", m.group(0));
105: assertEquals("view", m.group(1));
106: assertEquals("Bob, Alice, Betty", m.group(2));
107: assertFalse(m.find());
108:
109: acl = "[{ALLOW view Alice}] Test text.";
110: m = DefaultAclManager.ACL_PATTERN.matcher(acl);
111: assertTrue(m.find());
112: System.out.println(m.group());
113: assertEquals(2, m.groupCount());
114: assertEquals("[{ALLOW view Alice}]", m.group(0));
115: assertEquals("view", m.group(1));
116: assertEquals("Alice", m.group(2));
117: assertFalse(m.find());
118:
119: acl = "Test text [{ ALLOW view Alice }] Test text.";
120: m = DefaultAclManager.ACL_PATTERN.matcher(acl);
121: assertTrue(m.find());
122: System.out.println(m.group());
123: assertEquals(2, m.groupCount());
124: assertEquals("[{ ALLOW view Alice }]", m.group(0));
125: assertEquals("view", m.group(1));
126: assertEquals("Alice", m.group(2));
127: assertFalse(m.find());
128:
129: acl = "Test text [{ ALLOW view Alice , Bob }] Test text.";
130: m = DefaultAclManager.ACL_PATTERN.matcher(acl);
131: assertTrue(m.find());
132: System.out.println(m.group());
133: assertEquals(2, m.groupCount());
134: assertEquals("[{ ALLOW view Alice , Bob }]", m.group(0));
135: assertEquals("view", m.group(1));
136: assertEquals("Alice , Bob", m.group(2));
137: assertFalse(m.find());
138:
139: acl = "Test text [{ ALLOW view Alice , Bob }] Test text [{ALLOW edit Betty}].";
140: m = DefaultAclManager.ACL_PATTERN.matcher(acl);
141: assertTrue(m.find());
142: System.out.println(m.group());
143: assertEquals(2, m.groupCount());
144: assertEquals("[{ ALLOW view Alice , Bob }]", m.group(0));
145: assertEquals("view", m.group(1));
146: assertEquals("Alice , Bob", m.group(2));
147: assertTrue(m.find());
148: assertEquals(2, m.groupCount());
149: assertEquals("[{ALLOW edit Betty}]", m.group(0));
150: assertEquals("edit", m.group(1));
151: assertEquals("Betty", m.group(2));
152: assertFalse(m.find());
153: }
154:
155: public void testPrintAcl() {
156: // Verify that the printed Acl for the test page is OK
157: WikiPage page = m_engine.getPage("TestAclPage");
158: Acl acl = m_engine.getAclManager().getPermissions(page);
159: String aclString = DefaultAclManager.printAcl(acl);
160: assertEquals("[{ALLOW edit Charlie,Herman}]\n", aclString);
161:
162: // Create an ACL from scratch
163: acl = new AclImpl();
164: AclEntry entry = new AclEntryImpl();
165: entry.setPrincipal(new WikiPrincipal("Charlie"));
166: entry.addPermission(PermissionFactory.getPagePermission(
167: "Main:Foo", "view"));
168: entry.addPermission(PermissionFactory.getPagePermission(
169: "Main:Foo", "edit"));
170: acl.addEntry(entry);
171: entry = new AclEntryImpl();
172: entry.setPrincipal(new WikiPrincipal("Devin"));
173: entry.addPermission(PermissionFactory.getPagePermission(
174: "Main:Foo", "edit"));
175: entry.addPermission(PermissionFactory.getPagePermission(
176: "Main:Foo", "delete"));
177: acl.addEntry(entry);
178:
179: // Verify that the printed ACL is OK
180: String expectedValue = "[{ALLOW delete Devin}]\n[{ALLOW edit Charlie,Devin}]\n[{ALLOW view Charlie}]\n";
181: assertEquals(expectedValue, DefaultAclManager.printAcl(acl));
182: }
183:
184: public static Test suite() {
185: return new TestSuite(DefaultAclManagerTest.class);
186: }
187:
188: }
|