1
0
mirror of synced 2026-08-04 09:17:02 +00:00

Add authenticated().withAuthentication(Consumer<Authentication>)

This allows arbitrary assertions of the authenticated user

Fixes: gh-4996
This commit is contained in:
Rob Winch
2018-02-02 16:40:43 -06:00
parent 1cb581a0c6
commit e1a8d250de
3 changed files with 42 additions and 0 deletions
@@ -17,6 +17,7 @@ package org.springframework.security.test.web.servlet.response;
import java.util.ArrayList;
import java.util.Collection;
import java.util.function.Consumer;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
@@ -88,6 +89,7 @@ public final class SecurityMockMvcResultMatchers {
private Object expectedAuthenticationPrincipal;
private String expectedAuthenticationName;
private Collection<? extends GrantedAuthority> expectedGrantedAuthorities;
private Consumer<Authentication> assertAuthentication;
@Override
public void match(MvcResult result) throws Exception {
@@ -97,6 +99,10 @@ public final class SecurityMockMvcResultMatchers {
assertTrue("Authentication should not be null", auth != null);
if (this.assertAuthentication != null) {
this.assertAuthentication.accept(auth);
}
if (this.expectedContext != null) {
assertEquals(this.expectedContext + " does not equal " + context,
this.expectedContext, context);
@@ -140,6 +146,16 @@ public final class SecurityMockMvcResultMatchers {
}
}
/**
* Allows for any validating the authentication with arbitrary assertions
* @param assesrtAuthentication the Consumer which validates the authentication
* @return the AuthenticatedMatcher to perform additional assertions
*/
public AuthenticatedMatcher withAuthentication(Consumer<Authentication> assesrtAuthentication) {
this.assertAuthentication = assesrtAuthentication;
return this;
}
/**
* Specifies the expected username
*