refactored basic just-in-time login sequence into TestBase class
authormaxcooper <maxcooper>
Tue, 10 Jun 2003 11:29:33 +0000 (11:29 +0000)
committermaxcooper <maxcooper>
Tue, 10 Jun 2003 11:29:33 +0000 (11:29 +0000)
src/test/org/securityfilter/test/http/TestBase.java
src/test/org/securityfilter/test/http/form/JustInTimeTest.java

index a1c573f..9b311cc 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $Header: /cvsroot/securityfilter/securityfilter/src/test/org/securityfilter/test/http/TestBase.java,v 1.2 2003/06/09 12:06:23 maxcooper Exp $
- * $Revision: 1.2 $
- * $Date: 2003/06/09 12:06:23 $
+ * $Header: /cvsroot/securityfilter/securityfilter/src/test/org/securityfilter/test/http/TestBase.java,v 1.3 2003/06/10 11:29:33 maxcooper Exp $
+ * $Revision: 1.3 $
+ * $Date: 2003/06/10 11:29:33 $
  *
  * ====================================================================
  * The SecurityFilter Software License, Version 1.1
 
 package org.securityfilter.test.http;
 
-import com.meterware.httpunit.HttpUnitOptions;
+import com.meterware.httpunit.*;
 import junit.framework.TestCase;
+import org.securityfilter.example.Constants;
 
 /**
  * TestBase
  *
  * @author Max Cooper (max@maxcooper.com)
- * @version $Revision: 1.2 $ $Date: 2003/06/09 12:06:23 $
+ * @version $Revision: 1.3 $ $Date: 2003/06/10 11:29:33 $
  */
 public class TestBase extends TestCase {
 
@@ -89,4 +90,37 @@ public class TestBase extends TestCase {
    protected void assertBaseUrlIsSet() {
       assertNotNull("base.url is null", baseUrl);
    }
+
+   /**
+    * Performs a just-in-time login for the contextRelativeUri passed in.
+    * This method will assert that the login page is received, and then login with a valid
+    * username and password. The response from the login form submit is what gets returned.
+    *
+    * @param contextRelativeUri
+    * @return response after valid login form submittal
+    * @throws Exception
+    */
+   protected WebResponse performJustInTimeLogin(String contextRelativeUri) throws Exception {
+      // make sure the baseUrl was set
+      assertBaseUrlIsSet();
+
+      // request the secure page
+      WebConversation session = new WebConversation();
+      WebRequest request = new GetMethodWebRequest(baseUrl + contextRelativeUri);
+      WebResponse response = session.getResponse(request);
+
+      // make sure the response leads us to login page
+      String title = response.getTitle();
+      assertEquals(
+         "On request for " + contextRelativeUri + ", expected login page, got:" + title,
+         Constants.LOGIN_TITLE,
+         title
+      );
+
+      // submit valid login credentials
+      WebForm loginForm = response.getFormWithID(Constants.LOGIN_FORM_ID);
+      loginForm.setParameter(Constants.LOGIN_USERNAME_FIELD, Constants.VALID_USERNAME);
+      loginForm.setParameter(Constants.LOGIN_PASSWORD_FIELD, Constants.VALID_PASSWORD);
+      return session.getResponse(loginForm.getRequest());
+   }
 }
index 9e7f1e4..ae78d5a 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $Header: /cvsroot/securityfilter/securityfilter/src/test/org/securityfilter/test/http/form/JustInTimeTest.java,v 1.2 2003/06/09 12:07:06 maxcooper Exp $
- * $Revision: 1.2 $
- * $Date: 2003/06/09 12:07:06 $
+ * $Header: /cvsroot/securityfilter/securityfilter/src/test/org/securityfilter/test/http/form/JustInTimeTest.java,v 1.3 2003/06/10 11:29:34 maxcooper Exp $
+ * $Revision: 1.3 $
+ * $Date: 2003/06/10 11:29:34 $
  *
  * ====================================================================
  * The SecurityFilter Software License, Version 1.1
@@ -63,7 +63,7 @@ import org.securityfilter.test.http.TestBase;
  * JustInTimeTest - This tests basic just-in-time login behavior.
  *
  * @author Max Cooper (max@maxcooper.com)
- * @version $Revision: 1.2 $ $Date: 2003/06/09 12:07:06 $
+ * @version $Revision: 1.3 $ $Date: 2003/06/10 11:29:34 $
  */
 public class JustInTimeTest extends TestBase {
 
@@ -89,30 +89,11 @@ public class JustInTimeTest extends TestBase {
     * @throws Exception
     */
    public void testJustInTime() throws Exception {
-      // make sure the baseUrl was set
-      assertBaseUrlIsSet();
-
-      // request the secure page
-      WebConversation session = new WebConversation();
-      WebRequest request = new GetMethodWebRequest(baseUrl + "/securePage.jsp");
-      WebResponse response = session.getResponse(request);
-
-      // make sure the response leads us to login page
-      String title = response.getTitle();
-      assertEquals(
-         "On request for secure page, expected login page, got:" + title,
-         Constants.LOGIN_TITLE,
-         title
-      );
-
-      // submit valid login credentials
-      WebForm loginForm = response.getFormWithID(Constants.LOGIN_FORM_ID);
-      loginForm.setParameter(Constants.LOGIN_USERNAME_FIELD, Constants.VALID_USERNAME);
-      loginForm.setParameter(Constants.LOGIN_PASSWORD_FIELD, Constants.VALID_PASSWORD);
-      response = session.getResponse(loginForm.getRequest());
+      // request the secure page and login
+      WebResponse response = performJustInTimeLogin("/securePage.jsp");
 
       // make sure the response leads to the secure page
-      title = response.getTitle();
+      String title = response.getTitle();
       assertEquals(
          "Expected secure page, got:" + title,
          Constants.SECURE_TITLE,