18
18
19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .io .ObjectOutputStream ;
21
- import java .util .*;
21
+ import java .util .ArrayList ;
22
+ import java .util .Collection ;
23
+ import java .util .HashSet ;
24
+ import java .util .List ;
25
+ import java .util .Set ;
22
26
import java .util .function .Function ;
23
- import java .util .stream .Stream ;
24
27
25
28
import org .junit .jupiter .api .Test ;
26
-
27
29
import org .junit .jupiter .params .ParameterizedTest ;
28
- import org .junit .jupiter .params .provider .Arguments ;
29
- import org .junit .jupiter .params .provider .MethodSource ;
30
+ import org .junit .jupiter .params .provider .NullSource ;
31
+ import org .junit .jupiter .params .provider .ValueSource ;
32
+
30
33
import org .springframework .security .core .GrantedAuthority ;
31
34
import org .springframework .security .core .authority .AuthorityUtils ;
32
35
import org .springframework .security .core .authority .SimpleGrantedAuthority ;
33
- import org .springframework .util .CollectionUtils ;
34
36
35
37
import static org .assertj .core .api .Assertions .assertThat ;
36
38
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
@@ -46,14 +48,6 @@ public class UserTests {
46
48
47
49
private static final List <GrantedAuthority > ROLE_12 = AuthorityUtils .createAuthorityList ("ROLE_ONE" , "ROLE_TWO" );
48
50
49
- public static Stream <Arguments > testNewAuthoritiesShouldReplacePreviousAuthorities () {
50
- return Stream .of (
51
- Arguments .of ((Object ) new String [0 ]),
52
- Arguments .of ((Object ) new String []{"B7" , "C12" , "role" }),
53
- Arguments .of ((Object ) new String []{"A1" })
54
- );
55
- }
56
-
57
51
@ Test
58
52
public void equalsReturnsTrueIfUsernamesAreTheSame () {
59
53
User user1 = new User ("rod" , "koala" , true , true , true , true , ROLE_12 );
@@ -107,16 +101,28 @@ public void testNullWithinUserAuthoritiesIsRejected() {
107
101
.authorities (new String [] { null , null }).build ());
108
102
}
109
103
104
+ // gh-12533
110
105
@ ParameterizedTest
111
- @ MethodSource
112
- public void testNewAuthoritiesShouldReplacePreviousAuthorities (String [] authorities ) {
113
- UserDetails parent = User .builder ().username ("user" ).password ("password" ).authorities ("A1" , "A2" , "B1" ).build ();
114
- User .UserBuilder builder = User .withUserDetails (parent ).authorities (authorities );
106
+ @ NullSource
107
+ @ ValueSource (strings = { "ROLE_USER,ROLE_ADMIN,read" , "read" })
108
+ public void withUserDetailsWhenAuthoritiesThenOverridesPreviousAuthorities (String arg ) {
109
+ // @formatter:off
110
+ UserDetails parent = User .builder ()
111
+ .username ("user" )
112
+ .password ("password" )
113
+ .authorities ("one" , "two" , "three" )
114
+ .build ();
115
+ // @formatter:on
116
+ String [] authorities = (arg != null ) ? arg .split ("," ) : new String [0 ];
117
+ User .UserBuilder builder = User .withUserDetails (parent );
115
118
UserDetails user = builder .build ();
119
+ assertThat (AuthorityUtils .authorityListToSet (user .getAuthorities ())).containsOnly ("one" , "two" , "three" );
120
+ user = builder .authorities (authorities ).build ();
116
121
assertThat (AuthorityUtils .authorityListToSet (user .getAuthorities ())).containsOnly (authorities );
117
122
user = builder .authorities (AuthorityUtils .createAuthorityList (authorities )).build ();
118
123
assertThat (AuthorityUtils .authorityListToSet (user .getAuthorities ())).containsOnly (authorities );
119
- user = builder .authorities (AuthorityUtils .createAuthorityList (authorities ).toArray (GrantedAuthority []::new )).build ();
124
+ user = builder .authorities (AuthorityUtils .createAuthorityList (authorities ).toArray (GrantedAuthority []::new ))
125
+ .build ();
120
126
assertThat (AuthorityUtils .authorityListToSet (user .getAuthorities ())).containsOnly (authorities );
121
127
}
122
128
0 commit comments