From 24ebc61f5f6b0dee1f53e1be20bc5ab0eba62d27 Mon Sep 17 00:00:00 2001 From: HeoSeokMun Date: Fri, 15 Dec 2023 16:31:31 +0900 Subject: [PATCH] =?UTF-8?q?test:=20refresh=20token=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourssu/ssudateserver/common/BaseTest.kt | 7 ++ .../controller/RefreshTokenTest.kt | 72 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/test/kotlin/com/yourssu/ssudateserver/controller/RefreshTokenTest.kt 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입니다.") } + } + } +}