-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Test] 로그인 business layer 테스트 코드
- Loading branch information
Showing
13 changed files
with
356 additions
and
30 deletions.
There are no files selected for viewing
14 changes: 3 additions & 11 deletions
14
be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/Encoder.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 |
---|---|---|
@@ -1,15 +1,7 @@ | ||
package com.econovation.overflow.auth.domain.helper; | ||
|
||
import org.mindrot.jbcrypt.BCrypt; | ||
import org.springframework.stereotype.Component; | ||
public interface Encoder { | ||
String encode(String raw); | ||
|
||
@Component | ||
public class Encoder { | ||
public String encode(String raw) { | ||
return BCrypt.hashpw(raw, BCrypt.gensalt()); | ||
} | ||
|
||
public boolean matches(String raw, String hashed) { | ||
return BCrypt.checkpw(raw, hashed); | ||
} | ||
boolean matches(String raw, String hashed); | ||
} |
15 changes: 15 additions & 0 deletions
15
be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/EncoderImpl.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,15 @@ | ||
package com.econovation.overflow.auth.domain.helper; | ||
|
||
import org.mindrot.jbcrypt.BCrypt; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class EncoderImpl implements Encoder { | ||
public String encode(String raw) { | ||
return BCrypt.hashpw(raw, BCrypt.gensalt()); | ||
} | ||
|
||
public boolean matches(String raw, String hashed) { | ||
return BCrypt.checkpw(raw, hashed); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...erflow/src/main/java/com/econovation/overflow/auth/domain/service/GetAuthInfoService.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,23 @@ | ||
package com.econovation.overflow.auth.domain.service; | ||
|
||
import com.econovation.overflow.auth.domain.exception.AuthorizationException; | ||
import com.econovation.overflow.auth.persistence.entity.AuthInfoEntity; | ||
import com.econovation.overflow.auth.persistence.repository.AuthInfoRepository; | ||
import com.econovation.overflow.support.token.TokenResolver; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GetAuthInfoService { | ||
private final TokenResolver tokenResolver; | ||
private final AuthInfoRepository authInfoRepository; | ||
|
||
public AuthInfoEntity execute(String token) { | ||
Long userId = tokenResolver.getUserInfo(token); | ||
|
||
return authInfoRepository | ||
.findByUserIdAndToken(userId, token) | ||
.orElseThrow(() -> new AuthorizationException("잘못된 토큰 입니다")); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
...ow/src/test/java/com/econovation/overflow/auth/domain/service/CreateTokenServiceTest.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,52 @@ | ||
package com.econovation.overflow.auth.domain.service; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.econovation.overflow.auth.domain.dto.converter.TokenConverter; | ||
import com.econovation.overflow.auth.domain.dto.response.TokenResponse; | ||
import com.econovation.overflow.auth.persistence.converter.AuthInfoEntityConverter; | ||
import com.econovation.overflow.auth.persistence.entity.AuthInfoEntity; | ||
import com.econovation.overflow.auth.persistence.repository.AuthInfoRepository; | ||
import com.econovation.overflow.support.token.TokenProvider; | ||
import com.econovation.overflow.support.token.TokenResolver; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
import org.junit.jupiter.api.DisplayName; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CreateTokenServiceTest { | ||
@Mock private TokenProvider tokenProvider; | ||
@Mock private TokenResolver tokenResolver; | ||
@Mock private TokenConverter tokenConverter; | ||
@Mock private AuthInfoRepository authInfoRepository; | ||
@Mock private AuthInfoEntityConverter authInfoEntityConverter; | ||
@InjectMocks private CreateTokenService createTokenService; | ||
|
||
@Test | ||
@DisplayName("토큰을 저장 메시지를 전달한다.") | ||
void save_token() { | ||
// given | ||
Long userId = 1L; | ||
Date expiredDate = Date.from(Instant.EPOCH); | ||
|
||
String accessToken = tokenProvider.createAccessToken(userId); | ||
String refreshToken = tokenProvider.createRefreshToken(userId); | ||
TokenResponse tokenResponse = TokenResponse.builder().build(); | ||
|
||
AuthInfoEntity authInfoEntity = authInfoEntityConverter.from(userId, refreshToken); | ||
when(tokenResolver.getExpiredDate(accessToken)).thenReturn(expiredDate); | ||
when(tokenConverter.from(accessToken, expiredDate, refreshToken)).thenReturn(tokenResponse); | ||
|
||
// when | ||
createTokenService.execute(userId); | ||
|
||
// then | ||
verify(authInfoRepository).save(authInfoEntity); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...ow/src/test/java/com/econovation/overflow/auth/domain/service/GetAuthInfoServiceTest.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,59 @@ | ||
package com.econovation.overflow.auth.domain.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.econovation.overflow.auth.domain.exception.AuthorizationException; | ||
import com.econovation.overflow.auth.persistence.entity.AuthInfoEntity; | ||
import com.econovation.overflow.auth.persistence.repository.AuthInfoRepository; | ||
import com.econovation.overflow.support.token.TokenResolver; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.DisplayName; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class GetAuthInfoServiceTest { | ||
@Mock private TokenResolver tokenResolver; | ||
|
||
@Mock private AuthInfoRepository authInfoRepository; | ||
|
||
@InjectMocks private GetAuthInfoService getAuthInfoService; | ||
|
||
@Test | ||
@DisplayName("token으로부터 auth 정보를 가져온다.") | ||
void get_auth_info_by_token() { | ||
// given | ||
Long userId = 1L; | ||
String token = "token"; | ||
|
||
AuthInfoEntity actual = AuthInfoEntity.builder().build(); | ||
when(tokenResolver.getUserInfo(token)).thenReturn(userId); | ||
when(authInfoRepository.findByUserIdAndToken(userId, token)) | ||
.thenReturn(Optional.ofNullable(actual)); | ||
|
||
// then | ||
AuthInfoEntity expected = getAuthInfoService.execute(token); | ||
|
||
// then | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
@DisplayName("token과 유저 정보에 해당하는 auth정보가 없다면 예외가 발생한다.") | ||
void exception_when_get_auth_info_by_token() { | ||
// given | ||
Long userId = 1L; | ||
String token = "token"; | ||
|
||
when(tokenResolver.getUserInfo(token)).thenReturn(userId); | ||
when(authInfoRepository.findByUserIdAndToken(userId, token)) | ||
.thenThrow(AuthorizationException.class); | ||
|
||
// then & then | ||
assertThrows(AuthorizationException.class, () -> getAuthInfoService.execute(token)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...flow/src/test/java/com/econovation/overflow/auth/domain/service/SaveTokenServiceTest.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,39 @@ | ||
package com.econovation.overflow.auth.domain.service; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
import com.econovation.overflow.auth.persistence.converter.AuthInfoEntityConverter; | ||
import com.econovation.overflow.auth.persistence.entity.AuthInfoEntity; | ||
import com.econovation.overflow.auth.persistence.repository.AuthInfoRepository; | ||
import org.junit.jupiter.api.DisplayName; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SaveTokenServiceTest { | ||
@Mock private AuthInfoRepository authInfoRepository; | ||
|
||
@InjectMocks private SaveTokenService saveTokenService; | ||
|
||
@Mock private AuthInfoEntityConverter converter; | ||
|
||
@Test | ||
@DisplayName("유저 id, 토큰 정보를 가진 entity를 authInfoRepository에게 save메시지를 전달한다.") | ||
void execute() { | ||
// given | ||
Long userId = 1L; | ||
String token = "token"; | ||
|
||
AuthInfoEntity entity = AuthInfoEntity.builder().userId(userId).token(token).build(); | ||
when(converter.from(userId, token)).thenReturn(entity); | ||
|
||
// when | ||
saveTokenService.execute(userId, token); | ||
|
||
// then | ||
verify(authInfoRepository).save(entity); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...flow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.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,76 @@ | ||
package com.econovation.overflow.auth.domain.usecase; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.econovation.overflow.auth.domain.dto.request.LoginUserRequest; | ||
import com.econovation.overflow.auth.domain.dto.response.TokenResponse; | ||
import com.econovation.overflow.auth.domain.exception.NotFoundEmailException; | ||
import com.econovation.overflow.auth.domain.exception.NotFoundPasswordException; | ||
import com.econovation.overflow.auth.domain.helper.Encoder; | ||
import com.econovation.overflow.auth.domain.service.CreateTokenService; | ||
import com.econovation.overflow.auth.persistence.entity.UserEntity; | ||
import com.econovation.overflow.auth.persistence.repository.UserRepository; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class LoginUserUseCaseTest { | ||
@Mock private UserRepository userRepository; | ||
|
||
@Mock private CreateTokenService createTokenService; | ||
|
||
@InjectMocks private LoginUserUseCase loginUserUseCase; | ||
|
||
@Mock private Encoder encoder; | ||
|
||
@Test | ||
@DisplayName("로그인 시 존재하지 않는 이메일이면 예외가 발생한다.") | ||
void exist_email_check_when_login() { | ||
// given | ||
LoginUserRequest request = LoginUserRequest.builder().build(); | ||
when(userRepository.findByEmail(any())).thenReturn(Optional.empty()); | ||
|
||
// when & then | ||
assertThrows(NotFoundEmailException.class, () -> loginUserUseCase.execute(request)); | ||
} | ||
|
||
@Test | ||
@DisplayName("로그인 시 비밀번호가 맞지 않으면 예외가 발생한다.") | ||
void same_password_check_when_login() { | ||
// given | ||
LoginUserRequest request = LoginUserRequest.builder().build(); | ||
UserEntity entity = UserEntity.builder().build(); | ||
when(userRepository.findByEmail(any())).thenReturn(Optional.of(entity)); | ||
when(encoder.matches(any(), any())).thenReturn(Boolean.FALSE); | ||
|
||
// when & then | ||
assertThrows(NotFoundPasswordException.class, () -> loginUserUseCase.execute(request)); | ||
} | ||
|
||
@Test | ||
@DisplayName("로그인을 성공하면 토큰 정보를 전달한다.") | ||
void pass_token_info_when_success_login() { | ||
// given | ||
LoginUserRequest request = LoginUserRequest.builder().email(any()).build(); | ||
UserEntity entity = UserEntity.builder().build(); | ||
TokenResponse expected = TokenResponse.builder().build(); | ||
|
||
when(userRepository.findByEmail(request.getEmail())).thenReturn(Optional.of(entity)); | ||
when(encoder.matches(any(), any())).thenReturn(Boolean.TRUE); | ||
when(createTokenService.execute(any())).thenReturn(expected); | ||
|
||
// when | ||
TokenResponse actual = loginUserUseCase.execute(request); | ||
|
||
// then | ||
Assertions.assertEquals(expected, actual); | ||
} | ||
} |
Oops, something went wrong.