From 9c0f2cc28194a879b5748c4d4b716ed6b7cd0638 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Tue, 10 Apr 2018 15:05:21 -0600 Subject: [PATCH] AccessDeniedConfigTests groovy->java Issue: gh-4939 --- .../http/AccessDeniedConfigTests.groovy | 47 -------- .../config/http/AccessDeniedConfigTests.java | 101 ++++++++++++++++++ ...sDeniedConfigTests-AccessDeniedHandler.xml | 36 +++++++ ...AccessDeniedConfigTests-NoLeadingSlash.xml | 32 ++++++ ...AccessDeniedConfigTests-UsesPathAndRef.xml | 35 ++++++ .../security/config/http/userservice.xml | 29 +++++ 6 files changed, 233 insertions(+), 47 deletions(-) delete mode 100644 config/src/test/groovy/org/springframework/security/config/http/AccessDeniedConfigTests.groovy create mode 100644 config/src/test/java/org/springframework/security/config/http/AccessDeniedConfigTests.java create mode 100644 config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-AccessDeniedHandler.xml create mode 100644 config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-NoLeadingSlash.xml create mode 100644 config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-UsesPathAndRef.xml create mode 100644 config/src/test/resources/org/springframework/security/config/http/userservice.xml diff --git a/config/src/test/groovy/org/springframework/security/config/http/AccessDeniedConfigTests.groovy b/config/src/test/groovy/org/springframework/security/config/http/AccessDeniedConfigTests.groovy deleted file mode 100644 index b084d7888f..0000000000 --- a/config/src/test/groovy/org/springframework/security/config/http/AccessDeniedConfigTests.groovy +++ /dev/null @@ -1,47 +0,0 @@ -package org.springframework.security.config.http - -import org.springframework.beans.factory.BeanCreationException -import org.springframework.beans.factory.parsing.BeanDefinitionParsingException -import org.springframework.security.web.access.AccessDeniedHandlerImpl -import org.springframework.security.web.access.ExceptionTranslationFilter - -/** - * - * @author Luke Taylor - */ -class AccessDeniedConfigTests extends AbstractHttpConfigTests { - def invalidAccessDeniedUrlIsDetected() { - when: - httpAutoConfig() { - 'access-denied-handler'('error-page':'noLeadingSlash') - } - createAppContext(); - then: - thrown(BeanCreationException) - } - - def accessDeniedHandlerIsSetCorectly() { - httpAutoConfig() { - 'access-denied-handler'(ref: 'adh') - } - bean('adh', AccessDeniedHandlerImpl) - createAppContext(); - - def filter = getFilter(ExceptionTranslationFilter.class); - def adh = appContext.getBean("adh"); - - expect: - filter.accessDeniedHandler == adh - } - - def void accessDeniedHandlerPageAndRefAreMutuallyExclusive() { - when: - httpAutoConfig { - 'access-denied-handler'('error-page': '/go-away', ref: 'adh') - } - createAppContext(); - bean('adh', AccessDeniedHandlerImpl) - then: - thrown(BeanDefinitionParsingException) - } -} diff --git a/config/src/test/java/org/springframework/security/config/http/AccessDeniedConfigTests.java b/config/src/test/java/org/springframework/security/config/http/AccessDeniedConfigTests.java new file mode 100644 index 0000000000..beadb9ea1a --- /dev/null +++ b/config/src/test/java/org/springframework/security/config/http/AccessDeniedConfigTests.java @@ -0,0 +1,101 @@ +/* + * 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.eclipse.jetty.http.HttpStatus; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.config.test.SpringTestContext; +import org.springframework.security.config.test.SpringTestRule; +import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.web.access.AccessDeniedHandler; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.web.servlet.MockMvc; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * + * @author Luke Taylor + * @author Josh Cummings + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SecurityTestExecutionListeners +public class AccessDeniedConfigTests { + private static final String CONFIG_LOCATION_PREFIX = + "classpath:org/springframework/security/config/http/AccessDeniedConfigTests"; + + @Autowired + MockMvc mvc; + + @Rule + public final SpringTestRule spring = new SpringTestRule(); + + @Test + public void configureWhenAccessDeniedHandlerIsMissingLeadingSlashThenException() { + SpringTestContext context = this.spring.configLocations(this.xml("NoLeadingSlash")); + + assertThatThrownBy(() -> context.autowire()) + .isInstanceOf(BeanCreationException.class) + .hasMessageContaining("errorPage must begin with '/'"); + } + + @Test + @WithMockUser + public void configureWhenAccessDeniedHandlerRefThenAutowire() + throws Exception { + + this.spring.configLocations(this.xml("AccessDeniedHandler")).autowire(); + + this.mvc.perform(get("/")) + .andExpect(status().is(HttpStatus.GONE_410)); + } + + @Test + public void configureWhenAccessDeniedHandlerUsesPathAndRefThenException() { + SpringTestContext context = this.spring.configLocations(this.xml("UsesPathAndRef")); + + assertThatThrownBy(() -> context.autowire()) + .isInstanceOf(BeanDefinitionParsingException.class) + .hasMessageContaining("attribute error-page cannot be used together with the 'ref' attribute"); + } + + private String xml(String configName) { + return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml"; + } + + public static class GoneAccessDeniedHandler implements AccessDeniedHandler { + + @Override + public void handle(HttpServletRequest request, + HttpServletResponse response, + AccessDeniedException accessDeniedException) { + + response.setStatus(HttpStatus.GONE_410); + } + } +} diff --git a/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-AccessDeniedHandler.xml b/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-AccessDeniedHandler.xml new file mode 100644 index 0000000000..58d807a4c8 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-AccessDeniedHandler.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-NoLeadingSlash.xml b/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-NoLeadingSlash.xml new file mode 100644 index 0000000000..461aca3a75 --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-NoLeadingSlash.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + diff --git a/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-UsesPathAndRef.xml b/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-UsesPathAndRef.xml new file mode 100644 index 0000000000..82bdfbec2d --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/AccessDeniedConfigTests-UsesPathAndRef.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 new file mode 100644 index 0000000000..8d7071d98e --- /dev/null +++ b/config/src/test/resources/org/springframework/security/config/http/userservice.xml @@ -0,0 +1,29 @@ + + + + + + + +