From 2fa991c44f22644406eacd8ee2f3d659aa473f00 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Sun, 22 Jun 2008 21:42:25 +0000 Subject: [PATCH] Some reorganization of itest module --- sandbox/itest/pom.xml | 76 ++++++++++++++++- sandbox/itest/web/pom.xml | 55 ++++++------ .../web/src/main/resources/log4j.properties | 5 +- .../web/src/main/resources/test-server.ldif | 84 +++++++++++++++---- .../src/main/webapp/WEB-INF/http-security.xml | 5 +- .../webapp/WEB-INF/in-memory-provider.xml | 17 ++++ .../src/main/webapp/WEB-INF/ldap-provider.xml | 7 +- .../itest/web/src/main/webapp/WEB-INF/web.xml | 9 +- sandbox/itest/web/src/main/webapp/login.jsp | 35 ++++++++ .../web/src/main/webapp/secure/secure1.jsp | 12 +++ .../src/main/webapp/secure/secure1body.jsp | 2 + .../integration/AbstractWebLoginTests.java | 31 ------- .../AbstractWebServerIntegrationTests.java | 56 ++++++++++--- .../InMemoryProviderWebAppTests.java | 42 ++++++++++ ...ebLoginTests.java => LdapWebAppTests.java} | 2 +- 15 files changed, 338 insertions(+), 100 deletions(-) create mode 100644 sandbox/itest/web/src/main/webapp/WEB-INF/in-memory-provider.xml create mode 100644 sandbox/itest/web/src/main/webapp/login.jsp create mode 100644 sandbox/itest/web/src/main/webapp/secure/secure1.jsp create mode 100644 sandbox/itest/web/src/main/webapp/secure/secure1body.jsp delete mode 100644 sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebLoginTests.java create mode 100644 sandbox/itest/web/src/test/java/org/springframework/security/integration/InMemoryProviderWebAppTests.java rename sandbox/itest/web/src/test/java/org/springframework/security/integration/{LdapWebLoginTests.java => LdapWebAppTests.java} (81%) diff --git a/sandbox/itest/pom.xml b/sandbox/itest/pom.xml index 89b8f18321..3cd0587f54 100644 --- a/sandbox/itest/pom.xml +++ b/sandbox/itest/pom.xml @@ -14,6 +14,7 @@ 2.0.3-SNAPSHOT web + @@ -42,7 +43,7 @@ commons-logging - + org.springframework.security spring-security-core @@ -92,12 +93,59 @@ apacheds-core 1.0.2 runtime + + + commons-logging + commons-logging + + org.apache.directory.server apacheds-server-jndi 1.0.2 runtime + + + commons-logging + commons-logging + + + + + jwebunit + jwebunit + 1.2 + test + + + org.mortbay.jetty + jetty + ${jetty.version} + test + + + org.mortbay.jetty + jetty-naming + ${jetty.version} + test + + + org.mortbay.jetty + jetty-plus + ${jetty.version} + test + + + org.mortbay.jetty + jsp-2.1 + ${jetty.version} + test + + + org.mortbay.jetty + jsp-api-2.1 + ${jetty.version} org.springframework.ldap @@ -112,7 +160,11 @@ org.springframework spring-beans - + + + commons-logging + commons-logging + @@ -125,7 +177,7 @@ org.slf4j jcl104-over-slf4j 1.4.3 - provided + runtime log4j @@ -144,6 +196,19 @@ 1.5 + + org.apache.maven.plugins + maven-jar-plugin + 2.1 + + + + test-jar + + package + + + org.apache.maven.plugins maven-surefire-plugin @@ -168,5 +233,8 @@ - + + + 6.1.11 + diff --git a/sandbox/itest/web/pom.xml b/sandbox/itest/web/pom.xml index 31cb2b130b..94ba4d70a9 100644 --- a/sandbox/itest/web/pom.xml +++ b/sandbox/itest/web/pom.xml @@ -10,34 +10,41 @@ spring-security-itest-web Spring Security - Web Integration Tests war - + + diff --git a/sandbox/itest/web/src/main/resources/log4j.properties b/sandbox/itest/web/src/main/resources/log4j.properties index e5340c70b2..930f320239 100644 --- a/sandbox/itest/web/src/main/resources/log4j.properties +++ b/sandbox/itest/web/src/main/resources/log4j.properties @@ -1,8 +1,9 @@ -log4j.rootCategory=INFO, stdout +log4j.rootCategory=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%n -log4j.category.org.mortbay.log=INFO +log4j.category.org.apache.jasper=DEBUG +log4j.category.org.mortbay.log=DEBUG log4j.category.org.springframework.security=DEBUG \ No newline at end of file diff --git a/sandbox/itest/web/src/main/resources/test-server.ldif b/sandbox/itest/web/src/main/resources/test-server.ldif index f2caf17ca8..f8e41ed228 100644 --- a/sandbox/itest/web/src/main/resources/test-server.ldif +++ b/sandbox/itest/web/src/main/resources/test-server.ldif @@ -1,14 +1,12 @@ -dn: ou=groups,dc=springframework,dc=org -objectclass: top -objectclass: organizationalUnit -ou: groups -dn: ou=subgroups,ou=groups,dc=springframework,dc=org +# Users + +dn: ou=people,dc=springframework,dc=org objectclass: top objectclass: organizationalUnit -ou: subgroups +ou: people -dn: ou=people,dc=springframework,dc=org +dn: ou=musicians,dc=springframework,dc=org objectclass: top objectclass: organizationalUnit ou: people @@ -33,24 +31,76 @@ sn: Hamilton uid: bob userPassword: bobspassword +dn: uid=miles,ou=musicians,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Miles Davis +sn: Davis +uid: miles +userPassword: milespassword + +dn: uid=johnc,ou=musicians,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: John Coltrane +sn: Coltrane +uid: johnc +userPassword: johncspassword + +dn: uid=jimi,ou=musicians,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Jimi Hendrix +sn: Hendrix +uid: jimi +userPassword: {SSHA}S6jnyvykw4K5eF35OXvAkQsf3y2fPrRQ + + +# Groups + +dn: ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: groups + dn: cn=developers,ou=groups,dc=springframework,dc=org objectclass: top -objectclass: groupOfNames +objectclass: groupOfUniqueNames cn: developers ou: developer -member: uid=ben,ou=people,dc=springframework,dc=org -member: uid=bob,ou=people,dc=springframework,dc=org +uniqueMember: uid=ben,ou=people,dc=springframework,dc=org +uniqueMember: uid=bob,ou=people,dc=springframework,dc=org dn: cn=managers,ou=groups,dc=springframework,dc=org objectclass: top -objectclass: groupOfNames +objectclass: groupOfUniqueNames cn: managers ou: manager -member: uid=ben,ou=people,dc=springframework,dc=org +uniqueMember: uid=ben,ou=people,dc=springframework,dc=org + +dn: ou=genres,ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: genres -dn: cn=submanagers,ou=subgroups,ou=groups,dc=springframework,dc=org +dn: cn=rock,ou=genres,ou=groups,dc=springframework,dc=org objectclass: top -objectclass: groupOfNames -cn: submanagers -ou: submanager -member: uid=ben,ou=people,dc=springframework,dc=org +objectclass: groupOfUniqueNames +cn: rock +ou: rock +uniqueMember: uid=jimi,ou=musicians,dc=springframework,dc=org + +dn: cn=jazz,ou=genres,ou=groups,dc=springframework,dc=org +objectclass: top +objectclass: groupOfUniqueNames +cn: jazz +ou: jazz +uniqueMember: uid=miles,ou=musicians,dc=springframework,dc=org + + diff --git a/sandbox/itest/web/src/main/webapp/WEB-INF/http-security.xml b/sandbox/itest/web/src/main/webapp/WEB-INF/http-security.xml index 4e92b39a99..3e40cf1951 100644 --- a/sandbox/itest/web/src/main/webapp/WEB-INF/http-security.xml +++ b/sandbox/itest/web/src/main/webapp/WEB-INF/http-security.xml @@ -12,14 +12,15 @@ --> + - + - + diff --git a/sandbox/itest/web/src/main/webapp/WEB-INF/in-memory-provider.xml b/sandbox/itest/web/src/main/webapp/WEB-INF/in-memory-provider.xml new file mode 100644 index 0000000000..1038f4d764 --- /dev/null +++ b/sandbox/itest/web/src/main/webapp/WEB-INF/in-memory-provider.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox/itest/web/src/main/webapp/WEB-INF/ldap-provider.xml b/sandbox/itest/web/src/main/webapp/WEB-INF/ldap-provider.xml index 0156db26b8..8aa82ad53a 100644 --- a/sandbox/itest/web/src/main/webapp/WEB-INF/ldap-provider.xml +++ b/sandbox/itest/web/src/main/webapp/WEB-INF/ldap-provider.xml @@ -1,9 +1,8 @@ - + - + \ No newline at end of file diff --git a/sandbox/itest/web/src/main/webapp/WEB-INF/web.xml b/sandbox/itest/web/src/main/webapp/WEB-INF/web.xml index 1aa6ef9011..3ffd33b1d3 100644 --- a/sandbox/itest/web/src/main/webapp/WEB-INF/web.xml +++ b/sandbox/itest/web/src/main/webapp/WEB-INF/web.xml @@ -1,8 +1,9 @@ - + + + + - Integration Tests Webapp diff --git a/sandbox/itest/web/src/main/webapp/login.jsp b/sandbox/itest/web/src/main/webapp/login.jsp new file mode 100644 index 0000000000..d81c1fade0 --- /dev/null +++ b/sandbox/itest/web/src/main/webapp/login.jsp @@ -0,0 +1,35 @@ + + + + + + + Custom Spring Security Login + + + +

Custom Spring Security Login

+ +<% + if (request.getParameter("login_error") != null) { +%> + + Your login attempt was not successful, try again.

+
+<% + } +%> + +
+ + + + + + +
User:
Password:
Don't ask for my password for two weeks
+
+ + + + diff --git a/sandbox/itest/web/src/main/webapp/secure/secure1.jsp b/sandbox/itest/web/src/main/webapp/secure/secure1.jsp new file mode 100644 index 0000000000..015cb1c544 --- /dev/null +++ b/sandbox/itest/web/src/main/webapp/secure/secure1.jsp @@ -0,0 +1,12 @@ + + + + + A secure page + + + + + \ No newline at end of file diff --git a/sandbox/itest/web/src/main/webapp/secure/secure1body.jsp b/sandbox/itest/web/src/main/webapp/secure/secure1body.jsp new file mode 100644 index 0000000000..93bd287a5c --- /dev/null +++ b/sandbox/itest/web/src/main/webapp/secure/secure1body.jsp @@ -0,0 +1,2 @@ +Params: x=<%= request.getParameter("x") %>, y=<%= request.getParameter("y") %> +xcount=<%= request.getParameterValues("x").length %> \ No newline at end of file diff --git a/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebLoginTests.java b/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebLoginTests.java deleted file mode 100644 index ebb31bdcbe..0000000000 --- a/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebLoginTests.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.springframework.security.integration; - -import org.testng.annotations.Test; - -/** - * - * @author Luke Taylor - * @version $Id$ - */ -public abstract class AbstractWebLoginTests extends AbstractWebServerIntegrationTests { - - @Test - public void loginFailsWithinvalidPassword() { - beginAt("secure/index.html"); - assertFormPresent(); - setFormElement("j_username", "bob"); - setFormElement("j_password", "wrongpassword"); - submit(); - assertTextPresent("Your login attempt was not successful"); - } - - @Test - public void loginSucceedsWithCorrectPassword() { - beginAt("secure/index.html"); - assertFormPresent(); - setFormElement("j_username", "bob"); - setFormElement("j_password", "bobspassword"); - submit(); - assertTextPresent("A Secure Page"); - } -} diff --git a/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebServerIntegrationTests.java b/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebServerIntegrationTests.java index ebbc1883af..acea68194b 100644 --- a/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebServerIntegrationTests.java +++ b/sandbox/itest/web/src/test/java/org/springframework/security/integration/AbstractWebServerIntegrationTests.java @@ -3,6 +3,7 @@ package org.springframework.security.integration; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; +import org.springframework.util.StringUtils; import net.sourceforge.jwebunit.WebTester; @@ -13,6 +14,8 @@ import javax.servlet.ServletContext; import org.testng.annotations.*; +import com.meterware.httpunit.WebConversation; + /** * @author Luke Taylor * @version $Id$ @@ -20,9 +23,12 @@ import org.testng.annotations.*; public abstract class AbstractWebServerIntegrationTests { private Server server; private final Object SERVER_LOCK = new Object(); - protected final WebTester tester = new WebTester();; + protected final WebTester tester = new WebTester(); - /** Override to set the application context files that should be loaded */ + /** + * Override to set the application context files that should be loaded or return null + * to use web.xml. + */ protected abstract String getContextConfigLocations(); protected String getContextPath() { @@ -33,19 +39,27 @@ public abstract class AbstractWebServerIntegrationTests { public void startServer() throws Exception { synchronized(SERVER_LOCK) { if (server == null) { + //System.setProperty("DEBUG", "true"); + //System.setProperty("VERBOSE", "true"); + //System.setProperty("IGNORED", "true"); server = new Server(0); - WebAppContext webCtx = new WebAppContext("src/main/webapp", getContextPath()); - - webCtx.addEventListener(new ContextLoaderListener()); - webCtx.getInitParams().put("contextConfigLocation", getContextConfigLocations()); - - server.addHandler(webCtx); + server.addHandler(createWebContext()); server.start(); - tester.getTestContext().setBaseUrl(getBaseUrl()); } } } + + protected WebAppContext createWebContext() { + WebAppContext webCtx = new WebAppContext("src/main/webapp", getContextPath()); + + if (StringUtils.hasText(getContextConfigLocations())) { + webCtx.addEventListener(new ContextLoaderListener()); + webCtx.getInitParams().put("contextConfigLocation", getContextConfigLocations()); + } + + return webCtx; + } @AfterClass public void stopServer() throws Exception { @@ -56,8 +70,13 @@ public abstract class AbstractWebServerIntegrationTests { server = null; } } - - protected final String getBaseUrl() { + + @AfterMethod + public void resetWebConversation() { + tester.getTestContext().setWebClient(new WebConversation()); + } + + private final String getBaseUrl() { int port = server.getConnectors()[0].getLocalPort(); return "http://localhost:" + port + getContextPath() + "/"; } @@ -73,6 +92,10 @@ public abstract class AbstractWebServerIntegrationTests { return appCtx; } +// protected final HttpUnitDialog getDialog() { +// return tester.getDialog(); +// } + protected final void submit() { tester.submit(); } @@ -92,4 +115,15 @@ public abstract class AbstractWebServerIntegrationTests { protected final void assertTextPresent(String text) { tester.assertTextPresent(text); } + + + + // Security-specific utility methods + + protected void login(String username, String password) { + assertFormPresent(); + setFormElement("j_username", username); + setFormElement("j_password", password); + submit(); + } } diff --git a/sandbox/itest/web/src/test/java/org/springframework/security/integration/InMemoryProviderWebAppTests.java b/sandbox/itest/web/src/test/java/org/springframework/security/integration/InMemoryProviderWebAppTests.java new file mode 100644 index 0000000000..173f588039 --- /dev/null +++ b/sandbox/itest/web/src/test/java/org/springframework/security/integration/InMemoryProviderWebAppTests.java @@ -0,0 +1,42 @@ +package org.springframework.security.integration; + +import org.testng.annotations.*; + +/** + * @author Luke Taylor + * @version $Id$ + */ +public class InMemoryProviderWebAppTests extends AbstractWebServerIntegrationTests { + + protected String getContextConfigLocations() { + return "/WEB-INF/http-security.xml /WEB-INF/in-memory-provider.xml"; + } + + @Test + public void loginFailsWithinvalidPassword() { + beginAt("secure/index.html"); + login("jimi", "wrongPassword"); + assertTextPresent("Your login attempt was not successful"); + } + + @Test + public void loginSucceedsWithCorrectPassword() { + beginAt("secure/index.html"); + login("jimi", "jimispassword"); + assertTextPresent("A Secure Page"); + tester.gotoPage("/logout"); + } + + /* + * Checks use of with parameters in the secured page. + */ + @Test + public void savedRequestWithJspIncludeSeesCorrectParams() { + beginAt("secure/secure1.jsp?x=0"); + login("jimi", "jimispassword"); + // Included JSP has params ?x=1&y=2 + assertTextPresent("Params: x=1, y=2"); + assertTextPresent("xcount=2"); + } + +} \ No newline at end of file diff --git a/sandbox/itest/web/src/test/java/org/springframework/security/integration/LdapWebLoginTests.java b/sandbox/itest/web/src/test/java/org/springframework/security/integration/LdapWebAppTests.java similarity index 81% rename from sandbox/itest/web/src/test/java/org/springframework/security/integration/LdapWebLoginTests.java rename to sandbox/itest/web/src/test/java/org/springframework/security/integration/LdapWebAppTests.java index 56bce730f2..1dbf474fc0 100644 --- a/sandbox/itest/web/src/test/java/org/springframework/security/integration/LdapWebLoginTests.java +++ b/sandbox/itest/web/src/test/java/org/springframework/security/integration/LdapWebAppTests.java @@ -6,7 +6,7 @@ import org.testng.annotations.*; * @author Luke Taylor * @version $Id$ */ -public class LdapWebLoginTests extends AbstractWebLoginTests { +public class LdapWebAppTests extends AbstractWebServerIntegrationTests { protected String getContextConfigLocations() { return "/WEB-INF/http-security.xml /WEB-INF/ldap-provider.xml";