1
0
mirror of synced 2026-08-04 17:27:13 +00:00

Add SecurityAssertions

This commit introduces a simple, internal test API for
verifying aspects of an Authentication, like its name
and authorities.

Closes gh-17844
This commit is contained in:
Josh Cummings
2025-09-03 16:33:16 -06:00
parent de10e08348
commit c64b086878
15 changed files with 180 additions and 108 deletions
@@ -0,0 +1,100 @@
/*
* Copyright 2004-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.authentication;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.CollectionAssert;
import org.assertj.core.api.Condition;
import org.assertj.core.api.ObjectAssert;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
@NullMarked
public final class SecurityAssertions {
private SecurityAssertions() {
}
public static AuthenticationAssert assertThat(@Nullable Authentication authentication) {
Assertions.assertThat(authentication).isNotNull();
return new AuthenticationAssert(authentication);
}
public static final class AuthenticationAssert extends AbstractObjectAssert<AuthenticationAssert, Authentication> {
private final Authentication authentication;
private AuthenticationAssert(Authentication authentication) {
super(authentication, AuthenticationAssert.class);
this.authentication = authentication;
}
public AuthenticationAssert name(String name) {
Assertions.assertThat(this.authentication.getName()).isEqualTo(name);
return this;
}
public ObjectAssert<GrantedAuthority> hasAuthority(String authority) {
Collection<? extends GrantedAuthority> actual = this.authentication.getAuthorities();
for (GrantedAuthority element : actual) {
if (element.getAuthority().equals(authority)) {
return new ObjectAssert<>(element);
}
}
throw new AssertionError(actual + " does not contain " + authority + " as expected");
}
public CollectionAssert<GrantedAuthority> hasAuthorities(String... authorities) {
HasAuthoritiesPredicate test = new HasAuthoritiesPredicate(authorities);
return authorities().has(new Condition<>(test, "contains %s", Arrays.toString(authorities)));
}
public CollectionAssert<GrantedAuthority> authorities() {
return new CollectionAssert<>(this.authentication.getAuthorities());
}
}
private static final class HasAuthoritiesPredicate implements Predicate<Collection<? extends GrantedAuthority>> {
private final Collection<String> expected;
private HasAuthoritiesPredicate(String... expected) {
this.expected = List.of(expected);
}
@Override
public boolean test(Collection<? extends GrantedAuthority> actual) {
Set<String> asString = AuthorityUtils.authorityListToSet(actual);
return asString.containsAll(this.expected);
}
}
}