Skip to content

Commit

Permalink
#15 test : 유저 서비스 로그인 기능 테스트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
swinb committed Aug 8, 2023
1 parent 56439ae commit beff0e7
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 6 deletions.
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, () -> {
Expand All @@ -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);
});

}
Expand All @@ -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);
});

}
Expand All @@ -113,7 +114,7 @@ public void validateDuplicatedEmailSuccess() {
given(userRepository.findByEmail(any())).willReturn(Optional.empty());

assertDoesNotThrow(() -> {
// userValidator.validateDuplicatedEmail(signUpRequestDto);
userValidator.validateDuplicatedEmail(signUpRequestDto);
});
}

Expand All @@ -129,7 +130,7 @@ public void validateDuplicatedEmailFailed() {
given(userRepository.findByEmail(any())).willReturn(Optional.ofNullable(existUser));

assertThrows(CustomException.class, () -> {
// userValidator.validateDuplicatedEmail(signUpRequestDto);
userValidator.validateDuplicatedEmail(signUpRequestDto);
});
}

Expand Down

0 comments on commit beff0e7

Please sign in to comment.