Skip to content

Commit

Permalink
Merge pull request #687 from hmcts/RDCC-6555
Browse files Browse the repository at this point in the history
addresses Idam returning user with null roles assigned
  • Loading branch information
lukasz-wolski authored Mar 27, 2023
2 parents 76d90f4 + fc31d21 commit b967b6b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.lang.Boolean.TRUE;
Expand Down Expand Up @@ -53,7 +54,8 @@ public Collection<GrantedAuthority> convert(Jwt jwt) {
}

private List<GrantedAuthority> extractAuthorityFromClaims(List<String> roles) {
return roles.stream()
return Optional.ofNullable(roles).orElse(new ArrayList<>())
.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,25 @@ void test_shouldReturnEmptyAuthoritiesWhenIdamReturnsUsers() {
assertNotNull(userInfo);
}

@Test
void test_shouldReturnEmptyAuthoritiesWhenIdamReturnsUsersWithNullRoles() {
List<String> roles = null;

when(jwtMock.hasClaim(anyString())).thenReturn(true);
when(jwtMock.getClaim(anyString())).thenReturn("access_token");
when(jwtMock.getTokenValue()).thenReturn("access_token");
when(userInfoMock.getRoles()).thenReturn(roles);
when(idamRepositoryMock.getUserInfo(anyString())).thenReturn(userInfoMock);

Collection<GrantedAuthority> authorities = converter.convert(jwtMock);

assertNotNull(authorities);
assertEquals(0, authorities.size());
verify(jwtMock, times(1)).hasClaim(anyString());
verify(jwtMock, times(1)).getClaim(anyString());
verify(jwtMock, times(1)).getTokenValue();
verify(userInfoMock, times(1)).getRoles();
verify(idamRepositoryMock, times(1)).getUserInfo(anyString());
}

}

0 comments on commit b967b6b

Please sign in to comment.