001: package com.ecyrd.jspwiki.auth.login;
002:
003: import java.util.Properties;
004: import java.util.Set;
005:
006: import javax.security.auth.Subject;
007: import javax.security.auth.callback.CallbackHandler;
008: import javax.security.auth.login.LoginContext;
009: import javax.security.auth.login.LoginException;
010:
011: import junit.framework.TestCase;
012:
013: import com.ecyrd.jspwiki.NoRequiredPropertyException;
014: import com.ecyrd.jspwiki.TestEngine;
015: import com.ecyrd.jspwiki.WikiEngine;
016: import com.ecyrd.jspwiki.auth.WikiPrincipal;
017: import com.ecyrd.jspwiki.auth.authorize.Role;
018: import com.ecyrd.jspwiki.auth.user.UserDatabase;
019: import com.ecyrd.jspwiki.auth.user.XMLUserDatabase;
020:
021: /**
022: * @author Andrew R. Jaquith
023: */
024: public class UserDatabaseLoginModuleTest extends TestCase {
025: UserDatabase db;
026:
027: Subject subject;
028:
029: public final void testLogin() {
030: try {
031: // Log in with a user that isn't in the database
032: CallbackHandler handler = new WikiCallbackHandler(db,
033: "user", "password");
034: LoginContext context = new LoginContext("JSPWiki-custom",
035: subject, handler);
036: context.login();
037: Set principals = subject.getPrincipals();
038: assertEquals(3, principals.size());
039: assertTrue(principals
040: .contains(new PrincipalWrapper(new WikiPrincipal(
041: "user", WikiPrincipal.LOGIN_NAME))));
042: assertTrue(principals.contains(Role.AUTHENTICATED));
043: assertTrue(principals.contains(Role.ALL));
044:
045: // Login with a user that IS in the databasse
046: subject = new Subject();
047: handler = new WikiCallbackHandler(db, "janne", "myP@5sw0rd");
048: context = new LoginContext("JSPWiki-custom", subject,
049: handler);
050: context.login();
051: principals = subject.getPrincipals();
052: assertEquals(3, principals.size());
053: assertTrue(principals
054: .contains(new PrincipalWrapper(new WikiPrincipal(
055: "janne", WikiPrincipal.LOGIN_NAME))));
056: assertTrue(principals.contains(Role.AUTHENTICATED));
057: assertTrue(principals.contains(Role.ALL));
058: } catch (LoginException e) {
059: System.err.println(e.getMessage());
060: assertTrue(false);
061: }
062: }
063:
064: public final void testLogout() {
065: try {
066: CallbackHandler handler = new WikiCallbackHandler(db,
067: "user", "password");
068: LoginContext context = new LoginContext("JSPWiki-custom",
069: subject, handler);
070: context.login();
071: Set principals = subject.getPrincipals();
072: assertEquals(3, principals.size());
073: assertTrue(principals
074: .contains(new PrincipalWrapper(new WikiPrincipal(
075: "user", WikiPrincipal.LOGIN_NAME))));
076: assertTrue(principals.contains(Role.AUTHENTICATED));
077: assertTrue(principals.contains(Role.ALL));
078: context.logout();
079: assertEquals(0, principals.size());
080: } catch (LoginException e) {
081: System.err.println(e.getMessage());
082: assertTrue(false);
083: }
084: }
085:
086: /**
087: * @see junit.framework.TestCase#setUp()
088: */
089: protected void setUp() throws Exception {
090: Properties props = new Properties();
091: props.load(TestEngine.findTestProperties());
092: props.put(XMLUserDatabase.PROP_USERDATABASE,
093: "tests/etc/userdatabase.xml");
094: WikiEngine m_engine = new TestEngine(props);
095: db = new XMLUserDatabase();
096: subject = new Subject();
097: try {
098: db.initialize(m_engine, props);
099: } catch (NoRequiredPropertyException e) {
100: System.err.println(e.getMessage());
101: assertTrue(false);
102: }
103: }
104:
105: }
|