Author: sohil.shah(a)jboss.com
Date: 2007-08-23 02:49:51 -0400 (Thu, 23 Aug 2007)
New Revision: 8045
Added:
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/JOSSOTestCase.java
Modified:
branches/2_6_CAS_Integration/identity/build.xml
branches/2_6_CAS_Integration/identity/josso/config/josso-agent-config.xml
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/CASTestCase.java
branches/2_6_CAS_Integration/identity/src/resources/portal-identity-test-jar/org/jboss/portal/test/identity/sso-beans.xml
Log:
JOSSO Integration Testing
Modified: branches/2_6_CAS_Integration/identity/build.xml
===================================================================
--- branches/2_6_CAS_Integration/identity/build.xml 2007-08-23 00:14:43 UTC (rev 8044)
+++ branches/2_6_CAS_Integration/identity/build.xml 2007-08-23 06:49:51 UTC (rev 8045)
@@ -554,16 +554,17 @@
</target>
<target name="tests-sso" depends="init,package-tests">
- <execute-tests>
- <!--
+ <execute-tests>
+ <!--
<x-sysproperty>
<jvmarg value="-Xdebug"/>
<jvmarg
value="-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y"/>
</x-sysproperty>
- -->
+ -->
<x-test>
<test todir="${test.reports}"
name="org.jboss.portal.test.identity.sso.CASTestCase"/>
+ <test todir="${test.reports}"
name="org.jboss.portal.test.identity.sso.JOSSOTestCase"/>
</x-test>
<x-classpath>
Modified: branches/2_6_CAS_Integration/identity/josso/config/josso-agent-config.xml
===================================================================
--- branches/2_6_CAS_Integration/identity/josso/config/josso-agent-config.xml 2007-08-23
00:14:43 UTC (rev 8044)
+++ branches/2_6_CAS_Integration/identity/josso/config/josso-agent-config.xml 2007-08-23
06:49:51 UTC (rev 8045)
@@ -11,5 +11,9 @@
<partner-app>
<context>/portal</context>
</partner-app>
+ <!-- used for testing purposes -->
+ <partner-app>
+ <context>/portal2</context>
+ </partner-app>
</partner-apps>
</agent>
Modified:
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/CASTestCase.java
===================================================================
---
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/CASTestCase.java 2007-08-23
00:14:43 UTC (rev 8044)
+++
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/CASTestCase.java 2007-08-23
06:49:51 UTC (rev 8045)
@@ -69,6 +69,7 @@
this.runtimeContext = new
TestRuntimeContext("org/jboss/portal/test/identity/sso-beans.xml");
this.runtimeContext.addBean("CASConfig", this);
+ this.runtimeContext.addBean("JOSSOConfig", new JOSSOTestCase());
this.runtimeContext.start();
Added:
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/JOSSOTestCase.java
===================================================================
---
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/JOSSOTestCase.java
(rev 0)
+++
branches/2_6_CAS_Integration/identity/src/main/org/jboss/portal/test/identity/sso/JOSSOTestCase.java 2007-08-23
06:49:51 UTC (rev 8045)
@@ -0,0 +1,620 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, 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.jboss.portal.test.identity.sso;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.Iterator;
+import java.util.HashMap;
+
+import org.jboss.portal.test.framework.TestRuntimeContext;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.protocol.Protocol;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+public class JOSSOTestCase extends TestCase
+{
+ /**
+ *
+ */
+ private String portalServer = null;
+ private String firstPortalContext = null;
+ private String secondPortalContext = null;
+ private String userLoggedInIndicator = null;
+ private String username = null;
+ private String password = null;
+
+ /**
+ *
+ */
+ protected TestRuntimeContext runtimeContext = null;
+
+ /**
+ *
+ */
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ this.runtimeContext = new
TestRuntimeContext("org/jboss/portal/test/identity/sso-beans.xml");
+
+ this.runtimeContext.addBean("JOSSOConfig", this);
+ this.runtimeContext.addBean("CASConfig", new CASTestCase());
+
+ this.runtimeContext.start();
+
+ //SSL setup
+ Protocol.registerProtocol("https",new Protocol("https", new
EasySSLProtocolSocketFactory(), 443));
+ }
+
+ /**
+ *
+ */
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ //Service stoppage
+ this.runtimeContext.stop();
+
+ //Data cleanup
+ this.runtimeContext = null;
+ this.portalServer = null;
+ this.firstPortalContext = null;
+ this.secondPortalContext = null;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public String getFirstPortalContext()
+ {
+ return firstPortalContext;
+ }
+
+ /**
+ *
+ * @param firstPortalContext
+ */
+ public void setFirstPortalContext(String firstPortalContext)
+ {
+ this.firstPortalContext = firstPortalContext;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public String getPortalServer()
+ {
+ return portalServer;
+ }
+
+ /**
+ *
+ * @param portalServer
+ */
+ public void setPortalServer(String portalServer)
+ {
+ this.portalServer = portalServer;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public String getSecondPortalContext()
+ {
+ return secondPortalContext;
+ }
+
+ /**
+ *
+ * @param secondPortalContext
+ */
+ public void setSecondPortalContext(String secondPortalContext)
+ {
+ this.secondPortalContext = secondPortalContext;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public String getUserLoggedInIndicator()
+ {
+ return userLoggedInIndicator;
+ }
+
+ /**
+ *
+ * @param userLoggedInIndicator
+ */
+ public void setUserLoggedInIndicator(String userLoggedInIndicator)
+ {
+ this.userLoggedInIndicator = userLoggedInIndicator;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public String getPassword()
+ {
+ return password;
+ }
+
+ /**
+ *
+ * @param password
+ */
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public String getUsername()
+ {
+ return username;
+ }
+
+ /**
+ *
+ * @param username
+ */
+ public void setUsername(String username)
+ {
+ this.username = username;
+ }
+
+ /**
+ *
+ * @throws Exception
+ */
+ public void test() throws Exception
+ {
+ Cookie ssoCookie = null;
+ String firstPortalFinalResponse = null;
+ String secondPortalFinalResponse = null;
+
+ //Load the main portal page on firstPortalContext
+ String firstContextPortalUrl =
"http://"+this.portalServer+"/"+this.firstPortalContext;
+ WebConversation portalConversation = this.startConversation(firstContextPortalUrl);
+ TestCase.assertFalse(this.isUserLoggedIn(portalConversation.getResponse()));
+
+ //Click the Login link on the firstPortalContext
+ String firstContextLoginUrl = firstContextPortalUrl +
"/auth/portal/default/default";
+ this.sendGet(firstContextLoginUrl, portalConversation);
+
+ //Navigate to a secured resource on the portal
+ TestCase.assertNotNull(portalConversation.getRedirectLocation());
+ TestCase.assertEquals(portalConversation.getStatusCode(), 302);
+ String portalToJOSSO = portalConversation.getRedirectLocation();
+ this.sendGet(portalToJOSSO, portalConversation);
+
+ //When authentication is triggered, move over to the JOSSO server establishing an
SSO session with JOSSO
+ String jossoLocation = portalConversation.getRedirectLocation();
+ WebConversation ssoConversation = this.startConversation(jossoLocation);
+ String response = ssoConversation.getResponse();
+
+ //Extract the josso post action value
+ int searchIndex = response.indexOf("action=\"")+9;
+ int endIndex = response.indexOf('"', searchIndex);
+ String action = response.substring(searchIndex,endIndex);
+
+ //Perform HTTP Post based authentication with the JOSSO Server
+ Map postParams = new HashMap();
+ postParams.put("josso_username", this.username);
+ postParams.put("josso_password", this.password);
+ postParams.put("josso_cmd", "login");
+
this.sendPost("http://"+this.portalServer+"/"+action,postParams,
ssoConversation);
+
+ //Go back to the Portal since login has succeeded, starting with assertion on the
JOSSO Agent installed on the Portal
+ String assertUrl = ssoConversation.getRedirectLocation();
+ this.sendGet(assertUrl, portalConversation);
+
+ //Now go back to the original Portal resource requested. This time user should have
an authenticated session established
+ TestCase.assertNotNull(portalConversation.getRedirectLocation());
+ TestCase.assertEquals(portalConversation.getStatusCode(), 302);
+
TestCase.assertTrue(portalConversation.getRedirectLocation().indexOf(firstContextLoginUrl)
!= -1);
+ TestCase.assertNotNull(portalConversation.getSSOCookie());
+ String goBack = portalConversation.getRedirectLocation();
+ ssoCookie = ssoConversation.getSSOCookie();
+ this.sendGet(goBack, portalConversation);
+ firstPortalFinalResponse = portalConversation.getResponse();
+ TestCase.assertTrue(this.isUserLoggedIn(firstPortalFinalResponse));
+
+ //Load the main portal page on secondPortalContext
+ String secondContextPortalUrl =
"http://"+this.portalServer+"/"+this.secondPortalContext;
+ portalConversation = this.startConversation(secondContextPortalUrl);
+
+ //Click the Login Link on the secondPortalContext
+ String secondContextLoginUrl = secondContextPortalUrl +
"/auth/portal/default/default";
+ this.sendGet(secondContextLoginUrl, portalConversation);
+
+ //Perform re-direct to the JOSSO Server but this time sending in the JOSSO cookie
+ TestCase.assertNotNull(portalConversation.getRedirectLocation());
+ TestCase.assertEquals(portalConversation.getStatusCode(), 302);
+ portalToJOSSO = portalConversation.getRedirectLocation();
+ this.sendGet(portalToJOSSO, portalConversation);
+
+
+ //Assert the redirect and it should be to the JOSSO Server, but this time
+ //It should end up with an Authenticated session back to the secondPortalContext
+ jossoLocation = portalConversation.getRedirectLocation();
+ ssoConversation = this.startConversation(jossoLocation,ssoCookie);
+ secondPortalFinalResponse = ssoConversation.getResponse();
+ TestCase.assertTrue(this.isUserLoggedIn(secondPortalFinalResponse));
+
+ //Assert and make sure its the same user logged into both Portals
+ String firstPortalUser = this.extractLoggedInUser(firstPortalFinalResponse);
+ String secondPortalUser = this.extractLoggedInUser(secondPortalFinalResponse);
+ TestCase.assertEquals(firstPortalUser, this.username);
+ TestCase.assertEquals(secondPortalUser, this.username);
+ TestCase.assertEquals(firstPortalUser, secondPortalUser);
+ }
+
+ /**
+ *
+ * @param portalUrl
+ * @return
+ * @throws Exception
+ */
+ private WebConversation startConversation(String portalUrl) throws Exception
+ {
+ WebConversation conversation = null;
+
+ HttpClient httpClient = new HttpClient();
+ GetMethod getMethod = new GetMethod(portalUrl);
+ try
+ {
+ conversation = new WebConversation();
+
+ int statusCode = httpClient.executeMethod(getMethod);
+ String response = getMethod.getResponseBodyAsString();
+
+ Cookie[] cookies = httpClient.getState().getCookies();
+ for(int i=0;i<cookies.length;i++)
+ {
+ if(cookies[i].getName().equals("JSESSIONID"))
+ {
+ conversation.setSessionId(cookies[i].getValue());
+ }
+ }
+
+ conversation.setClient(httpClient);
+ conversation.setStatusCode(statusCode);
+ conversation.setResponse(response);
+ }
+ finally
+ {
+ if(getMethod != null)
+ {
+ getMethod.releaseConnection();
+ }
+ }
+
+ return conversation;
+ }
+
+ /**
+ *
+ * @param portalUrl
+ * @return
+ * @throws Exception
+ */
+ private WebConversation startConversation(String portalUrl, Cookie ssoCookie) throws
Exception
+ {
+ WebConversation conversation = null;
+
+ HttpClient httpClient = new HttpClient();
+ GetMethod getMethod = new GetMethod(portalUrl);
+
+ //Set ssoCookie to be sent in
+
getMethod.setRequestHeader("Cookie",ssoCookie.getName()+"="+ssoCookie.getValue());
+
+ try
+ {
+ conversation = new WebConversation();
+
+ int statusCode = httpClient.executeMethod(getMethod);
+ String response = getMethod.getResponseBodyAsString();
+
+ Cookie[] cookies = httpClient.getState().getCookies();
+ for(int i=0;i<cookies.length;i++)
+ {
+ if(cookies[i].getName().equals("JSESSIONID"))
+ {
+ conversation.setSessionId(cookies[i].getValue());
+ }
+ }
+
+ conversation.setClient(httpClient);
+ conversation.setStatusCode(statusCode);
+ conversation.setResponse(response);
+ }
+ finally
+ {
+ if(getMethod != null)
+ {
+ getMethod.releaseConnection();
+ }
+ }
+
+ return conversation;
+ }
+
+ /**
+ *
+ * @param portalUrl
+ * @param conversation
+ * @throws Exception
+ */
+ private void sendGet(String portalUrl,WebConversation conversation) throws Exception
+ {
+ HttpClient httpClient = conversation.getClient();
+
+ GetMethod getMethod = new GetMethod(portalUrl);
+ getMethod.setFollowRedirects(false);
+ try
+ {
+ int statusCode = httpClient.executeMethod(getMethod);
+
+ String response = getMethod.getResponseBodyAsString();
+
+ Cookie[] cookies = httpClient.getState().getCookies();
+ for(int i=0;i<cookies.length;i++)
+ {
+ if(cookies[i].getName().equals("JSESSIONID"))
+ {
+ conversation.setSessionId(cookies[i].getValue());
+ }
+ if(cookies[i].getName().equals("JOSSO_SESSIONID"))
+ {
+ conversation.setSSOCookie(cookies[i]);
+ }
+ }
+
+ conversation.setStatusCode(statusCode);
+ conversation.setResponse(response);
+
+ if(statusCode == 302)
+ {
+ //Store the redirect location
+ String location =
getMethod.getResponseHeader("Location").getValue();
+ conversation.setRedirectLocation(location);
+ }
+ }
+ finally
+ {
+ if(getMethod != null)
+ {
+ getMethod.releaseConnection();
+ }
+ }
+ }
+
+
+ /**
+ *
+ * @param url
+ * @param parameters
+ * @param conversation
+ * @throws Exception
+ */
+ private void sendPost(String url,Map parameters,WebConversation conversation) throws
Exception
+ {
+ HttpClient httpClient = conversation.getClient();
+
+ PostMethod postMethod = new PostMethod(url);
+
+ //Add post parameters
+ Set keySet = parameters.keySet();
+ NameValuePair[] postdata = new NameValuePair[keySet.size()];
+ int index = 0;
+ for(Iterator itr=keySet.iterator();itr.hasNext();)
+ {
+ String name = (String)itr.next();
+ String value = (String)parameters.get(name);
+ postdata[index++] = new NameValuePair(name,value);
+ }
+ postMethod.setRequestBody(postdata);
+ try
+ {
+ int statusCode = httpClient.executeMethod(postMethod);
+
+ String response = postMethod.getResponseBodyAsString();
+
+ Cookie[] cookies = httpClient.getState().getCookies();
+ for(int i=0;i<cookies.length;i++)
+ {
+ if(cookies[i].getName().equals("JSESSIONID"))
+ {
+ conversation.setSessionId(cookies[i].getValue());
+ }
+ if(cookies[i].getName().equals("JOSSO_SESSIONID"))
+ {
+ conversation.setSSOCookie(cookies[i]);
+ }
+ }
+
+ conversation.setStatusCode(statusCode);
+ conversation.setResponse(response);
+
+ if(statusCode == 302)
+ {
+ //Store the redirect location
+ String location =
postMethod.getResponseHeader("Location").getValue();
+ conversation.setRedirectLocation(location);
+ }
+ }
+ finally
+ {
+ if(postMethod != null)
+ {
+ postMethod.releaseConnection();
+ }
+ }
+ }
+
+ /**
+ *
+ * @param response
+ * @return
+ */
+ private boolean isUserLoggedIn(String response)
+ {
+ boolean isUserLoggedIn = false;
+
+ isUserLoggedIn = (response.indexOf(this.userLoggedInIndicator) != -1);
+
+ return isUserLoggedIn;
+ }
+
+ /**
+ *
+ * @param response
+ * @return
+ */
+ private String extractLoggedInUser(String response)
+ {
+ String loggedInUser = null;
+
+ int startIndex = response.indexOf(this.userLoggedInIndicator) +
this.userLoggedInIndicator.length();
+ int endIndex = response.indexOf("<",startIndex);
+
+ loggedInUser = response.substring(startIndex,endIndex).trim();
+
+ return loggedInUser;
+ }
+
+ /**
+ *
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+ private static class WebConversation
+ {
+ /**
+ *
+ */
+ private HttpClient client = null;
+ private String sessionId = null;
+ private int statusCode = 0;
+ private String response = null;
+ private String redirectLocation = null;
+ private Cookie ssoCookie = null;
+
+ /**
+ *
+ *
+ */
+ public WebConversation()
+ {
+
+ }
+
+ /**
+ *
+ * @return
+ */
+ public HttpClient getClient()
+ {
+ return this.client;
+ }
+
+ /**
+ *
+ * @param client
+ */
+ public void setClient(HttpClient client)
+ {
+ this.client = client;
+ }
+
+ public String getResponse()
+ {
+ return response;
+ }
+
+ public void setResponse(String response)
+ {
+ this.response = response;
+ }
+
+ public int getStatusCode()
+ {
+ return statusCode;
+ }
+
+ public void setStatusCode(int statusCode)
+ {
+ this.statusCode = statusCode;
+ }
+
+ public String getSessionId()
+ {
+ return sessionId;
+ }
+
+ public void setSessionId(String sessionId)
+ {
+ this.sessionId = sessionId;
+ }
+
+ public String getRedirectLocation()
+ {
+ return redirectLocation;
+ }
+
+ public void setRedirectLocation(String redirectLocation)
+ {
+ this.redirectLocation = redirectLocation;
+ }
+
+ public Cookie getSSOCookie()
+ {
+ return ssoCookie;
+ }
+
+ public void setSSOCookie(Cookie ssoCookie)
+ {
+ this.ssoCookie = ssoCookie;
+ }
+ }
+}
Modified:
branches/2_6_CAS_Integration/identity/src/resources/portal-identity-test-jar/org/jboss/portal/test/identity/sso-beans.xml
===================================================================
---
branches/2_6_CAS_Integration/identity/src/resources/portal-identity-test-jar/org/jboss/portal/test/identity/sso-beans.xml 2007-08-23
00:14:43 UTC (rev 8044)
+++
branches/2_6_CAS_Integration/identity/src/resources/portal-identity-test-jar/org/jboss/portal/test/identity/sso-beans.xml 2007-08-23
06:49:51 UTC (rev 8045)
@@ -25,7 +25,7 @@
<deployment
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
- xmlns="urn:jboss:bean-deployer">
+ xmlns="urn:jboss:bean-deployer">
<bean name="CASConfig"
class="org.jboss.portal.test.identity.sso.CASTestCase">
<constructor factoryMethod="getBean">
<factory bean="BeanFactory"/>
@@ -38,4 +38,17 @@
<property name="username">user</property>
<property name="password">user</property>
</bean>
+
+ <bean name="JOSSOConfig"
class="org.jboss.portal.test.identity.sso.JOSSOTestCase">
+ <constructor factoryMethod="getBean">
+ <factory bean="BeanFactory"/>
+ <parameter>JOSSOConfig</parameter>
+ </constructor>
+ <property name="portalServer">localhost</property>
+ <property name="firstPortalContext">portal</property>
+ <property name="secondPortalContext">portal2</property>
+ <property name="userLoggedInIndicator">Logged in
as:</property>
+ <property name="username">user</property>
+ <property name="password">user</property>
+ </bean>
</deployment>