@Test public void loadUserWhenOAuth2UserThenUserInfoNotNull() { Map<String, Object> attributes = new HashMap<>(); attributes.put(StandardClaimNames.SUB, "sub123"); attributes.put("user", "rob"); OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), attributes, "user"); when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.just(oauth2User)); assertThat(this.userService.loadUser(userRequest()).block().getUserInfo()).isNotNull(); }
@Test public void loadUserWhenOAuth2UserAndUser() { this.registration.userNameAttributeName("user"); Map<String, Object> attributes = new HashMap<>(); attributes.put(StandardClaimNames.SUB, "sub123"); attributes.put("user", "rob"); OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), attributes, "user"); when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.just(oauth2User)); assertThat(this.userService.loadUser(userRequest()).block().getName()).isEqualTo("rob"); }
@Test public void loadUserWhenOAuth2UserSubjectNotEqualThenOAuth2AuthenticationException() { Map<String, Object> attributes = new HashMap<>(); attributes.put(StandardClaimNames.SUB, "not-equal"); attributes.put("user", "rob"); OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), attributes, "user"); when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.just(oauth2User)); assertThatCode(() -> this.userService.loadUser(userRequest()).block()) .isInstanceOf(OAuth2AuthenticationException.class); }
@Test public void loadUserWhenOAuth2UserEmptyThenNullUserInfo() { when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.empty()); OidcUser user = this.userService.loadUser(userRequest()).block(); assertThat(user.getUserInfo()).isNull(); }
@Test public void loadUserWhenOAuth2UserSubjectNullThenOAuth2AuthenticationException() { OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), Collections.singletonMap("user", "rob"), "user"); when(this.oauth2UserService.loadUser(any())).thenReturn(Mono.just(oauth2User)); assertThatCode(() -> this.userService.loadUser(userRequest()).block()) .isInstanceOf(OAuth2AuthenticationException.class); }
@Test public void loadUserWhenUserInfoUriNullThenUserInfoNotRetrieved() { this.registration.userInfoUri(null); OidcUser user = this.userService.loadUser(userRequest()).block(); assertThat(user.getUserInfo()).isNull(); }