This repository was archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 141_user_database_advanced
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/test/java/de/bonndan/nivio/security/CustomOAuth2UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |