Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Merge branch 'develop' into 141_user_database_advanced
Browse files Browse the repository at this point in the history
  • Loading branch information
jenarp committed Jan 19, 2022
2 parents b427b8d + 5f96a89 commit a141f45
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -52,8 +53,15 @@ public static CustomOAuth2User fromGitHubUser(@NonNull final OAuth2User user,

var name = "";
if (StringUtils.hasLength(nameAttribute)) {
name = String.valueOf(user.getAttribute(nameAttribute) == null ? "" : user.getAttribute(nameAttribute));
Object val = user.getAttribute(nameAttribute);
if (val == null) {
Object login = Objects.requireNonNull(user.getAttribute("login"));
name = String.valueOf(login);
} else {
name = String.valueOf(val);
}
}

return new CustomOAuth2User(
id,
StringUtils.hasLength(aliasAttribute) ? Optional.ofNullable((String) user.getAttribute(aliasAttribute)).orElse("") : "",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package de.bonndan.nivio.security;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

class CustomOAuth2UserServiceTest {

private OAuth2User oAuth2User;

private String userName = "Mary";
private String login = "foo";
private String avatarUrl = "https://www.avatar.com";

private Collection<OAuth2UserAuthority> authorities;


@BeforeEach
public void setup() {
oAuth2User = mock(OAuth2User.class);
when(oAuth2User.getAttribute("login")).thenReturn(login);
when(oAuth2User.getAttribute("avatar_url")).thenReturn(avatarUrl);
when(oAuth2User.getAttribute("id")).thenReturn(123);
when(oAuth2User.getAttributes()).thenReturn(Map.of());

Map<String, Object> authorityAttributes = Map.of("key", new Object());
OAuth2UserAuthority grantedAuthority = new OAuth2UserAuthority(authorityAttributes);
authorities = List.of(grantedAuthority);
doReturn(authorities).when(oAuth2User).getAuthorities();
}

@Test
void fromGitHubUser() {

//given
when(oAuth2User.getAttribute("name")).thenReturn(userName);

//when
CustomOAuth2User customOAuth2User = CustomOAuth2UserService.fromGitHubUser(oAuth2User, "login", "name");

//then
assertThat(customOAuth2User).isNotNull();
assertThat(customOAuth2User.getAlias()).isEqualTo(login);
}

@Test
void fromGitHubUserWithMissingNameFallsBackToLogin() {

//given
when(oAuth2User.getAttribute("name")).thenReturn(null);

//when
CustomOAuth2User customOAuth2User = CustomOAuth2UserService.fromGitHubUser(oAuth2User, "login", "name");

//then
assertThat(customOAuth2User.getName()).isEqualTo(login);
}
}

0 comments on commit a141f45

Please sign in to comment.