Formatting Polish
This commit is contained in:
parent
1dbd3f5906
commit
8a28a27225
|
@ -15,9 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.security.test.web.servlet.response;
|
package org.springframework.security.test.web.servlet.response;
|
||||||
|
|
||||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
|
||||||
import static org.springframework.test.util.AssertionErrors.assertTrue;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@ -32,6 +29,9 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
import org.springframework.test.web.servlet.ResultMatcher;
|
import org.springframework.test.web.servlet.ResultMatcher;
|
||||||
|
|
||||||
|
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
||||||
|
import static org.springframework.test.util.AssertionErrors.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Security related {@link MockMvc} {@link ResultMatcher}s.
|
* Security related {@link MockMvc} {@link ResultMatcher}s.
|
||||||
*
|
*
|
||||||
|
@ -78,8 +78,8 @@ public final class SecurityMockMvcResultMatchers {
|
||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
*/
|
*/
|
||||||
public static final class AuthenticatedMatcher extends
|
public static final class AuthenticatedMatcher
|
||||||
AuthenticationMatcher<AuthenticatedMatcher> {
|
extends AuthenticationMatcher<AuthenticatedMatcher> {
|
||||||
|
|
||||||
private SecurityContext expectedContext;
|
private SecurityContext expectedContext;
|
||||||
private Authentication expectedAuthentication;
|
private Authentication expectedAuthentication;
|
||||||
|
@ -87,6 +87,7 @@ public final class SecurityMockMvcResultMatchers {
|
||||||
private String expectedAuthenticationName;
|
private String expectedAuthenticationName;
|
||||||
private Collection<? extends GrantedAuthority> expectedGrantedAuthorities;
|
private Collection<? extends GrantedAuthority> expectedGrantedAuthorities;
|
||||||
|
|
||||||
|
@Override
|
||||||
public void match(MvcResult result) throws Exception {
|
public void match(MvcResult result) throws Exception {
|
||||||
SecurityContext context = load(result);
|
SecurityContext context = load(result);
|
||||||
|
|
||||||
|
@ -109,10 +110,11 @@ public final class SecurityMockMvcResultMatchers {
|
||||||
if (this.expectedAuthenticationPrincipal != null) {
|
if (this.expectedAuthenticationPrincipal != null) {
|
||||||
assertTrue("Authentication cannot be null",
|
assertTrue("Authentication cannot be null",
|
||||||
context.getAuthentication() != null);
|
context.getAuthentication() != null);
|
||||||
assertEquals(this.expectedAuthenticationPrincipal + " does not equal "
|
assertEquals(
|
||||||
|
this.expectedAuthenticationPrincipal + " does not equal "
|
||||||
+ context.getAuthentication().getPrincipal(),
|
+ context.getAuthentication().getPrincipal(),
|
||||||
this.expectedAuthenticationPrincipal, context.getAuthentication()
|
this.expectedAuthenticationPrincipal,
|
||||||
.getPrincipal());
|
context.getAuthentication().getPrincipal());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.expectedAuthenticationName != null) {
|
if (this.expectedAuthenticationName != null) {
|
||||||
|
@ -126,7 +128,8 @@ public final class SecurityMockMvcResultMatchers {
|
||||||
assertTrue("Authentication cannot be null", auth != null);
|
assertTrue("Authentication cannot be null", auth != null);
|
||||||
Collection<? extends GrantedAuthority> authorities = auth
|
Collection<? extends GrantedAuthority> authorities = auth
|
||||||
.getAuthorities();
|
.getAuthorities();
|
||||||
assertTrue(authorities + " does not contain the same authorities as "
|
assertTrue(
|
||||||
|
authorities + " does not contain the same authorities as "
|
||||||
+ this.expectedGrantedAuthorities,
|
+ this.expectedGrantedAuthorities,
|
||||||
authorities.containsAll(this.expectedGrantedAuthorities));
|
authorities.containsAll(this.expectedGrantedAuthorities));
|
||||||
assertTrue(this.expectedGrantedAuthorities
|
assertTrue(this.expectedGrantedAuthorities
|
||||||
|
@ -195,7 +198,8 @@ public final class SecurityMockMvcResultMatchers {
|
||||||
* @param expected the {@link Authentication#getAuthorities()}
|
* @param expected the {@link Authentication#getAuthorities()}
|
||||||
* @return the {@link AuthenticatedMatcher} for further customization
|
* @return the {@link AuthenticatedMatcher} for further customization
|
||||||
*/
|
*/
|
||||||
public AuthenticatedMatcher withAuthorities(Collection<? extends GrantedAuthority> expected) {
|
public AuthenticatedMatcher withAuthorities(
|
||||||
|
Collection<? extends GrantedAuthority> expected) {
|
||||||
this.expectedGrantedAuthorities = expected;
|
this.expectedGrantedAuthorities = expected;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,13 +15,10 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.security.test.web.servlet.response;
|
package org.springframework.security.test.web.servlet.response;
|
||||||
|
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
|
||||||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
|
||||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
@ -36,6 +33,10 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
|
||||||
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
||||||
|
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
|
||||||
|
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = SecurityMockMvcResultMatchersTests.Config.class)
|
@ContextConfiguration(classes = SecurityMockMvcResultMatchersTests.Config.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
|
@ -47,21 +48,32 @@ public class SecurityMockMvcResultMatchersTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity())
|
// @formatter:off
|
||||||
|
this.mockMvc = MockMvcBuilders
|
||||||
|
.webAppContextSetup(this.context)
|
||||||
|
.apply(springSecurity())
|
||||||
.build();
|
.build();
|
||||||
|
// @formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
// SEC-2719
|
// SEC-2719
|
||||||
@Test
|
@Test
|
||||||
public void withRolesNotOrderSensitive() throws Exception {
|
public void withRolesNotOrderSensitive() throws Exception {
|
||||||
mockMvc.perform(formLogin())
|
// @formatter:off
|
||||||
|
this.mockMvc
|
||||||
|
.perform(formLogin())
|
||||||
.andExpect(authenticated().withRoles("USER", "SELLER"))
|
.andExpect(authenticated().withRoles("USER", "SELLER"))
|
||||||
.andExpect(authenticated().withRoles("SELLER", "USER"));
|
.andExpect(authenticated().withRoles("SELLER", "USER"));
|
||||||
|
// @formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = AssertionError.class)
|
@Test(expected = AssertionError.class)
|
||||||
public void withRolesFailsIfNotAllRoles() throws Exception {
|
public void withRolesFailsIfNotAllRoles() throws Exception {
|
||||||
mockMvc.perform(formLogin()).andExpect(authenticated().withRoles("USER"));
|
// @formatter:off
|
||||||
|
this.mockMvc
|
||||||
|
.perform(formLogin())
|
||||||
|
.andExpect(authenticated().withRoles("USER"));
|
||||||
|
// @formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
|
|
Loading…
Reference in New Issue