From 3b2e2eb476fc78ca4bf4cb7411871c20079d731b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:11:29 +0900 Subject: [PATCH 01/14] test auth controller setting --- .../controller/auth/AuthControllerTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java diff --git a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java new file mode 100644 index 0000000..e6a9506 --- /dev/null +++ b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java @@ -0,0 +1,23 @@ +package team.themoment.officialgsm.admin.controller.auth; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ExtendWith(MockitoExtension.class) +class AuthControllerTest { + + @InjectMocks + AuthController authController; + + private MockMvc mockMvc; + + @BeforeEach + public void init() { + mockMvc = MockMvcBuilders.standaloneSetup(authController) + .build(); + } +} \ No newline at end of file From 9905a6192fe583468d6ff57a1b160d986e225d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:16:33 +0900 Subject: [PATCH 02/14] test nameModify controller test --- .../controller/auth/AuthControllerTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java index e6a9506..aa29f65 100644 --- a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java +++ b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java @@ -1,11 +1,27 @@ package team.themoment.officialgsm.admin.controller.auth; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; +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; +import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import team.themoment.officialgsm.admin.controller.auth.dto.request.UserNameModifyRequest; +import team.themoment.officialgsm.admin.controller.auth.manager.UserManager; +import team.themoment.officialgsm.admin.controller.auth.mapper.AuthDataMapper; +import team.themoment.officialgsm.domain.auth.usecase.ModifyNameUseCase; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @ExtendWith(MockitoExtension.class) class AuthControllerTest { @@ -13,6 +29,15 @@ class AuthControllerTest { @InjectMocks AuthController authController; + @Mock + ModifyNameUseCase modifyNameUseCase; + + @Mock + UserManager userManager; + + @Mock + AuthDataMapper userDataMapper; + private MockMvc mockMvc; @BeforeEach @@ -20,4 +45,21 @@ public void init() { mockMvc = MockMvcBuilders.standaloneSetup(authController) .build(); } + + @Test + void nameModify() throws Exception { + // given + UserNameModifyRequest request = new UserNameModifyRequest("신희성"); + + // when + ResultActions resultActions = mockMvc.perform(patch("/api/auth/username") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(request))); + + + // then + resultActions.andExpect(status().isOk()); + + verify(modifyNameUseCase, times(1)).execute(eq(userDataMapper.toDto(request)), any()); + } } \ No newline at end of file From d4ae9f9cc1c98e7b106bb58f1e676a7ee88174b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:18:02 +0900 Subject: [PATCH 03/14] test userInfoFind controller test --- .../controller/auth/AuthControllerTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java index aa29f65..36bc081 100644 --- a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java +++ b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java @@ -12,15 +12,22 @@ import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import team.themoment.officialgsm.admin.controller.auth.dto.request.UserNameModifyRequest; +import team.themoment.officialgsm.admin.controller.auth.dto.response.UserInfoResponse; import team.themoment.officialgsm.admin.controller.auth.manager.UserManager; import team.themoment.officialgsm.admin.controller.auth.mapper.AuthDataMapper; +import team.themoment.officialgsm.domain.auth.dto.UserInfoDto; +import team.themoment.officialgsm.domain.auth.usecase.FindUserInfoUseCase; import team.themoment.officialgsm.domain.auth.usecase.ModifyNameUseCase; +import team.themoment.officialgsm.domain.user.Role; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @ExtendWith(MockitoExtension.class) @@ -32,6 +39,9 @@ class AuthControllerTest { @Mock ModifyNameUseCase modifyNameUseCase; + @Mock + FindUserInfoUseCase findUserInfoUseCase; + @Mock UserManager userManager; @@ -62,4 +72,27 @@ void nameModify() throws Exception { verify(modifyNameUseCase, times(1)).execute(eq(userDataMapper.toDto(request)), any()); } + + @Test + void userInfoFind() throws Exception { + // given + UserInfoDto mockUserInfoDto = new UserInfoDto("신희성", Role.ADMIN, "s23012@gsm.hs.kr"); + UserInfoResponse mockUserInfoResponse = new UserInfoResponse("신희성", Role.ADMIN, "s23012@gsm.hs.kr"); + + given(findUserInfoUseCase.execute(any())).willReturn(mockUserInfoDto); + given(userDataMapper.toInfoResponse(any(UserInfoDto.class))).willReturn(mockUserInfoResponse); + + // when + ResultActions resultActions = mockMvc.perform(get("/api/auth/userinfo")); + + // then + resultActions + .andExpect(status().isOk()) + .andExpect(jsonPath("$.userName").value("신희성")) + .andExpect(jsonPath("$.role").value("ADMIN")) + .andExpect(jsonPath("$.userEmail").value("s23012@gsm.hs.kr")); + + verify(findUserInfoUseCase, times(1)).execute(any()); + verify(userDataMapper, times(1)).toInfoResponse(mockUserInfoDto); + } } \ No newline at end of file From 1e3cd0c742e481b89f8e8c01782c4c92b94375d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:19:54 +0900 Subject: [PATCH 04/14] test logout controller test --- .../controller/auth/AuthControllerTest.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java index 36bc081..b6b19c6 100644 --- a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java +++ b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java @@ -13,10 +13,13 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import team.themoment.officialgsm.admin.controller.auth.dto.request.UserNameModifyRequest; import team.themoment.officialgsm.admin.controller.auth.dto.response.UserInfoResponse; +import team.themoment.officialgsm.admin.controller.auth.manager.CookieManager; import team.themoment.officialgsm.admin.controller.auth.manager.UserManager; import team.themoment.officialgsm.admin.controller.auth.mapper.AuthDataMapper; +import team.themoment.officialgsm.common.util.ConstantsUtil; import team.themoment.officialgsm.domain.auth.dto.UserInfoDto; import team.themoment.officialgsm.domain.auth.usecase.FindUserInfoUseCase; +import team.themoment.officialgsm.domain.auth.usecase.LogoutUseCase; import team.themoment.officialgsm.domain.auth.usecase.ModifyNameUseCase; import team.themoment.officialgsm.domain.user.Role; @@ -25,8 +28,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -42,6 +44,12 @@ class AuthControllerTest { @Mock FindUserInfoUseCase findUserInfoUseCase; + @Mock + LogoutUseCase logoutUseCase; + + @Mock + CookieManager cookieManager; + @Mock UserManager userManager; @@ -95,4 +103,21 @@ void userInfoFind() throws Exception { verify(findUserInfoUseCase, times(1)).execute(any()); verify(userDataMapper, times(1)).toInfoResponse(mockUserInfoDto); } + + + @Test + void logout() throws Exception { + // given + String accessToken = "0"; + + given(cookieManager.getCookieValue(any(), eq(ConstantsUtil.accessToken))).willReturn(accessToken); + + // when + ResultActions resultActions = mockMvc.perform(delete("/api/auth/logout")); + + // then + resultActions.andExpect(status().isNoContent()); + + verify(logoutUseCase, times(1)).execute(eq(accessToken), any()); + } } \ No newline at end of file From e2c520d6f783d61c8eea3e45c2a01d40ac84ba20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:22:33 +0900 Subject: [PATCH 05/14] test tokenReissue controller test --- .../controller/auth/AuthControllerTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java index b6b19c6..95c8601 100644 --- a/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java +++ b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java @@ -17,10 +17,13 @@ import team.themoment.officialgsm.admin.controller.auth.manager.UserManager; import team.themoment.officialgsm.admin.controller.auth.mapper.AuthDataMapper; import team.themoment.officialgsm.common.util.ConstantsUtil; +import team.themoment.officialgsm.domain.auth.dto.ReissueTokenDto; import team.themoment.officialgsm.domain.auth.dto.UserInfoDto; +import team.themoment.officialgsm.domain.auth.spi.TokenProvider; import team.themoment.officialgsm.domain.auth.usecase.FindUserInfoUseCase; import team.themoment.officialgsm.domain.auth.usecase.LogoutUseCase; import team.themoment.officialgsm.domain.auth.usecase.ModifyNameUseCase; +import team.themoment.officialgsm.domain.auth.usecase.TokenReissueUseCase; import team.themoment.officialgsm.domain.user.Role; import static org.mockito.ArgumentMatchers.any; @@ -47,6 +50,12 @@ class AuthControllerTest { @Mock LogoutUseCase logoutUseCase; + @Mock + TokenReissueUseCase tokenReissueUseCase; + + @Mock + TokenProvider tokenProvider; + @Mock CookieManager cookieManager; @@ -120,4 +129,23 @@ void logout() throws Exception { verify(logoutUseCase, times(1)).execute(eq(accessToken), any()); } + + @Test + void tokenReissue() throws Exception { + // given + String token = "0"; + + ReissueTokenDto reissueTokenDto = new ReissueTokenDto("0", "0"); + + given(cookieManager.getCookieValue(any(), eq(ConstantsUtil.refreshToken))).willReturn(token); + given(tokenReissueUseCase.execute(eq(token))).willReturn(reissueTokenDto); + + // when + ResultActions resultActions = mockMvc.perform(get("/api/auth/token/refresh")); + + // then + resultActions.andExpect(status().isOk()); + + verify(tokenReissueUseCase, times(1)).execute(eq(token)); + } } \ No newline at end of file From c786944d6f7babe15d3373d37b37370e4273f2cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:24:10 +0900 Subject: [PATCH 06/14] test ModifyNameUseCaseTest --- .../auth/usecase/ModifyNameUseCaseTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/ModifyNameUseCaseTest.java diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/ModifyNameUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/ModifyNameUseCaseTest.java new file mode 100644 index 0000000..d401fd3 --- /dev/null +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/ModifyNameUseCaseTest.java @@ -0,0 +1,38 @@ +package team.themoment.officialgsm.domain.auth.usecase; + +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; +import team.themoment.officialgsm.domain.auth.dto.UserNameDto; +import team.themoment.officialgsm.domain.user.Role; +import team.themoment.officialgsm.domain.user.User; +import team.themoment.officialgsm.repository.user.UserRepository; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class ModifyNameUseCaseTest { + + @Mock + private UserRepository userRepository; + + @InjectMocks + private ModifyNameUseCase modifyNameUseCase; + + @Test + void execute() { + // given + UserNameDto dto = new UserNameDto("신희성"); + User currentUser = new User("0", null, "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + User modifiedUser = new User("0", "신희성", "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + + // when + modifyNameUseCase.execute(dto, currentUser); + + // then + verify(userRepository, times(1)).save(modifiedUser); + } +} \ No newline at end of file From 00bba4943047b2b3aaa7cde6c3aae9fe794b15ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:24:29 +0900 Subject: [PATCH 07/14] test FindUserInfoUseCaseTest --- .../auth/usecase/FindUserInfoUseCaseTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/FindUserInfoUseCaseTest.java diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/FindUserInfoUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/FindUserInfoUseCaseTest.java new file mode 100644 index 0000000..4797673 --- /dev/null +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/FindUserInfoUseCaseTest.java @@ -0,0 +1,32 @@ +package team.themoment.officialgsm.domain.auth.usecase; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; +import team.themoment.officialgsm.domain.auth.dto.UserInfoDto; +import team.themoment.officialgsm.domain.user.Role; +import team.themoment.officialgsm.domain.user.User; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class FindUserInfoUseCaseTest { + + @InjectMocks + private FindUserInfoUseCase findUserInfoUseCase; + + @Test + void execute() { + // given + User user = new User("0", "신희성", "s23012@gsm.hs.kr", Role.ADMIN, null, null, null); + + // when + UserInfoDto result = findUserInfoUseCase.execute(user); + + // then + assertThat("신희성").isEqualTo(result.getUserName()); + assertThat("s23012@gsm.hs.kr").isEqualTo(result.getUserEmail()); + assertThat(Role.ADMIN).isEqualTo(result.getRole()); + } +} \ No newline at end of file From 990f4df70e23e8849a6fc0d4ec57e5f3c95265a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:25:18 +0900 Subject: [PATCH 08/14] test LogoutUseCaseTest --- .../auth/usecase/LogoutUseCaseTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java new file mode 100644 index 0000000..6d0601c --- /dev/null +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java @@ -0,0 +1,58 @@ +package team.themoment.officialgsm.domain.auth.usecase; + +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; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import team.themoment.officialgsm.domain.token.BlackList; +import team.themoment.officialgsm.domain.token.RefreshToken; +import team.themoment.officialgsm.domain.user.Role; +import team.themoment.officialgsm.domain.user.User; +import team.themoment.officialgsm.repository.token.BlackListRepository; +import team.themoment.officialgsm.repository.token.RefreshTokenRepository; + +import java.util.Optional; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class LogoutUseCaseTest { + + @InjectMocks + private LogoutUseCase logoutUseCase; + + @Mock + private BlackListRepository blackListRepository; + + @Mock + private RefreshTokenRepository refreshTokenRepository; + + @Mock + private RedisTemplate redisTemplate; + + @Test + void execute() { + // given + String accessTokenValue = "0"; + String refreshTokenValue = "0"; + String oauthId = "0"; + + User user = new User(oauthId, "신희성", "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + RefreshToken refreshToken = new RefreshToken(oauthId, refreshTokenValue, 7200L); + + given(refreshTokenRepository.findByOauthId(oauthId)).willReturn(Optional.of(refreshToken)); + + given(redisTemplate.opsForValue()).willReturn(mock(ValueOperations.class)); + + // when + logoutUseCase.execute(accessTokenValue, user); + + // then + verify(refreshTokenRepository, times(1)).delete(refreshToken); + verify(blackListRepository, times(1)).save(new BlackList(oauthId, accessTokenValue, 7200L)); + } +} \ No newline at end of file From d8fbbc1499a0ecdce5ae845cbf0b6b1adedffb4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:26:03 +0900 Subject: [PATCH 09/14] test execute_refreshTokenNotFound --- .../auth/usecase/LogoutUseCaseTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java index 6d0601c..ae1a854 100644 --- a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java @@ -7,6 +7,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; +import team.themoment.officialgsm.common.exception.CustomException; import team.themoment.officialgsm.domain.token.BlackList; import team.themoment.officialgsm.domain.token.RefreshToken; import team.themoment.officialgsm.domain.user.Role; @@ -16,6 +17,7 @@ import java.util.Optional; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.*; @@ -55,4 +57,22 @@ void execute() { verify(refreshTokenRepository, times(1)).delete(refreshToken); verify(blackListRepository, times(1)).save(new BlackList(oauthId, accessTokenValue, 7200L)); } + + @Test + void execute_refreshTokenNotFound() { + // given + String accessTokenValue = "0"; + String oauthId = "0"; + + User user = new User(oauthId, "신희성", "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + + given(refreshTokenRepository.findByOauthId(oauthId)).willReturn(Optional.empty()); + + // when + assertThrows(CustomException.class, () -> logoutUseCase.execute(accessTokenValue, user)); + + // then + verify(refreshTokenRepository, never()).delete(any()); + verify(blackListRepository, never()).save(any()); + } } \ No newline at end of file From 68c7a667df4da644dd271393ab88896d25111aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:29:45 +0900 Subject: [PATCH 10/14] test execute_blackListConflict --- .../auth/usecase/LogoutUseCaseTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java index ae1a854..595793b 100644 --- a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java @@ -75,4 +75,28 @@ void execute_refreshTokenNotFound() { verify(refreshTokenRepository, never()).delete(any()); verify(blackListRepository, never()).save(any()); } + + @Test + void execute_blackListConflict() { + // given + String accessTokenValue = "0"; + String refreshTokenValue = "0"; + String oauthId = "0"; + + User user = new User(oauthId, "신희성", "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + RefreshToken refreshToken = new RefreshToken(oauthId, refreshTokenValue, 7200L); + + given(refreshTokenRepository.findByOauthId(oauthId)).willReturn(Optional.of(refreshToken)); + + ValueOperations valueOperationsMock = mock(ValueOperations.class); + given(redisTemplate.opsForValue()).willReturn(valueOperationsMock); + given(valueOperationsMock.get(accessTokenValue)).willReturn("0"); + + // when + assertThrows(CustomException.class, () -> logoutUseCase.execute(accessTokenValue, user)); + + // then + verify(refreshTokenRepository, never()).save(any()); + verify(blackListRepository, never()).save(any()); + } } \ No newline at end of file From ca1a831281e9d9fab75f8b156eb97c4c7b2832d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:31:03 +0900 Subject: [PATCH 11/14] test TokenReissueUseCaseTest --- .../auth/usecase/TokenReissueUseCaseTest.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java new file mode 100644 index 0000000..962d7f1 --- /dev/null +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java @@ -0,0 +1,67 @@ +package team.themoment.officialgsm.domain.auth.usecase; + +import jakarta.servlet.http.HttpServletResponse; +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; +import team.themoment.officialgsm.domain.auth.dto.ReissueTokenDto; +import team.themoment.officialgsm.domain.auth.spi.TokenProvider; +import team.themoment.officialgsm.domain.token.RefreshToken; +import team.themoment.officialgsm.domain.user.Role; +import team.themoment.officialgsm.domain.user.User; +import team.themoment.officialgsm.repository.token.RefreshTokenRepository; +import team.themoment.officialgsm.repository.user.UserRepository; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.*; + + +@ExtendWith(MockitoExtension.class) +class TokenReissueUseCaseTest { + + @InjectMocks + private TokenReissueUseCase tokenReissueUseCase; + + @Mock + private RefreshTokenRepository refreshTokenRepository; + + @Mock + private UserRepository userRepository; + + @Mock + private TokenProvider tokenProvider; + + @Test + void execute() { + // given + String refreshTokenValue = "0"; + String oauthId = "0"; + String refreshSecret = "0"; + + User user = new User(oauthId, "신희성", "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + RefreshToken refreshToken = new RefreshToken(oauthId, refreshTokenValue, 0L); + + given(tokenProvider.getRefreshSecert()).willReturn(refreshSecret); + given(tokenProvider.getRefreshTokenOauthId(refreshTokenValue, refreshSecret)).willReturn(oauthId); + + given(userRepository.findByOauthId(oauthId)).willReturn(Optional.of(user)); + given(refreshTokenRepository.findByOauthId(oauthId)).willReturn(Optional.of(refreshToken)); + + given(tokenProvider.generatedAccessToken(oauthId)).willReturn("1"); + given(tokenProvider.generatedRefreshToken(oauthId)).willReturn("1"); + + // when + ReissueTokenDto reissueTokenDto = tokenReissueUseCase.execute(refreshTokenValue); + + // then + verify(refreshTokenRepository, times(1)).save(refreshToken.updateRefreshToken("1")); + + assertThat(reissueTokenDto.getAccessToken()).isEqualTo("1"); + assertThat(reissueTokenDto.getRefreshToken()).isEqualTo("1"); + } +} \ No newline at end of file From 20e101a1387a4f60e4a21318e7cac39d65e0d035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:31:41 +0900 Subject: [PATCH 12/14] tset execute_refreshTokenIsNull --- .../auth/usecase/TokenReissueUseCaseTest.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java index 962d7f1..40f5f1e 100644 --- a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java @@ -1,11 +1,11 @@ package team.themoment.officialgsm.domain.auth.usecase; -import jakarta.servlet.http.HttpServletResponse; 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; +import team.themoment.officialgsm.common.exception.CustomException; import team.themoment.officialgsm.domain.auth.dto.ReissueTokenDto; import team.themoment.officialgsm.domain.auth.spi.TokenProvider; import team.themoment.officialgsm.domain.token.RefreshToken; @@ -17,8 +17,11 @@ import java.util.Optional; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.*; +import static org.mockito.Mockito.never; @ExtendWith(MockitoExtension.class) @@ -64,4 +67,14 @@ void execute() { assertThat(reissueTokenDto.getAccessToken()).isEqualTo("1"); assertThat(reissueTokenDto.getRefreshToken()).isEqualTo("1"); } + + @Test + void execute_refreshTokenIsNull() { + // given & when + assertThrows(CustomException.class, () -> tokenReissueUseCase.execute(null)); + + // then + verify(userRepository, never()).findByOauthId(any()); + verify(refreshTokenRepository, never()).save(any()); + } } \ No newline at end of file From 2824f55f72bc67bcb0f45a4ce39e401931424057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:31:57 +0900 Subject: [PATCH 13/14] test execute_userNotFound --- .../auth/usecase/TokenReissueUseCaseTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java index 40f5f1e..f933766 100644 --- a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java @@ -77,4 +77,25 @@ void execute_refreshTokenIsNull() { verify(userRepository, never()).findByOauthId(any()); verify(refreshTokenRepository, never()).save(any()); } + + @Test + void execute_userNotFound() { + // given + String refreshTokenValue = "0"; + String oauthId = "0"; + String refreshSecret = "0"; + + given(tokenProvider.getRefreshSecert()).willReturn(refreshSecret); + given(tokenProvider.getRefreshTokenOauthId(refreshTokenValue, refreshSecret)).willReturn(oauthId); + + given(userRepository.findByOauthId(oauthId)).willReturn(Optional.empty()); + + // when + assertThrows(CustomException.class, () -> tokenReissueUseCase.execute(refreshTokenValue)); + + // then + verify(refreshTokenRepository, never()).findByOauthId(any()); + + verify(refreshTokenRepository, never()).save(any()); + } } \ No newline at end of file From 1f27f217a0acf6e20b2e65a2e5bac06fe46dbfa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=ED=9D=AC=EC=84=B1?= Date: Sat, 2 Mar 2024 16:32:26 +0900 Subject: [PATCH 14/14] test execute_refreshTokenIsNotValid --- .../auth/usecase/TokenReissueUseCaseTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java index f933766..923c32f 100644 --- a/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java @@ -98,4 +98,27 @@ void execute_userNotFound() { verify(refreshTokenRepository, never()).save(any()); } + + @Test + void execute_refreshTokenIsNotValid() { + // given + String refreshTokenValue = "0"; + String oauthId = "0"; + String refreshSecret = "0"; + + User user = new User(oauthId, "신희성", "s23012@gsm.hs.kr", Role.UNAPPROVED, null, null, null); + RefreshToken refreshToken = new RefreshToken(oauthId, refreshTokenValue + 1, 0L); + + given(tokenProvider.getRefreshSecert()).willReturn(refreshSecret); + given(tokenProvider.getRefreshTokenOauthId(refreshTokenValue, refreshSecret)).willReturn(oauthId); + + given(userRepository.findByOauthId(oauthId)).willReturn(Optional.of(user)); + given(refreshTokenRepository.findByOauthId(oauthId)).willReturn(Optional.of(refreshToken)); + + // when + assertThrows(CustomException.class, () -> tokenReissueUseCase.execute(refreshTokenValue)); + + // then + verify(refreshTokenRepository, never()).save(any()); + } } \ No newline at end of file