|
| 1 | +/* |
| 2 | + * Copyright 2012-present 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.security.test.autoconfigure.webmvc; |
| 18 | + |
| 19 | +import java.util.concurrent.Executor; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import jakarta.servlet.Filter; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.openqa.selenium.htmlunit.HtmlUnitDriver; |
| 26 | + |
| 27 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 28 | +import org.springframework.boot.security.test.autoconfigure.webmvc.SecurityMockMvcAutoConfiguration.SecurityMockMvcBuilderCustomizer; |
| 29 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 30 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 31 | +import org.springframework.boot.webmvc.test.autoconfigure.MockMvcHtmlUnitDriverCustomizer; |
| 32 | +import org.springframework.security.concurrent.DelegatingSecurityContextExecutor; |
| 33 | +import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; |
| 34 | +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; |
| 35 | +import org.springframework.test.web.servlet.MockMvc; |
| 36 | +import org.springframework.test.web.servlet.request.RequestPostProcessor; |
| 37 | +import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; |
| 38 | +import org.springframework.test.web.servlet.setup.MockMvcConfigurer; |
| 39 | +import org.springframework.web.context.WebApplicationContext; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | +import static org.mockito.BDDMockito.then; |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | + |
| 45 | +/** |
| 46 | + * Tests for {@link SecurityMockMvcAutoConfiguration}. |
| 47 | + * |
| 48 | + * @author Dmytro Nosan |
| 49 | + */ |
| 50 | +class SecurityMockMvcAutoConfigurationTests { |
| 51 | + |
| 52 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 53 | + .withConfiguration(AutoConfigurations.of(SecurityMockMvcAutoConfiguration.class)); |
| 54 | + |
| 55 | + @Test |
| 56 | + void securityMockMvcBuilderCustomizerIsNotRegisteredWhenMockMvcIsNotOnTheClasspath() { |
| 57 | + this.contextRunner.withClassLoader(new FilteredClassLoader(MockMvc.class)) |
| 58 | + .with(securityFilterChain()) |
| 59 | + .run((context) -> assertThat(context).doesNotHaveBean(MockMvcHtmlUnitDriverCustomizer.class) |
| 60 | + .doesNotHaveBean(SecurityMockMvcBuilderCustomizer.class)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void securityMockMvcBuilderCustomizerIsNotRegisteredWhenSecurityFilterChainIsMissing() { |
| 65 | + this.contextRunner |
| 66 | + .run((context) -> assertThat(context).doesNotHaveBean(SecurityMockMvcBuilderCustomizer.class)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void htmlUnitDriverCustomizerIsNotRegisteredWhenHtmlUnitIsNotOnTheClasspath() { |
| 71 | + this.contextRunner.withClassLoader(new FilteredClassLoader(HtmlUnitDriver.class)) |
| 72 | + .run((context) -> assertThat(context).doesNotHaveBean(MockMvcHtmlUnitDriverCustomizer.class)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void registersSecurityMockMvcBuilderCustomizerWhenSecurityFilterChainIsPresent() { |
| 77 | + this.contextRunner.with(securityFilterChain()) |
| 78 | + .run((context) -> assertThat(context).hasSingleBean(SecurityMockMvcBuilderCustomizer.class) |
| 79 | + .getBean(SecurityMockMvcBuilderCustomizer.class) |
| 80 | + .satisfies((customizer) -> { |
| 81 | + ConfigurableMockMvcBuilder<?> builder = mock(ConfigurableMockMvcBuilder.class); |
| 82 | + customizer.customize(builder); |
| 83 | + ArgumentCaptor<MockMvcConfigurer> configurerCaptor = ArgumentCaptor |
| 84 | + .forClass(MockMvcConfigurer.class); |
| 85 | + then(builder).should().apply(configurerCaptor.capture()); |
| 86 | + RequestPostProcessor postProcessor = configurerCaptor.getValue() |
| 87 | + .beforeMockMvcCreated(builder, mock(WebApplicationContext.class)); |
| 88 | + assertThat(postProcessor) |
| 89 | + .hasSameClassAs(SecurityMockMvcRequestPostProcessors.testSecurityContext()); |
| 90 | + })); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void registersSecurityMockMvcHtmlUnitDriverCustomizerWhenHtmlUnitIsPresent() { |
| 95 | + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(MockMvcHtmlUnitDriverCustomizer.class) |
| 96 | + .getBean(MockMvcHtmlUnitDriverCustomizer.class) |
| 97 | + .satisfies((customizer) -> { |
| 98 | + HtmlUnitDriver htmlUnitDriver = mock(HtmlUnitDriver.class); |
| 99 | + customizer.customize(htmlUnitDriver); |
| 100 | + ArgumentCaptor<Executor> executorCaptor = ArgumentCaptor.forClass(Executor.class); |
| 101 | + then(htmlUnitDriver).should().setExecutor(executorCaptor.capture()); |
| 102 | + assertThat(executorCaptor.getValue()).isInstanceOf(DelegatingSecurityContextExecutor.class); |
| 103 | + })); |
| 104 | + } |
| 105 | + |
| 106 | + private Function<ApplicationContextRunner, ApplicationContextRunner> securityFilterChain() { |
| 107 | + return (contextRunner) -> contextRunner.withBean(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, |
| 108 | + Filter.class, () -> mock(Filter.class)); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments