From 4cc25d16a35224673daa9dcb34c666cdbe68b9d1 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 02:49:24 +0900 Subject: [PATCH 01/12] =?UTF-8?q?feat=20:=20encoder=20interface=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../overflow/auth/domain/helper/Encoder.java | 14 +++----------- .../overflow/auth/domain/helper/EncoderImpl.java | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/EncoderImpl.java diff --git a/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/Encoder.java b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/Encoder.java index d82bb17..2591ab0 100644 --- a/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/Encoder.java +++ b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/Encoder.java @@ -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); } diff --git a/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/EncoderImpl.java b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/EncoderImpl.java new file mode 100644 index 0000000..10fffeb --- /dev/null +++ b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/helper/EncoderImpl.java @@ -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); + } +} From ad08e0060731388ede79a7545b57b2ae202a3344 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 02:51:08 +0900 Subject: [PATCH 02/12] =?UTF-8?q?test=20:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=8B=9C=20=EC=A1=B4=EC=9E=AC=ED=95=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=20=EC=9D=B4=EB=A9=94=EC=9D=BC=EC=9D=B4=EB=A9=B4=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EA=B0=80=20=EB=B0=9C=EC=83=9D=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/usecase/LoginUserUseCaseTest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java new file mode 100644 index 0000000..cb119d3 --- /dev/null +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java @@ -0,0 +1,43 @@ +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.exception.NotFoundEmailException; +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.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(); + UserEntity entity = UserEntity.builder().build(); + + when(userRepository.findByEmail(any())).thenReturn(Optional.empty()); + + // when & then + assertThrows(NotFoundEmailException.class, () -> loginUserUseCase.execute(request)); + } +} From 6d749919a04f410712a9c75a36d2a371a45ae203 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 02:51:57 +0900 Subject: [PATCH 03/12] =?UTF-8?q?fix=20:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=BD=94=EB=93=9C=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../overflow/auth/domain/usecase/LoginUserUseCaseTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java index cb119d3..44b8bc2 100644 --- a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java @@ -8,7 +8,6 @@ import com.econovation.overflow.auth.domain.exception.NotFoundEmailException; 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.DisplayName; @@ -33,8 +32,6 @@ class LoginUserUseCaseTest { void exist_email_check_when_login() { // given LoginUserRequest request = LoginUserRequest.builder().build(); - UserEntity entity = UserEntity.builder().build(); - when(userRepository.findByEmail(any())).thenReturn(Optional.empty()); // when & then From 1254cda9afb1c0689b69f60172086d8e1cf3c2c8 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 20:23:17 +0900 Subject: [PATCH 04/12] =?UTF-8?q?fix=20:=20=EA=B5=AC=EC=B2=B4=EC=A0=81?= =?UTF-8?q?=EC=9D=B8=20=20encoder=20=EC=9D=98=EC=A1=B4=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../converter/UserModelConverterTest.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java index 347ce5e..9737a90 100644 --- a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java @@ -1,7 +1,11 @@ package com.econovation.overflow.auth.domain.model.converter; +import static org.mockito.Mockito.verify; + import com.econovation.overflow.auth.domain.dto.request.SaveUserRequest; +import com.econovation.overflow.auth.domain.helper.Encoder; import com.econovation.overflow.auth.domain.model.UserModel; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -14,10 +18,23 @@ @ExtendWith(MockitoExtension.class) class UserModelConverterTest { - @Mock private PasswordEncoder passwordEncoder; + @Mock private Encoder encoder; @InjectMocks private UserModelConverter userModelConverter; private static final String PASSWORD = "password"; + @Test + @DisplayName("request를 model로 변환한다.") + void request_convert_model() { + // given + SaveUserRequest request = SaveUserRequest.builder().build(); + + // when + UserModel model = userModelConverter.from(request); + + // then + Assertions.assertThat(model).isInstanceOf(UserModel.class); + } + @Test @DisplayName("request를 model로 변환할 때 암호화를 진행한다.") void encode_convert_model() { @@ -28,6 +45,6 @@ void encode_convert_model() { UserModel model = userModelConverter.from(request); // then - Mockito.verify(passwordEncoder).encode(PASSWORD); + verify(encoder).encode(PASSWORD); } } From 54835ef3bfa4b21fa41740b3ec06c6ab14060f7e Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 20:24:07 +0900 Subject: [PATCH 05/12] =?UTF-8?q?test=20:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8/=EC=84=B1=EA=B3=B5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/usecase/LoginUserUseCaseTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java index 44b8bc2..7012ce1 100644 --- a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/LoginUserUseCaseTest.java @@ -5,11 +5,15 @@ 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; @@ -37,4 +41,36 @@ void exist_email_check_when_login() { // 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); + } } From 640f1e7d3177ab5bd93a47cd3c987b086d4b2936 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 20:24:57 +0900 Subject: [PATCH 06/12] =?UTF-8?q?chore=20:=20lint=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/econovation/overflow/support/token/TokenResolver.java | 1 + .../auth/domain/model/converter/UserModelConverterTest.java | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/be/overflow/src/main/java/com/econovation/overflow/support/token/TokenResolver.java b/be/overflow/src/main/java/com/econovation/overflow/support/token/TokenResolver.java index a142c78..afaaf6d 100644 --- a/be/overflow/src/main/java/com/econovation/overflow/support/token/TokenResolver.java +++ b/be/overflow/src/main/java/com/econovation/overflow/support/token/TokenResolver.java @@ -10,6 +10,7 @@ import javax.crypto.SecretKey; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; + @Component public class TokenResolver { diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java index 9737a90..dd877b4 100644 --- a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/model/converter/UserModelConverterTest.java @@ -11,9 +11,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.security.crypto.password.PasswordEncoder; @ExtendWith(MockitoExtension.class) class UserModelConverterTest { From d13fa54a124e8f0e4b0142d8effab61208c876dd Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 20:25:31 +0900 Subject: [PATCH 07/12] =?UTF-8?q?test=20=20:=20SaveTokenService=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/service/SaveTokenServiceTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/SaveTokenServiceTest.java diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/SaveTokenServiceTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/SaveTokenServiceTest.java new file mode 100644 index 0000000..ec01d83 --- /dev/null +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/SaveTokenServiceTest.java @@ -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); + } +} From 8fd56d5089b055a67334f3777b3b5787a26eb5bc Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 22:27:20 +0900 Subject: [PATCH 08/12] =?UTF-8?q?fix=20:=20token=20=EC=B6=94=EC=B6=9C=20?= =?UTF-8?q?=ED=9B=84=20authInfo=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8A=94=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/service/GetAuthInfoService.java | 23 +++++++++++++++++++ .../auth/domain/usecase/ReissueUseCase.java | 16 ++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 be/overflow/src/main/java/com/econovation/overflow/auth/domain/service/GetAuthInfoService.java diff --git a/be/overflow/src/main/java/com/econovation/overflow/auth/domain/service/GetAuthInfoService.java b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/service/GetAuthInfoService.java new file mode 100644 index 0000000..e66a879 --- /dev/null +++ b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/service/GetAuthInfoService.java @@ -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("잘못된 토큰 입니다")); + } +} diff --git a/be/overflow/src/main/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCase.java b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCase.java index 5c867a9..25fd2eb 100644 --- a/be/overflow/src/main/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCase.java +++ b/be/overflow/src/main/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCase.java @@ -1,11 +1,10 @@ package com.econovation.overflow.auth.domain.usecase; import com.econovation.overflow.auth.domain.dto.response.TokenResponse; -import com.econovation.overflow.auth.domain.exception.AuthorizationException; import com.econovation.overflow.auth.domain.service.CreateTokenService; +import com.econovation.overflow.auth.domain.service.GetAuthInfoService; 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 lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -16,20 +15,15 @@ @Transactional(readOnly = true) @Slf4j public class ReissueUseCase { + private final GetAuthInfoService getAuthInfoService; private final CreateTokenService createTokenService; - private final TokenResolver tokenResolver; private final AuthInfoRepository authInfoRepository; @Transactional public TokenResponse execute(final String token) { - Long userId = tokenResolver.getUserInfo(token); - - AuthInfoEntity authInfoEntity = - authInfoRepository - .findByUserIdAndToken(userId, token) - .orElseThrow(() -> new AuthorizationException("잘못된 토큰 입니다")); - + AuthInfoEntity authInfoEntity = getAuthInfoService.execute(token); authInfoRepository.delete(authInfoEntity); - return createTokenService.execute(userId); + + return createTokenService.execute(authInfoEntity.getUserId()); } } From 1b1ee7c01e83d155ff80e14e7778024ca95055bb Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 23:30:43 +0900 Subject: [PATCH 09/12] =?UTF-8?q?test=20:=20GetAuthInfoService=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/GetAuthInfoServiceTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/GetAuthInfoServiceTest.java diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/GetAuthInfoServiceTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/GetAuthInfoServiceTest.java new file mode 100644 index 0000000..6d12eb3 --- /dev/null +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/GetAuthInfoServiceTest.java @@ -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)); + } +} From dcaed81f7788a6ba27f81e36f052da8f4bc2bf11 Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 23:30:58 +0900 Subject: [PATCH 10/12] =?UTF-8?q?feat=20:=20ReissueUseCase=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/usecase/ReissueUseCaseTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCaseTest.java diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCaseTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCaseTest.java new file mode 100644 index 0000000..3beee65 --- /dev/null +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ReissueUseCaseTest.java @@ -0,0 +1,60 @@ +package com.econovation.overflow.auth.domain.usecase; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.econovation.overflow.auth.domain.dto.response.TokenResponse; +import com.econovation.overflow.auth.domain.service.CreateTokenService; +import com.econovation.overflow.auth.domain.service.GetAuthInfoService; +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 ReissueUseCaseTest { + @InjectMocks private ReissueUseCase reissueUseCase; + + @Mock private AuthInfoRepository authInfoRepository; + @Mock private GetAuthInfoService getAuthInfoService; + @Mock private CreateTokenService createTokenService; + + @Test + @DisplayName("기존에 존재하는 토큰을 삭제한다.") + void delete_token_when_reissue() { + // given + AuthInfoEntity authInfoEntity = AuthInfoEntity.builder().build(); + TokenResponse tokenResponse = TokenResponse.builder().build(); + + when(getAuthInfoService.execute(any())).thenReturn(authInfoEntity); + when(createTokenService.execute(any())).thenReturn(tokenResponse); + + // when + reissueUseCase.execute(any()); + + // then + verify(authInfoRepository).delete(authInfoEntity); + } + + @Test + @DisplayName("재발급 요청 메시지를 전달한다.") + void crate_token_when_reissue() { + // given + AuthInfoEntity authInfoEntity = AuthInfoEntity.builder().build(); + + when(getAuthInfoService.execute(any())).thenReturn(authInfoEntity); + doNothing().when(authInfoRepository).delete(authInfoEntity); + + // when + reissueUseCase.execute(any()); + + // then + verify(createTokenService).execute(any()); + } +} From 8e2bdd3c0582969d42ee8dde84c906bf150e101f Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Wed, 11 Oct 2023 23:58:33 +0900 Subject: [PATCH 11/12] =?UTF-8?q?test/fix=20:=20public=20=EC=A0=91?= =?UTF-8?q?=EA=B7=BC=20=EC=A7=80=EC=8B=9C=EC=9E=90=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../overflow/auth/domain/usecase/ValidEmailUseCaseTest.java | 4 ++-- .../auth/domain/usecase/ValidNicknameUseCaseTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidEmailUseCaseTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidEmailUseCaseTest.java index c65fc38..136b274 100644 --- a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidEmailUseCaseTest.java +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidEmailUseCaseTest.java @@ -21,7 +21,7 @@ class ValidEmailUseCaseTest { @Test @DisplayName("이메일 중복되면 예외가 발생한다.") - public void duplicated_email() { + void duplicated_email() { // given SaveUserRequest request = SaveUserRequest.builder().email("email@email").build(); @@ -33,7 +33,7 @@ public void duplicated_email() { @Test @DisplayName("이메일 중복이 되지 않으면 예외가 발생하지 않는다.") - public void not_duplicated_email() { + void not_duplicated_email() { // given SaveUserRequest request = SaveUserRequest.builder().email("email@email").build(); diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidNicknameUseCaseTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidNicknameUseCaseTest.java index b98d0cf..c7a14e0 100644 --- a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidNicknameUseCaseTest.java +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/usecase/ValidNicknameUseCaseTest.java @@ -21,7 +21,7 @@ class ValidNicknameUseCaseTest { @Test @DisplayName("닉네임 중복되면 예외가 발생한다.") - public void duplicated_email() { + void duplicated_email() { // given SaveUserRequest request = SaveUserRequest.builder().nickname("nickname").build(); @@ -33,7 +33,7 @@ public void duplicated_email() { @Test @DisplayName("닉네임 중복이 되지 않으면 예외가 발생하지 않는다.") - public void not_duplicated_email() { + void not_duplicated_email() { // given SaveUserRequest request = SaveUserRequest.builder().nickname("nickname").build(); From cb66c0666c7d06fa24e791687cd07fb96a3d20dd Mon Sep 17 00:00:00 2001 From: kssumin <201566@jnu.ac.kr> Date: Thu, 12 Oct 2023 00:02:06 +0900 Subject: [PATCH 12/12] =?UTF-8?q?test=20:=20createTokenService=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/CreateTokenServiceTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/CreateTokenServiceTest.java diff --git a/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/CreateTokenServiceTest.java b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/CreateTokenServiceTest.java new file mode 100644 index 0000000..94a6b3b --- /dev/null +++ b/be/overflow/src/test/java/com/econovation/overflow/auth/domain/service/CreateTokenServiceTest.java @@ -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); + } +}