Skip to content

Commit 766c443

Browse files
andifalkjzheaux
authored andcommitted
Improve test coverage of JwtGrantedAuthoritiesConverter
Some negative test cases were missing. Added these to have full test coverage for JwtGrantedAuthoritiesConverter.
1 parent 0a058c9 commit 766c443

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverterTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,43 @@ public void convertWhenTokenHasEmptyScopeAndNonEmptyScpThenScopeAttributeIsTrans
139139
assertThat(authorities).isEmpty();
140140
}
141141

142+
@Test
143+
public void convertWhenTokenHasEmptyScopeAndEmptyScpAttributeThenTranslatesToNoAuthorities() {
144+
Map<String, Object> claims = new HashMap<>();
145+
claims.put("scp", Collections.emptyList());
146+
claims.put("scope", Collections.emptyList());
147+
Jwt jwt = this.jwt(claims);
148+
149+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
150+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
151+
152+
assertThat(authorities).isEmpty();
153+
}
154+
155+
@Test
156+
public void convertWhenTokenHasNoScopeAndNoScpAttributeThenTranslatesToNoAuthorities() {
157+
Map<String, Object> claims = new HashMap<>();
158+
claims.put("roles", Arrays.asList("message:read", "message:write"));
159+
Jwt jwt = this.jwt(claims);
160+
161+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
162+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
163+
164+
assertThat(authorities).isEmpty();
165+
}
166+
167+
@Test
168+
public void convertWhenTokenHasUnsupportedTypeForScopeThenTranslatesToNoAuthorities() {
169+
Map<String, Object> claims = new HashMap<>();
170+
claims.put("scope", new String[] {"message:read", "message:write"});
171+
Jwt jwt = this.jwt(claims);
172+
173+
JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
174+
Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
175+
176+
assertThat(authorities).isEmpty();
177+
}
178+
142179
@Test
143180
public void convertWhenTokenHasCustomClaimNameThenCustomClaimNameAttributeIsTranslatedToAuthorities() {
144181
Map<String, Object> claims = new HashMap<>();

0 commit comments

Comments
 (0)