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 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..595793b --- /dev/null +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/LogoutUseCaseTest.java @@ -0,0 +1,102 @@ +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.common.exception.CustomException; +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.junit.jupiter.api.Assertions.assertThrows; +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)); + } + + @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()); + } + + @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 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 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..923c32f --- /dev/null +++ b/application/src/test/java/team/themoment/officialgsm/domain/auth/usecase/TokenReissueUseCaseTest.java @@ -0,0 +1,124 @@ +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.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; +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.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) +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"); + } + + @Test + void execute_refreshTokenIsNull() { + // given & when + assertThrows(CustomException.class, () -> tokenReissueUseCase.execute(null)); + + // then + 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()); + } + + @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 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..95c8601 --- /dev/null +++ b/presentation/src/test/java/team/themoment/officialgsm/admin/controller/auth/AuthControllerTest.java @@ -0,0 +1,151 @@ +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.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.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; +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.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@ExtendWith(MockitoExtension.class) +class AuthControllerTest { + + @InjectMocks + AuthController authController; + + @Mock + ModifyNameUseCase modifyNameUseCase; + + @Mock + FindUserInfoUseCase findUserInfoUseCase; + + @Mock + LogoutUseCase logoutUseCase; + + @Mock + TokenReissueUseCase tokenReissueUseCase; + + @Mock + TokenProvider tokenProvider; + + @Mock + CookieManager cookieManager; + + @Mock + UserManager userManager; + + @Mock + AuthDataMapper userDataMapper; + + private MockMvc mockMvc; + + @BeforeEach + 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()); + } + + @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); + } + + + @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()); + } + + @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