001: package com.ecyrd.jspwiki.auth;
002:
003: import java.security.Principal;
004: import java.util.Properties;
005:
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009:
010: import com.ecyrd.jspwiki.TestEngine;
011: import com.ecyrd.jspwiki.WikiSession;
012: import com.ecyrd.jspwiki.WikiSessionTest;
013: import com.ecyrd.jspwiki.auth.authorize.Group;
014: import com.ecyrd.jspwiki.auth.authorize.GroupManager;
015: import com.ecyrd.jspwiki.auth.authorize.Role;
016:
017: /**
018: * Tests the AuthorizationManager class.
019: * @author Janne Jalkanen
020: */
021: public class AuthenticationManagerTest extends TestCase {
022: private AuthenticationManager m_auth;
023:
024: private TestEngine m_engine;
025:
026: private GroupManager m_groupMgr;
027:
028: private WikiSession m_session;
029:
030: public AuthenticationManagerTest(String s) {
031: super (s);
032: }
033:
034: public void setUp() throws Exception {
035: Properties props = new Properties();
036: props.load(TestEngine.findTestProperties());
037: m_engine = new TestEngine(props);
038: m_auth = m_engine.getAuthenticationManager();
039: m_groupMgr = m_engine.getGroupManager();
040: m_session = WikiSessionTest.adminSession(m_engine);
041: }
042:
043: public void testIsUserPrincipal() {
044: assertTrue(AuthenticationManager
045: .isUserPrincipal(new WikiPrincipal("Foo")));
046: assertFalse(AuthenticationManager
047: .isUserPrincipal(new GroupPrincipal("Group1")));
048: assertFalse(AuthenticationManager.isUserPrincipal(new Role(
049: "Role1")));
050: assertFalse(AuthenticationManager
051: .isUserPrincipal(Role.ANONYMOUS));
052: }
053:
054: public void testLoginCustom() throws Exception {
055: WikiSession session = WikiSessionTest.authenticatedSession(
056: m_engine, Users.JANNE, Users.JANNE_PASS);
057: assertTrue(session.hasPrincipal(Role.ALL));
058: assertTrue(session.hasPrincipal(Role.AUTHENTICATED));
059: assertTrue(session.hasPrincipal(new WikiPrincipal(Users.JANNE,
060: WikiPrincipal.LOGIN_NAME)));
061: assertTrue(session.hasPrincipal(new WikiPrincipal(
062: "JanneJalkanen", WikiPrincipal.WIKI_NAME)));
063: assertTrue(session.hasPrincipal(new WikiPrincipal(
064: "Janne Jalkanen", WikiPrincipal.FULL_NAME)));
065: }
066:
067: public void testLoginCustomWithGroup() throws Exception {
068: // Flush any pre-existing groups (left over from previous failures, perhaps)
069: try {
070: m_groupMgr.removeGroup("Test1");
071: m_groupMgr.removeGroup("Test2");
072: } catch (NoSuchPrincipalException e) {
073:
074: }
075:
076: // Log in 'janne' and verify there are 5 principals in the subject
077: // (ALL, AUTHENTICATED, login, fullname, wikiname Principals)
078: WikiSession session = WikiSession.guestSession(m_engine);
079: m_auth.login(session, Users.JANNE, Users.JANNE_PASS);
080: assertEquals(3, session.getPrincipals().length);
081: assertEquals(2, session.getRoles().length);
082: assertTrue(session.hasPrincipal(new WikiPrincipal(
083: "JanneJalkanen", WikiPrincipal.WIKI_NAME)));
084:
085: // Listen for any manager group-add events
086: GroupManager manager = m_engine.getGroupManager();
087: SecurityEventTrap trap = new SecurityEventTrap();
088: manager.addWikiEventListener(trap);
089:
090: // Create two groups; one with Janne in it, and one without
091: Group groupTest1 = m_groupMgr.parseGroup("Test1",
092: "JanneJalkanen \n Bob \n Charlie", true);
093: m_groupMgr.setGroup(m_session, groupTest1);
094: groupTest1 = m_groupMgr.getGroup("Test1");
095: Principal principalTest1 = groupTest1.getPrincipal();
096:
097: Group groupTest2 = m_groupMgr.parseGroup("Test2",
098: "Alice \n Bob \n Charlie", true);
099: m_groupMgr.setGroup(m_session, groupTest2);
100: groupTest2 = m_groupMgr.getGroup("Test2");
101: Principal principalTest2 = groupTest2.getPrincipal();
102:
103: // We should see two security events (one for each group create)
104: // We should also see a GroupPrincipal for group Test1, but not Test2
105: assertEquals(2, trap.events().length);
106: assertTrue(session.hasPrincipal(principalTest1));
107: assertFalse(session.hasPrincipal(principalTest2));
108:
109: // If we remove Test1, the GroupPrincipal should disappear
110: m_groupMgr.removeGroup("Test1");
111: assertFalse(session.hasPrincipal(principalTest1));
112: assertFalse(session.hasPrincipal(principalTest2));
113:
114: // Now, add 'JanneJalkanen' to Test2 group manually; we should see the GroupPrincipal
115: groupTest2.add(new WikiPrincipal("JanneJalkanen"));
116: m_groupMgr.setGroup(session, groupTest2);
117: assertFalse(session.hasPrincipal(principalTest1));
118: assertTrue(session.hasPrincipal(principalTest2));
119:
120: // Remove 'JanneJalkenen' manually; the GroupPrincipal should disappear
121: groupTest2.remove(new WikiPrincipal("JanneJalkanen"));
122: m_groupMgr.setGroup(session, groupTest2);
123: assertFalse(session.hasPrincipal(principalTest1));
124: assertFalse(session.hasPrincipal(principalTest2));
125:
126: // Clean up
127: m_groupMgr.removeGroup("Test2");
128: }
129:
130: public static Test suite() {
131: TestSuite suite = new TestSuite("Authentication Manager test");
132: suite.addTestSuite(AuthenticationManagerTest.class);
133: return suite;
134: }
135: }
|