Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add empty authorities by default to easily build a user #12555

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public static final class UserBuilder {

private String password;

private List<GrantedAuthority> authorities;
private List<GrantedAuthority> authorities = new ArrayList<>();

private boolean accountExpired;

Expand Down Expand Up @@ -427,6 +427,7 @@ public UserBuilder roles(String... roles) {
* @see #roles(String...)
*/
public UserBuilder authorities(GrantedAuthority... authorities) {
Assert.notNull(authorities, "authorities cannot be null");
return authorities(Arrays.asList(authorities));
}

Expand All @@ -439,7 +440,8 @@ public UserBuilder authorities(GrantedAuthority... authorities) {
* @see #roles(String...)
*/
public UserBuilder authorities(Collection<? extends GrantedAuthority> authorities) {
this.authorities = new ArrayList<>(authorities);
Assert.notNull(authorities, "authorities cannot be null");
this.authorities.addAll(authorities);
return this;
}

Expand All @@ -452,6 +454,7 @@ public UserBuilder authorities(Collection<? extends GrantedAuthority> authoritie
* @see #roles(String...)
*/
public UserBuilder authorities(String... authorities) {
Assert.notNull(authorities, "authorities cannot be null");
return authorities(AuthorityUtils.createAuthorityList(authorities));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -37,6 +39,7 @@
* Tests {@link User}.
*
* @author Ben Alex
* @author Ilya Starchenko
*/
public class UserTests {

Expand Down Expand Up @@ -68,6 +71,33 @@ public void testNoArgConstructorDoesntExist() {
.isThrownBy(() -> User.class.getDeclaredConstructor((Class[]) null));
}

@Test
public void testBuildUserWithNoAuthorities() {
UserDetails user = User.builder().username("user").password("password").build();
assertThat(user.getAuthorities()).isEmpty();
}

@Test
public void testNullWithinUserAuthoritiesIsRejected() {
assertThatIllegalArgumentException().isThrownBy(() -> User.builder().username("user").password("password")
.authorities((Collection<? extends GrantedAuthority>) null).build());
List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(null);
authorities.add(null);
assertThatIllegalArgumentException().isThrownBy(
() -> User.builder().username("user").password("password").authorities(authorities).build());

assertThatIllegalArgumentException().isThrownBy(() -> User.builder().username("user").password("password")
.authorities((GrantedAuthority[]) null).build());
assertThatIllegalArgumentException().isThrownBy(() -> User.builder().username("user").password("password")
.authorities(new GrantedAuthority[] { null, null }).build());

assertThatIllegalArgumentException().isThrownBy(
() -> User.builder().username("user").password("password").authorities((String[]) null).build());
assertThatIllegalArgumentException().isThrownBy(() -> User.builder().username("user").password("password")
.authorities(new String[] { null, null }).build());
}

@Test
public void testNullValuesRejected() {
assertThatIllegalArgumentException().isThrownBy(() -> new User(null, "koala", true, true, true, true, ROLE_12));
Expand Down