001: package com.ecyrd.jspwiki.auth;
002:
003: import java.security.Principal;
004: import java.util.Properties;
005:
006: import org.apache.commons.lang.ArrayUtils;
007:
008: import junit.framework.Test;
009: import junit.framework.TestCase;
010: import junit.framework.TestSuite;
011:
012: import com.ecyrd.jspwiki.TestEngine;
013: import com.ecyrd.jspwiki.WikiException;
014: import com.ecyrd.jspwiki.WikiSession;
015: import com.ecyrd.jspwiki.WikiSessionTest;
016: import com.ecyrd.jspwiki.auth.authorize.Group;
017: import com.ecyrd.jspwiki.auth.authorize.GroupManager;
018: import com.ecyrd.jspwiki.event.WikiSecurityEvent;
019:
020: public class GroupManagerTest extends TestCase {
021: private TestEngine m_engine;
022:
023: private GroupManager m_groupMgr;
024:
025: private SecurityEventTrap m_trap = new SecurityEventTrap();
026:
027: private WikiSession m_session;
028:
029: public GroupManagerTest(String s) {
030: super (s);
031: }
032:
033: public void setUp() throws Exception {
034: Properties props = new Properties();
035: props.load(TestEngine.findTestProperties());
036:
037: m_engine = new TestEngine(props);
038: m_groupMgr = m_engine.getGroupManager();
039: m_session = WikiSessionTest.adminSession(m_engine);
040:
041: // Flush any pre-existing groups (left over from previous failures, perhaps)
042: try {
043: m_groupMgr.removeGroup("Test");
044: m_groupMgr.removeGroup("Test2");
045: m_groupMgr.removeGroup("Test3");
046: } catch (NoSuchPrincipalException e) {
047: // It's not a problem if we can't find the principals...
048: }
049:
050: m_groupMgr.addWikiEventListener(m_trap);
051: m_trap.clearEvents();
052:
053: // Add 3 test groups
054: Group group;
055: group = m_groupMgr.parseGroup("Test",
056: "Alice \n Bob \n Charlie", true);
057: m_groupMgr.setGroup(m_session, group);
058: group = m_groupMgr.parseGroup("Test2", "Bob", true);
059: m_groupMgr.setGroup(m_session, group);
060: group = m_groupMgr.parseGroup("Test3", "Fred Flintstone", true);
061: m_groupMgr.setGroup(m_session, group);
062:
063: // We should see 3 events: 1 for each group add
064: assertEquals(3, m_trap.events().length);
065: m_trap.clearEvents();
066: }
067:
068: public void tearDown() throws WikiException {
069: m_groupMgr.removeGroup("Test");
070: m_groupMgr.removeGroup("Test2");
071: m_groupMgr.removeGroup("Test3");
072: }
073:
074: public void testParseGroup() throws WikiSecurityException {
075: String members = "Biff";
076: Group group = m_groupMgr.parseGroup("Group1", members, true);
077: assertEquals(1, group.members().length);
078: assertTrue(group.isMember(new WikiPrincipal("Biff")));
079:
080: members = "Biff \n SteveAustin \n FredFlintstone";
081: group = m_groupMgr.parseGroup("Group2", members, true);
082: assertEquals(3, group.members().length);
083: assertTrue(group.isMember(new WikiPrincipal("Biff")));
084: assertTrue(group.isMember(new WikiPrincipal("SteveAustin")));
085: assertTrue(group.isMember(new WikiPrincipal("FredFlintstone")));
086: }
087:
088: public void testGetRoles() {
089: Principal[] roles = m_groupMgr.getRoles();
090: assertTrue("Found Test", ArrayUtils.contains(roles,
091: new GroupPrincipal("Test")));
092: assertTrue("Found Test2", ArrayUtils.contains(roles,
093: new GroupPrincipal("Test2")));
094: assertTrue("Found Test3", ArrayUtils.contains(roles,
095: new GroupPrincipal("Test3")));
096: }
097:
098: public void testGroupMembership() throws Exception {
099: WikiSession s;
100:
101: // Anonymous; should belong to NO groups
102: s = WikiSessionTest.anonymousSession(m_engine);
103: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
104: "Test")));
105: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
106: "Test2")));
107: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
108: "Test3")));
109: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
110: "NonExistant")));
111:
112: // Alice is asserted; should belong to NO groups
113: s = WikiSessionTest.assertedSession(m_engine, Users.ALICE);
114: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
115: "Test")));
116: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
117: "Test2")));
118: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
119: "Test3")));
120: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
121: "NonExistant")));
122:
123: // Alice is authenticated; should belong to Test
124: s = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE,
125: Users.ALICE_PASS);
126: assertTrue(m_groupMgr.isUserInRole(s,
127: new GroupPrincipal("Test")));
128: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
129: "Test2")));
130: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
131: "Test3")));
132: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
133: "NonExistant")));
134:
135: // Bob is authenticated; should belong to Test & Test2
136: s = WikiSessionTest.authenticatedSession(m_engine, Users.BOB,
137: Users.BOB_PASS);
138: assertTrue(m_groupMgr.isUserInRole(s,
139: new GroupPrincipal("Test")));
140: assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal(
141: "Test2")));
142: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
143: "Test3")));
144: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
145: "NonExistant")));
146:
147: // Charlie is authenticated; should belong to Test
148: s = WikiSessionTest.authenticatedSession(m_engine,
149: Users.CHARLIE, Users.CHARLIE_PASS);
150: assertTrue(m_groupMgr.isUserInRole(s,
151: new GroupPrincipal("Test")));
152: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
153: "Test2")));
154: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
155: "Test3")));
156: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
157: "NonExistant")));
158:
159: // Fred is authenticated; should belong to Test3
160: s = WikiSessionTest.authenticatedSession(m_engine, Users.FRED,
161: Users.FRED_PASS);
162: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
163: "Test")));
164: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
165: "Test2")));
166: assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal(
167: "Test3")));
168: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
169: "NonExistant")));
170:
171: // Nobody loves Biff!
172: s = WikiSessionTest.authenticatedSession(m_engine, Users.BIFF,
173: Users.BIFF_PASS);
174: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
175: "Test")));
176: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
177: "Test2")));
178: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
179: "Test3")));
180: assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal(
181: "NonExistant")));
182: }
183:
184: public void testGroupAddEvents() throws Exception {
185: // Flush any pre-existing groups (left over from previous failures, perhaps)
186: try {
187: m_groupMgr.removeGroup("Events");
188: } catch (NoSuchPrincipalException e) {
189: // It's not a problem if we get here...
190: }
191: m_trap.clearEvents();
192:
193: Group group = m_groupMgr.parseGroup("Events", "", true);
194: m_groupMgr.setGroup(m_session, group);
195: WikiSecurityEvent event;
196: group = m_groupMgr.getGroup("Events");
197: group.add(new WikiPrincipal("Alice"));
198: group.add(new WikiPrincipal("Bob"));
199: group.add(new WikiPrincipal("Charlie"));
200:
201: // We should see a GROUP_ADD event
202: WikiSecurityEvent[] events = m_trap.events();
203: assertEquals(1, events.length);
204: event = events[0];
205: assertEquals(m_groupMgr, event.getSource());
206: assertEquals(WikiSecurityEvent.GROUP_ADD, event.getType());
207: assertEquals(group, event.getTarget());
208:
209: // Clean up
210: m_groupMgr.removeGroup("Events");
211: }
212:
213: public static Test suite() {
214: TestSuite suite = new TestSuite("Group manager tests");
215: suite.addTestSuite(GroupManagerTest.class);
216: return suite;
217: }
218:
219: }
|