-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix username attributes for sso login
- Loading branch information
1 parent
367af55
commit 6f8d04c
Showing
7 changed files
with
93 additions
and
28 deletions.
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
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
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
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
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
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
56 changes: 56 additions & 0 deletions
56
core/src/test/java/io/aiven/klaw/helpers/UtilMethodsTest.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,56 @@ | ||
package io.aiven.klaw.helpers; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
|
||
class UtilMethodsTest { | ||
|
||
@Test | ||
public void testGetUserNameFromOAuth2User_PreferredUserName() { | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("preferred_username", "testUser"); | ||
attributes.put("email", "[email protected]"); | ||
|
||
DefaultOAuth2User oAuth2User = mock(DefaultOAuth2User.class); | ||
when(oAuth2User.getAttributes()).thenReturn(attributes); | ||
|
||
String result = UtilMethods.getUserName(oAuth2User, "preferred_username", "email"); | ||
assertEquals("testUser", result); | ||
} | ||
|
||
@Test | ||
public void testGetUserNameFromOAuth2User_Email() { | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("email", "[email protected]"); | ||
|
||
DefaultOAuth2User oAuth2User = mock(DefaultOAuth2User.class); | ||
when(oAuth2User.getAttributes()).thenReturn(attributes); | ||
|
||
String result = UtilMethods.getUserName(oAuth2User, "preferred_username", "email"); | ||
assertEquals("[email protected]", result); | ||
} | ||
|
||
@Test | ||
public void testGetUserNameFromStringPrincipal() { | ||
String principal = "testUser"; | ||
|
||
String result = UtilMethods.getUserName(principal, "preferred_username", "email"); | ||
assertEquals("testUser", result); | ||
} | ||
|
||
@Test | ||
public void testGetUserNameFromUserDetails() { | ||
UserDetails userDetails = mock(UserDetails.class); | ||
when(userDetails.getUsername()).thenReturn("testUser"); | ||
|
||
String result = UtilMethods.getUserName(userDetails, "preferred_username", "email"); | ||
assertEquals("testUser", result); | ||
} | ||
} |