[gatein-commits] gatein SVN: r8260 - in components/wci/trunk: test/core/src/main/resources/org/gatein/portal/test/web/endpoint and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Dec 20 04:06:11 EST 2011


Author: mposolda
Date: 2011-12-20 04:06:07 -0500 (Tue, 20 Dec 2011)
New Revision: 8260

Added:
   components/wci/trunk/test/core/src/main/java/org/gatein/wci/endpoint/LoginControllerTestCase.java
Modified:
   components/wci/trunk/test/core/src/main/resources/org/gatein/portal/test/web/endpoint/server-beans.xml
   components/wci/trunk/wci/src/main/java/org/gatein/wci/security/WCILoginController.java
Log:
GTNWCI-28 Protectet methods in WCILoginController for dealing with credentials

Added: components/wci/trunk/test/core/src/main/java/org/gatein/wci/endpoint/LoginControllerTestCase.java
===================================================================
--- components/wci/trunk/test/core/src/main/java/org/gatein/wci/endpoint/LoginControllerTestCase.java	                        (rev 0)
+++ components/wci/trunk/test/core/src/main/java/org/gatein/wci/endpoint/LoginControllerTestCase.java	2011-12-20 09:06:07 UTC (rev 8260)
@@ -0,0 +1,133 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2011, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.gatein.wci.endpoint;
+
+import org.gatein.wci.TestServlet;
+import org.gatein.wci.WebRequest;
+import org.gatein.wci.WebResponse;
+import org.gatein.wci.security.Credentials;
+import org.gatein.wci.security.WCILoginController;
+import org.jboss.unit.Failure;
+import org.jboss.unit.driver.DriverCommand;
+import org.jboss.unit.driver.DriverResponse;
+import org.jboss.unit.driver.response.EndTestResponse;
+import org.jboss.unit.driver.response.FailureResponse;
+import org.jboss.unit.remote.driver.handler.http.response.InvokeGetResponse;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.jboss.unit.api.Assert.assertEquals;
+import static org.jboss.unit.api.Assert.assertNotNull;
+import static org.jboss.unit.api.Assert.assertNull;
+
+/**
+ * @author <a href="mailto:mposolda at redhat.com">Marek Posolda</a>
+ */
+public class LoginControllerTestCase extends EndPointTestCase
+{
+   private final TestWCILoginController wciLoginController = new TestWCILoginController();
+
+   @Override
+   public DriverResponse service(TestServlet testServlet, WebRequest req, WebResponse resp) throws ServletException, IOException
+   {
+      if (getRequestCount() == 0)
+      {
+         // Test that credentials are not set
+         assertNull("Credentials should be null before first invocation of WCILoginController", wciLoginController.getCredentials(req));
+
+         // Processing request with wciLoginController
+         wciLoginController.service(req, resp);
+
+         // Test that credentials are set at this moment and are equalst to "root"/"gtn"
+         Credentials credentials = wciLoginController.getCredentials(req);
+         testCredentials(credentials, "root", "gtn");
+
+         // Test that we can change credentials by invoke of setCredentials
+         Credentials johnCredentials = new Credentials("john", "johnPassword");
+         wciLoginController.setCredentials(req, johnCredentials);
+         testCredentials(wciLoginController.getCredentials(req), "john", "johnPassword");
+
+         // Use mary credentials for next request
+         Map<String, String[]> params = new HashMap<String, String[]>();
+         params.put("username", new String[]{"mary"});
+         params.put("password", new String[]{"maryPassword"});
+         String url = resp.renderURL("/", params, null);
+         return new InvokeGetResponse(url);
+      }
+      else if (getRequestCount() == 1)
+      {
+         // Test that we still have credentials of john
+         testCredentials(wciLoginController.getCredentials(req), "john", "johnPassword");
+
+         // Test that we have credentials of mary after processing request
+         wciLoginController.service(req, resp);
+         testCredentials(wciLoginController.getCredentials(req), "mary", "maryPassword");
+
+         return new EndTestResponse();
+      }
+
+      return new FailureResponse(Failure.createAssertionFailure("End test reached"));
+   }
+
+   @Override
+   public DriverResponse invoke(TestServlet testServlet, DriverCommand driverCommand)
+   {
+      if (getRequestCount() == -1)
+      {
+         return new InvokeGetResponse(rewriteURL(testServlet, "/?username=root&password=gtn"));
+      }
+      else
+      {
+         return new FailureResponse(Failure.createAssertionFailure(""));
+      }
+   }
+
+   private void testCredentials(Credentials credentials, String expectedUsername, String expectedPassword)
+   {
+      assertNotNull("Credentials should not be null", credentials);
+      assertEquals(credentials.getUsername(), expectedUsername);
+      assertEquals(credentials.getPassword(), expectedPassword);
+   }
+
+   // This subclass is needed for access to protected methods getCredentials and setCredentials of WCILoginController
+   private class TestWCILoginController extends WCILoginController
+   {
+
+      @Override
+      protected Credentials getCredentials(HttpServletRequest req)
+      {
+         return super.getCredentials(req);
+      }
+
+      @Override
+      protected void setCredentials(HttpServletRequest req, Credentials credentials)
+      {
+         super.setCredentials(req, credentials);
+      }
+   }
+}

Modified: components/wci/trunk/test/core/src/main/resources/org/gatein/portal/test/web/endpoint/server-beans.xml
===================================================================
--- components/wci/trunk/test/core/src/main/resources/org/gatein/portal/test/web/endpoint/server-beans.xml	2011-12-18 21:14:23 UTC (rev 8259)
+++ components/wci/trunk/test/core/src/main/resources/org/gatein/portal/test/web/endpoint/server-beans.xml	2011-12-20 09:06:07 UTC (rev 8260)
@@ -102,5 +102,14 @@
       </uninstall>
    </bean>
 
+   <bean name="LoginControllerTestCase" class="org.gatein.wci.endpoint.LoginControllerTestCase">
+      <install bean="TestSuite" method="mount">
+         <parameter><this/></parameter>
+      </install>
+      <uninstall bean="TestSuite" method="unmount">
+         <parameter><this/></parameter>
+      </uninstall>
+   </bean>
+
 </deployment>
 

Modified: components/wci/trunk/wci/src/main/java/org/gatein/wci/security/WCILoginController.java
===================================================================
--- components/wci/trunk/wci/src/main/java/org/gatein/wci/security/WCILoginController.java	2011-12-18 21:14:23 UTC (rev 8259)
+++ components/wci/trunk/wci/src/main/java/org/gatein/wci/security/WCILoginController.java	2011-12-20 09:06:07 UTC (rev 8260)
@@ -43,7 +43,7 @@
       String password = req.getParameter("password");
 
       if (
-            req.getSession().getAttribute(Credentials.CREDENTIALS) != null
+            getCredentials(req) != null
             && username == null
             && password == null
        ) return;
@@ -52,9 +52,9 @@
       
       if (username != null && password != null)
       {         
-         log.debug("Found username and password and set credentials in http session");
+         log.debug("Found username and password. Save credentials for later use.");
          Credentials credentials = new Credentials(username, password);
-         req.getSession().setAttribute(Credentials.CREDENTIALS, credentials);
+         setCredentials(req, credentials);
       }
    }
 
@@ -62,4 +62,14 @@
    {
       doGet(req, resp);
    }
+
+   protected Credentials getCredentials(HttpServletRequest req)
+   {
+      return (Credentials)req.getSession().getAttribute(Credentials.CREDENTIALS);
+   }
+
+   protected void setCredentials(HttpServletRequest req, Credentials credentials)
+   {
+      req.getSession().setAttribute(Credentials.CREDENTIALS, credentials);
+   }
 }



More information about the gatein-commits mailing list