-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
be/issue/src/test/java/codesquad/issueTracker/user/service/UserServiceTest.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,153 @@ | ||
package codesquad.issueTracker.user.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mindrot.jbcrypt.BCrypt; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
|
||
import annotation.ServiceTest; | ||
import codesquad.issueTracker.global.exception.CustomException; | ||
import codesquad.issueTracker.jwt.domain.Jwt; | ||
import codesquad.issueTracker.jwt.domain.Token; | ||
import codesquad.issueTracker.jwt.util.JwtProvider; | ||
import codesquad.issueTracker.user.domain.LoginType; | ||
import codesquad.issueTracker.user.domain.User; | ||
import codesquad.issueTracker.user.dto.LoginRequestDto; | ||
import codesquad.issueTracker.user.dto.LoginResponseDto; | ||
import codesquad.issueTracker.user.repository.UserRepository; | ||
|
||
@ServiceTest | ||
public class UserServiceTest { | ||
|
||
@InjectMocks | ||
UserService userService; | ||
@Mock | ||
UserRepository userRepository; | ||
@Mock | ||
UserValidator userValidator; | ||
@Mock | ||
JwtProvider jwtProvider; | ||
|
||
@DisplayName("로그인 성공") | ||
@Test | ||
void loginSuccess() { | ||
//given | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.password(BCrypt.hashpw("password", BCrypt.gensalt())) | ||
.loginType(LoginType.LOCAL) | ||
.build(); | ||
|
||
Jwt mockJwt = new Jwt("accessToken", "refreshToken"); | ||
|
||
given(userRepository.findByEmail(mockUser.getEmail())).willReturn(Optional.of(mockUser)); | ||
given(jwtProvider.createJwt(anyMap())).willReturn(mockJwt); | ||
|
||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "password"); | ||
|
||
//when | ||
LoginResponseDto response = userService.login(loginRequestDto); | ||
|
||
//then | ||
assertAll( | ||
() -> assertEquals(mockUser.getId(), response.getUserId()), | ||
() -> assertEquals("accessToken", response.getAccessToken()), | ||
() -> assertEquals("refreshToken", response.getRefreshToken()) | ||
); | ||
} | ||
|
||
@DisplayName("존재하지 않는 유저 로그인 실패") | ||
@Test | ||
void loginFailedByNotFoundUser() { | ||
given(userRepository.findByEmail(any())).willReturn(Optional.empty()); | ||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "password"); | ||
|
||
//when | ||
assertThrows(CustomException.class, () -> userService.login(loginRequestDto)); | ||
} | ||
|
||
@DisplayName("비밀번호 불일치 로그인 실패") | ||
@Test | ||
void loginFailedByWrongPassword() { | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.password(BCrypt.hashpw("password", BCrypt.gensalt())) | ||
.loginType(LoginType.LOCAL) | ||
.build(); | ||
|
||
given(userRepository.findByEmail(mockUser.getEmail())).willReturn(Optional.of(mockUser)); | ||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "passwor"); | ||
|
||
//when | ||
assertThrows(CustomException.class, () -> userService.login(loginRequestDto)); | ||
} | ||
|
||
@DisplayName("로그인타입 불일치 로그인 실패") | ||
@Test | ||
void loginFailedByLoginTypeDiff() { | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.password(BCrypt.hashpw("password", BCrypt.gensalt())) | ||
.loginType(LoginType.GITHUB) | ||
.build(); | ||
|
||
given(userRepository.findByEmail(mockUser.getEmail())).willReturn(Optional.of(mockUser)); | ||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "password"); | ||
|
||
//when | ||
assertThrows(CustomException.class, () -> userService.login(loginRequestDto)); | ||
} | ||
|
||
@DisplayName("로그인 시 리프레쉬 토큰이 존재하지 않을 때 토큰을 생성하는지 테스트") | ||
@Test | ||
void loginWhenRefreshTokenIsNull() { | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.password(BCrypt.hashpw("password", BCrypt.gensalt())) | ||
.loginType(LoginType.LOCAL) | ||
.build(); | ||
|
||
Jwt mockJwt = new Jwt("accessToken", "refreshToken"); | ||
|
||
given(userRepository.findByEmail(mockUser.getEmail())).willReturn(Optional.of(mockUser)); | ||
given(jwtProvider.createJwt(anyMap())).willReturn(mockJwt); | ||
given(userRepository.findTokenByUserId(1L)).willReturn(Optional.empty()); | ||
|
||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "password"); | ||
userService.login(loginRequestDto); | ||
verify(userRepository, times(1)).insertRefreshToken(1L, mockJwt.getRefreshToken()); | ||
verify(userRepository, times(0)).updateRefreshToken(1L, mockJwt.getRefreshToken()); | ||
} | ||
|
||
@DisplayName("로그인 시 리프레쉬 토큰이 존재할 때 토큰을 업데이트하는지 테스트") | ||
@Test | ||
void loginWhenRefreshTokenIsExist() { | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.email("[email protected]") | ||
.password(BCrypt.hashpw("password", BCrypt.gensalt())) | ||
.loginType(LoginType.LOCAL) | ||
.build(); | ||
|
||
Jwt mockJwt = new Jwt("accessToken", "refreshToken"); | ||
|
||
given(userRepository.findByEmail(mockUser.getEmail())).willReturn(Optional.of(mockUser)); | ||
given(jwtProvider.createJwt(anyMap())).willReturn(mockJwt); | ||
given(userRepository.findTokenByUserId(1L)).willReturn(Optional.of(new Token(1L, 1L, "dummy"))); | ||
|
||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "password"); | ||
userService.login(loginRequestDto); | ||
verify(userRepository, times(0)).insertRefreshToken(1L, mockJwt.getRefreshToken()); | ||
verify(userRepository, times(1)).updateRefreshToken(1L, mockJwt.getRefreshToken()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -52,10 +52,11 @@ void validateLoginUserFailed() { | |
.id(1L) | ||
.email("[email protected]") | ||
.password(BCrypt.hashpw("12345678", BCrypt.gensalt())) | ||
.loginType(LoginType.LOCAL) | ||
.build(); | ||
|
||
given(userRepository.findByEmail("asd123@ddd.com")).willReturn(Optional.of(user)); | ||
User findUser = userRepository.findByEmail("asd123@ddd.com").orElseThrow(); | ||
given(userRepository.findByEmail("asd123@dddd.com")).willReturn(Optional.of(user)); | ||
User findUser = userRepository.findByEmail("asd123@dddd.com").orElseThrow(); | ||
LoginRequestDto loginRequestDto = new LoginRequestDto("[email protected]", "123dd45678"); | ||
|
||
assertThrows(CustomException.class, () -> { | ||
|
@@ -77,7 +78,7 @@ public void validateLoginTypeSuccess() { | |
given(userRepository.findByEmail(any())).willReturn(Optional.ofNullable(existUser)); | ||
LoginType existUserLoginType = userRepository.findByEmail(existUser.getEmail()).get().getLoginType(); | ||
assertDoesNotThrow(() -> { | ||
// userValidator.validateLoginType(input, existUserLoginType); | ||
userValidator.validateLoginType(input, existUserLoginType); | ||
}); | ||
|
||
} | ||
|
@@ -96,7 +97,7 @@ public void validateLoginTypeFailed() { | |
given(userRepository.findByEmail(any())).willReturn(Optional.ofNullable(existUser)); | ||
LoginType existUserLoginType = userRepository.findByEmail(existUser.getEmail()).get().getLoginType(); | ||
assertThrows(CustomException.class, () -> { | ||
// userValidator.validateLoginType(input, existUserLoginType); | ||
userValidator.validateLoginType(input, existUserLoginType); | ||
}); | ||
|
||
} | ||
|
@@ -113,7 +114,7 @@ public void validateDuplicatedEmailSuccess() { | |
given(userRepository.findByEmail(any())).willReturn(Optional.empty()); | ||
|
||
assertDoesNotThrow(() -> { | ||
// userValidator.validateDuplicatedEmail(signUpRequestDto); | ||
userValidator.validateDuplicatedEmail(signUpRequestDto); | ||
}); | ||
} | ||
|
||
|
@@ -129,7 +130,7 @@ public void validateDuplicatedEmailFailed() { | |
given(userRepository.findByEmail(any())).willReturn(Optional.ofNullable(existUser)); | ||
|
||
assertThrows(CustomException.class, () -> { | ||
// userValidator.validateDuplicatedEmail(signUpRequestDto); | ||
userValidator.validateDuplicatedEmail(signUpRequestDto); | ||
}); | ||
} | ||
|
||
|