diff --git a/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt b/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt index 60939f1..997d573 100644 --- a/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt +++ b/src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt @@ -11,6 +11,7 @@ import com.yourssu.ssudateserver.enums.MaleAnimals import com.yourssu.ssudateserver.enums.RoleType import com.yourssu.ssudateserver.repository.FollowRepository import com.yourssu.ssudateserver.repository.UserRepository +import com.yourssu.ssudateserver.service.RefreshTokenService import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.springframework.beans.factory.annotation.Autowired @@ -35,6 +36,9 @@ class BaseTest { @Autowired protected lateinit var followRepository: FollowRepository + @Autowired + lateinit var tokenService: RefreshTokenService + // private fun createMockUser() { val userList = mutableListOf() @@ -81,6 +85,9 @@ class BaseTest { followRepository.save( Follow(fromUserId = user.id!!, toUserId = user.id!! + 2L, createdAt = now()) ) + + tokenService.saveTokenInfo(oauthName = "oauthName1", "refreshToken") + } @BeforeEach diff --git a/src/test/kotlin/com/yourssu/ssudateserver/controller/RefreshTokenTest.kt b/src/test/kotlin/com/yourssu/ssudateserver/controller/RefreshTokenTest.kt new file mode 100644 index 0000000..956c9a8 --- /dev/null +++ b/src/test/kotlin/com/yourssu/ssudateserver/controller/RefreshTokenTest.kt @@ -0,0 +1,72 @@ +package com.yourssu.ssudateserver.controller + +import com.yourssu.ssudateserver.common.BaseTest +import com.yourssu.ssudateserver.dto.request.RefreshTokenRequestDto +import com.yourssu.ssudateserver.fixture.PrincipalFixture.Companion.setPrincipal +import org.junit.jupiter.api.Test +import org.springframework.http.MediaType +import org.springframework.security.test.context.support.WithMockUser +import org.springframework.test.context.ActiveProfiles +import org.springframework.test.web.servlet.post + +@ActiveProfiles("test") +class RefreshTokenTest : BaseTest() { + + @Test + @WithMockUser + fun refreshTokenTest() { + setPrincipal() + + val refreshTokenRequestDto = RefreshTokenRequestDto("refreshToken") + + val test = mockMvc.post("/refresh") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(refreshTokenRequestDto) + } + + test + .andExpect { + status { isOk() } + jsonPath("accessToken") { exists() } + jsonPath("refreshToken") { exists() } + } + + } + @Test + @WithMockUser + fun refreshTokenNotFoundTest() { + setPrincipal("oauthName2") + + val refreshTokenRequestDto = RefreshTokenRequestDto("refreshToken") + + val test = mockMvc.post("/refresh") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(refreshTokenRequestDto) + } + + test + .andExpect { + status { isBadRequest() } + jsonPath("message") { value("유저의 refreshToken이 존재하지 않습니다.") } + } + } + + @Test + @WithMockUser + fun invalidRefreshTokenTest() { + setPrincipal() + + val refreshTokenRequestDto = RefreshTokenRequestDto("validToken") + + val test = mockMvc.post("/refresh") { + contentType = MediaType.APPLICATION_JSON + content = objectMapper.writeValueAsString(refreshTokenRequestDto) + } + + test + .andExpect { + status { isBadRequest() } + jsonPath("message") { value("잘못된 refreshToken입니다.") } + } + } +}