From 65a8d9ec199f9b63fcdb3bd815a034b3eeb9a45a Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Wed, 27 Nov 2024 21:53:16 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#92=20[refactor]=20:=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=EB=A5=BC=20=EC=9D=B4=EB=8F=99=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/{ => presentation}/UserEntityControllerTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) rename src/test/java/kusitms/backend/user/{ => presentation}/UserEntityControllerTest.java (99%) diff --git a/src/test/java/kusitms/backend/user/UserEntityControllerTest.java b/src/test/java/kusitms/backend/user/presentation/UserEntityControllerTest.java similarity index 99% rename from src/test/java/kusitms/backend/user/UserEntityControllerTest.java rename to src/test/java/kusitms/backend/user/presentation/UserEntityControllerTest.java index d548fb0..48a13f1 100644 --- a/src/test/java/kusitms/backend/user/UserEntityControllerTest.java +++ b/src/test/java/kusitms/backend/user/presentation/UserEntityControllerTest.java @@ -1,4 +1,4 @@ -package kusitms.backend.user; +package kusitms.backend.user.presentation; import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper; import com.epages.restdocs.apispec.ResourceSnippetParameters; @@ -10,7 +10,6 @@ import kusitms.backend.user.application.dto.request.SignUpRequestDto; import kusitms.backend.user.application.dto.response.AuthTokenResponseDto; import kusitms.backend.user.application.dto.response.UserInfoResponseDto; -import kusitms.backend.user.presentation.UserController; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -224,4 +223,4 @@ public void reIssueToken() throws Exception { )); } -} \ No newline at end of file +} From 8838af6525c8fe381d5bf8e9e1dcf3ba9c2a056c Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Wed, 27 Nov 2024 21:53:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?#92=20[feat]=20:=20UserApplicationService?= =?UTF-8?q?=20=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/UserApplicationServiceTest.java | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/test/java/kusitms/backend/user/application/service/UserApplicationServiceTest.java diff --git a/src/test/java/kusitms/backend/user/application/service/UserApplicationServiceTest.java b/src/test/java/kusitms/backend/user/application/service/UserApplicationServiceTest.java new file mode 100644 index 0000000..b793070 --- /dev/null +++ b/src/test/java/kusitms/backend/user/application/service/UserApplicationServiceTest.java @@ -0,0 +1,174 @@ +package kusitms.backend.user.application.service; + +import kusitms.backend.global.exception.CustomException; +import kusitms.backend.global.redis.DistributedLockManager; +import kusitms.backend.global.redis.RedisManager; +import kusitms.backend.user.application.UserApplicationService; +import kusitms.backend.user.application.dto.request.CheckNicknameRequestDto; +import kusitms.backend.user.application.dto.request.SendAuthCodeRequestDto; +import kusitms.backend.user.application.dto.request.VerifyAuthCodeRequestDto; +import kusitms.backend.user.application.dto.response.AuthTokenResponseDto; +import kusitms.backend.user.domain.enums.ProviderStatusType; +import kusitms.backend.user.domain.model.User; +import kusitms.backend.user.domain.repository.UserRepository; +import kusitms.backend.user.infra.jwt.JWTUtil; +import kusitms.backend.user.infra.sms.SmsManager; +import kusitms.backend.user.status.AuthErrorStatus; +import kusitms.backend.user.status.UserErrorStatus; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class UserApplicationServiceTest { + + @InjectMocks + private UserApplicationService service; + + @Mock + private UserRepository userRepository; + + @Mock + private JWTUtil jwtUtil; + + @Mock + private SmsManager smsManager; + + @Mock + private RedisManager redisManager; + + @Mock + private DistributedLockManager distributedLockManager; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + /** + * 닉네임 중복 확인 성공 테스트. + */ + @Test + void testCheckNickname_Success() { + // Given + String nickname = "newNickname"; + CheckNicknameRequestDto request = new CheckNicknameRequestDto(nickname); + String lockKey = "lock:nickname:" + nickname; + + when(distributedLockManager.acquireLock(lockKey, 5000)).thenReturn(true); + when(userRepository.findUserByNickname(nickname)).thenReturn(null); + + // When & Then + assertDoesNotThrow(() -> service.checkNickname(request)); + verify(distributedLockManager, times(1)).releaseLock(lockKey); + } + + /** + * 닉네임 중복 확인 실패 테스트 (중복된 닉네임). + */ + @Test + void testCheckNickname_Failure() { + // Given + String nickname = "existingNickname"; + CheckNicknameRequestDto request = new CheckNicknameRequestDto(nickname); + String lockKey = "lock:nickname:" + nickname; + + when(distributedLockManager.acquireLock(lockKey, 5000)).thenReturn(true); + when(userRepository.findUserByNickname(nickname)) + .thenReturn(User.toDomain(1L, ProviderStatusType.KAKAO, "providerId", "email@example.com", nickname)); + + // When & Then + CustomException exception = assertThrows(CustomException.class, () -> service.checkNickname(request)); + assertEquals(UserErrorStatus._DUPLICATED_NICKNAME, exception.getErrorCode()); + verify(distributedLockManager, times(1)).releaseLock(lockKey); + } + + /** + * 인증 코드 발송 테스트. + */ + @Test + void testSendAuthCode_Success() { + // Given + String phoneNumber = "01012345678"; + SendAuthCodeRequestDto request = new SendAuthCodeRequestDto(phoneNumber); + + // When + service.sendAuthCode(request); + + // Then + verify(redisManager, times(1)).savePhoneVerificationCode(eq(phoneNumber), anyString()); + verify(smsManager, times(1)).sendSms(eq(phoneNumber), contains("히트존 인증 코드 번호")); + } + + /** + * 인증 코드 검증 성공 테스트. + */ + @Test + void testVerifyAuthCode_Success() { + // Given + String phoneNumber = "01012345678"; + String authCode = "123456"; + VerifyAuthCodeRequestDto request = new VerifyAuthCodeRequestDto(phoneNumber, authCode); + + when(redisManager.getPhoneVerificationCode(phoneNumber)).thenReturn(authCode); + + // When & Then + assertDoesNotThrow(() -> service.verifyAuthCode(request)); + } + + /** + * 인증 코드 검증 실패 테스트 (코드 불일치). + */ + @Test + void testVerifyAuthCode_Mismatch() { + // Given + String phoneNumber = "01012345678"; + String savedAuthCode = "123456"; + String wrongAuthCode = "654321"; + VerifyAuthCodeRequestDto request = new VerifyAuthCodeRequestDto(phoneNumber, wrongAuthCode); + + when(redisManager.getPhoneVerificationCode(phoneNumber)).thenReturn(savedAuthCode); + + // When & Then + CustomException exception = assertThrows(CustomException.class, () -> service.verifyAuthCode(request)); + assertEquals(UserErrorStatus._MISS_MATCH_AUTH_CODE, exception.getErrorCode()); + } + + /** + * Refresh Token 재발급 성공 테스트. + */ + @Test + void testReIssueToken_Success() { + // Given + String refreshToken = "validRefreshToken"; + Long userId = 1L; + + when(redisManager.validateAndExtractUserId(refreshToken)).thenReturn(userId); + when(jwtUtil.generateAccessOrRefreshToken(userId, jwtUtil.getAccessTokenExpirationTime())).thenReturn("newAccessToken"); + when(jwtUtil.generateAccessOrRefreshToken(userId, jwtUtil.getRefreshTokenExpirationTime())).thenReturn("newRefreshToken"); + + // When + AuthTokenResponseDto result = service.reIssueToken(refreshToken); + + // Then + assertNotNull(result); + assertEquals("newRefreshToken", result.refreshToken()); + } + + /** + * Refresh Token 재발급 실패 테스트 (토큰 만료). + */ + @Test + void testReIssueToken_Failure() { + // Given + String expiredRefreshToken = null; + + // When & Then + CustomException exception = assertThrows(CustomException.class, () -> service.reIssueToken(expiredRefreshToken)); + assertEquals(AuthErrorStatus._EXPIRED_REFRESH_TOKEN, exception.getErrorCode()); + } +}