Skip to content

Commit

Permalink
test: refresh token 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
GGHDMS committed Dec 15, 2023
1 parent 4576fba commit 24ebc61
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/test/kotlin/com/yourssu/ssudateserver/common/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +36,9 @@ class BaseTest {
@Autowired
protected lateinit var followRepository: FollowRepository

@Autowired
lateinit var tokenService: RefreshTokenService

//
private fun createMockUser() {
val userList = mutableListOf<User>()
Expand Down Expand Up @@ -81,6 +85,9 @@ class BaseTest {
followRepository.save(
Follow(fromUserId = user.id!!, toUserId = user.id!! + 2L, createdAt = now())
)

tokenService.saveTokenInfo(oauthName = "oauthName1", "refreshToken")

}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
@@ -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입니다.") }
}
}
}

0 comments on commit 24ebc61

Please sign in to comment.