|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.web.servlet.filter; |
| 18 | + |
| 19 | +import javax.servlet.FilterChain; |
| 20 | +import javax.servlet.RequestDispatcher; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 26 | +import org.springframework.context.ApplicationContext; |
| 27 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 28 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 29 | +import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.mockito.ArgumentMatchers.any; |
| 33 | +import static org.mockito.ArgumentMatchers.anyString; |
| 34 | +import static org.mockito.BDDMockito.given; |
| 35 | +import static org.mockito.BDDMockito.willThrow; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.verify; |
| 38 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link ErrorPageSecurityFilter}. |
| 42 | + * |
| 43 | + * @author Madhura Bhave |
| 44 | + */ |
| 45 | +class ErrorPageSecurityFilterTests { |
| 46 | + |
| 47 | + private final WebInvocationPrivilegeEvaluator privilegeEvaluator = mock(WebInvocationPrivilegeEvaluator.class); |
| 48 | + |
| 49 | + private final ApplicationContext context = mock(ApplicationContext.class); |
| 50 | + |
| 51 | + private final MockHttpServletRequest request = new MockHttpServletRequest(); |
| 52 | + |
| 53 | + private final MockHttpServletResponse response = new MockHttpServletResponse(); |
| 54 | + |
| 55 | + private final FilterChain filterChain = mock(FilterChain.class); |
| 56 | + |
| 57 | + private ErrorPageSecurityFilter securityFilter; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setup() { |
| 61 | + given(this.context.getBean(WebInvocationPrivilegeEvaluator.class)).willReturn(this.privilegeEvaluator); |
| 62 | + this.securityFilter = new ErrorPageSecurityFilter(this.context); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void whenAccessIsAllowedShouldContinueDownFilterChain() throws Exception { |
| 67 | + given(this.privilegeEvaluator.isAllowed(anyString(), any())).willReturn(true); |
| 68 | + this.securityFilter.doFilter(this.request, this.response, this.filterChain); |
| 69 | + verify(this.filterChain).doFilter(this.request, this.response); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void whenAccessIsDeniedShouldCallSendError() throws Exception { |
| 74 | + given(this.privilegeEvaluator.isAllowed(anyString(), any())).willReturn(false); |
| 75 | + this.request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, 403); |
| 76 | + this.securityFilter.doFilter(this.request, this.response, this.filterChain); |
| 77 | + verifyNoInteractions(this.filterChain); |
| 78 | + assertThat(this.response.getStatus()).isEqualTo(403); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void whenAccessIsDeniedAndNoErrorCodeAttributeOnRequest() throws Exception { |
| 83 | + given(this.privilegeEvaluator.isAllowed(anyString(), any())).willReturn(false); |
| 84 | + this.securityFilter.doFilter(this.request, this.response, this.filterChain); |
| 85 | + verifyNoInteractions(this.filterChain); |
| 86 | + assertThat(this.response.getStatus()).isEqualTo(401); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void whenPrivilegeEvaluatorIsNotPresentAccessIsAllowed() throws Exception { |
| 91 | + ApplicationContext context = mock(ApplicationContext.class); |
| 92 | + willThrow(NoSuchBeanDefinitionException.class).given(context).getBean(WebInvocationPrivilegeEvaluator.class); |
| 93 | + ErrorPageSecurityFilter securityFilter = new ErrorPageSecurityFilter(context); |
| 94 | + securityFilter.doFilter(this.request, this.response, this.filterChain); |
| 95 | + verify(this.filterChain).doFilter(this.request, this.response); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments