Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserPreferencesActionBeanTest.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserPreferencesActionBeanTest.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserPreferencesActionBeanTest.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserPreferencesActionBeanTest.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,119 @@ +package com.ecyrd.jspwiki.action; + +import java.util.Properties; + +import javax.servlet.http.Cookie; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.sourceforge.stripes.mock.MockHttpServletRequest; +import net.sourceforge.stripes.mock.MockHttpServletResponse; +import net.sourceforge.stripes.mock.MockRoundtrip; +import net.sourceforge.stripes.mock.MockServletContext; + +import com.ecyrd.jspwiki.TestEngine; +import com.ecyrd.jspwiki.WikiSession; +import com.ecyrd.jspwiki.auth.Users; +import com.ecyrd.jspwiki.auth.login.CookieAssertionLoginModule; + +public class UserPreferencesActionBeanTest extends TestCase +{ + TestEngine m_engine; + + public void setUp() + { + // Start the WikiEngine, and stash reference + Properties props = new Properties(); + try + { + props.load( TestEngine.findTestProperties() ); + m_engine = new TestEngine( props ); + } + catch (Exception e) + { + throw new RuntimeException("Could not set up TestEngine: " + e.getMessage()); + } + } + + public void testCreateAssertedName() throws Exception + { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserPreferencesActionBean bean; + + // Create session; set 'assertion' param; verify it got saved + trip = new MockRoundtrip(ctx, "/UserPreferences.action"); + trip.setParameter("assertedName", "MyAssertedIdentity"); + trip.setParameter("createAssertedName", "true"); + trip.execute(); + bean = trip.getActionBean(UserPreferencesActionBean.class); + assertEquals("/", trip.getDestination()); + + // Verify that the asserted name cookie is present in the Response + MockHttpServletResponse response = (MockHttpServletResponse)bean.getContext().getResponse(); + Cookie[] cookies = response.getCookies(); + assertEquals(1, cookies.length); + Cookie cookie = cookies[0]; + assertEquals(CookieAssertionLoginModule.PREFS_COOKIE_NAME,cookie.getName()); + assertEquals("MyAssertedIdentity",cookie.getValue()); + } + + public void testCreateAssertedNameAfterLogin() throws Exception + { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserPreferencesActionBean bean; + + // Create session; login in as Janne + trip = new MockRoundtrip(ctx, "/UserPreferences.action"); + MockHttpServletRequest request = trip.getRequest(); + WikiSession wikiSession = WikiSession.getWikiSession(m_engine, request); + boolean login = m_engine.getAuthenticationManager().login(wikiSession, Users.JANNE,Users.JANNE_PASS); + assertTrue("Could not log in.", login); + + // Set 'assertion' param; verify redirect to front page + trip.setParameter("assertedName", "MyAssertedIdentity"); + trip.setParameter("createAssertedName", "true"); + trip.execute(); + bean = trip.getActionBean(UserPreferencesActionBean.class); + assertEquals("/", trip.getDestination()); + + // Verify that the asserted name cookie is NOT present in the Response + // (authenticated users cannot set the assertion cookie) + MockHttpServletResponse response = (MockHttpServletResponse)bean.getContext().getResponse(); + Cookie[] cookies = response.getCookies(); + assertEquals(0, cookies.length); + } + + public void testClearAssertedName() throws Exception + { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserPreferencesActionBean bean; + + // Create session; set 'assertion' param; verify it got saved + trip = new MockRoundtrip(ctx, "/UserPreferences.action"); + MockHttpServletRequest request = trip.getRequest(); + Cookie cookie = new Cookie(CookieAssertionLoginModule.PREFS_COOKIE_NAME, "MyAssertedIdentity"); + request.setCookies(new Cookie[]{cookie}); + trip.setParameter("clearAssertedName", "true"); + trip.execute(); + bean = trip.getActionBean(UserPreferencesActionBean.class); + assertEquals("/Logout.jsp", trip.getDestination()); + + // Verify that the asserted name cookie is gone from the Response + MockHttpServletResponse response = (MockHttpServletResponse)bean.getContext().getResponse(); + Cookie[] cookies = response.getCookies(); + assertEquals(1, cookies.length); + cookie = cookies[0]; + assertEquals(CookieAssertionLoginModule.PREFS_COOKIE_NAME,cookie.getName()); + assertEquals("",cookie.getValue()); + } + + public static Test suite() + { + return new TestSuite( UserPreferencesActionBeanTest.class ); + } + +}
Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,386 @@ +package com.ecyrd.jspwiki.action; + +import java.util.Properties; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.sourceforge.stripes.mock.MockHttpServletRequest; +import net.sourceforge.stripes.mock.MockRoundtrip; +import net.sourceforge.stripes.mock.MockServletContext; +import net.sourceforge.stripes.validation.ValidationErrors; + +import com.ecyrd.jspwiki.TestEngine; +import com.ecyrd.jspwiki.WikiSession; +import com.ecyrd.jspwiki.auth.NoSuchPrincipalException; +import com.ecyrd.jspwiki.auth.user.UserDatabase; +import com.ecyrd.jspwiki.auth.user.UserProfile; + +public class UserProfileActionBeanTest extends TestCase +{ + TestEngine m_engine; + + public void setUp() + { + // Start the WikiEngine, and stash reference + Properties props = new Properties(); + try + { + props.load( TestEngine.findTestProperties() ); + m_engine = new TestEngine( props ); + } + catch (Exception e) + { + throw new RuntimeException("Could not set up TestEngine: " + e.getMessage()); + } + } + + public void testMissingParameters() throws Exception { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserProfileActionBean bean; + ValidationErrors errors; + + // Get the profile, but don't set any parameters; should fail with 4 errors + // profile.fullname + // profile.loginName + // profile.passwordAgain + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 3, errors.size()); + assertTrue( errors.containsKey( "profile.loginName" ) ); + assertTrue( errors.containsKey( "profile.fullname" ) ); + assertTrue( errors.containsKey( "profile.password" ) ); + // Validate that the bean values are set (or not!) as expected + assertEquals(null, bean.getProfile().getLoginName() ); + assertEquals(null, bean.getProfile().getFullname() ); + assertEquals(null, bean.getProfile().getPassword() ); + assertEquals(null, bean.getPasswordAgain() ); + assertEquals(null, bean.getProfile().getEmail() ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + + // Submit just the e-mail param + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + errors = bean.getContext().getValidationErrors(); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 3, errors.size()); + assertTrue( errors.containsKey( "profile.loginName" ) ); + assertTrue( errors.containsKey( "profile.fullname" ) ); + assertTrue( errors.containsKey( "profile.password" ) ); + // Validate that the bean values are set (or not!) as expected + assertEquals(null, bean.getProfile().getLoginName() ); + assertEquals(null, bean.getProfile().getFullname() ); + assertEquals(null, bean.getProfile().getPassword() ); + assertEquals(null, bean.getPasswordAgain() ); + assertEquals("[EMAIL PROTECTED]", bean.getProfile().getEmail() ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + + // Submit just the full name param + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.fullname", "Fred Friendly"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 2, errors.size()); + assertTrue( errors.containsKey( "profile.loginName" ) ); + assertTrue( errors.containsKey( "profile.password" ) ); + // Validate that the bean values are set (or not!) as expected + assertEquals(null, bean.getProfile().getLoginName() ); + assertEquals("Fred Friendly", bean.getProfile().getFullname() ); + assertEquals(null, bean.getProfile().getPassword() ); + assertEquals(null, bean.getPasswordAgain() ); + assertEquals(null, bean.getProfile().getEmail() ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + + // Submit just the login name param + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "fred"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 2, errors.size()); + assertTrue( errors.containsKey( "profile.fullname" ) ); + assertTrue( errors.containsKey( "profile.password" ) ); + // Validate that the bean values are set (or not!) as expected + assertEquals("fred", bean.getProfile().getLoginName() ); + assertEquals(null, bean.getProfile().getFullname() ); + assertEquals(null, bean.getProfile().getPassword() ); + assertEquals(null, bean.getPasswordAgain() ); + assertEquals(null, bean.getProfile().getEmail() ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + + // Submit just the first password field + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.password", "myPassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 3, errors.size()); + assertTrue( errors.containsKey( "profile.loginName" ) ); + assertTrue( errors.containsKey( "profile.fullname" ) ); + assertTrue( errors.containsKey( "profile.password" ) ); + // Validate that the bean values are set (or not!) as expected + assertEquals(null, bean.getProfile().getLoginName() ); + assertEquals(null, bean.getProfile().getFullname() ); + assertEquals("myPassword", bean.getProfile().getPassword() ); + assertEquals(null, bean.getPasswordAgain() ); + assertEquals(null, bean.getProfile().getEmail() ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + + // Submit just the second password field + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("passwordAgain", "myPassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 3, errors.size()); + assertTrue( errors.containsKey( "profile.loginName" ) ); + assertTrue( errors.containsKey( "profile.fullname" ) ); + assertTrue( errors.containsKey( "profile.password" ) ); + // Validate that the bean values are set (or not!) as expected + assertEquals(null, bean.getProfile().getLoginName() ); + assertEquals(null, bean.getProfile().getFullname() ); + assertEquals(null, bean.getProfile().getPassword() ); + assertEquals("myPassword", bean.getPasswordAgain() ); + assertEquals(null, bean.getProfile().getEmail() ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + } + + public void testMismatchedPasswords() throws Exception { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserProfileActionBean bean; + ValidationErrors errors; + + // Set different passwords + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "fred"); + trip.setParameter("profile.fullname", "Fred Friendly"); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "Mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 1, errors.size()); + assertTrue( errors.containsKey( "profile.password") ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + } + + public void testIllegalEmail() throws Exception { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserProfileActionBean bean; + ValidationErrors errors; + + // Set an illegal e-mail address + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "fred"); + trip.setParameter("profile.fullname", "Fred Friendly"); + trip.setParameter("profile.email", "illegalEmail"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 1, errors.size()); + assertTrue( errors.containsKey( "profile.email") ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + } + + public void testSaveProfile() throws Exception + { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserProfileActionBean bean; + ValidationErrors errors; + + // Generate user ID and validate it doesn't exist already + String suffix = String.valueOf(System.currentTimeMillis()); + assertFalse( userExists("user" + suffix)); + + // Create new user + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "user"+suffix); + trip.setParameter("profile.fullname", "Fred Friendly"+suffix); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + errors = bean.getContext().getValidationErrors(); + // Check to make sure no validation errors here... + assertEquals( 0, errors.size()); + assertEquals("/", trip.getDestination() ); + + // Verify user was saved + assertTrue( userExists("user" + suffix)); + + // Verify that wikiname and timestamps were set too + UserDatabase db = m_engine.getUserManager().getUserDatabase(); + UserProfile profile = db.findByLoginName("user"+suffix); + assertEquals("FredFriendly"+suffix, profile.getWikiName()); + assertNotNull(profile.getCreated()); + assertNotNull(profile.getLastModified()); + + // Delete the user we just created + m_engine.getUserManager().getUserDatabase().deleteByLoginName("user"+suffix); + } + + public void testSaveProfileWithCollisions() throws Exception + { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserProfileActionBean bean; + ValidationErrors errors; + + // Create user #1; save; verify it saved ok + String suffix1 = String.valueOf(System.currentTimeMillis()); + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "user"+suffix1); + trip.setParameter("profile.fullname", "Fred Friendly"+suffix1); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure no validation errors here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 0, errors.size()); + assertEquals("/", trip.getDestination() ); + assertTrue( userExists("user" + suffix1)); + + // Create user #2, but same loginName as #1; save; verify it did NOT save + // (because loginnames collided), and redirected back to .Action + String suffix2 = String.valueOf(System.currentTimeMillis()); + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "user"+suffix1); + trip.setParameter("profile.fullname", "Fred Friendly"+suffix2); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 1, errors.size()); + assertTrue( errors.containsKey( "profile.loginName") ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + assertFalse( userExists("user" + suffix2)); + + // Create user #2, but same fullname as #1; save; verify it did NOT save + // (because fullnames collided), and redirected back to .Action + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "user"+suffix2); + trip.setParameter("profile.fullname", "Fred Friendly"+suffix1); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + // Check to make sure all our expected validation errors are here... + errors = bean.getContext().getValidationErrors(); + assertEquals( 1, errors.size()); + assertTrue( errors.containsKey( "profile.fullname") ); + assertEquals(MockRoundtrip.DEFAULT_SOURCE_PAGE, trip.getDestination() ); + assertFalse( userExists("user" + suffix2)); + + // Delete the first user we just created + m_engine.getUserManager().getUserDatabase().deleteByLoginName("user"+suffix1); + } + + public void testSaveProfileAgain() throws Exception + { + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip; + UserProfileActionBean bean; + ValidationErrors errors; + + // Create user; save; verify it saved ok + String suffix = String.valueOf(System.currentTimeMillis()); + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + trip.setParameter("profile.loginName", "user"+suffix); + trip.setParameter("profile.fullname", "Fred Friendly"+suffix); + trip.setParameter("profile.email", "[EMAIL PROTECTED]"); + trip.setParameter("profile.password", "mypassword"); + trip.setParameter("passwordAgain", "mypassword"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + errors = bean.getContext().getValidationErrors(); + assertEquals( 0, errors.size()); + assertEquals("/", trip.getDestination() ); + assertTrue( userExists("user" + suffix)); + + // Create new session and login as new user... + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + MockHttpServletRequest request = trip.getRequest(); + WikiSession wikiSession = WikiSession.getWikiSession(m_engine, request); + boolean login = m_engine.getAuthenticationManager().login(wikiSession, "user"+suffix,"mypassword"); + assertTrue("Could not log in.", login); + + // Make sure the saved profile is loaded when we access prefs page + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + assertEquals("user"+suffix, bean.getProfile().getLoginName()); + assertEquals("Fred Friendly"+suffix, bean.getProfile().getFullname()); + assertEquals("[EMAIL PROTECTED]", bean.getProfile().getEmail()); + + // Now, create another session, and log in again.... + trip = new MockRoundtrip(ctx, "/UserProfile.action"); + request = trip.getRequest(); + wikiSession = WikiSession.getWikiSession(m_engine, request); + login = m_engine.getAuthenticationManager().login(wikiSession, "user"+suffix,"mypassword"); + assertTrue("Could not lot in.", login); + + // Pass new values for the mutable fields (except the password). + // The e-mails, loginname and fullname should all change + trip.addParameter("profile.loginName", "wilma"); + trip.addParameter("profile.fullname", "Wilma Flintstone"); + trip.addParameter("profile.email", "[EMAIL PROTECTED]"); + trip.execute("save"); + bean = trip.getActionBean(UserProfileActionBean.class); + assertEquals("wilma", bean.getProfile().getLoginName()); + assertEquals("Wilma Flintstone", bean.getProfile().getFullname()); + assertEquals("[EMAIL PROTECTED]", bean.getProfile().getEmail()); + assertNull(bean.getProfile().getPassword()); + assertNull(bean.getPasswordAgain()); + + // Delete the user we just created + m_engine.getUserManager().getUserDatabase().deleteByLoginName("wilma"); + } + + public static Test suite() + { + return new TestSuite( UserProfileActionBeanTest.class ); + } + + private boolean userExists(String name) + { + UserDatabase db = m_engine.getUserManager().getUserDatabase(); + boolean found = false; + try + { + db.find(name); + found = true; + } + catch (NoSuchPrincipalException e) + { + // Swallow + } + return found; + } + +} Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/ViewActionBeanTest.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/ViewActionBeanTest.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/ViewActionBeanTest.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/ViewActionBeanTest.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,77 @@ +package com.ecyrd.jspwiki.action; + +import java.util.Properties; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.sourceforge.stripes.mock.MockRoundtrip; +import net.sourceforge.stripes.mock.MockServletContext; + +import com.ecyrd.jspwiki.TestEngine; +import com.ecyrd.jspwiki.WikiPage; + +public class ViewActionBeanTest extends TestCase +{ + TestEngine m_engine; + + public void setUp() + { + // Start the WikiEngine, and stash reference + Properties props = new Properties(); + try + { + props.load( TestEngine.findTestProperties() ); + m_engine = new TestEngine( props ); + } + catch (Exception e) + { + throw new RuntimeException("Could not set up TestEngine: " + e.getMessage()); + } + } + + public void testActionBean() throws Exception { + // Save page Main + m_engine.saveText("Test", "This is a test."); + WikiPage page = m_engine.getPage("Test"); + assertNotNull("Did not save page Test!", page); + + // Set the 'page' request parameter to 'Main'... + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip = new MockRoundtrip(ctx, "/Wiki.action"); + trip.setParameter("page", "Test"); + trip.execute("view"); + + // ...we should automatically see Test bound to the ActionBean (nice!) + ViewActionBean bean = trip.getActionBean(ViewActionBean.class); + assertEquals( page, bean.getPage() ); + + // ...and the destination should be Wiki.jsp (aka /View.action) + assertEquals("/Wiki.action", trip.getDestination() ); + } + + public void testActionBeanNoParameter() throws Exception { + // Save page Main + m_engine.saveText("Main", "This is the main page."); + WikiPage page = m_engine.getPage("Main"); + assertNotNull("Did not save page Main!", page); + + // Execute the request without specifying a page + MockServletContext ctx = m_engine.getServletContext(); + MockRoundtrip trip = new MockRoundtrip(ctx, "/Wiki.action"); + trip.execute("view"); + + // ...we should automatically see Main bound to the ActionBean (nice!) + ViewActionBean bean = trip.getActionBean(ViewActionBean.class); + page = m_engine.getPage("Main"); + assertEquals( page, bean.getPage() ); + + // ...and the destination should be Wiki.jsp (aka /View.action) + assertEquals("/Wiki.action", trip.getDestination() ); + } + + public static Test suite() + { + return new TestSuite( ViewActionBeanTest.class ); + } +} Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/WikiActionBeanFactoryTest.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/WikiActionBeanFactoryTest.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/WikiActionBeanFactoryTest.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/WikiActionBeanFactoryTest.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,160 @@ +/* + * (C) Janne Jalkanen 2005 + * + */ + +package com.ecyrd.jspwiki.action; +import java.util.Properties; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.sourceforge.stripes.action.UrlBinding; +import net.sourceforge.stripes.mock.MockHttpServletRequest; +import net.sourceforge.stripes.mock.MockHttpServletResponse; +import net.sourceforge.stripes.mock.MockHttpSession; +import net.sourceforge.stripes.mock.MockRoundtrip; + +import com.ecyrd.jspwiki.*; +import com.ecyrd.jspwiki.action.*; + +public class WikiActionBeanFactoryTest extends TestCase +{ + TestEngine testEngine; + WikiActionBeanFactory resolver; + + protected void setUp() throws Exception + { + Properties props = new Properties(); + props.load( TestEngine.findTestProperties() ); + props.put( WikiEngine.PROP_MATCHPLURALS, "yes" ); + testEngine = new TestEngine( props ); + resolver = testEngine.getWikiActionBeanFactory(); + testEngine.saveText( "SinglePage", "This is a test." ); + testEngine.saveText( "PluralPages", "This is a test." ); + } + + protected void tearDown() throws Exception + { + testEngine.deletePage( "TestPage" ); + } + + public void testGetUrlPatterns() + { + // If we look for action with "edit" request context, we get EDIT action + assertEquals( WikiContext.EDIT, EditActionBean.class.getAnnotation(WikiRequestContext.class).value() ); + + // Ditto for prefs context + assertEquals( WikiContext.PREFS, UserPreferencesActionBean.class.getAnnotation(WikiRequestContext.class).value() ); + + // Ditto for group view context + assertEquals( WikiContext.VIEW_GROUP, GroupActionBean.class.getAnnotation(WikiRequestContext.class).value() ); + } + + public void testActionBeansNoParams() throws WikiException + { + WikiActionBean bean; + MockRoundtrip trip = testEngine.guestTrip( ViewActionBean.class ); + MockHttpServletRequest request = trip.getRequest(); + MockHttpServletResponse response = trip.getResponse(); + MockHttpSession session = (MockHttpSession)request.getSession(); + + // Passing an EDIT request with no explicit page params means the EDIT action + bean = resolver.newActionBean( request, response, EditActionBean.class ); + assertEquals( WikiContext.EDIT, bean.getRequestContext() ); + assertNull( ((WikiContext)bean).getPage() ); + + // Ditto for prefs context + bean = resolver.newActionBean( request, response, UserPreferencesActionBean.class ); + assertEquals( WikiContext.PREFS, bean.getRequestContext() ); + + // Ditto for group view context + bean = resolver.newActionBean( request, response, GroupActionBean.class ); + assertEquals( WikiContext.VIEW_GROUP, bean.getRequestContext() ); + assertNull( ((GroupContext)bean).getGroup() ); + + // Request for "UserPreference.jsp" should resolve to PREFS action + request = new MockHttpServletRequest( testEngine.getServletContext().getServletContextName(), "/UserPreferences.jsp"); + request.setSession( session ); + bean = resolver.newActionBean( request, response, UserPreferencesActionBean.class ); + assertEquals( WikiContext.PREFS, bean.getRequestContext() ); + + // We don't care about JSPs not mapped to actions, because the bean we get only depends on the class we pass + // FIXME: this won't work because WikiActionBeanResolver doesn't keep a cache of URLBindings + request = new MockHttpServletRequest( testEngine.getServletContext().getServletContextName(), "/NonExistent.jsp"); + request.setSession( session ); + bean = resolver.newActionBean( request, response, EditActionBean.class ); + assertEquals( WikiContext.EDIT, bean.getRequestContext() ); + assertNull( ((WikiContext)bean).getPage() ); + } + + public void testActionBeansWithParams() throws Exception + { + WikiActionBean bean; + WikiPage page = testEngine.getPage( "SinglePage" ); + MockRoundtrip trip = testEngine.guestTrip( ViewActionBean.class ); + MockHttpServletRequest request = trip.getRequest(); + MockHttpServletResponse response = trip.getResponse(); + MockHttpSession session = (MockHttpSession)request.getSession(); + + // Passing an EDIT request with page param yields an ActionBean with a non-null page property + request = new MockHttpServletRequest( testEngine.getServletContext().getServletContextName(), "/Edit.jsp"); + request.setSession( session ); + request.getParameterMap().put( "page", new String[]{"SinglePage"} ); + bean = resolver.newActionBean( request, response, EditActionBean.class ); + assertEquals( WikiContext.EDIT, bean.getRequestContext() ); + assertEquals( page, ((WikiContext)bean).getPage()); + + // Passing a VIEW request with page=FindPage yields an ordinary page name, not a special page or JSP + // FIXME: this won't work because WikiActionBeanResolver doesn't keep a cache of URLBindings + request = new MockHttpServletRequest( testEngine.getServletContext().getServletContextName(), "/Wiki.jsp"); + request.setSession( session ); + request.getParameterMap().put( "page", new String[]{"FindPage"} ); + bean = resolver.newActionBean( request, response, ViewActionBean.class ); + assertEquals( WikiContext.VIEW, bean.getRequestContext() ); + + // Passing a VIEW_GROUP request with group="Art" gets a ViewGroupActionBean + request = new MockHttpServletRequest( testEngine.getServletContext().getServletContextName(), "/Wiki.jsp"); + request.setSession( session ); + request.getParameterMap().put( "group", new String[]{"Art"} ); + bean = resolver.newActionBean( request, response, GroupActionBean.class ); + assertEquals( WikiContext.VIEW_GROUP, bean.getRequestContext() ); + assertEquals( "/Group.action", bean.getClass().getAnnotation(UrlBinding.class).value() ); + } + + public void testFinalPageName() throws Exception + { + String page; + page = resolver.getFinalPageName( "SinglePage" ); + assertEquals( "SinglePage", page ); + page = resolver.getFinalPageName( "SinglePages" ); + assertEquals( "SinglePage", page ); + + page = resolver.getFinalPageName( "PluralPages" ); + assertEquals( "PluralPages", page ); + page = resolver.getFinalPageName( "PluralPage" ); + assertEquals( "PluralPages", page ); + + page = resolver.getFinalPageName( "NonExistentPage" ); + assertNull( page ); + } + + public void testSpecialPageReference() + { + String url; + url = resolver.getSpecialPageReference( "RecentChanges" ); + assertEquals( "RecentChanges.jsp", url ); + + url = resolver.getSpecialPageReference( "FindPage" ); + assertEquals( "Search.jsp", url ); + + // UserPrefs doesn't exist in our test properties + url = resolver.getSpecialPageReference( "UserPrefs" ); + assertNull( url ); + } + + public static Test suite() + { + return new TestSuite( WikiActionBeanFactoryTest.class ); + } +} Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AllTests.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AllTests.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AllTests.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AllTests.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,21 @@ + +package com.ecyrd.jspwiki.attachment; + +import junit.framework.*; + +public class AllTests extends TestCase +{ + public AllTests( String s ) + { + super( s ); + } + + public static Test suite() + { + TestSuite suite = new TestSuite("Attachment package"); + + suite.addTest( AttachmentManagerTest.suite() ); + + return suite; + } +} Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AttachmentManagerTest.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AttachmentManagerTest.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AttachmentManagerTest.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/attachment/AttachmentManagerTest.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,327 @@ + +package com.ecyrd.jspwiki.attachment; + +import junit.framework.*; +import java.io.*; +import java.util.*; + +import com.ecyrd.jspwiki.*; + +public class AttachmentManagerTest extends TestCase +{ + public static final String NAME1 = "TestPage"; + public static final String NAMEU = "TestPage\u00e6"; + + Properties props = new Properties(); + + TestEngine m_engine; + AttachmentManager m_manager; + + static String c_fileContents = "ABCDEFGHIJKLMNOPQRSTUVWxyz"; + + public AttachmentManagerTest( String s ) + { + super( s ); + } + + public void setUp() + throws Exception + { + props.load( TestEngine.findTestProperties() ); + + m_engine = new TestEngine(props); + m_manager = m_engine.getAttachmentManager(); + + m_engine.saveText( NAME1, "Foobar" ); + m_engine.saveText( NAMEU, "Foobar" ); + } + + private File makeAttachmentFile() + throws Exception + { + File tmpFile = File.createTempFile("test","txt"); + tmpFile.deleteOnExit(); + + FileWriter out = new FileWriter( tmpFile ); + + FileUtil.copyContents( new StringReader( c_fileContents ), out ); + + out.close(); + + return tmpFile; + } + + public void tearDown() + { + TestEngine.deleteTestPage( NAME1 ); + TestEngine.deleteTestPage( NAMEU ); + + m_engine.deleteAttachments(NAME1); + m_engine.deleteAttachments(NAMEU); + + TestEngine.emptyWorkDir(); + } + + public void testEnabled() + { + assertTrue( "not enabled", m_manager.attachmentsEnabled() ); + } + + public void testSimpleStore() + throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1.txt" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + Attachment att2 = m_manager.getAttachmentInfo( m_engine.getWikiActionBeanFactory().newViewActionBean( + new WikiPage(m_engine, NAME1)), + "test1.txt" ); + + assertNotNull( "attachment disappeared", att2 ); + assertEquals( "name", att.getName(), att2.getName() ); + assertEquals( "author", att.getAuthor(), att2.getAuthor() ); + assertEquals( "size", c_fileContents.length(), att2.getSize() ); + + InputStream in = m_manager.getAttachmentStream( att2 ); + + assertNotNull( "stream", in ); + + StringWriter sout = new StringWriter(); + FileUtil.copyContents( new InputStreamReader(in), sout ); + + in.close(); + sout.close(); + + assertEquals( "contents", c_fileContents, sout.toString() ); + } + + public void testSimpleStoreSpace() + throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test file.txt" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + Attachment att2 = m_manager.getAttachmentInfo( m_engine.getWikiActionBeanFactory().newViewActionBean( + new WikiPage(m_engine, NAME1)), + "test file.txt" ); + + assertNotNull( "attachment disappeared", att2 ); + assertEquals( "name", att.getName(), att2.getName() ); + assertEquals( "author", att.getAuthor(), att2.getAuthor() ); + assertEquals( "size", c_fileContents.length(), att2.getSize() ); + + InputStream in = m_manager.getAttachmentStream( att2 ); + + assertNotNull( "stream", in ); + + StringWriter sout = new StringWriter(); + FileUtil.copyContents( new InputStreamReader(in), sout ); + + in.close(); + sout.close(); + + assertEquals( "contents", c_fileContents, sout.toString() ); + } + + public void testSimpleStoreByVersion() + throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1.txt" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + Attachment att2 = m_manager.getAttachmentInfo( m_engine.getWikiActionBeanFactory().newViewActionBean( + new WikiPage(m_engine, NAME1)), + "test1.txt", 1 ); + + assertNotNull( "attachment disappeared", att2 ); + assertEquals( "version", 1, att2.getVersion() ); + assertEquals( "name", att.getName(), att2.getName() ); + assertEquals( "author", att.getAuthor(), att2.getAuthor() ); + assertEquals( "size", c_fileContents.length(), att2.getSize() ); + + InputStream in = m_manager.getAttachmentStream( att2 ); + + assertNotNull( "stream", in ); + + StringWriter sout = new StringWriter(); + FileUtil.copyContents( new InputStreamReader(in), sout ); + + in.close(); + sout.close(); + + assertEquals( "contents", c_fileContents, sout.toString() ); + } + + public void testMultipleStore() + throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1.txt" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + att.setAuthor( "FooBar" ); + m_manager.storeAttachment( att, makeAttachmentFile() ); + + Attachment att2 = m_manager.getAttachmentInfo( m_engine.getWikiActionBeanFactory().newViewActionBean( + new WikiPage(m_engine, NAME1)), + "test1.txt" ); + + assertNotNull( "attachment disappeared", att2 ); + assertEquals( "name", att.getName(), att2.getName() ); + assertEquals( "author", att.getAuthor(), att2.getAuthor() ); + assertEquals( "version", 2, att2.getVersion() ); + + InputStream in = m_manager.getAttachmentStream( att2 ); + + assertNotNull( "stream", in ); + + StringWriter sout = new StringWriter(); + FileUtil.copyContents( new InputStreamReader(in), sout ); + + in.close(); + sout.close(); + + assertEquals( "contents", c_fileContents, sout.toString() ); + + + // + // Check that first author did not disappear + // + + Attachment att3 = m_manager.getAttachmentInfo( m_engine.getWikiActionBeanFactory().newViewActionBean( + new WikiPage(m_engine, NAME1)), + "test1.txt", + 1 ); + assertEquals( "version of v1", 1, att3.getVersion() ); + assertEquals( "name of v1", "FirstPost", att3.getAuthor() ); + } + + public void testListAttachments() + throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1.txt" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + Collection c = m_manager.listAttachments( new WikiPage(m_engine, NAME1) ); + + assertEquals( "Length", 1, c.size() ); + + Attachment att2 = (Attachment) c.toArray()[0]; + + assertEquals( "name", att.getName(), att2.getName() ); + assertEquals( "author", att.getAuthor(), att2.getAuthor() ); + } + + public void testSimpleStoreWithoutExt() throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + Attachment att2 = m_manager.getAttachmentInfo( m_engine.getWikiActionBeanFactory().newViewActionBean( + new WikiPage(m_engine, NAME1)), + "test1" ); + + assertNotNull( "attachment disappeared", att2 ); + assertEquals( "name", att.getName(), att2.getName() ); + assertEquals( "author", "FirstPost", att2.getAuthor() ); + assertEquals( "size", c_fileContents.length(), att2.getSize() ); + assertEquals( "version", 1, att2.getVersion() ); + + InputStream in = m_manager.getAttachmentStream( att2 ); + + assertNotNull( "stream", in ); + + StringWriter sout = new StringWriter(); + FileUtil.copyContents( new InputStreamReader(in), sout ); + + in.close(); + sout.close(); + + assertEquals( "contents", c_fileContents, sout.toString() ); + } + + + public void testExists() throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + assertTrue( "attachment disappeared", + m_engine.pageExists( NAME1+"/test1" ) ); + } + + public void testExists2() throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test1.bin" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + assertTrue( "attachment disappeared", + m_engine.pageExists( att.getName() ) ); + } + + public void testExistsSpace() throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test file.bin" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + assertTrue( "attachment disappeared", + m_engine.pageExists( NAME1+"/test file.bin" ) ); + } + + public void testExistsUTF1() throws Exception + { + Attachment att = new Attachment( m_engine, NAME1, "test\u00e4.bin" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + assertTrue( "attachment disappeared", + m_engine.pageExists( att.getName() ) ); + } + + public void testExistsUTF2() throws Exception + { + Attachment att = new Attachment( m_engine, NAMEU, "test\u00e4.bin" ); + + att.setAuthor( "FirstPost" ); + + m_manager.storeAttachment( att, makeAttachmentFile() ); + + assertTrue( "attachment disappeared", + m_engine.pageExists( att.getName() ) ); + } + + public static Test suite() + { + return new TestSuite( AttachmentManagerTest.class ); + } + + +} Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AllTests.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AllTests.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AllTests.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AllTests.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,36 @@ + +package com.ecyrd.jspwiki.auth; + +import com.ecyrd.jspwiki.TextUtil; + +import junit.framework.*; + +public class AllTests extends TestCase +{ + public AllTests( String s ) + { + super( s ); + } + + public static Test suite() + { + TestSuite suite = new TestSuite("AAA package tests"); + + String runTests = System.getProperty( "jspwiki.tests.auth", "false" ); + + if( TextUtil.isPositive(runTests) ) + { + suite.addTest( AuthenticationManagerTest.suite() ); + suite.addTest( AuthorizationManagerTest.suite() ); + suite.addTest( GroupManagerTest.suite() ); + suite.addTest( com.ecyrd.jspwiki.auth.acl.AllTests.suite() ); + suite.addTest( com.ecyrd.jspwiki.auth.authorize.AllTests.suite() ); + suite.addTest( com.ecyrd.jspwiki.auth.login.AllTests.suite() ); + suite.addTest( com.ecyrd.jspwiki.auth.permissions.AllTests.suite() ); + suite.addTest( com.ecyrd.jspwiki.auth.user.AllTests.suite() ); + suite.addTestSuite( com.ecyrd.jspwiki.auth.UserManagerTest.class ); + } + + return suite; + } +} Added: incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AuthenticationManagerTest.java URL: http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AuthenticationManagerTest.java?rev=627271&view=auto ============================================================================== --- incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AuthenticationManagerTest.java (added) +++ incubator/jspwiki/branches/JSPWIKI_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/auth/AuthenticationManagerTest.java Tue Feb 12 22:24:02 2008 @@ -0,0 +1,134 @@ +package com.ecyrd.jspwiki.auth; + +import java.security.Principal; +import java.util.Properties; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.ecyrd.jspwiki.TestEngine; +import com.ecyrd.jspwiki.WikiSession; +import com.ecyrd.jspwiki.WikiSessionTest; +import com.ecyrd.jspwiki.auth.authorize.Group; +import com.ecyrd.jspwiki.auth.authorize.GroupManager; +import com.ecyrd.jspwiki.auth.authorize.Role; + +/** + * Tests the AuthorizationManager class. + * @author Janne Jalkanen + */ +public class AuthenticationManagerTest extends TestCase +{ + private AuthenticationManager m_auth; + + private TestEngine m_engine; + + private GroupManager m_groupMgr; + + private WikiSession m_session; + + public AuthenticationManagerTest( String s ) + { + super( s ); + } + + public void setUp() throws Exception + { + Properties props = new Properties(); + props.load( TestEngine.findTestProperties() ); + m_engine = new TestEngine( props ); + m_auth = m_engine.getAuthenticationManager(); + m_groupMgr = m_engine.getGroupManager(); + m_session = WikiSessionTest.adminSession( m_engine ); + } + + public void testIsUserPrincipal() + { + assertTrue( AuthenticationManager.isUserPrincipal( new WikiPrincipal( "Foo" ) ) ); + assertFalse( AuthenticationManager.isUserPrincipal( new GroupPrincipal( "Group1" ) ) ); + assertFalse( AuthenticationManager.isUserPrincipal( new Role( "Role1" ) ) ); + assertFalse( AuthenticationManager.isUserPrincipal( Role.ANONYMOUS ) ); + } + + public void testLoginCustom() throws Exception + { + WikiSession session = WikiSessionTest.authenticatedSession( m_engine, Users.JANNE, Users.JANNE_PASS ); + assertTrue( session.hasPrincipal( Role.ALL ) ); + assertTrue( session.hasPrincipal( Role.AUTHENTICATED ) ); + assertTrue( session.hasPrincipal( new WikiPrincipal( Users.JANNE, WikiPrincipal.LOGIN_NAME ) ) ); + assertTrue( session.hasPrincipal( new WikiPrincipal( "JanneJalkanen", WikiPrincipal.WIKI_NAME ) ) ); + assertTrue( session.hasPrincipal( new WikiPrincipal( "Janne Jalkanen", WikiPrincipal.FULL_NAME ) ) ); + } + + public void testLoginCustomWithGroup() throws Exception + { + // Flush any pre-existing groups (left over from previous failures, perhaps) + try + { + m_groupMgr.removeGroup( "Test1" ); + m_groupMgr.removeGroup( "Test2" ); + } + catch ( NoSuchPrincipalException e ) + { + + } + + // Log in 'janne' and verify there are 5 principals in the subject + // (ALL, AUTHENTICATED, login, fullname, wikiname Principals) + WikiSession session = WikiSession.guestSession( m_engine ); + m_auth.login( session, Users.JANNE, Users.JANNE_PASS ); + assertEquals( 3, session.getPrincipals().length ); + assertEquals( 2, session.getRoles().length ); + assertTrue( session.hasPrincipal( new WikiPrincipal( "JanneJalkanen", WikiPrincipal.WIKI_NAME ) ) ); + + // Listen for any manager group-add events + GroupManager manager = m_engine.getGroupManager(); + SecurityEventTrap trap = new SecurityEventTrap(); + manager.addWikiEventListener( trap ); + + // Create two groups; one with Janne in it, and one without + Group groupTest1 = m_groupMgr.parseGroup( "Test1", "JanneJalkanen \n Bob \n Charlie", true ); + m_groupMgr.setGroup( m_session, groupTest1 ); + groupTest1 = m_groupMgr.getGroup( "Test1" ); + Principal principalTest1 = groupTest1.getPrincipal(); + + Group groupTest2 = m_groupMgr.parseGroup( "Test2", "Alice \n Bob \n Charlie", true ); + m_groupMgr.setGroup( m_session, groupTest2 ); + groupTest2 = m_groupMgr.getGroup( "Test2" ); + Principal principalTest2 = groupTest2.getPrincipal(); + + // We should see two security events (one for each group create) + // We should also see a GroupPrincipal for group Test1, but not Test2 + assertEquals( 2, trap.events().length ); + assertTrue( session.hasPrincipal( principalTest1 ) ); + assertFalse( session.hasPrincipal( principalTest2 ) ); + + // If we remove Test1, the GroupPrincipal should disappear + m_groupMgr.removeGroup( "Test1" ); + assertFalse( session.hasPrincipal( principalTest1 ) ); + assertFalse( session.hasPrincipal( principalTest2 ) ); + + // Now, add 'JanneJalkanen' to Test2 group manually; we should see the GroupPrincipal + groupTest2.add( new WikiPrincipal( "JanneJalkanen" ) ); + m_groupMgr.setGroup( session, groupTest2 ); + assertFalse( session.hasPrincipal( principalTest1 ) ); + assertTrue( session.hasPrincipal( principalTest2 ) ); + + // Remove 'JanneJalkenen' manually; the GroupPrincipal should disappear + groupTest2.remove( new WikiPrincipal( "JanneJalkanen" ) ); + m_groupMgr.setGroup( session, groupTest2 ); + assertFalse( session.hasPrincipal( principalTest1 ) ); + assertFalse( session.hasPrincipal( principalTest2 ) ); + + // Clean up + m_groupMgr.removeGroup( "Test2" ); + } + + public static Test suite() + { + TestSuite suite = new TestSuite("Authentication Manager test"); + suite.addTestSuite( AuthenticationManagerTest.class ); + return suite; + } +} \ No newline at end of file
