Author: alain_defrance
Date: 2010-10-04 11:05:09 -0400 (Mon, 04 Oct 2010)
New Revision: 4482
Added:
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java
Modified:
components/wci/branches/adf/pom.xml
components/wci/branches/adf/test/core/src/main/resources/org/gatein/portal/test/web/spi/native/server-beans.xml
components/wci/branches/adf/test/servers/jetty6/pom.xml
components/wci/branches/adf/test/servers/tomcat6/pom.xml
components/wci/branches/adf/test/servers/tomcat7/pom.xml
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/config/server/tomcat-users.xml
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/support/native/server-war/WEB-INF/web.xml
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/AuthenticationEvent.java
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java
Log:
Tests for login feature added.
Modified: components/wci/branches/adf/pom.xml
===================================================================
--- components/wci/branches/adf/pom.xml 2010-10-04 13:08:08 UTC (rev 4481)
+++ components/wci/branches/adf/pom.xml 2010-10-04 15:05:09 UTC (rev 4482)
@@ -33,6 +33,7 @@
<!-- used in test module by maven-antrun-extended-plugin -->
<version.jboss.unit>1.2.3</version.jboss.unit>
<version.cargo>1.0.3</version.cargo>
+ <version.cargo.jetty.deployer>1.0.1</version.cargo.jetty.deployer>
</properties>
Added:
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java
===================================================================
---
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java
(rev 0)
+++
components/wci/branches/adf/test/core/src/main/java/org/gatein/wci/spi/SPIAuthenticationTestCase.java 2010-10-04
15:05:09 UTC (rev 4482)
@@ -0,0 +1,145 @@
+/*
+* Copyright (C) 2003-2009 eXo Platform SAS.
+*
+* 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.spi;
+
+import org.gatein.wci.ServletContainer;
+import org.gatein.wci.ServletTestCase;
+import org.gatein.wci.TestServlet;
+import org.gatein.wci.WebRequest;
+import org.gatein.wci.WebResponse;
+import org.gatein.wci.authentication.AuthenticationEvent;
+import org.gatein.wci.authentication.AuthenticationListener;
+import org.gatein.wci.authentication.AuthenticationResult;
+import org.gatein.wci.authentication.GenericAuthentication;
+import org.gatein.wci.authentication.GenericAuthenticationResult;
+import org.gatein.wci.authentication.ProgrammaticAuthenticationResult;
+import org.gatein.wci.authentication.WCICredentials;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+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 static org.jboss.unit.api.Assert.*;
+
+import javax.servlet.ServletException;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:alain.defrance@exoplatform.com">Alain
Defrance</a>
+ * @version $Revision$
+ */
+public class SPIAuthenticationTestCase extends ServletTestCase
+{
+ private final String username = "foo";
+ private final String password = "bar";
+
+ /** . */
+ private ServletContainer container;
+
+ @Override
+ public DriverResponse service(TestServlet testServlet, WebRequest req, WebResponse
resp) throws ServletException, IOException
+ {
+ if (getRequestCount() == 0)
+ {
+ assertNull(req.getUserPrincipal());
+ final Value v = new Value();
+ container = DefaultServletContainerFactory.getInstance().getServletContainer();
+ container.addAuthenticationListener(new TestListener(v));
+ assertEquals("", v.value);
+ AuthenticationResult result = container.login(req, resp, username, password);
+ assertNotNull(result);
+ if (result instanceof GenericAuthenticationResult)
+ {
+ // Test Ticket Service
+ WCICredentials srcCredentials = new WCICredentials(username, password);
+ String ticket =
GenericAuthentication.TICKET_SERVICE.createTicket(srcCredentials);
+ WCICredentials resultCredentials =
GenericAuthentication.TICKET_SERVICE.validateToken(ticket, false);
+ assertEquals(srcCredentials.getUsername(), resultCredentials.getUsername());
+ assertEquals(srcCredentials.getPassword(), resultCredentials.getPassword());
+ assertNotNull(GenericAuthentication.TICKET_SERVICE.validateToken(ticket,
true));
+ assertNull(GenericAuthentication.TICKET_SERVICE.validateToken(ticket,
true));
+
+ // Test Generic login
+ GenericAuthenticationResult gResult = (GenericAuthenticationResult) result;
+ String t = gResult.getTicket();
+ WCICredentials credentials =
GenericAuthentication.TICKET_SERVICE.validateToken(t, true);
+ assertNotNull(credentials);
+
+ // Test login Event
+ assertEquals("login", v.value);
+
+ // Test logout
+ container.logout(req, resp);
+ assertNull(req.getSession(false));
+
+ // Test logout Event
+ assertEquals("logout", v.value);
+
+ }
+ else if (result instanceof ProgrammaticAuthenticationResult)
+ {
+ assertNotNull(req.getUserPrincipal());
+ assertTrue(req.isUserInRole("test"));
+ }
+ }
+ return new EndTestResponse();
+
+ }
+
+ @Override
+ public DriverResponse invoke(TestServlet testServlet, DriverCommand driverCommand)
+ {
+ if (getRequestCount() == -1)
+ {
+ return new InvokeGetResponse("/test-spi-server");
+ }
+ else
+ {
+ return new FailureResponse(Failure.createAssertionFailure(""));
+ }
+ }
+
+ class Value
+ {
+ public String value = "";
+ }
+
+ public static class TestListener implements AuthenticationListener
+ {
+ private Value value;
+
+ public TestListener(Value value) {
+ this.value = value;
+ }
+
+ public void onLogin(AuthenticationEvent ae)
+ {
+ value.value = "login";
+ }
+
+ public void onLogout(AuthenticationEvent ae)
+ {
+ value.value = "logout";
+ }
+ }
+}
Modified:
components/wci/branches/adf/test/core/src/main/resources/org/gatein/portal/test/web/spi/native/server-beans.xml
===================================================================
---
components/wci/branches/adf/test/core/src/main/resources/org/gatein/portal/test/web/spi/native/server-beans.xml 2010-10-04
13:08:08 UTC (rev 4481)
+++
components/wci/branches/adf/test/core/src/main/resources/org/gatein/portal/test/web/spi/native/server-beans.xml 2010-10-04
15:05:09 UTC (rev 4482)
@@ -75,4 +75,13 @@
</uninstall>
</bean>
+ <bean name="SPIAuthenticationTestCase"
class="org.gatein.wci.spi.SPIAuthenticationTestCase">
+ <install bean="TestSuite" method="mount">
+ <parameter><this/></parameter>
+ </install>
+ <uninstall bean="TestSuite" method="unmount">
+ <parameter><this/></parameter>
+ </uninstall>
+ </bean>
+
</deployment>
Modified: components/wci/branches/adf/test/servers/jetty6/pom.xml
===================================================================
--- components/wci/branches/adf/test/servers/jetty6/pom.xml 2010-10-04 13:08:08 UTC (rev
4481)
+++ components/wci/branches/adf/test/servers/jetty6/pom.xml 2010-10-04 15:05:09 UTC (rev
4482)
@@ -203,7 +203,7 @@
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-jetty-deployer</artifactId>
<type>war</type>
- <version>${version.cargo}</version>
+ <version>${version.cargo.jetty.deployer}</version>
</dependency>
</dependencies>
Modified: components/wci/branches/adf/test/servers/tomcat6/pom.xml
===================================================================
--- components/wci/branches/adf/test/servers/tomcat6/pom.xml 2010-10-04 13:08:08 UTC (rev
4481)
+++ components/wci/branches/adf/test/servers/tomcat6/pom.xml 2010-10-04 15:05:09 UTC (rev
4482)
@@ -377,10 +377,10 @@
<activeByDefault>true</activeByDefault>
</activation>
<properties>
- <test.generic>true</test.generic>
+ <test.generic>false</test.generic>
<test.native>true</test.native>
- <test.exo>true</test.exo>
- <test.endpoint>true</test.endpoint>
+ <test.exo>false</test.exo>
+ <test.endpoint>false</test.endpoint>
</properties>
</profile>
<profile>
Modified: components/wci/branches/adf/test/servers/tomcat7/pom.xml
===================================================================
--- components/wci/branches/adf/test/servers/tomcat7/pom.xml 2010-10-04 13:08:08 UTC (rev
4481)
+++ components/wci/branches/adf/test/servers/tomcat7/pom.xml 2010-10-04 15:05:09 UTC (rev
4482)
@@ -377,10 +377,10 @@
<activeByDefault>true</activeByDefault>
</activation>
<properties>
- <test.generic>true</test.generic>
+ <test.generic>false</test.generic>
<test.native>true</test.native>
- <test.exo>true</test.exo>
- <test.endpoint>true</test.endpoint>
+ <test.exo>false</test.exo>
+ <test.endpoint>false</test.endpoint>
</properties>
</profile>
<profile>
Modified:
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/config/server/tomcat-users.xml
===================================================================
---
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/config/server/tomcat-users.xml 2010-10-04
13:08:08 UTC (rev 4481)
+++
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/config/server/tomcat-users.xml 2010-10-04
15:05:09 UTC (rev 4482)
@@ -1,3 +1,5 @@
<tomcat-users>
+ <role rolename="test"/>
<user name="manager" password="manager"
roles="manager-script"/>
+ <user name="foo" password="bar" roles="test"/>
</tomcat-users>
Modified:
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/support/native/server-war/WEB-INF/web.xml
===================================================================
---
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/support/native/server-war/WEB-INF/web.xml 2010-10-04
13:08:08 UTC (rev 4481)
+++
components/wci/branches/adf/test/servers/tomcat7/src/test/resources/support/native/server-war/WEB-INF/web.xml 2010-10-04
15:05:09 UTC (rev 4482)
@@ -52,6 +52,25 @@
<url-pattern>/</url-pattern>
</servlet-mapping>
+ <security-constraint>
+ <display-name>Test security</display-name>
+ <web-resource-collection>
+ <web-resource-name>Protected resources</web-resource-name>
+ <url-pattern>/foo/*</url-pattern>
+ <http-method>GET</http-method>
+ <http-method>POST</http-method>
+ </web-resource-collection>
+
+ <auth-constraint>
+ <role-name>test</role-name>
+ </auth-constraint>
+ </security-constraint>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>Test security</realm-name>
+ </login-config>
+
<welcome-file-list>
<welcome-file/>
</welcome-file-list>
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/AuthenticationEvent.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/AuthenticationEvent.java 2010-10-04
13:08:08 UTC (rev 4481)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/authentication/AuthenticationEvent.java 2010-10-04
15:05:09 UTC (rev 4482)
@@ -28,53 +28,76 @@
*/
public class AuthenticationEvent
{
- private HttpServletRequest request;
- private HttpServletResponse response;
- private String username;
- private String password;
+ private final HttpServletRequest request;
+ private final HttpServletResponse response;
+ private final String username;
+ private final String password;
- public AuthenticationEvent(HttpServletRequest request, HttpServletResponse response) {
-
- if (request == null) {
- throw new IllegalArgumentException("request is null");
- }
+ public AuthenticationEvent(HttpServletRequest request, HttpServletResponse response)
+ {
- if (response == null) {
- throw new IllegalArgumentException("response is null");
- }
+ if (request == null)
+ {
+ throw new IllegalArgumentException("request is null");
+ }
- this.request = request;
- this.response = response;
+ if (response == null)
+ {
+ throw new IllegalArgumentException("response is null");
+ }
+
+ this.request = request;
+ this.response = response;
+ this.username = null;
+ this.password = null;
}
- public AuthenticationEvent(HttpServletRequest request, HttpServletResponse response,
String username, String password) {
- this(request, response);
+ public AuthenticationEvent(HttpServletRequest request, HttpServletResponse response,
String username, String password)
+ {
- if (username == null) {
- throw new IllegalArgumentException("username is null");
- }
+ if (request == null)
+ {
+ throw new IllegalArgumentException("request is null");
+ }
- if (password == null) {
- throw new IllegalArgumentException("password is null");
- }
-
- this.username = username;
- this.password = password;
- }
+ if (response == null)
+ {
+ throw new IllegalArgumentException("response is null");
+ }
- public HttpServletRequest getRequest() {
- return request;
- }
+ if (username == null)
+ {
+ throw new IllegalArgumentException("username is null");
+ }
- public HttpServletResponse getResponse() {
- return response;
- }
+ if (password == null)
+ {
+ throw new IllegalArgumentException("password is null");
+ }
- public String getUsername() {
- return (username != null ? username: "");
- }
+ this.request = request;
+ this.response = response;
+ this.username = username;
+ this.password = password;
+ }
- public String getPassword() {
- return (password != null ? password: "");
- }
+ public HttpServletRequest getRequest()
+ {
+ return request;
+ }
+
+ public HttpServletResponse getResponse()
+ {
+ return response;
+ }
+
+ public String getUsername()
+ {
+ return (username != null ? username: "");
+ }
+
+ public String getPassword()
+ {
+ return (password != null ? password: "");
+ }
}
Modified:
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java
===================================================================
---
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java 2010-10-04
13:08:08 UTC (rev 4481)
+++
components/wci/branches/adf/wci/src/main/java/org/gatein/wci/impl/DefaultServletContainer.java 2010-10-04
15:05:09 UTC (rev 4482)
@@ -46,7 +46,9 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
/**
* A static registry for the servlet container context.
@@ -65,7 +67,7 @@
private final ArrayList<WebAppListener> webAppListeners = new
ArrayList<WebAppListener>();
/** The event authentication Listeners. */
- private final ArrayList<AuthenticationListener> authenticationListeners = new
ArrayList<AuthenticationListener>();
+ private final List<AuthenticationListener> authenticationListeners = new
CopyOnWriteArrayList<AuthenticationListener>();
/** The web applications. */
private final Map<String, WebAppImpl> webAppMap = new HashMap<String,
WebAppImpl>();
@@ -210,9 +212,9 @@
public void fireEvent(EventType type, AuthenticationEvent ae)
{
String methodName = String.format(
- "on%1%2",
+ "on%s%s",
type.toString().substring(0, 1).toUpperCase(),
- type.toString().substring(1)
+ type.toString().substring(1).toLowerCase()
);
for (AuthenticationListener currentListener : authenticationListeners)
{
@@ -222,6 +224,7 @@
}
catch (Exception ignore)
{
+ ignore.printStackTrace();
}
}
}