Repository navigation
[Fix]#19 로그인 제공자별 회원가입 및 소셜 계정 연동 충돌 처리 #71
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
Merged
Merged
Changes from all commits
Commits
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
src/test/java/com/quespot/domain/user/service/AuthServiceTest.java
This file contains hidden or 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,104 @@ | ||
| package com.quespot.domain.user.service; | ||
|
|
||
| import com.quespot.domain.user.dto.req.LoginRequestDTO; | ||
| import com.quespot.domain.user.dto.req.SignUpRequestDTO; | ||
| import com.quespot.domain.user.entity.User; | ||
| import com.quespot.domain.user.enums.EmailVerificationPurpose; | ||
| import com.quespot.domain.user.enums.LoginProvider; | ||
| import com.quespot.domain.user.repository.UserProfileRepository; | ||
| import com.quespot.domain.user.repository.UserRepository; | ||
| import com.quespot.domain.user.repository.UserSocialAccountRepository; | ||
| import com.quespot.global.security.provider.JwtTokenPair; | ||
| import com.quespot.global.security.provider.JwtTokenProvider; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class AuthServiceTest { | ||
|
|
||
| private static final Long USER_ID = 1L; | ||
| private static final String EMAIL = "member@example.com"; | ||
| private static final String PASSWORD = "password1234"; | ||
|
|
||
| @Mock | ||
| private UserRepository userRepository; | ||
|
|
||
| @Mock | ||
| private UserProfileRepository userProfileRepository; | ||
|
|
||
| @Mock | ||
| private UserSocialAccountRepository userSocialAccountRepository; | ||
|
|
||
| @Mock | ||
| private OAuth2UnlinkQueueService oAuth2UnlinkQueueService; | ||
|
|
||
| @Mock | ||
| private EmailVerificationService emailVerificationService; | ||
|
|
||
| @Mock | ||
| private JwtTokenProvider jwtTokenProvider; | ||
|
|
||
| @Mock | ||
| private RefreshTokenService refreshTokenService; | ||
|
|
||
| @Mock | ||
| private PasswordEncoder passwordEncoder; | ||
|
|
||
| @InjectMocks | ||
| private AuthService authService; | ||
|
|
||
| @Test | ||
| void signUpChecksDuplicateEmailWithinEmailProvider() { | ||
| SignUpRequestDTO request = new SignUpRequestDTO(EMAIL, PASSWORD, PASSWORD); | ||
| when(userRepository.existsByProviderAndEmail(LoginProvider.EMAIL, EMAIL)).thenReturn(false); | ||
| when(emailVerificationService.isEmailVerified(EMAIL, EmailVerificationPurpose.SIGN_UP)) | ||
| .thenReturn(true); | ||
| when(passwordEncoder.encode(PASSWORD)).thenReturn("encoded-password"); | ||
| when(userRepository.saveAndFlush(any(User.class))) | ||
| .thenAnswer(invocation -> userWithId(invocation.getArgument(0))); | ||
|
|
||
| var response = authService.signUp(request); | ||
|
|
||
| assertThat(response.userId()).isEqualTo(USER_ID); | ||
| assertThat(response.email()).isEqualTo(EMAIL); | ||
| verify(userRepository).existsByProviderAndEmail(LoginProvider.EMAIL, EMAIL); | ||
| } | ||
|
|
||
| @Test | ||
| void loginFindsEmailProviderAccount() { | ||
| User user = userWithId(User.createEmailUser(EMAIL, "encoded-password")); | ||
| JwtTokenPair tokenPair = new JwtTokenPair( | ||
| "access-token", | ||
| "refresh-token", | ||
| "session-id", | ||
| 1209600L | ||
| ); | ||
| when(userRepository.findByProviderAndEmailForUpdate(LoginProvider.EMAIL, EMAIL)) | ||
| .thenReturn(Optional.of(user)); | ||
| when(passwordEncoder.matches(PASSWORD, "encoded-password")).thenReturn(true); | ||
| when(jwtTokenProvider.issueTokenPair(USER_ID, user.getRole())).thenReturn(tokenPair); | ||
| when(userProfileRepository.existsByUserId(USER_ID)).thenReturn(false); | ||
|
|
||
| var result = authService.login(new LoginRequestDTO(EMAIL, PASSWORD)); | ||
|
|
||
| assertThat(result.response().userId()).isEqualTo(USER_ID); | ||
| verify(userRepository).findByProviderAndEmailForUpdate(LoginProvider.EMAIL, EMAIL); | ||
| } | ||
|
|
||
| private User userWithId(User user) { | ||
| ReflectionTestUtils.setField(user, "id", USER_ID); | ||
| return user; | ||
| } | ||
| } |
This file contains hidden or 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
145 changes: 145 additions & 0 deletions
145
src/test/java/com/quespot/domain/user/service/OAuth2LoginServiceTest.java
This file contains hidden or 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,145 @@ | ||
| package com.quespot.domain.user.service; | ||
|
|
||
| import com.quespot.domain.user.entity.User; | ||
| import com.quespot.domain.user.entity.UserSocialAccount; | ||
| import com.quespot.domain.user.enums.LoginProvider; | ||
| import com.quespot.domain.user.exception.AuthException; | ||
| import com.quespot.domain.user.exception.code.AuthErrorCode; | ||
| import com.quespot.domain.user.repository.UserProfileRepository; | ||
| import com.quespot.domain.user.repository.UserRepository; | ||
| import com.quespot.domain.user.repository.UserSocialAccountRepository; | ||
| import com.quespot.global.security.provider.JwtTokenProvider; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.security.oauth2.core.user.OAuth2User; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class OAuth2LoginServiceTest { | ||
|
|
||
| private static final Long USER_ID = 1L; | ||
| private static final String EMAIL = "member@example.com"; | ||
| private static final String PROVIDER_USER_ID = "google-user-id"; | ||
| private static final OAuth2ProviderToken PROVIDER_TOKEN = | ||
| new OAuth2ProviderToken("access-token", "refresh-token", null); | ||
|
|
||
| @Mock | ||
| private UserRepository userRepository; | ||
|
|
||
| @Mock | ||
| private UserSocialAccountRepository userSocialAccountRepository; | ||
|
|
||
| @Mock | ||
| private UserProfileRepository userProfileRepository; | ||
|
|
||
| @Mock | ||
| private OAuth2LoginCodeService oAuth2LoginCodeService; | ||
|
|
||
| @Mock | ||
| private OAuth2LinkRequestService oAuth2LinkRequestService; | ||
|
|
||
| @Mock | ||
| private OAuth2TokenCipher oAuth2TokenCipher; | ||
|
|
||
| @Mock | ||
| private OAuth2UnlinkTaskStateService oAuth2UnlinkTaskStateService; | ||
|
|
||
| @Mock | ||
| private JwtTokenProvider jwtTokenProvider; | ||
|
|
||
| @Mock | ||
| private RefreshTokenService refreshTokenService; | ||
|
|
||
| @InjectMocks | ||
| private OAuth2LoginService oAuth2LoginService; | ||
|
|
||
| @Test | ||
| void createsSocialUserWithoutCheckingEmailUsedByAnotherProvider() { | ||
| OAuth2User oAuth2User = googleUser(); | ||
| when(userSocialAccountRepository.findByProviderAndProviderUserId( | ||
| LoginProvider.GOOGLE, | ||
| PROVIDER_USER_ID | ||
| )).thenReturn(Optional.empty()); | ||
| when(oAuth2TokenCipher.encrypt("access-token")).thenReturn("encrypted-access-token"); | ||
| when(oAuth2TokenCipher.encrypt("refresh-token")).thenReturn("encrypted-refresh-token"); | ||
| when(userRepository.saveAndFlush(any(User.class))).thenAnswer(invocation -> { | ||
| User user = invocation.getArgument(0); | ||
| ReflectionTestUtils.setField(user, "id", USER_ID); | ||
| return user; | ||
| }); | ||
| when(userSocialAccountRepository.saveAndFlush(any(UserSocialAccount.class))) | ||
| .thenAnswer(invocation -> invocation.getArgument(0)); | ||
| when(oAuth2LoginCodeService.issue(USER_ID)).thenReturn("login-code"); | ||
|
|
||
| String loginCode = oAuth2LoginService.prepareLogin("google", oAuth2User, PROVIDER_TOKEN); | ||
|
|
||
| assertThat(loginCode).isEqualTo("login-code"); | ||
| ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class); | ||
| verify(userRepository).saveAndFlush(userCaptor.capture()); | ||
| assertThat(userCaptor.getValue().getProvider()).isEqualTo(LoginProvider.GOOGLE); | ||
| assertThat(userCaptor.getValue().getEmail()).isEqualTo(EMAIL); | ||
| verify(userRepository, never()).existsByProviderAndEmail(any(), anyString()); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsLinkWhenSocialAccountBelongsToAnotherUser() { | ||
| User currentUser = userWithId(User.createEmailUser(EMAIL, "encoded-password"), USER_ID); | ||
| User otherUser = userWithId( | ||
| User.createSocialUser("other@example.com", LoginProvider.GOOGLE), | ||
| 2L | ||
| ); | ||
| UserSocialAccount existingAccount = UserSocialAccount.create( | ||
| otherUser, | ||
| LoginProvider.GOOGLE, | ||
| PROVIDER_USER_ID, | ||
| EMAIL, | ||
| "encrypted-access-token", | ||
| "encrypted-refresh-token", | ||
| null | ||
| ); | ||
| when(oAuth2LinkRequestService.consume("link-nonce")) | ||
| .thenReturn(new OAuth2LinkRequestService.LinkRequest(USER_ID, LoginProvider.GOOGLE)); | ||
| when(userRepository.findByIdForUpdate(USER_ID)).thenReturn(Optional.of(currentUser)); | ||
| when(userSocialAccountRepository.findByProviderAndProviderUserId( | ||
| LoginProvider.GOOGLE, | ||
| PROVIDER_USER_ID | ||
| )).thenReturn(Optional.of(existingAccount)); | ||
|
|
||
| assertThatThrownBy(() -> oAuth2LoginService.linkAccount( | ||
| "link-nonce", | ||
| "google", | ||
| googleUser(), | ||
| PROVIDER_TOKEN | ||
| )).isInstanceOfSatisfying(AuthException.class, exception -> | ||
| assertThat(exception.getErrorCode()) | ||
| .isEqualTo(AuthErrorCode.SOCIAL_ACCOUNT_LINKED_TO_ANOTHER_USER) | ||
| ); | ||
| } | ||
|
|
||
| private OAuth2User googleUser() { | ||
| OAuth2User oAuth2User = org.mockito.Mockito.mock(OAuth2User.class); | ||
| when(oAuth2User.<Boolean>getAttribute("email_verified")).thenReturn(true); | ||
| when(oAuth2User.<String>getAttribute("sub")).thenReturn(PROVIDER_USER_ID); | ||
| when(oAuth2User.<String>getAttribute("email")).thenReturn(EMAIL); | ||
| return oAuth2User; | ||
| } | ||
|
|
||
| private User userWithId(User user, Long id) { | ||
| ReflectionTestUtils.setField(user, "id", id); | ||
| return user; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.