diff --git a/config/src/test/groovy/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.groovy b/config/src/test/groovy/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.groovy deleted file mode 100644 index 4df5946b4d..0000000000 --- a/config/src/test/groovy/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.groovy +++ /dev/null @@ -1,153 +0,0 @@ -package org.springframework.security.config.http - -import org.springframework.mock.web.MockFilterChain -import org.springframework.mock.web.MockHttpServletRequest -import org.springframework.mock.web.MockHttpServletResponse -import org.springframework.security.web.WebAttributes - -/** - * - * @author Luke Taylor - */ -class FormLoginBeanDefinitionParserTests extends AbstractHttpConfigTests { - - def 'form-login default login page'() { - setup: - MockHttpServletRequest request = new MockHttpServletRequest(method:'GET',requestURI:'/login') - MockHttpServletResponse response = new MockHttpServletResponse() - MockFilterChain chain = new MockFilterChain() - httpAutoConfig { - csrf(disabled:true) - } - createAppContext() - when: - springSecurityFilterChain.doFilter(request,response,chain) - then: - response.getContentAsString() == """Login Page -

Login with Username and Password

- - - - -
User:
Password:
-
""" - } - - def 'form-login default login page custom attributes'() { - setup: - MockHttpServletRequest request = new MockHttpServletRequest(method:'GET',requestURI:'/login') - MockHttpServletResponse response = new MockHttpServletResponse() - MockFilterChain chain = new MockFilterChain() - httpAutoConfig { - 'form-login'('login-processing-url':'/login_custom','username-parameter':'custom_user','password-parameter':'custom_password') - csrf(disabled:true) - } - createAppContext() - when: - springSecurityFilterChain.doFilter(request,response,chain) - then: - response.getContentAsString() == """Login Page -

Login with Username and Password

- - - - -
User:
Password:
-
""" - } - - def 'openid-login default login page'() { - setup: - MockHttpServletRequest request = new MockHttpServletRequest(method:'GET',requestURI:'/login') - MockHttpServletResponse response = new MockHttpServletResponse() - MockFilterChain chain = new MockFilterChain() - httpAutoConfig { - 'openid-login'() - csrf(disabled:true) - } - createAppContext() - when: - springSecurityFilterChain.doFilter(request,response,chain) - then: - response.getContentAsString() == """Login Page -

Login with Username and Password

- - - - -
User:
Password:
-

Login with OpenID Identity

- - - -
Identity:
-
""" - } - - def 'openid-login default login page custom attributes'() { - setup: - MockHttpServletRequest request = new MockHttpServletRequest(method:'GET',requestURI:'/login') - MockHttpServletResponse response = new MockHttpServletResponse() - MockFilterChain chain = new MockFilterChain() - httpAutoConfig { - 'openid-login'('login-processing-url':'/login_custom') - csrf(disabled:true) - } - createAppContext() - when: - springSecurityFilterChain.doFilter(request,response,chain) - then: - response.getContentAsString() == """Login Page -

Login with Username and Password

- - - - -
User:
Password:
-

Login with OpenID Identity

- - - -
Identity:
-
""" - } - - def 'form-login forward authentication failure handler'() { - setup: - MockHttpServletRequest request = new MockHttpServletRequest(method:'POST',servletPath:'/login') - request.setParameter("username", "bob") - request.setParameter("password", "invalidpassword") - MockHttpServletResponse response = new MockHttpServletResponse() - MockFilterChain chain = new MockFilterChain() - httpAutoConfig { - 'form-login'('authentication-failure-forward-url':'/failure_forward_url') - csrf(disabled:true) - } - createAppContext() - when: - springSecurityFilterChain.doFilter(request,response,chain) - then: - response.getStatus() == 200 - response.forwardedUrl == "/failure_forward_url" - request.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION) != null; - } - - def 'form-login forward authentication success handler'() { - setup: - MockHttpServletRequest request = new MockHttpServletRequest(method:'POST',servletPath:'/login') - request.setParameter("username", "bob") - request.setParameter("password", "bobspassword") - MockHttpServletResponse response = new MockHttpServletResponse() - MockFilterChain chain = new MockFilterChain() - httpAutoConfig { - 'form-login'('authentication-success-forward-url':'/success_forward_url') - csrf(disabled:true) - } - createAppContext() - when: - springSecurityFilterChain.doFilter(request,response,chain) - then: - response.getStatus() == 200 - response.forwardedUrl == "/success_forward_url" - } -} diff --git a/config/src/test/java/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.java b/config/src/test/java/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.java new file mode 100644 index 0000000000..86a3290c65 --- /dev/null +++ b/config/src/test/java/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests.java @@ -0,0 +1,166 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.config.http; + +import org.junit.Rule; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.config.test.SpringTestRule; +import org.springframework.security.web.WebAttributes; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.nullValue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +/** + * + * @author Luke Taylor + * @author Josh Cummings + */ +public class FormLoginBeanDefinitionParserTests { + private static final String CONFIG_LOCATION_PREFIX = + "classpath:org/springframework/security/config/http/FormLoginBeanDefinitionParserTests"; + + @Rule + public final SpringTestRule spring = new SpringTestRule(); + + @Autowired + MockMvc mvc; + + @Test + public void getLoginWhenAutoConfigThenShowsDefaultLoginPage() + throws Exception { + + this.spring.configLocations(this.xml("Simple")).autowire(); + + String expectedContent = + "Login Page\n" + + "

Login with Username and Password

\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "
User:
Password:
\n" + + "
"; + + this.mvc.perform(get("/login")).andExpect(content().string(expectedContent)); + } + + @Test + public void getLoginWhenConfiguredWithCustomAttributesThenLoginPageReflects() + throws Exception { + + this.spring.configLocations(this.xml("WithCustomAttributes")).autowire(); + + String expectedContent = + "Login Page\n" + + "

Login with Username and Password

\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "
User:
Password:
\n" + + "
"; + + this.mvc.perform(get("/login")).andExpect(content().string(expectedContent)); + } + + @Test + public void getLoginWhenConfiguredForOpenIdThenLoginPageReflects() + throws Exception { + + this.spring.configLocations(this.xml("WithOpenId")).autowire(); + + String expectedContent = + "Login Page\n" + + "

Login with Username and Password

\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "
User:
Password:
\n" + + "

Login with OpenID Identity

\n" + + "\n" + + " \n" + + " \n" + + "
Identity:
\n" + + "
"; + + this.mvc.perform(get("/login")).andExpect(content().string(expectedContent)); + } + + @Test + public void getLoginWhenConfiguredForOpenIdWithCustomAttributesThenLoginPageReflects() + throws Exception { + + this.spring.configLocations(this.xml("WithOpenIdCustomAttributes")).autowire(); + + String expectedContent = + "Login Page\n" + + "

Login with Username and Password

\n" + + "\n" + + " \n" + + " \n" + + " \n" + + "
User:
Password:
\n" + + "

Login with OpenID Identity

\n" + + "\n" + + " \n" + + " \n" + + "
Identity:
\n" + + "
"; + + this.mvc.perform(get("/login")).andExpect(content().string(expectedContent)); + } + + @Test + public void failedLoginWhenConfiguredWithCustomAuthenticationFailureThenForwardsAccordingly() + throws Exception { + + this.spring.configLocations(this.xml("WithAuthenticationFailureForwardUrl")).autowire(); + + this.mvc.perform(post("/login") + .param("username", "bob") + .param("password", "invalidpassword")) + .andExpect(status().isOk()) + .andExpect(forwardedUrl("/failure_forward_url")) + .andExpect(request().attribute(WebAttributes.AUTHENTICATION_EXCEPTION, not(nullValue()))); + } + + @Test + public void successfulLoginWhenConfiguredWithCustomAuthenticationSuccessThenForwardsAccordingly() + throws Exception { + + this.spring.configLocations(this.xml("WithAuthenticationSuccessForwardUrl")).autowire(); + + this.mvc.perform(post("/login") + .param("username", "user") + .param("password", "password")) + .andExpect(status().isOk()) + .andExpect(forwardedUrl("/success_forward_url")); + } + + private String xml(String configName) { + return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml"; + } +} diff --git a/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-Simple.xml b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-Simple.xml new file mode 100644 index 0000000000..d8a8c6ee78 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-Simple.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithAuthenticationFailureForwardUrl.xml b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithAuthenticationFailureForwardUrl.xml new file mode 100644 index 0000000000..711d853a47 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithAuthenticationFailureForwardUrl.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithAuthenticationSuccessForwardUrl.xml b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithAuthenticationSuccessForwardUrl.xml new file mode 100644 index 0000000000..afd9945790 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithAuthenticationSuccessForwardUrl.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithCustomAttributes.xml b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithCustomAttributes.xml new file mode 100644 index 0000000000..7213df5ca5 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithCustomAttributes.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithOpenId.xml b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithOpenId.xml new file mode 100644 index 0000000000..20a9cf3238 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithOpenId.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithOpenIdCustomAttributes.xml b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithOpenIdCustomAttributes.xml new file mode 100644 index 0000000000..a5a84da603 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/FormLoginBeanDefinitionParserTests-WithOpenIdCustomAttributes.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/userservice.xml b/config/src/test/resources/org/springframework/security/config/http/userservice.xml index 8d7071d98e..80314c3acc 100644 --- a/config/src/test/resources/org/springframework/security/config/http/userservice.xml +++ b/config/src/test/resources/org/springframework/security/config/http/userservice.xml @@ -24,6 +24,6 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - +