Always use 'this.' when accessing fields
Apply an Eclipse cleanup rules to ensure that fields are always accessed using `this.`. This aligns with the style used by Spring Framework and helps users quickly see the difference between a local and member variable. Issue gh-8945
This commit is contained in:
+7
-7
@@ -36,37 +36,37 @@ class DelegatingTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
@Override
|
||||
public void beforeTestClass(TestContext testContext) throws Exception {
|
||||
delegate.beforeTestClass(testContext);
|
||||
this.delegate.beforeTestClass(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
delegate.prepareTestInstance(testContext);
|
||||
this.delegate.prepareTestInstance(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
delegate.beforeTestMethod(testContext);
|
||||
this.delegate.beforeTestMethod(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestExecution(TestContext testContext) throws Exception {
|
||||
delegate.beforeTestExecution(testContext);
|
||||
this.delegate.beforeTestExecution(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestExecution(TestContext testContext) throws Exception {
|
||||
delegate.afterTestExecution(testContext);
|
||||
this.delegate.afterTestExecution(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
delegate.afterTestMethod(testContext);
|
||||
this.delegate.afterTestMethod(testContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestClass(TestContext testContext) throws Exception {
|
||||
delegate.afterTestClass(testContext);
|
||||
this.delegate.afterTestClass(testContext);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
-6
@@ -87,12 +87,12 @@ public class ReactorContextTestExecutionListener extends DelegatingTestExecution
|
||||
|
||||
@Override
|
||||
public Context currentContext() {
|
||||
Context context = delegate.currentContext();
|
||||
Context context = this.delegate.currentContext();
|
||||
if (context.hasKey(CONTEXT_DEFAULTED_ATTR_NAME)) {
|
||||
return context;
|
||||
}
|
||||
context = context.put(CONTEXT_DEFAULTED_ATTR_NAME, Boolean.TRUE);
|
||||
Authentication authentication = securityContext.getAuthentication();
|
||||
Authentication authentication = this.securityContext.getAuthentication();
|
||||
if (authentication == null) {
|
||||
return context;
|
||||
}
|
||||
@@ -102,22 +102,22 @@ public class ReactorContextTestExecutionListener extends DelegatingTestExecution
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
delegate.onSubscribe(s);
|
||||
this.delegate.onSubscribe(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
delegate.onNext(t);
|
||||
this.delegate.onNext(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
delegate.onError(t);
|
||||
this.delegate.onError(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
delegate.onComplete();
|
||||
this.delegate.onComplete();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-8
@@ -32,7 +32,7 @@ public class TestSecurityContextHolderTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
context = SecurityContextHolder.createEmptyContext();
|
||||
this.context = SecurityContextHolder.createEmptyContext();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -42,13 +42,13 @@ public class TestSecurityContextHolderTests {
|
||||
|
||||
@Test
|
||||
public void clearContextClearsBoth() {
|
||||
SecurityContextHolder.setContext(context);
|
||||
TestSecurityContextHolder.setContext(context);
|
||||
SecurityContextHolder.setContext(this.context);
|
||||
TestSecurityContextHolder.setContext(this.context);
|
||||
|
||||
TestSecurityContextHolder.clearContext();
|
||||
|
||||
assertThat(SecurityContextHolder.getContext()).isNotSameAs(context);
|
||||
assertThat(TestSecurityContextHolder.getContext()).isNotSameAs(context);
|
||||
assertThat(SecurityContextHolder.getContext()).isNotSameAs(this.context);
|
||||
assertThat(TestSecurityContextHolder.getContext()).isNotSameAs(this.context);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,10 +59,10 @@ public class TestSecurityContextHolderTests {
|
||||
|
||||
@Test
|
||||
public void setContextSetsBoth() {
|
||||
TestSecurityContextHolder.setContext(context);
|
||||
TestSecurityContextHolder.setContext(this.context);
|
||||
|
||||
assertThat(TestSecurityContextHolder.getContext()).isSameAs(context);
|
||||
assertThat(SecurityContextHolder.getContext()).isSameAs(context);
|
||||
assertThat(TestSecurityContextHolder.getContext()).isSameAs(this.context);
|
||||
assertThat(SecurityContextHolder.getContext()).isSameAs(this.context);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+3
-3
@@ -39,7 +39,7 @@ public class CustomUserDetails implements UserDetails {
|
||||
}
|
||||
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return authorities;
|
||||
return this.authorities;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
@@ -47,7 +47,7 @@ public class CustomUserDetails implements UserDetails {
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public boolean isAccountNonExpired() {
|
||||
@@ -68,7 +68,7 @@ public class CustomUserDetails implements UserDetails {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomUserDetails{" + "username='" + username + '\'' + '}';
|
||||
return "CustomUserDetails{" + "username='" + this.username + '\'' + '}';
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -41,7 +41,7 @@ public class WithMockUserParentTests extends WithMockUserParent {
|
||||
|
||||
@Test
|
||||
public void getMessageWithMockUser() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("user");
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -44,34 +44,34 @@ public class WithMockUserTests {
|
||||
|
||||
@Test(expected = AuthenticationCredentialsNotFoundException.class)
|
||||
public void getMessageUnauthenticated() {
|
||||
messageService.getMessage();
|
||||
this.messageService.getMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void getMessageWithMockUser() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("user");
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser("customUsername")
|
||||
public void getMessageWithMockUserCustomUsername() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("customUsername");
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "admin", roles = { "USER", "ADMIN" })
|
||||
public void getMessageWithMockUserCustomUser() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("admin").contains("ROLE_USER").contains("ROLE_ADMIN");
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "admin", authorities = { "ADMIN", "USER" })
|
||||
public void getMessageWithMockUserCustomAuthorities() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("admin").contains("ADMIN").contains("USER").doesNotContain("ROLE_");
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -49,13 +49,13 @@ public class WithUserDetailsTests {
|
||||
|
||||
@Test(expected = AuthenticationCredentialsNotFoundException.class)
|
||||
public void getMessageUnauthenticated() {
|
||||
messageService.getMessage();
|
||||
this.messageService.getMessage();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithUserDetails
|
||||
public void getMessageWithUserDetails() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("user");
|
||||
assertThat(getPrincipal()).isInstanceOf(CustomUserDetails.class);
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class WithUserDetailsTests {
|
||||
@Test
|
||||
@WithUserDetails("customUsername")
|
||||
public void getMessageWithUserDetailsCustomUsername() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("customUsername");
|
||||
assertThat(getPrincipal()).isInstanceOf(CustomUserDetails.class);
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class WithUserDetailsTests {
|
||||
@Test
|
||||
@WithUserDetails(value = "customUsername", userDetailsServiceBeanName = "myUserDetailsService")
|
||||
public void getMessageWithUserDetailsServiceBeanName() {
|
||||
String message = messageService.getMessage();
|
||||
String message = this.messageService.getMessage();
|
||||
assertThat(message).contains("customUsername");
|
||||
assertThat(getPrincipal()).isInstanceOf(CustomUserDetails.class);
|
||||
}
|
||||
|
||||
+34
-33
@@ -34,73 +34,74 @@ public class WithMockUserSecurityContextFactoryTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
factory = new WithMockUserSecurityContextFactory();
|
||||
this.factory = new WithMockUserSecurityContextFactory();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void usernameNull() {
|
||||
factory.createSecurityContext(withUser);
|
||||
this.factory.createSecurityContext(this.withUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valueDefaultsUsername() {
|
||||
when(withUser.value()).thenReturn("valueUser");
|
||||
when(withUser.password()).thenReturn("password");
|
||||
when(withUser.roles()).thenReturn(new String[] { "USER" });
|
||||
when(withUser.authorities()).thenReturn(new String[] {});
|
||||
when(this.withUser.value()).thenReturn("valueUser");
|
||||
when(this.withUser.password()).thenReturn("password");
|
||||
when(this.withUser.roles()).thenReturn(new String[] { "USER" });
|
||||
when(this.withUser.authorities()).thenReturn(new String[] {});
|
||||
|
||||
assertThat(factory.createSecurityContext(withUser).getAuthentication().getName()).isEqualTo(withUser.value());
|
||||
assertThat(this.factory.createSecurityContext(this.withUser).getAuthentication().getName())
|
||||
.isEqualTo(this.withUser.value());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usernamePrioritizedOverValue() {
|
||||
when(withUser.username()).thenReturn("customUser");
|
||||
when(withUser.password()).thenReturn("password");
|
||||
when(withUser.roles()).thenReturn(new String[] { "USER" });
|
||||
when(withUser.authorities()).thenReturn(new String[] {});
|
||||
when(this.withUser.username()).thenReturn("customUser");
|
||||
when(this.withUser.password()).thenReturn("password");
|
||||
when(this.withUser.roles()).thenReturn(new String[] { "USER" });
|
||||
when(this.withUser.authorities()).thenReturn(new String[] {});
|
||||
|
||||
assertThat(factory.createSecurityContext(withUser).getAuthentication().getName())
|
||||
.isEqualTo(withUser.username());
|
||||
assertThat(this.factory.createSecurityContext(this.withUser).getAuthentication().getName())
|
||||
.isEqualTo(this.withUser.username());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rolesWorks() {
|
||||
when(withUser.value()).thenReturn("valueUser");
|
||||
when(withUser.password()).thenReturn("password");
|
||||
when(withUser.roles()).thenReturn(new String[] { "USER", "CUSTOM" });
|
||||
when(withUser.authorities()).thenReturn(new String[] {});
|
||||
when(this.withUser.value()).thenReturn("valueUser");
|
||||
when(this.withUser.password()).thenReturn("password");
|
||||
when(this.withUser.roles()).thenReturn(new String[] { "USER", "CUSTOM" });
|
||||
when(this.withUser.authorities()).thenReturn(new String[] {});
|
||||
|
||||
assertThat(factory.createSecurityContext(withUser).getAuthentication().getAuthorities()).extracting("authority")
|
||||
.containsOnly("ROLE_USER", "ROLE_CUSTOM");
|
||||
assertThat(this.factory.createSecurityContext(this.withUser).getAuthentication().getAuthorities())
|
||||
.extracting("authority").containsOnly("ROLE_USER", "ROLE_CUSTOM");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authoritiesWorks() {
|
||||
when(withUser.value()).thenReturn("valueUser");
|
||||
when(withUser.password()).thenReturn("password");
|
||||
when(withUser.roles()).thenReturn(new String[] { "USER" });
|
||||
when(withUser.authorities()).thenReturn(new String[] { "USER", "CUSTOM" });
|
||||
when(this.withUser.value()).thenReturn("valueUser");
|
||||
when(this.withUser.password()).thenReturn("password");
|
||||
when(this.withUser.roles()).thenReturn(new String[] { "USER" });
|
||||
when(this.withUser.authorities()).thenReturn(new String[] { "USER", "CUSTOM" });
|
||||
|
||||
assertThat(factory.createSecurityContext(withUser).getAuthentication().getAuthorities()).extracting("authority")
|
||||
.containsOnly("USER", "CUSTOM");
|
||||
assertThat(this.factory.createSecurityContext(this.withUser).getAuthentication().getAuthorities())
|
||||
.extracting("authority").containsOnly("USER", "CUSTOM");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void authoritiesAndRolesInvalid() {
|
||||
when(withUser.value()).thenReturn("valueUser");
|
||||
when(withUser.roles()).thenReturn(new String[] { "CUSTOM" });
|
||||
when(withUser.authorities()).thenReturn(new String[] { "USER", "CUSTOM" });
|
||||
when(this.withUser.value()).thenReturn("valueUser");
|
||||
when(this.withUser.roles()).thenReturn(new String[] { "CUSTOM" });
|
||||
when(this.withUser.authorities()).thenReturn(new String[] { "USER", "CUSTOM" });
|
||||
|
||||
factory.createSecurityContext(withUser);
|
||||
this.factory.createSecurityContext(this.withUser);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rolesWithRolePrefixFails() {
|
||||
when(withUser.value()).thenReturn("valueUser");
|
||||
when(withUser.roles()).thenReturn(new String[] { "ROLE_FAIL" });
|
||||
when(withUser.authorities()).thenReturn(new String[] {});
|
||||
when(this.withUser.value()).thenReturn("valueUser");
|
||||
when(this.withUser.roles()).thenReturn(new String[] { "ROLE_FAIL" });
|
||||
when(this.withUser.authorities()).thenReturn(new String[] {});
|
||||
|
||||
factory.createSecurityContext(withUser);
|
||||
this.factory.createSecurityContext(this.withUser);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
-10
@@ -60,15 +60,15 @@ public class WithSecurityContextTestExcecutionListenerTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
listener = new WithSecurityContextTestExecutionListener();
|
||||
context = new AnnotationConfigApplicationContext(Config.class);
|
||||
this.listener = new WithSecurityContextTestExecutionListener();
|
||||
this.context = new AnnotationConfigApplicationContext(Config.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
TestSecurityContextHolder.clearContext();
|
||||
if (context != null) {
|
||||
context.close();
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,20 +76,20 @@ public class WithSecurityContextTestExcecutionListenerTests {
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void beforeTestMethodNullSecurityContextNoError() throws Exception {
|
||||
Class testClass = FakeTest.class;
|
||||
when(testContext.getTestClass()).thenReturn(testClass);
|
||||
when(testContext.getTestMethod()).thenReturn(ReflectionUtils.findMethod(testClass, "testNoAnnotation"));
|
||||
when(this.testContext.getTestClass()).thenReturn(testClass);
|
||||
when(this.testContext.getTestMethod()).thenReturn(ReflectionUtils.findMethod(testClass, "testNoAnnotation"));
|
||||
|
||||
listener.beforeTestMethod(testContext);
|
||||
this.listener.beforeTestMethod(this.testContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void beforeTestMethodNoApplicationContext() throws Exception {
|
||||
Class testClass = FakeTest.class;
|
||||
when(testContext.getApplicationContext()).thenThrow(new IllegalStateException());
|
||||
when(testContext.getTestMethod()).thenReturn(ReflectionUtils.findMethod(testClass, "testWithMockUser"));
|
||||
when(this.testContext.getApplicationContext()).thenThrow(new IllegalStateException());
|
||||
when(this.testContext.getTestMethod()).thenReturn(ReflectionUtils.findMethod(testClass, "testWithMockUser"));
|
||||
|
||||
listener.beforeTestMethod(testContext);
|
||||
this.listener.beforeTestMethod(this.testContext);
|
||||
|
||||
assertThat(TestSecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("user");
|
||||
}
|
||||
|
||||
+26
-26
@@ -57,33 +57,33 @@ public class WithUserDetailsSecurityContextFactoryTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
factory = new WithUserDetailsSecurityContextFactory(beans);
|
||||
this.factory = new WithUserDetailsSecurityContextFactory(this.beans);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void createSecurityContextNullValue() {
|
||||
factory.createSecurityContext(withUserDetails);
|
||||
this.factory.createSecurityContext(this.withUserDetails);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void createSecurityContextEmptyValue() {
|
||||
|
||||
when(withUserDetails.value()).thenReturn("");
|
||||
factory.createSecurityContext(withUserDetails);
|
||||
when(this.withUserDetails.value()).thenReturn("");
|
||||
this.factory.createSecurityContext(this.withUserDetails);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSecurityContextWithExistingUser() {
|
||||
String username = "user";
|
||||
when(this.beans.getBean(ReactiveUserDetailsService.class)).thenThrow(new NoSuchBeanDefinitionException(""));
|
||||
when(beans.getBean(UserDetailsService.class)).thenReturn(userDetailsService);
|
||||
when(withUserDetails.value()).thenReturn(username);
|
||||
when(userDetailsService.loadUserByUsername(username)).thenReturn(userDetails);
|
||||
when(this.beans.getBean(UserDetailsService.class)).thenReturn(this.userDetailsService);
|
||||
when(this.withUserDetails.value()).thenReturn(username);
|
||||
when(this.userDetailsService.loadUserByUsername(username)).thenReturn(this.userDetails);
|
||||
|
||||
SecurityContext context = factory.createSecurityContext(withUserDetails);
|
||||
SecurityContext context = this.factory.createSecurityContext(this.withUserDetails);
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails);
|
||||
verify(beans).getBean(UserDetailsService.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(this.userDetails);
|
||||
verify(this.beans).getBean(UserDetailsService.class);
|
||||
}
|
||||
|
||||
// gh-3346
|
||||
@@ -93,27 +93,27 @@ public class WithUserDetailsSecurityContextFactoryTests {
|
||||
String username = "user";
|
||||
when(this.beans.getBean(beanName, ReactiveUserDetailsService.class)).thenThrow(
|
||||
new BeanNotOfRequiredTypeException("", ReactiveUserDetailsService.class, UserDetailsService.class));
|
||||
when(withUserDetails.value()).thenReturn(username);
|
||||
when(withUserDetails.userDetailsServiceBeanName()).thenReturn(beanName);
|
||||
when(userDetailsService.loadUserByUsername(username)).thenReturn(userDetails);
|
||||
when(beans.getBean(beanName, UserDetailsService.class)).thenReturn(userDetailsService);
|
||||
when(this.withUserDetails.value()).thenReturn(username);
|
||||
when(this.withUserDetails.userDetailsServiceBeanName()).thenReturn(beanName);
|
||||
when(this.userDetailsService.loadUserByUsername(username)).thenReturn(this.userDetails);
|
||||
when(this.beans.getBean(beanName, UserDetailsService.class)).thenReturn(this.userDetailsService);
|
||||
|
||||
SecurityContext context = factory.createSecurityContext(withUserDetails);
|
||||
SecurityContext context = this.factory.createSecurityContext(this.withUserDetails);
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails);
|
||||
verify(beans).getBean(beanName, UserDetailsService.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(this.userDetails);
|
||||
verify(this.beans).getBean(beanName, UserDetailsService.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSecurityContextWithReactiveUserDetailsService() {
|
||||
String username = "user";
|
||||
when(withUserDetails.value()).thenReturn(username);
|
||||
when(this.withUserDetails.value()).thenReturn(username);
|
||||
when(this.beans.getBean(ReactiveUserDetailsService.class)).thenReturn(this.reactiveUserDetailsService);
|
||||
when(this.reactiveUserDetailsService.findByUsername(username)).thenReturn(Mono.just(userDetails));
|
||||
when(this.reactiveUserDetailsService.findByUsername(username)).thenReturn(Mono.just(this.userDetails));
|
||||
|
||||
SecurityContext context = factory.createSecurityContext(withUserDetails);
|
||||
SecurityContext context = this.factory.createSecurityContext(this.withUserDetails);
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(this.userDetails);
|
||||
verify(this.beans).getBean(ReactiveUserDetailsService.class);
|
||||
}
|
||||
|
||||
@@ -121,15 +121,15 @@ public class WithUserDetailsSecurityContextFactoryTests {
|
||||
public void createSecurityContextWithReactiveUserDetailsServiceAndBeanName() {
|
||||
String beanName = "secondUserDetailsServiceBean";
|
||||
String username = "user";
|
||||
when(withUserDetails.value()).thenReturn(username);
|
||||
when(withUserDetails.userDetailsServiceBeanName()).thenReturn(beanName);
|
||||
when(this.withUserDetails.value()).thenReturn(username);
|
||||
when(this.withUserDetails.userDetailsServiceBeanName()).thenReturn(beanName);
|
||||
when(this.beans.getBean(beanName, ReactiveUserDetailsService.class))
|
||||
.thenReturn(this.reactiveUserDetailsService);
|
||||
when(this.reactiveUserDetailsService.findByUsername(username)).thenReturn(Mono.just(userDetails));
|
||||
when(this.reactiveUserDetailsService.findByUsername(username)).thenReturn(Mono.just(this.userDetails));
|
||||
|
||||
SecurityContext context = factory.createSecurityContext(withUserDetails);
|
||||
SecurityContext context = this.factory.createSecurityContext(this.withUserDetails);
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isEqualTo(this.userDetails);
|
||||
verify(this.beans).getBean(beanName, ReactiveUserDetailsService.class);
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -50,7 +50,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
|
||||
private GrantedAuthority authority2 = new SimpleGrantedAuthority("two");
|
||||
|
||||
private WebTestClient client = WebTestClient.bindToController(securityContextController)
|
||||
private WebTestClient client = WebTestClient.bindToController(this.securityContextController)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter())
|
||||
.argumentResolvers(resolvers -> resolvers
|
||||
.addCustomResolver(new CurrentSecurityContextArgumentResolver(new ReactiveAdapterRegistry())))
|
||||
@@ -61,7 +61,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
public void mockOpaqueTokenWhenUsingDefaultsThenBearerTokenAuthentication() {
|
||||
this.client.mutateWith(mockOpaqueToken()).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(BearerTokenAuthentication.class);
|
||||
BearerTokenAuthentication token = (BearerTokenAuthentication) context.getAuthentication();
|
||||
assertThat(token.getAuthorities()).isNotEmpty();
|
||||
@@ -74,7 +74,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
this.client.mutateWith(mockOpaqueToken().authorities(this.authority1, this.authority2)).get().exchange()
|
||||
.expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(this.authority1,
|
||||
this.authority2);
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
this.client.mutateWith(mockOpaqueToken().attributes(attributes -> attributes.put(SUBJECT, sub))).get()
|
||||
.exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(BearerTokenAuthentication.class);
|
||||
BearerTokenAuthentication token = (BearerTokenAuthentication) context.getAuthentication();
|
||||
assertThat(token.getTokenAttributes().get(SUBJECT)).isSameAs(sub);
|
||||
@@ -96,7 +96,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
OAuth2AuthenticatedPrincipal principal = active();
|
||||
this.client.mutateWith(mockOpaqueToken().principal(principal)).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(BearerTokenAuthentication.class);
|
||||
BearerTokenAuthentication token = (BearerTokenAuthentication) context.getAuthentication();
|
||||
assertThat(token.getPrincipal()).isSameAs(principal);
|
||||
@@ -109,7 +109,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
this.client.mutateWith(mockOpaqueToken().attributes(a -> a.put(SUBJECT, "foo")).principal(principal)).get()
|
||||
.exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(BearerTokenAuthentication.class);
|
||||
BearerTokenAuthentication token = (BearerTokenAuthentication) context.getAuthentication();
|
||||
assertThat((String) ((OAuth2AuthenticatedPrincipal) token.getPrincipal()).getAttribute(SUBJECT))
|
||||
@@ -118,7 +118,7 @@ public class SecurityMockServerConfigurerOpaqueTokenTests extends AbstractMockSe
|
||||
this.client.mutateWith(mockOpaqueToken().principal(principal).attributes(a -> a.put(SUBJECT, "bar"))).get()
|
||||
.exchange().expectStatus().isOk();
|
||||
|
||||
context = securityContextController.removeSecurityContext();
|
||||
context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(BearerTokenAuthentication.class);
|
||||
token = (BearerTokenAuthentication) context.getAuthentication();
|
||||
assertThat((String) ((OAuth2AuthenticatedPrincipal) token.getPrincipal()).getAttribute(SUBJECT))
|
||||
|
||||
+20
-19
@@ -43,17 +43,17 @@ import static org.springframework.security.test.web.reactive.server.SecurityMock
|
||||
@SecurityTestExecutionListeners
|
||||
public class SecurityMockServerConfigurersAnnotatedTests extends AbstractMockServerConfigurersTests {
|
||||
|
||||
WebTestClient client = WebTestClient.bindToController(controller)
|
||||
WebTestClient client = WebTestClient.bindToController(this.controller)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter()).apply(springSecurity()).configureClient()
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void withMockUserWhenOnMethodThenSuccess() {
|
||||
client.get().exchange().expectStatus().isOk();
|
||||
this.client.get().exchange().expectStatus().isOk();
|
||||
|
||||
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -61,13 +61,14 @@ public class SecurityMockServerConfigurersAnnotatedTests extends AbstractMockSer
|
||||
public void withMockUserWhenGlobalMockPrincipalThenOverridesAnnotation() {
|
||||
TestingAuthenticationToken authentication = new TestingAuthenticationToken("authentication", "secret",
|
||||
"ROLE_USER");
|
||||
client = WebTestClient.bindToController(controller).webFilter(new SecurityContextServerWebExchangeWebFilter())
|
||||
.apply(springSecurity()).apply(mockAuthentication(authentication)).configureClient()
|
||||
this.client = WebTestClient.bindToController(this.controller)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter()).apply(springSecurity())
|
||||
.apply(mockAuthentication(authentication)).configureClient()
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
|
||||
client.get().exchange().expectStatus().isOk();
|
||||
this.client.get().exchange().expectStatus().isOk();
|
||||
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -75,9 +76,9 @@ public class SecurityMockServerConfigurersAnnotatedTests extends AbstractMockSer
|
||||
public void withMockUserWhenMutateWithMockPrincipalThenOverridesAnnotation() {
|
||||
TestingAuthenticationToken authentication = new TestingAuthenticationToken("authentication", "secret",
|
||||
"ROLE_USER");
|
||||
client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk();
|
||||
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,22 +86,22 @@ public class SecurityMockServerConfigurersAnnotatedTests extends AbstractMockSer
|
||||
public void withMockUserWhenMutateWithMockPrincipalAndNoMutateThenOverridesAnnotationAndUsesAnnotation() {
|
||||
TestingAuthenticationToken authentication = new TestingAuthenticationToken("authentication", "secret",
|
||||
"ROLE_USER");
|
||||
client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk();
|
||||
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
|
||||
client.get().exchange().expectStatus().isOk();
|
||||
this.client.get().exchange().expectStatus().isOk();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(controller.removePrincipal(), userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(this.controller.removePrincipal(), this.userBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void withMockUserWhenOnMethodAndRequestIsExecutedOnDifferentThreadThenSuccess() {
|
||||
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
|
||||
ForkJoinPool.commonPool().submit(() -> client.get().exchange().expectStatus().isOk()).join();
|
||||
ForkJoinPool.commonPool().submit(() -> this.client.get().exchange().expectStatus().isOk()).join();
|
||||
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,14 +111,14 @@ public class SecurityMockServerConfigurersAnnotatedTests extends AbstractMockSer
|
||||
"ROLE_USER");
|
||||
|
||||
ForkJoinPool.commonPool().submit(
|
||||
() -> client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk())
|
||||
() -> this.client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk())
|
||||
.join();
|
||||
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
|
||||
ForkJoinPool.commonPool().submit(() -> client.get().exchange().expectStatus().isOk()).join();
|
||||
ForkJoinPool.commonPool().submit(() -> this.client.get().exchange().expectStatus().isOk()).join();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(controller.removePrincipal(), userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(this.controller.removePrincipal(), this.userBuilder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-8
@@ -44,37 +44,37 @@ import static org.springframework.security.test.web.reactive.server.SecurityMock
|
||||
@SecurityTestExecutionListeners
|
||||
public class SecurityMockServerConfigurersClassAnnotatedTests extends AbstractMockServerConfigurersTests {
|
||||
|
||||
WebTestClient client = WebTestClient.bindToController(controller)
|
||||
WebTestClient client = WebTestClient.bindToController(this.controller)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter()).apply(springSecurity()).configureClient()
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
|
||||
@Test
|
||||
public void wheMockUserWhenClassAnnotatedThenSuccess() {
|
||||
client.get().exchange().expectStatus().isOk().expectBody(String.class)
|
||||
this.client.get().exchange().expectStatus().isOk().expectBody(String.class)
|
||||
.consumeWith(response -> assertThat(response.getResponseBody()).contains("\"username\":\"user\""));
|
||||
|
||||
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser("method-user")
|
||||
public void withMockUserWhenClassAndMethodAnnotationThenMethodOverrides() {
|
||||
client.get().exchange().expectStatus().isOk().expectBody(String.class).consumeWith(
|
||||
this.client.get().exchange().expectStatus().isOk().expectBody(String.class).consumeWith(
|
||||
response -> assertThat(response.getResponseBody()).contains("\"username\":\"method-user\""));
|
||||
|
||||
Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication();
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withMockUserWhenMutateWithThenMustateWithOverrides() {
|
||||
client.mutateWith(mockUser("mutateWith-mockUser")).get().exchange().expectStatus().isOk()
|
||||
this.client.mutateWith(mockUser("mutateWith-mockUser")).get().exchange().expectStatus().isOk()
|
||||
.expectBody(String.class).consumeWith(response -> assertThat(response.getResponseBody())
|
||||
.contains("\"username\":\"mutateWith-mockUser\""));
|
||||
|
||||
Principal principal = controller.removePrincipal();
|
||||
assertPrincipalCreatedFromUserDetails(principal, userBuilder.username("mutateWith-mockUser").build());
|
||||
Principal principal = this.controller.removePrincipal();
|
||||
assertPrincipalCreatedFromUserDetails(principal, this.userBuilder.username("mutateWith-mockUser").build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-13
@@ -54,7 +54,7 @@ public class SecurityMockServerConfigurersJwtTests extends AbstractMockServerCon
|
||||
@Mock
|
||||
GrantedAuthority authority2;
|
||||
|
||||
WebTestClient client = WebTestClient.bindToController(securityContextController)
|
||||
WebTestClient client = WebTestClient.bindToController(this.securityContextController)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter())
|
||||
.argumentResolvers(resolvers -> resolvers
|
||||
.addCustomResolver(new CurrentSecurityContextArgumentResolver(new ReactiveAdapterRegistry())))
|
||||
@@ -63,9 +63,9 @@ public class SecurityMockServerConfigurersJwtTests extends AbstractMockServerCon
|
||||
|
||||
@Test
|
||||
public void mockJwtWhenUsingDefaultsTheCreatesJwtAuthentication() {
|
||||
client.mutateWith(mockJwt()).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockJwt()).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(JwtAuthenticationToken.class);
|
||||
JwtAuthenticationToken token = (JwtAuthenticationToken) context.getAuthentication();
|
||||
assertThat(token.getAuthorities()).isNotEmpty();
|
||||
@@ -77,9 +77,9 @@ public class SecurityMockServerConfigurersJwtTests extends AbstractMockServerCon
|
||||
@Test
|
||||
public void mockJwtWhenProvidingBuilderConsumerThenProducesJwtAuthentication() {
|
||||
String name = new String("user");
|
||||
client.mutateWith(mockJwt().jwt(jwt -> jwt.subject(name))).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockJwt().jwt(jwt -> jwt.subject(name))).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(JwtAuthenticationToken.class);
|
||||
JwtAuthenticationToken token = (JwtAuthenticationToken) context.getAuthentication();
|
||||
assertThat(token.getToken().getSubject()).isSameAs(name);
|
||||
@@ -87,30 +87,30 @@ public class SecurityMockServerConfigurersJwtTests extends AbstractMockServerCon
|
||||
|
||||
@Test
|
||||
public void mockJwtWhenProvidingCustomAuthoritiesThenProducesJwtAuthentication() {
|
||||
client.mutateWith(mockJwt().jwt(jwt -> jwt.claim("scope", "ignored authorities")).authorities(this.authority1,
|
||||
this.authority2)).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockJwt().jwt(jwt -> jwt.claim("scope", "ignored authorities"))
|
||||
.authorities(this.authority1, this.authority2)).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(this.authority1,
|
||||
this.authority2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockJwtWhenProvidingScopedAuthoritiesThenProducesJwtAuthentication() {
|
||||
client.mutateWith(mockJwt().jwt(jwt -> jwt.claim("scope", "scoped authorities"))).get().exchange()
|
||||
this.client.mutateWith(mockJwt().jwt(jwt -> jwt.claim("scope", "scoped authorities"))).get().exchange()
|
||||
.expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(
|
||||
new SimpleGrantedAuthority("SCOPE_scoped"), new SimpleGrantedAuthority("SCOPE_authorities"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockJwtWhenProvidingGrantedAuthoritiesThenProducesJwtAuthentication() {
|
||||
client.mutateWith(mockJwt().jwt(jwt -> jwt.claim("scope", "ignored authorities"))
|
||||
this.client.mutateWith(mockJwt().jwt(jwt -> jwt.claim("scope", "ignored authorities"))
|
||||
.authorities(jwt -> Arrays.asList(this.authority1))).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(this.authority1);
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ public class SecurityMockServerConfigurersJwtTests extends AbstractMockServerCon
|
||||
Jwt originalToken = TestJwts.jwt().header("header1", "value1").subject("some_user").build();
|
||||
this.client.mutateWith(mockJwt().jwt(originalToken)).get().exchange().expectStatus().isOk();
|
||||
|
||||
SecurityContext context = securityContextController.removeSecurityContext();
|
||||
SecurityContext context = this.securityContextController.removeSecurityContext();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(JwtAuthenticationToken.class);
|
||||
JwtAuthenticationToken retrievedToken = (JwtAuthenticationToken) context.getAuthentication();
|
||||
assertThat(retrievedToken.getToken().getSubject()).isEqualTo("some_user");
|
||||
|
||||
+27
-26
@@ -42,7 +42,7 @@ import static org.springframework.security.test.web.reactive.server.SecurityMock
|
||||
*/
|
||||
public class SecurityMockServerConfigurersTests extends AbstractMockServerConfigurersTests {
|
||||
|
||||
WebTestClient client = WebTestClient.bindToController(controller)
|
||||
WebTestClient client = WebTestClient.bindToController(this.controller)
|
||||
.webFilter(new CsrfWebFilter(), new SecurityContextServerWebExchangeWebFilter()).apply(springSecurity())
|
||||
.configureClient().defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
|
||||
@@ -50,70 +50,71 @@ public class SecurityMockServerConfigurersTests extends AbstractMockServerConfig
|
||||
public void mockAuthenticationWhenLocalThenSuccess() {
|
||||
TestingAuthenticationToken authentication = new TestingAuthenticationToken("authentication", "secret",
|
||||
"ROLE_USER");
|
||||
client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk();
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.client.mutateWith(mockAuthentication(authentication)).get().exchange().expectStatus().isOk();
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockAuthenticationWhenGlobalThenSuccess() {
|
||||
TestingAuthenticationToken authentication = new TestingAuthenticationToken("authentication", "secret",
|
||||
"ROLE_USER");
|
||||
client = WebTestClient.bindToController(controller).webFilter(new SecurityContextServerWebExchangeWebFilter())
|
||||
.apply(springSecurity()).apply(mockAuthentication(authentication)).configureClient()
|
||||
this.client = WebTestClient.bindToController(this.controller)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter()).apply(springSecurity())
|
||||
.apply(mockAuthentication(authentication)).configureClient()
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
client.get().exchange().expectStatus().isOk();
|
||||
controller.assertPrincipalIsEqualTo(authentication);
|
||||
this.client.get().exchange().expectStatus().isOk();
|
||||
this.controller.assertPrincipalIsEqualTo(authentication);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockUserWhenDefaultsThenSuccess() {
|
||||
client.mutateWith(mockUser()).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockUser()).get().exchange().expectStatus().isOk();
|
||||
|
||||
Principal actual = controller.removePrincipal();
|
||||
Principal actual = this.controller.removePrincipal();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(actual, userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(actual, this.userBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockUserWhenGlobalThenSuccess() {
|
||||
client = WebTestClient.bindToController(controller).webFilter(new SecurityContextServerWebExchangeWebFilter())
|
||||
.apply(springSecurity()).apply(mockUser()).configureClient()
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
client.get().exchange().expectStatus().isOk();
|
||||
this.client = WebTestClient.bindToController(this.controller)
|
||||
.webFilter(new SecurityContextServerWebExchangeWebFilter()).apply(springSecurity()).apply(mockUser())
|
||||
.configureClient().defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();
|
||||
this.client.get().exchange().expectStatus().isOk();
|
||||
|
||||
Principal actual = controller.removePrincipal();
|
||||
Principal actual = this.controller.removePrincipal();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(actual, userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(actual, this.userBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockUserStringWhenLocalThenSuccess() {
|
||||
client.mutateWith(mockUser(userBuilder.build().getUsername())).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockUser(this.userBuilder.build().getUsername())).get().exchange().expectStatus().isOk();
|
||||
|
||||
Principal actual = controller.removePrincipal();
|
||||
Principal actual = this.controller.removePrincipal();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(actual, userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(actual, this.userBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockUserStringWhenCustomThenSuccess() {
|
||||
this.userBuilder = User.withUsername("admin").password("secret").roles("USER", "ADMIN");
|
||||
client.mutateWith(mockUser("admin").password("secret").roles("USER", "ADMIN")).get().exchange().expectStatus()
|
||||
.isOk();
|
||||
this.client.mutateWith(mockUser("admin").password("secret").roles("USER", "ADMIN")).get().exchange()
|
||||
.expectStatus().isOk();
|
||||
|
||||
Principal actual = controller.removePrincipal();
|
||||
Principal actual = this.controller.removePrincipal();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(actual, userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(actual, this.userBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockUserUserDetailsLocalThenSuccess() {
|
||||
UserDetails userDetails = this.userBuilder.build();
|
||||
client.mutateWith(mockUser(userDetails)).get().exchange().expectStatus().isOk();
|
||||
this.client.mutateWith(mockUser(userDetails)).get().exchange().expectStatus().isOk();
|
||||
|
||||
Principal actual = controller.removePrincipal();
|
||||
Principal actual = this.controller.removePrincipal();
|
||||
|
||||
assertPrincipalCreatedFromUserDetails(actual, userBuilder.build());
|
||||
assertPrincipalCreatedFromUserDetails(actual, this.userBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+13
-13
@@ -57,47 +57,47 @@ public class Sec2935Tests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
// SEC-2935
|
||||
@Test
|
||||
public void postProcessorUserNoUser() throws Exception {
|
||||
mvc.perform(get("/admin/abc").with(user("user").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
this.mvc.perform(get("/admin/abc").with(user("user").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
|
||||
mvc.perform(get("/admin/abc")).andExpect(status().isUnauthorized()).andExpect(unauthenticated());
|
||||
this.mvc.perform(get("/admin/abc")).andExpect(status().isUnauthorized()).andExpect(unauthenticated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessorUserOtherUser() throws Exception {
|
||||
mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
this.mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user1"));
|
||||
|
||||
mvc.perform(get("/admin/abc").with(user("user2").roles("USER"))).andExpect(status().isForbidden())
|
||||
this.mvc.perform(get("/admin/abc").with(user("user2").roles("USER"))).andExpect(status().isForbidden())
|
||||
.andExpect(authenticated().withUsername("user2"));
|
||||
}
|
||||
|
||||
@WithMockUser
|
||||
@Test
|
||||
public void postProcessorUserWithMockUser() throws Exception {
|
||||
mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
this.mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user1"));
|
||||
|
||||
mvc.perform(get("/admin/abc")).andExpect(status().isForbidden())
|
||||
this.mvc.perform(get("/admin/abc")).andExpect(status().isForbidden())
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
}
|
||||
|
||||
// SEC-2941
|
||||
@Test
|
||||
public void defaultRequest() throws Exception {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity())
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity())
|
||||
.defaultRequest(get("/").with(user("default"))).build();
|
||||
|
||||
mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
this.mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user1"));
|
||||
|
||||
mvc.perform(get("/admin/abc")).andExpect(status().isForbidden())
|
||||
this.mvc.perform(get("/admin/abc")).andExpect(status().isForbidden())
|
||||
.andExpect(authenticated().withUsername("default"));
|
||||
}
|
||||
|
||||
@@ -105,13 +105,13 @@ public class Sec2935Tests {
|
||||
@WithMockUser
|
||||
@Test
|
||||
public void defaultRequestOverridesWithMockUser() throws Exception {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity())
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity())
|
||||
.defaultRequest(get("/").with(user("default"))).build();
|
||||
|
||||
mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
this.mvc.perform(get("/admin/abc").with(user("user1").roles("ADMIN", "USER"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user1"));
|
||||
|
||||
mvc.perform(get("/admin/abc")).andExpect(status().isForbidden())
|
||||
this.mvc.perform(get("/admin/abc")).andExpect(status().isForbidden())
|
||||
.andExpect(authenticated().withUsername("default"));
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -45,12 +45,12 @@ public class SecurityMockMvcRequestBuildersFormLogoutTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
servletContext = new MockServletContext();
|
||||
this.servletContext = new MockServletContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaults() {
|
||||
MockHttpServletRequest request = logout().buildRequest(servletContext);
|
||||
MockHttpServletRequest request = logout().buildRequest(this.servletContext);
|
||||
|
||||
CsrfToken token = (CsrfToken) request
|
||||
.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
|
||||
@@ -62,7 +62,7 @@ public class SecurityMockMvcRequestBuildersFormLogoutTests {
|
||||
|
||||
@Test
|
||||
public void custom() {
|
||||
MockHttpServletRequest request = logout("/admin/logout").buildRequest(servletContext);
|
||||
MockHttpServletRequest request = logout("/admin/logout").buildRequest(this.servletContext);
|
||||
|
||||
CsrfToken token = (CsrfToken) request
|
||||
.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
|
||||
@@ -75,7 +75,7 @@ public class SecurityMockMvcRequestBuildersFormLogoutTests {
|
||||
@Test
|
||||
public void customWithUriVars() {
|
||||
MockHttpServletRequest request = logout().logoutUrl("/uri-logout/{var1}/{var2}", "val1", "val2")
|
||||
.buildRequest(servletContext);
|
||||
.buildRequest(this.servletContext);
|
||||
|
||||
CsrfToken token = (CsrfToken) request
|
||||
.getAttribute(CsrfRequestPostProcessor.TestCsrfTokenRepository.TOKEN_ATTR_NAME);
|
||||
|
||||
+3
-3
@@ -53,20 +53,20 @@ public class SecurityMockMvcRequestPostProcessorsAuthenticationStatelessTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
// SEC-2593
|
||||
@Test
|
||||
public void userRequestPostProcessorWorksWithStateless() throws Exception {
|
||||
mvc.perform(get("/").with(user("user"))).andExpect(status().is2xxSuccessful());
|
||||
this.mvc.perform(get("/").with(user("user"))).andExpect(status().is2xxSuccessful());
|
||||
}
|
||||
|
||||
// SEC-2593
|
||||
@WithMockUser
|
||||
@Test
|
||||
public void withMockUserWorksWithStateless() throws Exception {
|
||||
mvc.perform(get("/")).andExpect(status().is2xxSuccessful());
|
||||
this.mvc.perform(get("/")).andExpect(status().is2xxSuccessful());
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+7
-6
@@ -62,7 +62,7 @@ public class SecurityMockMvcRequestPostProcessorsAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
mockWebTestUtils();
|
||||
}
|
||||
|
||||
@@ -73,16 +73,17 @@ public class SecurityMockMvcRequestPostProcessorsAuthenticationTests {
|
||||
|
||||
@Test
|
||||
public void userDetails() {
|
||||
authentication(authentication).postProcessRequest(request);
|
||||
authentication(this.authentication).postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
assertThat(context.getAuthentication()).isSameAs(authentication);
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat(context.getAuthentication()).isSameAs(this.authentication);
|
||||
}
|
||||
|
||||
private void mockWebTestUtils() {
|
||||
spy(WebTestUtils.class);
|
||||
when(WebTestUtils.getSecurityContextRepository(request)).thenReturn(repository);
|
||||
when(WebTestUtils.getSecurityContextRepository(this.request)).thenReturn(this.repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-4
@@ -38,22 +38,22 @@ public class SecurityMockMvcRequestPostProcessorsCertificateTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void x509SingleCertificate() {
|
||||
MockHttpServletRequest postProcessedRequest = x509(certificate).postProcessRequest(request);
|
||||
MockHttpServletRequest postProcessedRequest = x509(this.certificate).postProcessRequest(this.request);
|
||||
|
||||
X509Certificate[] certificates = (X509Certificate[]) postProcessedRequest
|
||||
.getAttribute("javax.servlet.request.X509Certificate");
|
||||
|
||||
assertThat(certificates).containsOnly(certificate);
|
||||
assertThat(certificates).containsOnly(this.certificate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void x509ResourceName() throws Exception {
|
||||
MockHttpServletRequest postProcessedRequest = x509("rod.cer").postProcessRequest(request);
|
||||
MockHttpServletRequest postProcessedRequest = x509("rod.cer").postProcessRequest(this.request);
|
||||
|
||||
X509Certificate[] certificates = (X509Certificate[]) postProcessedRequest
|
||||
.getAttribute("javax.servlet.request.X509Certificate");
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ public class SecurityMockMvcRequestPostProcessorsCsrfDebugFilterTests {
|
||||
// SEC-3836
|
||||
@Test
|
||||
public void findCookieCsrfTokenRepository() {
|
||||
MockHttpServletRequest request = post("/").buildRequest(wac.getServletContext());
|
||||
MockHttpServletRequest request = post("/").buildRequest(this.wac.getServletContext());
|
||||
CsrfTokenRepository csrfTokenRepository = WebTestUtils.getCsrfTokenRepository(request);
|
||||
assertThat(csrfTokenRepository).isNotNull();
|
||||
assertThat(csrfTokenRepository).isEqualTo(Config.cookieCsrfTokenRepository);
|
||||
|
||||
+23
-20
@@ -53,16 +53,16 @@ public class SecurityMockMvcRequestPostProcessorsDigestTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.password = "password";
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
|
||||
entryPoint = new DigestAuthenticationEntryPoint();
|
||||
entryPoint.setKey("key");
|
||||
entryPoint.setRealmName("Spring Security");
|
||||
filter = new DigestAuthenticationFilter();
|
||||
filter.setUserDetailsService(
|
||||
username -> new User(username, password, AuthorityUtils.createAuthorityList("ROLE_USER")));
|
||||
filter.setAuthenticationEntryPoint(entryPoint);
|
||||
filter.afterPropertiesSet();
|
||||
this.entryPoint = new DigestAuthenticationEntryPoint();
|
||||
this.entryPoint.setKey("key");
|
||||
this.entryPoint.setRealmName("Spring Security");
|
||||
this.filter = new DigestAuthenticationFilter();
|
||||
this.filter.setUserDetailsService(
|
||||
username -> new User(username, this.password, AuthorityUtils.createAuthorityList("ROLE_USER")));
|
||||
this.filter.setAuthenticationEntryPoint(this.entryPoint);
|
||||
this.filter.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -72,7 +72,7 @@ public class SecurityMockMvcRequestPostProcessorsDigestTests {
|
||||
|
||||
@Test
|
||||
public void digestWithFilter() throws Exception {
|
||||
MockHttpServletRequest postProcessedRequest = digest().postProcessRequest(request);
|
||||
MockHttpServletRequest postProcessedRequest = digest().postProcessRequest(this.request);
|
||||
|
||||
assertThat(extractUser()).isEqualTo("user");
|
||||
}
|
||||
@@ -80,7 +80,7 @@ public class SecurityMockMvcRequestPostProcessorsDigestTests {
|
||||
@Test
|
||||
public void digestWithFilterCustomUsername() throws Exception {
|
||||
String username = "admin";
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).postProcessRequest(request);
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).postProcessRequest(this.request);
|
||||
|
||||
assertThat(extractUser()).isEqualTo(username);
|
||||
}
|
||||
@@ -88,8 +88,9 @@ public class SecurityMockMvcRequestPostProcessorsDigestTests {
|
||||
@Test
|
||||
public void digestWithFilterCustomPassword() throws Exception {
|
||||
String username = "custom";
|
||||
password = "secret";
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).password(password).postProcessRequest(request);
|
||||
this.password = "secret";
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).password(this.password)
|
||||
.postProcessRequest(this.request);
|
||||
|
||||
assertThat(extractUser()).isEqualTo(username);
|
||||
}
|
||||
@@ -97,9 +98,9 @@ public class SecurityMockMvcRequestPostProcessorsDigestTests {
|
||||
@Test
|
||||
public void digestWithFilterCustomRealm() throws Exception {
|
||||
String username = "admin";
|
||||
entryPoint.setRealmName("Custom");
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).realm(entryPoint.getRealmName())
|
||||
.postProcessRequest(request);
|
||||
this.entryPoint.setRealmName("Custom");
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).realm(this.entryPoint.getRealmName())
|
||||
.postProcessRequest(this.request);
|
||||
|
||||
assertThat(extractUser()).isEqualTo(username);
|
||||
}
|
||||
@@ -107,20 +108,22 @@ public class SecurityMockMvcRequestPostProcessorsDigestTests {
|
||||
@Test
|
||||
public void digestWithFilterFails() throws Exception {
|
||||
String username = "admin";
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).realm("Invalid").postProcessRequest(request);
|
||||
MockHttpServletRequest postProcessedRequest = digest(username).realm("Invalid")
|
||||
.postProcessRequest(this.request);
|
||||
|
||||
assertThat(extractUser()).isNull();
|
||||
}
|
||||
|
||||
private String extractUser() throws IOException, ServletException {
|
||||
filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain() {
|
||||
this.filter.doFilter(this.request, new MockHttpServletResponse(), new MockFilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
username = authentication == null ? null : authentication.getName();
|
||||
SecurityMockMvcRequestPostProcessorsDigestTests.this.username = authentication == null ? null
|
||||
: authentication.getName();
|
||||
}
|
||||
});
|
||||
return username;
|
||||
return this.username;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
-5
@@ -61,7 +61,7 @@ public class SecurityMockMvcRequestPostProcessorsSecurityContextTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
mockWebTestUtils();
|
||||
}
|
||||
|
||||
@@ -72,16 +72,17 @@ public class SecurityMockMvcRequestPostProcessorsSecurityContextTests {
|
||||
|
||||
@Test
|
||||
public void userDetails() {
|
||||
securityContext(expectedContext).postProcessRequest(request);
|
||||
securityContext(this.expectedContext).postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat(context).isSameAs(this.expectedContext);
|
||||
}
|
||||
|
||||
private void mockWebTestUtils() {
|
||||
spy(WebTestUtils.class);
|
||||
when(WebTestUtils.getSecurityContextRepository(request)).thenReturn(repository);
|
||||
when(WebTestUtils.getSecurityContextRepository(this.request)).thenReturn(this.repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -57,14 +57,14 @@ public class SecurityMockMvcRequestPostProcessorsTestSecurityContextStatelessTes
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain)
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).addFilters(this.springSecurityFilterChain)
|
||||
.defaultRequest(get("/").with(testSecurityContext())).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void testSecurityContextWithMockUserWorksWithStateless() throws Exception {
|
||||
mvc.perform(get("/")).andExpect(status().is2xxSuccessful());
|
||||
this.mvc.perform(get("/")).andExpect(status().is2xxSuccessful());
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+7
-7
@@ -56,7 +56,7 @@ public class SecurityMockMvcRequestPostProcessorsTestSecurityContextTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
mockWebTestUtils();
|
||||
}
|
||||
|
||||
@@ -67,25 +67,25 @@ public class SecurityMockMvcRequestPostProcessorsTestSecurityContextTests {
|
||||
|
||||
@Test
|
||||
public void testSecurityContextSaves() {
|
||||
TestSecurityContextHolder.setContext(context);
|
||||
TestSecurityContextHolder.setContext(this.context);
|
||||
|
||||
testSecurityContext().postProcessRequest(request);
|
||||
testSecurityContext().postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(eq(context), eq(request), any(HttpServletResponse.class));
|
||||
verify(this.repository).saveContext(eq(this.context), eq(this.request), any(HttpServletResponse.class));
|
||||
}
|
||||
|
||||
// Ensure it does not fail if TestSecurityContextHolder is not initialized
|
||||
@Test
|
||||
public void testSecurityContextNoContext() {
|
||||
testSecurityContext().postProcessRequest(request);
|
||||
testSecurityContext().postProcessRequest(this.request);
|
||||
|
||||
verify(repository, never()).saveContext(any(SecurityContext.class), eq(request),
|
||||
verify(this.repository, never()).saveContext(any(SecurityContext.class), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
}
|
||||
|
||||
private void mockWebTestUtils() {
|
||||
spy(WebTestUtils.class);
|
||||
when(WebTestUtils.getSecurityContextRepository(request)).thenReturn(repository);
|
||||
when(WebTestUtils.getSecurityContextRepository(this.request)).thenReturn(this.repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
-6
@@ -63,7 +63,7 @@ public class SecurityMockMvcRequestPostProcessorsUserDetailsTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
mockWebTestUtils();
|
||||
}
|
||||
|
||||
@@ -74,17 +74,18 @@ public class SecurityMockMvcRequestPostProcessorsUserDetailsTests {
|
||||
|
||||
@Test
|
||||
public void userDetails() {
|
||||
user(userDetails).postProcessRequest(request);
|
||||
user(this.userDetails).postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isSameAs(userDetails);
|
||||
assertThat(context.getAuthentication().getPrincipal()).isSameAs(this.userDetails);
|
||||
}
|
||||
|
||||
private void mockWebTestUtils() {
|
||||
spy(WebTestUtils.class);
|
||||
when(WebTestUtils.getSecurityContextRepository(request)).thenReturn(repository);
|
||||
when(WebTestUtils.getSecurityContextRepository(this.request)).thenReturn(this.repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+23
-19
@@ -69,7 +69,7 @@ public class SecurityMockMvcRequestPostProcessorsUserTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
this.request = new MockHttpServletRequest();
|
||||
mockWebTestUtils();
|
||||
}
|
||||
|
||||
@@ -82,10 +82,11 @@ public class SecurityMockMvcRequestPostProcessorsUserTests {
|
||||
public void userWithDefaults() {
|
||||
String username = "userabc";
|
||||
|
||||
user(username).postProcessRequest(request);
|
||||
user(username).postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getName()).isEqualTo(username);
|
||||
assertThat(context.getAuthentication().getCredentials()).isEqualTo("password");
|
||||
@@ -96,10 +97,11 @@ public class SecurityMockMvcRequestPostProcessorsUserTests {
|
||||
public void userWithCustom() {
|
||||
String username = "customuser";
|
||||
|
||||
user(username).roles("CUSTOM", "ADMIN").password("newpass").postProcessRequest(request);
|
||||
user(username).roles("CUSTOM", "ADMIN").password("newpass").postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat(context.getAuthentication()).isInstanceOf(UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(context.getAuthentication().getName()).isEqualTo(username);
|
||||
assertThat(context.getAuthentication().getCredentials()).isEqualTo("newpass");
|
||||
@@ -111,34 +113,36 @@ public class SecurityMockMvcRequestPostProcessorsUserTests {
|
||||
public void userCustomAuthoritiesVarargs() {
|
||||
String username = "customuser";
|
||||
|
||||
user(username).authorities(authority1, authority2).postProcessRequest(request);
|
||||
user(username).authorities(this.authority1, this.authority2).postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(authority1,
|
||||
authority2);
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(this.authority1,
|
||||
this.authority2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void userRolesWithRolePrefixErrors() {
|
||||
user("user").roles("ROLE_INVALID").postProcessRequest(request);
|
||||
user("user").roles("ROLE_INVALID").postProcessRequest(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userCustomAuthoritiesList() {
|
||||
String username = "customuser";
|
||||
|
||||
user(username).authorities(Arrays.asList(authority1, authority2)).postProcessRequest(request);
|
||||
user(username).authorities(Arrays.asList(this.authority1, this.authority2)).postProcessRequest(this.request);
|
||||
|
||||
verify(repository).saveContext(contextCaptor.capture(), eq(request), any(HttpServletResponse.class));
|
||||
SecurityContext context = contextCaptor.getValue();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(authority1,
|
||||
authority2);
|
||||
verify(this.repository).saveContext(this.contextCaptor.capture(), eq(this.request),
|
||||
any(HttpServletResponse.class));
|
||||
SecurityContext context = this.contextCaptor.getValue();
|
||||
assertThat((List<GrantedAuthority>) context.getAuthentication().getAuthorities()).containsOnly(this.authority1,
|
||||
this.authority2);
|
||||
}
|
||||
|
||||
private void mockWebTestUtils() {
|
||||
spy(WebTestUtils.class);
|
||||
when(WebTestUtils.getSecurityContextRepository(request)).thenReturn(repository);
|
||||
when(WebTestUtils.getSecurityContextRepository(this.request)).thenReturn(this.repository);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-3
@@ -57,7 +57,7 @@ public class SecurityMockWithAuthoritiesMvcResultMatchersTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,14 +65,14 @@ public class SecurityMockWithAuthoritiesMvcResultMatchersTests {
|
||||
List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<>();
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_SELLER"));
|
||||
mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities));
|
||||
this.mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities));
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void withAuthoritiesFailsIfNotAllRoles() throws Exception {
|
||||
List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<>();
|
||||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
|
||||
mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities));
|
||||
this.mockMvc.perform(formLogin()).andExpect(authenticated().withAuthorities(grantedAuthorities));
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+4
-4
@@ -50,22 +50,22 @@ public class CsrfShowcaseTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithCsrfWorks() throws Exception {
|
||||
mvc.perform(post("/").with(csrf())).andExpect(status().isNotFound());
|
||||
this.mvc.perform(post("/").with(csrf())).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithCsrfWorksWithPut() throws Exception {
|
||||
mvc.perform(put("/").with(csrf())).andExpect(status().isNotFound());
|
||||
this.mvc.perform(put("/").with(csrf())).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithNoCsrfForbidden() throws Exception {
|
||||
mvc.perform(post("/")).andExpect(status().isForbidden());
|
||||
this.mvc.perform(post("/")).andExpect(status().isForbidden());
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+4
-4
@@ -57,18 +57,18 @@ public class CustomCsrfShowcaseTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).defaultRequest(get("/").with(csrf())).apply(springSecurity())
|
||||
.build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).defaultRequest(get("/").with(csrf()))
|
||||
.apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithCsrfWorks() throws Exception {
|
||||
mvc.perform(post("/").with(csrf())).andExpect(status().isNotFound());
|
||||
this.mvc.perform(post("/").with(csrf())).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithCsrfWorksWithPut() throws Exception {
|
||||
mvc.perform(put("/").with(csrf())).andExpect(status().isNotFound());
|
||||
this.mvc.perform(put("/").with(csrf())).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+4
-4
@@ -51,18 +51,18 @@ public class DefaultCsrfShowcaseTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).defaultRequest(get("/").with(csrf())).apply(springSecurity())
|
||||
.build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).defaultRequest(get("/").with(csrf()))
|
||||
.apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithCsrfWorks() throws Exception {
|
||||
mvc.perform(post("/")).andExpect(status().isNotFound());
|
||||
this.mvc.perform(post("/")).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postWithCsrfWorksWithPut() throws Exception {
|
||||
mvc.perform(put("/")).andExpect(status().isNotFound());
|
||||
this.mvc.perform(put("/")).andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@EnableWebSecurity
|
||||
|
||||
+6
-6
@@ -57,30 +57,30 @@ public class AuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity())
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity())
|
||||
.defaultRequest(get("/").accept(MediaType.TEXT_HTML)).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requiresAuthentication() throws Exception {
|
||||
mvc.perform(get("/")).andExpect(status().isFound());
|
||||
this.mvc.perform(get("/")).andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpBasicAuthenticationSuccess() throws Exception {
|
||||
mvc.perform(get("/secured/butnotfound").with(httpBasic("user", "password"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
this.mvc.perform(get("/secured/butnotfound").with(httpBasic("user", "password")))
|
||||
.andExpect(status().isNotFound()).andExpect(authenticated().withUsername("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationSuccess() throws Exception {
|
||||
mvc.perform(formLogin()).andExpect(status().isFound()).andExpect(redirectedUrl("/"))
|
||||
this.mvc.perform(formLogin()).andExpect(status().isFound()).andExpect(redirectedUrl("/"))
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationFailed() throws Exception {
|
||||
mvc.perform(formLogin().user("user").password("invalid")).andExpect(status().isFound())
|
||||
this.mvc.perform(formLogin().user("user").password("invalid")).andExpect(status().isFound())
|
||||
.andExpect(redirectedUrl("/login?error")).andExpect(unauthenticated());
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -62,25 +62,25 @@ public class CustomConfigAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationSuccess() throws Exception {
|
||||
mvc.perform(formLogin("/authenticate").user("user", "user").password("pass", "password"))
|
||||
this.mvc.perform(formLogin("/authenticate").user("user", "user").password("pass", "password"))
|
||||
.andExpect(status().isFound()).andExpect(redirectedUrl("/"))
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withUserSuccess() throws Exception {
|
||||
mvc.perform(get("/").with(user("user"))).andExpect(status().isNotFound())
|
||||
this.mvc.perform(get("/").with(user("user"))).andExpect(status().isNotFound())
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationFailed() throws Exception {
|
||||
mvc.perform(formLogin("/authenticate").user("user", "notfound").password("pass", "invalid"))
|
||||
this.mvc.perform(formLogin("/authenticate").user("user", "notfound").password("pass", "invalid"))
|
||||
.andExpect(status().isFound()).andExpect(redirectedUrl("/authenticate?error"))
|
||||
.andExpect(unauthenticated());
|
||||
}
|
||||
|
||||
+3
-3
@@ -56,18 +56,18 @@ public class CustomLoginRequestBuilderAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationSuccess() throws Exception {
|
||||
mvc.perform(login()).andExpect(status().isFound()).andExpect(redirectedUrl("/"))
|
||||
this.mvc.perform(login()).andExpect(status().isFound()).andExpect(redirectedUrl("/"))
|
||||
.andExpect(authenticated().withUsername("user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticationFailed() throws Exception {
|
||||
mvc.perform(login().user("notfound").password("invalid")).andExpect(status().isFound())
|
||||
this.mvc.perform(login().user("notfound").password("invalid")).andExpect(status().isFound())
|
||||
.andExpect(redirectedUrl("/authenticate?error")).andExpect(unauthenticated());
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -52,13 +52,13 @@ public class DefaultfSecurityRequestsTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).defaultRequest(get("/").with(user("user").roles("ADMIN")))
|
||||
.apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.defaultRequest(get("/").with(user("user").roles("ADMIN"))).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithUser() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -67,7 +67,7 @@ public class DefaultfSecurityRequestsTests {
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/admin"))
|
||||
this.mvc.perform(get("/admin"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -76,7 +76,7 @@ public class DefaultfSecurityRequestsTests {
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithAnonymous() throws Exception {
|
||||
mvc.perform(get("/admin").with(anonymous()))
|
||||
this.mvc.perform(get("/admin").with(anonymous()))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isUnauthorized())
|
||||
// Ensure it appears we are authenticated with user
|
||||
|
||||
+6
-6
@@ -59,12 +59,12 @@ public class SecurityRequestsTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithUser() throws Exception {
|
||||
mvc.perform(get("/").with(user("user")))
|
||||
this.mvc.perform(get("/").with(user("user")))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -73,7 +73,7 @@ public class SecurityRequestsTests {
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/admin").with(user("admin").roles("ADMIN")))
|
||||
this.mvc.perform(get("/admin").with(user("admin").roles("ADMIN")))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with admin
|
||||
@@ -82,8 +82,8 @@ public class SecurityRequestsTests {
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithUserDetails() throws Exception {
|
||||
UserDetails user = userDetailsService.loadUserByUsername("user");
|
||||
mvc.perform(get("/").with(user(user)))
|
||||
UserDetails user = this.userDetailsService.loadUserByUsername("user");
|
||||
this.mvc.perform(get("/").with(user(user)))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -93,7 +93,7 @@ public class SecurityRequestsTests {
|
||||
@Test
|
||||
public void requestProtectedUrlWithAuthentication() throws Exception {
|
||||
Authentication authentication = new TestingAuthenticationToken("test", "notused", "ROLE_USER");
|
||||
mvc.perform(get("/").with(authentication(authentication)))
|
||||
this.mvc.perform(get("/").with(authentication(authentication)))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
|
||||
+5
-4
@@ -50,13 +50,14 @@ public class WithUserAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(SecurityMockMvcConfigurers.springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(SecurityMockMvcConfigurers.springSecurity())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser
|
||||
public void requestProtectedUrlWithUser() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -66,7 +67,7 @@ public class WithUserAuthenticationTests {
|
||||
@Test
|
||||
@WithAdminRob
|
||||
public void requestProtectedUrlWithAdminRob() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -76,7 +77,7 @@ public class WithUserAuthenticationTests {
|
||||
@Test
|
||||
@WithMockUser(roles = "ADMIN")
|
||||
public void requestProtectedUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/admin"))
|
||||
this.mvc.perform(get("/admin"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
|
||||
+4
-4
@@ -53,12 +53,12 @@ public class WithUserClassLevelAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithUser() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -67,7 +67,7 @@ public class WithUserClassLevelAuthenticationTests {
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/admin"))
|
||||
this.mvc.perform(get("/admin"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -77,7 +77,7 @@ public class WithUserClassLevelAuthenticationTests {
|
||||
@Test
|
||||
@WithAnonymousUser
|
||||
public void requestProtectedUrlWithAnonymous() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure did not get past security
|
||||
.andExpect(status().isUnauthorized())
|
||||
// Ensure not authenticated
|
||||
|
||||
+3
-3
@@ -52,13 +52,13 @@ public class WithUserDetailsAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithUserDetails
|
||||
public void requestProtectedUrlWithUser() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -68,7 +68,7 @@ public class WithUserDetailsAuthenticationTests {
|
||||
@Test
|
||||
@WithUserDetails("admin")
|
||||
public void requestProtectedUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/admin"))
|
||||
this.mvc.perform(get("/admin"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
|
||||
+3
-3
@@ -53,12 +53,12 @@ public class WithUserDetailsClassLevelAuthenticationTests {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
|
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestRootUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/"))
|
||||
this.mvc.perform(get("/"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
@@ -67,7 +67,7 @@ public class WithUserDetailsClassLevelAuthenticationTests {
|
||||
|
||||
@Test
|
||||
public void requestProtectedUrlWithAdmin() throws Exception {
|
||||
mvc.perform(get("/admin"))
|
||||
this.mvc.perform(get("/admin"))
|
||||
// Ensure we got past Security
|
||||
.andExpect(status().isNotFound())
|
||||
// Ensure it appears we are authenticated with user
|
||||
|
||||
Reference in New Issue
Block a user