-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit tests for auth feature #259
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3de5d12
refactor: move auth feature, remove unnecessary class
m-rudyk 2f2ec23
test: add unit tests for auth feature
m-rudyk 1ecf451
fix: suppress unsafe cast in order to avoid build errors
m-rudyk b4b0d60
fix: fix changes after review comment
m-rudyk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 0 additions & 41 deletions
41
src/main/java/com/lpvs/auth/MyAuthenticationSuccessHandler.java
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. | ||
* <p> | ||
* Use of this source code is governed by a MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package com.lpvs.entity.auth; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.lpvs.entity.LPVSMember; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
|
||
public class MemberProfileTest { | ||
|
||
@Test | ||
public void testConstructor() { | ||
MemberProfile profile = new MemberProfile(); | ||
assertNull(profile.getName()); | ||
assertNull(profile.getEmail()); | ||
assertNull(profile.getProvider()); | ||
assertNull(profile.getNickname()); | ||
} | ||
|
||
@Test | ||
public void testToMember() { | ||
MemberProfile profile = new MemberProfile(); | ||
profile.setName("John"); | ||
profile.setEmail("[email protected]"); | ||
profile.setProvider("OAuth2"); | ||
LPVSMember member = profile.toMember(); | ||
|
||
assertEquals("John", member.getName()); | ||
assertEquals("[email protected]", member.getEmail()); | ||
assertEquals("OAuth2", member.getProvider()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/com/lpvs/entity/auth/OAuthAttributesTest.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,40 @@ | ||
/** | ||
* Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. | ||
* <p> | ||
* Use of this source code is governed by a MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package com.lpvs.entity.auth; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class OAuthAttributesTest { | ||
|
||
@Test | ||
public void testExtractOAuthAttributes() { | ||
Map<String, Object> attributes = new HashMap<String, Object>() {{ | ||
put("name", "testName"); | ||
put("email", "testEmail"); | ||
}};; | ||
MemberProfile profile = OAuthAttributes.extract("google", attributes); | ||
assertEquals("testName", profile.getName()); | ||
assertEquals("testEmail", profile.getEmail()); | ||
} | ||
|
||
@Test | ||
public void testExtractOAuthAttributesUnknownProvider() { | ||
Map<String, Object> attributes = new HashMap<String, Object>() {{ | ||
put("name", "testName"); | ||
put("email", "testEmail"); | ||
}};; | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
OAuthAttributes.extract("unknown", attributes); | ||
}); | ||
} | ||
} |
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,62 @@ | ||
package com.lpvs.entity.auth; | ||
o-kopysov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import com.lpvs.repository.LPVSMemberRepository; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistration; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.core.OAuth2AccessToken; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import static org.mockito.Mockito.*; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class OAuthServiceTest { | ||
|
||
@Test | ||
public void testLoadUser() throws OAuth2AuthenticationException { | ||
|
||
// Create a mock LPVSMemberRepository | ||
LPVSMemberRepository lpvsMemberRepository = mock(LPVSMemberRepository.class); | ||
|
||
// Create a sample OAuth2UserRequest | ||
ClientRegistration clientRegistration = ClientRegistration | ||
.withRegistrationId("google") | ||
.userInfoUri("https://example.com/userinfo") | ||
.userNameAttributeName("email") | ||
.authorizationGrantType(AuthorizationGrantType.PASSWORD) | ||
.clientId("id") | ||
.tokenUri("https://example.com/tokenuri") | ||
.build(); | ||
OAuth2UserRequest userRequest = new OAuth2UserRequest( | ||
clientRegistration, | ||
new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "access-token", null, null)); | ||
|
||
Map<String, Object> attributes = new LinkedHashMap<>(); | ||
attributes.put("email", "testEmail"); | ||
attributes.put("name", "testName"); | ||
OAuth2User oAuth2User = new DefaultOAuth2User(Collections.singleton(new SimpleGrantedAuthority("USER")), attributes, "email"); | ||
|
||
// Mock the behavior of DefaultOAuth2UserService | ||
DefaultOAuth2UserService defaultUserService = Mockito.mock(DefaultOAuth2UserService.class); | ||
when(defaultUserService.loadUser(userRequest)).thenReturn(oAuth2User); | ||
|
||
// Create an instance of your OAuthService with the mocked DefaultOAuth2UserService | ||
OAuthService oAuthService = new OAuthService(lpvsMemberRepository, defaultUserService); | ||
|
||
OAuth2User loadedUser = oAuthService.loadUser(userRequest); | ||
|
||
assertEquals("testEmail", loadedUser.getAttribute("email")); | ||
assertEquals("testName", loadedUser.getAttribute("name")); | ||
assertEquals("google", loadedUser.getAttribute("provider")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't seem to be a substantial change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, good point.