Author: ajaquith
Date: Wed Aug 13 21:39:30 2008
New Revision: 685767

URL: http://svn.apache.org/viewvc?rev=685767&view=rev
Log:
Added LoginActionBeanTest. Logins/logouts via beans work.

Added:
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/LoginActionBeanTest.java
Modified:
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/WikiSession.java
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/action/LoginActionBean.java
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/TestEngine.java
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/AllTests.java
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/HandlerInfoTest.java
    
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java

Modified: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/WikiSession.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/WikiSession.java?rev=685767&r1=685766&r2=685767&view=diff
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/WikiSession.java
 (original)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/WikiSession.java
 Wed Aug 13 21:39:30 2008
@@ -610,6 +610,8 @@
         m_subject.getPrincipals().add( WikiPrincipal.GUEST );
         m_subject.getPrincipals().add( Role.ANONYMOUS );
         m_subject.getPrincipals().add( Role.ALL );
+        m_userPrincipal = WikiPrincipal.GUEST;
+        m_loginPrincipal = WikiPrincipal.GUEST;
     }
 
     /**

Modified: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/action/LoginActionBean.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/action/LoginActionBean.java?rev=685767&r1=685766&r2=685767&view=diff
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/action/LoginActionBean.java
 (original)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/src/com/ecyrd/jspwiki/action/LoginActionBean.java
 Wed Aug 13 21:39:30 2008
@@ -3,15 +3,19 @@
 import java.security.Principal;
 import java.util.ResourceBundle;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
 import net.sourceforge.stripes.action.*;
 import net.sourceforge.stripes.util.UrlBuilder;
 import net.sourceforge.stripes.validation.SimpleError;
+import net.sourceforge.stripes.validation.Validate;
 import net.sourceforge.stripes.validation.ValidationErrors;
 
 import org.apache.log4j.Logger;
 
+import com.ecyrd.jspwiki.WikiEngine;
 import com.ecyrd.jspwiki.WikiSession;
 import com.ecyrd.jspwiki.auth.WikiSecurityException;
 import com.ecyrd.jspwiki.auth.login.CookieAssertionLoginModule;
@@ -43,14 +47,12 @@
             CookieAuthenticationLoginModule.setLoginCookie( getEngine(), 
getContext().getResponse(), principal.getName() );
         }
 
-        Resolution r = null;
+        UrlBuilder builder = new UrlBuilder( getContext().getLocale(), 
ViewActionBean.class, false );
         if( pageName != null )
         {
-            UrlBuilder builder = new UrlBuilder( getContext().getLocale(), 
ViewActionBean.class, false );
             builder.addParameter( "page", pageName );
-            r = new RedirectResolution( builder.toString() );
         }
-        return r;
+        return new RedirectResolution( builder.toString() );
     }
 
     private String m_username = null;
@@ -139,9 +141,24 @@
     @WikiRequestContext( "logout" )
     public Resolution logout()
     {
-        return null;
+        WikiEngine engine = getEngine();
+        HttpServletRequest request = getContext().getRequest();
+        HttpServletResponse response = getContext().getResponse();
+        engine.getAuthenticationManager().logout( request );
+        
+        // Clear the asserted name cookie
+        CookieAssertionLoginModule.clearUserCookie( response );
+
+        // Delete the authentication cookie
+        if ( engine.getAuthenticationManager().allowsCookieAuthentication() )
+        {
+            CookieAuthenticationLoginModule.clearLoginCookie( engine, request, 
response );
+        }
+        
+        return new RedirectResolution( ViewActionBean.class );
     }
 
+    @Validate( required = true, on = "login", minlength = 1, maxlength = 128 )
     public void setJ_password( String password )
     {
         m_password = password;
@@ -152,6 +169,7 @@
         m_remember = remember;
     }
 
+    @Validate( required = true, on = "login", minlength = 1, maxlength = 128 )
     public void setJ_username( String username )
     {
         m_username = username;
@@ -165,7 +183,7 @@
 
     /**
      * Default handler that executes when the login page is viewed. It checks 
to
-     * see if the user is already authenticated, and if so, sents a "forbidden"
+     * see if the user is already authenticated, and if so, sends a "forbidden"
      * message.
      * 
      * @return the resolution
@@ -182,10 +200,6 @@
         {
             // Set cookies as needed and redirect
             r = saveCookiesAndRedirect( m_redirect, m_remember );
-            if( r == null )
-            {
-                r = new RedirectResolution( ViewActionBean.class );
-            }
         }
 
         if( getEngine().getAuthenticationManager().isContainerAuthenticated() )

Modified: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/TestEngine.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/TestEngine.java?rev=685767&r1=685766&r2=685767&view=diff
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/TestEngine.java
 (original)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/TestEngine.java
 Wed Aug 13 21:39:30 2008
@@ -421,7 +421,7 @@
         }
         MockRoundtrip trip = new MockRoundtrip( servletContext, beanClass );
         WikiSession session = WikiSession.getWikiSession( this, 
trip.getRequest() );
-        this.getAuthenticationManager().login( session, Users.ADMIN, 
Users.ADMIN_PASS );
+        this.getAuthenticationManager().login( session, user, password );
         return trip;
     }
     

Modified: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/AllTests.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/AllTests.java?rev=685767&r1=685766&r2=685767&view=diff
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/AllTests.java
 (original)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/AllTests.java
 Wed Aug 13 21:39:30 2008
@@ -16,6 +16,7 @@
 
         suite.addTest( GroupActionBeanTest.suite() );
         suite.addTest( HandlerInfoTest.suite() );
+        suite.addTest( LoginActionBeanTest.suite() );
         suite.addTest( RenameActionBeanTest.suite() );
         suite.addTest( UserPreferencesActionBeanTest.suite() );
         suite.addTest( UserProfileActionBeanTest.suite() );

Modified: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/HandlerInfoTest.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/HandlerInfoTest.java?rev=685767&r1=685766&r2=685767&view=diff
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/HandlerInfoTest.java
 (original)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/HandlerInfoTest.java
 Wed Aug 13 21:39:30 2008
@@ -9,7 +9,6 @@
 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.auth.permissions.GroupPermission;
@@ -86,7 +85,6 @@
 
     public void testEvaluatedPermissionAnnotation() throws Exception
     {
-        MockServletContext ctx = (MockServletContext) 
m_engine.getServletContext();
         MockRoundtrip trip;
         GroupActionBean bean;
         Method method;

Added: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/LoginActionBeanTest.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/LoginActionBeanTest.java?rev=685767&view=auto
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/LoginActionBeanTest.java
 (added)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/LoginActionBeanTest.java
 Wed Aug 13 21:39:30 2008
@@ -0,0 +1,198 @@
+package com.ecyrd.jspwiki.action;
+
+import java.util.Properties;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import net.sourceforge.stripes.mock.MockRoundtrip;
+import net.sourceforge.stripes.validation.ValidationErrors;
+
+import com.ecyrd.jspwiki.TestEngine;
+import com.ecyrd.jspwiki.WikiSession;
+import com.ecyrd.jspwiki.auth.SessionMonitor;
+import com.ecyrd.jspwiki.auth.Users;
+import com.ecyrd.jspwiki.auth.login.CookieAssertionLoginModule;
+
+public class LoginActionBeanTest extends TestCase
+{
+    public static Test suite()
+    {
+        return new TestSuite( LoginActionBeanTest.class );
+    }
+
+    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 testLogin() throws Exception
+    {
+        MockRoundtrip trip;
+        LoginActionBean bean;
+        ValidationErrors errors;
+
+        // Verify that the initial user is anonymous
+        trip = m_engine.guestTrip( "/Login.jsp" );
+        HttpServletRequest request = trip.getRequest();
+        WikiSession wikiSession = SessionMonitor.getInstance( m_engine ).find( 
request.getSession() );
+        assertNotSame( Users.JANNE, wikiSession.getLoginPrincipal().getName() 
);
+
+        // Log in
+        trip.setParameter( "j_username", Users.JANNE );
+        trip.setParameter( "j_password", Users.JANNE_PASS );
+        trip.execute( "login" );
+
+        // Verify we logged in correctly
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 0, errors.size() );
+        assertEquals( Users.JANNE, wikiSession.getLoginPrincipal().getName() );
+        assertEquals( "/Wiki.jsp", trip.getDestination() );
+
+        // Should be just one cookie (the JSPWiki asserted name cookie)
+        Cookie[] cookies = trip.getResponse().getCookies();
+        assertEquals( 1, cookies.length );
+        assertEquals( CookieAssertionLoginModule.PREFS_COOKIE_NAME, 
cookies[0].getName() );
+        assertEquals( "Janne+Jalkanen", cookies[0].getValue() );
+    }
+
+    public void testLoginNoParams() throws Exception
+    {
+        MockRoundtrip trip;
+        LoginActionBean bean;
+        ValidationErrors errors;
+
+        // Verify that the initial user is anonymous
+        trip = m_engine.guestTrip( "/Login.jsp" );
+        HttpServletRequest request = trip.getRequest();
+        WikiSession wikiSession = SessionMonitor.getInstance( m_engine ).find( 
request.getSession() );
+        assertNotSame( Users.JANNE, wikiSession.getLoginPrincipal().getName() 
);
+
+        // Log in; should see two errors (because we did not specify a username
+        // or password)
+        trip.execute( "login" );
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 2, errors.size() );
+
+        // Log in again with just a password; should see one error
+        trip = m_engine.guestTrip( "/Login.jsp" );
+        trip.setParameter( "j_password", Users.JANNE_PASS );
+        trip.execute( "login" );
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 1, errors.size() );
+
+        // Log in again with just a username; should see one error
+        trip = m_engine.guestTrip( "/Login.jsp" );
+        trip.setParameter( "j_username", Users.JANNE );
+        trip.execute( "login" );
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 1, errors.size() );
+    }
+
+    public void testLoginRedirect() throws Exception
+    {
+        MockRoundtrip trip;
+        LoginActionBean bean;
+        ValidationErrors errors;
+
+        // Verify that the initial user is anonymous
+        trip = m_engine.guestTrip( "/Login.jsp" );
+        HttpServletRequest request = trip.getRequest();
+        WikiSession wikiSession = SessionMonitor.getInstance( m_engine ).find( 
request.getSession() );
+        assertNotSame( Users.JANNE, wikiSession.getLoginPrincipal().getName() 
);
+
+        // Log in
+        trip.setParameter( "j_username", Users.JANNE );
+        trip.setParameter( "j_password", Users.JANNE_PASS );
+        trip.setParameter( "redirect", "Foo" );
+        trip.execute( "login" );
+
+        // Verify we logged in correctly
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 0, errors.size() );
+        assertEquals( Users.JANNE, wikiSession.getLoginPrincipal().getName() );
+        assertEquals( "/Wiki.jsp?page=Foo", trip.getDestination() );
+    }
+
+    public void testLoginRememberMe() throws Exception
+    {
+        MockRoundtrip trip;
+        LoginActionBean bean;
+        ValidationErrors errors;
+
+        // Verify that the initial user is anonymous
+        trip = m_engine.guestTrip( "/Login.jsp" );
+        HttpServletRequest request = trip.getRequest();
+        WikiSession wikiSession = SessionMonitor.getInstance( m_engine ).find( 
request.getSession() );
+        assertNotSame( Users.JANNE, wikiSession.getLoginPrincipal().getName() 
);
+
+        // Log in
+        trip.setParameter( "j_username", Users.JANNE );
+        trip.setParameter( "j_password", Users.JANNE_PASS );
+        trip.setParameter( "j_remember", "true" );
+        trip.execute( "login" );
+
+        // Verify we logged in correctly
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 0, errors.size() );
+        assertEquals( Users.JANNE, wikiSession.getLoginPrincipal().getName() );
+        assertEquals( "/Wiki.jsp", trip.getDestination() );
+
+        // Should be two cookies (the JSPWiki asserted name cookie plus the
+        // Remember Me? cookie)
+        Cookie[] cookies = trip.getResponse().getCookies();
+        assertEquals( 2, cookies.length );
+    }
+
+    public void testLogout() throws Exception
+    {
+        MockRoundtrip trip;
+        LoginActionBean bean;
+        ValidationErrors errors;
+
+        // Start with an authenticated user
+        trip = m_engine.authenticatedTrip( Users.JANNE, Users.JANNE_PASS, 
LoginActionBean.class );
+        HttpServletRequest request = trip.getRequest();
+        WikiSession wikiSession = SessionMonitor.getInstance( m_engine ).find( 
request.getSession() );
+        assertEquals( Users.JANNE, wikiSession.getLoginPrincipal().getName() );
+
+        // Log out
+        trip.execute( "logout" );
+
+        // Verify we logged out correctly
+        bean = trip.getActionBean( LoginActionBean.class );
+        errors = bean.getContext().getValidationErrors();
+        assertEquals( 0, errors.size() );
+        assertNotSame( Users.JANNE, wikiSession.getLoginPrincipal().getName() 
);
+        assertEquals( "/Wiki.jsp", trip.getDestination() );
+
+        // Should be just one cookie (the JSPWiki asserted name cookie), and it
+        // should be empty
+        Cookie[] cookies = trip.getResponse().getCookies();
+        assertEquals( 1, cookies.length );
+        assertEquals( CookieAssertionLoginModule.PREFS_COOKIE_NAME, 
cookies[0].getName() );
+        assertEquals( "", cookies[0].getValue() );
+    }
+
+}

Modified: 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java
URL: 
http://svn.apache.org/viewvc/incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java?rev=685767&r1=685766&r2=685767&view=diff
==============================================================================
--- 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java
 (original)
+++ 
incubator/jspwiki/branches/JSPWIKI_2_9_STRIPES_BRANCH/tests/com/ecyrd/jspwiki/action/UserProfileActionBeanTest.java
 Wed Aug 13 21:39:30 2008
@@ -7,7 +7,6 @@
 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;


Reply via email to