|
@@ -15,16 +15,13 @@
|
|
|
*/
|
|
|
package org.springframework.security.test.web.support;
|
|
|
|
|
|
-import static org.assertj.core.api.Assertions.assertThat;
|
|
|
-import static org.springframework.security.test.web.support.WebTestUtils.getCsrfTokenRepository;
|
|
|
-import static org.springframework.security.test.web.support.WebTestUtils.getSecurityContextRepository;
|
|
|
-
|
|
|
import org.junit.After;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.mockito.Mock;
|
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
|
+
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
@@ -39,6 +36,10 @@ import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
|
|
|
import org.springframework.web.context.WebApplicationContext;
|
|
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
|
|
|
|
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
|
+import static org.springframework.security.test.web.support.WebTestUtils.getCsrfTokenRepository;
|
|
|
+import static org.springframework.security.test.web.support.WebTestUtils.getSecurityContextRepository;
|
|
|
+
|
|
|
@RunWith(MockitoJUnitRunner.class)
|
|
|
public class WebTestUtilsTests {
|
|
|
@Mock
|
|
@@ -51,72 +52,72 @@ public class WebTestUtilsTests {
|
|
|
|
|
|
@Before
|
|
|
public void setup() {
|
|
|
- request = new MockHttpServletRequest();
|
|
|
+ this.request = new MockHttpServletRequest();
|
|
|
}
|
|
|
|
|
|
@After
|
|
|
public void cleanup() {
|
|
|
- if (context != null) {
|
|
|
- context.close();
|
|
|
+ if (this.context != null) {
|
|
|
+ this.context.close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getCsrfTokenRepositorytNoWac() {
|
|
|
- assertThat(getCsrfTokenRepository(request)).isInstanceOf(
|
|
|
- HttpSessionCsrfTokenRepository.class);
|
|
|
+ assertThat(getCsrfTokenRepository(this.request))
|
|
|
+ .isInstanceOf(HttpSessionCsrfTokenRepository.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getCsrfTokenRepositorytNoSecurity() {
|
|
|
loadConfig(Config.class);
|
|
|
- assertThat(getCsrfTokenRepository(request)).isInstanceOf(
|
|
|
- HttpSessionCsrfTokenRepository.class);
|
|
|
+ assertThat(getCsrfTokenRepository(this.request))
|
|
|
+ .isInstanceOf(HttpSessionCsrfTokenRepository.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getCsrfTokenRepositorytSecurityNoCsrf() {
|
|
|
loadConfig(SecurityNoCsrfConfig.class);
|
|
|
- assertThat(getCsrfTokenRepository(request)).isInstanceOf(
|
|
|
- HttpSessionCsrfTokenRepository.class);
|
|
|
+ assertThat(getCsrfTokenRepository(this.request))
|
|
|
+ .isInstanceOf(HttpSessionCsrfTokenRepository.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getCsrfTokenRepositorytSecurityCustomRepo() {
|
|
|
- CustomSecurityConfig.CONTEXT_REPO = contextRepo;
|
|
|
- CustomSecurityConfig.CSRF_REPO = csrfRepo;
|
|
|
+ CustomSecurityConfig.CONTEXT_REPO = this.contextRepo;
|
|
|
+ CustomSecurityConfig.CSRF_REPO = this.csrfRepo;
|
|
|
loadConfig(CustomSecurityConfig.class);
|
|
|
- assertThat(getCsrfTokenRepository(request)).isSameAs(csrfRepo);
|
|
|
+ assertThat(getCsrfTokenRepository(this.request)).isSameAs(this.csrfRepo);
|
|
|
}
|
|
|
|
|
|
// getSecurityContextRepository
|
|
|
|
|
|
@Test
|
|
|
public void getSecurityContextRepositoryNoWac() {
|
|
|
- assertThat(getSecurityContextRepository(request)).isInstanceOf(
|
|
|
- HttpSessionSecurityContextRepository.class);
|
|
|
+ assertThat(getSecurityContextRepository(this.request))
|
|
|
+ .isInstanceOf(HttpSessionSecurityContextRepository.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getSecurityContextRepositoryNoSecurity() {
|
|
|
loadConfig(Config.class);
|
|
|
- assertThat(getSecurityContextRepository(request)).isInstanceOf(
|
|
|
- HttpSessionSecurityContextRepository.class);
|
|
|
+ assertThat(getSecurityContextRepository(this.request))
|
|
|
+ .isInstanceOf(HttpSessionSecurityContextRepository.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getSecurityContextRepositorySecurityNoCsrf() {
|
|
|
loadConfig(SecurityNoCsrfConfig.class);
|
|
|
- assertThat(getSecurityContextRepository(request)).isInstanceOf(
|
|
|
- HttpSessionSecurityContextRepository.class);
|
|
|
+ assertThat(getSecurityContextRepository(this.request))
|
|
|
+ .isInstanceOf(HttpSessionSecurityContextRepository.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void getSecurityContextRepositorySecurityCustomRepo() {
|
|
|
- CustomSecurityConfig.CONTEXT_REPO = contextRepo;
|
|
|
- CustomSecurityConfig.CSRF_REPO = csrfRepo;
|
|
|
+ CustomSecurityConfig.CONTEXT_REPO = this.contextRepo;
|
|
|
+ CustomSecurityConfig.CSRF_REPO = this.csrfRepo;
|
|
|
loadConfig(CustomSecurityConfig.class);
|
|
|
- assertThat(getSecurityContextRepository(request)).isSameAs(contextRepo);
|
|
|
+ assertThat(getSecurityContextRepository(this.request)).isSameAs(this.contextRepo);
|
|
|
}
|
|
|
|
|
|
// gh-3343
|
|
@@ -124,7 +125,8 @@ public class WebTestUtilsTests {
|
|
|
public void findFilterNoMatchingFilters() {
|
|
|
loadConfig(PartialSecurityConfig.class);
|
|
|
|
|
|
- assertThat(WebTestUtils.findFilter(request, SecurityContextPersistenceFilter.class)).isNull();
|
|
|
+ assertThat(WebTestUtils.findFilter(this.request,
|
|
|
+ SecurityContextPersistenceFilter.class)).isNull();
|
|
|
}
|
|
|
|
|
|
private void loadConfig(Class<?> config) {
|
|
@@ -132,7 +134,7 @@ public class WebTestUtilsTests {
|
|
|
context.register(config);
|
|
|
context.refresh();
|
|
|
this.context = context;
|
|
|
- request.getServletContext().setAttribute(
|
|
|
+ this.request.getServletContext().setAttribute(
|
|
|
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
|
|
|
}
|
|
|
|
|
@@ -167,8 +169,6 @@ public class WebTestUtilsTests {
|
|
|
// @formatter:on
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
static class PartialSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|