generated from pricelees/issue-pr-template
[#9] API 테스트를 @SpringbootTest에서 @WebMvcTest 기반으로 전환 #10
@ -1,23 +1,34 @@
|
|||||||
package roomescape.auth.web
|
package roomescape.auth.web
|
||||||
|
|
||||||
|
import com.ninjasquad.springmockk.SpykBean
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import org.hamcrest.Matchers.containsString
|
import org.hamcrest.Matchers.containsString
|
||||||
import org.hamcrest.Matchers.`is`
|
import org.hamcrest.Matchers.equalTo
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
|
import roomescape.auth.service.AuthService
|
||||||
import roomescape.common.exception.ErrorType
|
import roomescape.common.exception.ErrorType
|
||||||
import roomescape.util.MemberFixture
|
import roomescape.util.MemberFixture
|
||||||
import roomescape.util.RoomescapeApiTest
|
import roomescape.util.RoomescapeApiTest
|
||||||
|
|
||||||
class AuthControllerTest : RoomescapeApiTest() {
|
@WebMvcTest(controllers = [AuthController::class])
|
||||||
|
class AuthControllerTest(
|
||||||
|
@Autowired mockMvc: MockMvc
|
||||||
|
) : RoomescapeApiTest() {
|
||||||
|
|
||||||
|
@SpykBean
|
||||||
|
private lateinit var authService: AuthService
|
||||||
|
|
||||||
val userRequest: LoginRequest = MemberFixture.userLoginRequest()
|
val userRequest: LoginRequest = MemberFixture.userLoginRequest()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
Given("로그인 요청을 보낼 때") {
|
Given("로그인 요청을 보낼 때") {
|
||||||
val endpoint: String = "/login"
|
val endpoint = "/login"
|
||||||
|
|
||||||
When("로그인에 성공하면") {
|
When("로그인에 성공하면") {
|
||||||
val expectedToken: String = "expectedToken"
|
val expectedToken = "expectedToken"
|
||||||
|
|
||||||
every {
|
every {
|
||||||
memberRepository.findByEmailAndPassword(userRequest.email, userRequest.password)
|
memberRepository.findByEmailAndPassword(userRequest.email, userRequest.password)
|
||||||
@ -28,12 +39,19 @@ class AuthControllerTest : RoomescapeApiTest() {
|
|||||||
} returns expectedToken
|
} returns expectedToken
|
||||||
|
|
||||||
Then("토큰을 쿠키에 담아 응답한다") {
|
Then("토큰을 쿠키에 담아 응답한다") {
|
||||||
runPostTest(endpoint, body = MemberFixture.userLoginRequest()) {
|
runPostTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
cookie("accessToken", expectedToken)
|
endpoint = endpoint,
|
||||||
header("Set-Cookie", containsString("Max-Age=1800"))
|
body = userRequest,
|
||||||
header("Set-Cookie", containsString("HttpOnly"))
|
log = true
|
||||||
header("Set-Cookie", containsString("Secure"))
|
) {
|
||||||
|
status { isOk() }
|
||||||
|
header {
|
||||||
|
string("Set-Cookie", containsString("accessToken=$expectedToken"))
|
||||||
|
string("Set-Cookie", containsString("Max-Age=1800"))
|
||||||
|
string("Set-Cookie", containsString("HttpOnly"))
|
||||||
|
string("Set-Cookie", containsString("Secure"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -44,10 +62,14 @@ class AuthControllerTest : RoomescapeApiTest() {
|
|||||||
} returns null
|
} returns null
|
||||||
|
|
||||||
Then("400 에러를 응답한다") {
|
Then("400 에러를 응답한다") {
|
||||||
runPostTest(endpoint, body = userRequest) {
|
runPostTest(
|
||||||
log().all()
|
mockMvc = mockMvc,
|
||||||
statusCode(400)
|
endpoint = endpoint,
|
||||||
body("errorType", `is`(ErrorType.MEMBER_NOT_FOUND.name))
|
body = userRequest,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isBadRequest() }
|
||||||
|
jsonPath("$.errorType", equalTo(ErrorType.MEMBER_NOT_FOUND.name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,36 +79,47 @@ class AuthControllerTest : RoomescapeApiTest() {
|
|||||||
Then("이메일 형식이 잘못된 경우") {
|
Then("이메일 형식이 잘못된 경우") {
|
||||||
val invalidRequest: LoginRequest = userRequest.copy(email = "invalid")
|
val invalidRequest: LoginRequest = userRequest.copy(email = "invalid")
|
||||||
|
|
||||||
runPostTest(endpoint, body = invalidRequest) {
|
runPostTest(
|
||||||
log().all()
|
mockMvc = mockMvc,
|
||||||
statusCode(400)
|
endpoint = endpoint,
|
||||||
body("message", `is`("이메일 형식이 일치하지 않습니다. 예시: abc123@gmail.com"))
|
body = invalidRequest,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isBadRequest() }
|
||||||
|
jsonPath("$.message", containsString("이메일 형식이 일치하지 않습니다."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Then("비밀번호가 공백인 경우") {
|
Then("비밀번호가 공백인 경우") {
|
||||||
val invalidRequest = userRequest.copy(password = " ")
|
val invalidRequest = userRequest.copy(password = " ")
|
||||||
|
|
||||||
runPostTest(endpoint, body = invalidRequest) {
|
runPostTest(
|
||||||
log().all()
|
mockMvc = mockMvc,
|
||||||
statusCode(400)
|
endpoint = endpoint,
|
||||||
body("message", `is`("비밀번호는 공백일 수 없습니다."))
|
body = invalidRequest,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isBadRequest() }
|
||||||
|
jsonPath("$.message", containsString("비밀번호는 공백일 수 없습니다."))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Given("로그인 상태를 확인할 때") {
|
Given("로그인 상태를 확인할 때") {
|
||||||
val endpoint: String = "/login/check"
|
val endpoint = "/login/check"
|
||||||
|
|
||||||
When("로그인된 회원의 ID로 요청하면") {
|
When("로그인된 회원의 ID로 요청하면") {
|
||||||
every { jwtHandler.getMemberIdFromToken(any()) } returns user.id!!
|
loginAsUser()
|
||||||
every { memberRepository.findByIdOrNull(user.id!!) } returns user
|
|
||||||
|
|
||||||
Then("회원의 이름을 응답한다") {
|
Then("회원의 이름을 응답한다") {
|
||||||
runGetTest(endpoint) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
body("data.name", `is`(user.name))
|
endpoint = endpoint,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
|
jsonPath("$.data.name", equalTo(user.name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,39 +131,55 @@ class AuthControllerTest : RoomescapeApiTest() {
|
|||||||
every { memberRepository.findByIdOrNull(invalidMemberId) } returns null
|
every { memberRepository.findByIdOrNull(invalidMemberId) } returns null
|
||||||
|
|
||||||
Then("400 에러를 응답한다.") {
|
Then("400 에러를 응답한다.") {
|
||||||
runGetTest(endpoint) {
|
runGetTest(
|
||||||
statusCode(400)
|
mockMvc = mockMvc,
|
||||||
body("errorType", `is`(ErrorType.MEMBER_NOT_FOUND.name))
|
endpoint = endpoint,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isBadRequest() }
|
||||||
|
jsonPath("$.errorType", equalTo(ErrorType.MEMBER_NOT_FOUND.name))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Given("로그아웃 요청을 보낼 때") {
|
Given("로그아웃 요청을 보낼 때") {
|
||||||
val endpoint: String = "/logout"
|
val endpoint = "/logout"
|
||||||
|
|
||||||
When("로그인 상태가 아니라면") {
|
When("로그인 상태가 아니라면") {
|
||||||
setUpNotLoggedIn()
|
doNotLogin()
|
||||||
|
|
||||||
Then("로그인 페이지로 이동한다.") {
|
Then("로그인 페이지로 이동한다.") {
|
||||||
runPostTest(endpoint) {
|
runPostTest(
|
||||||
log().all()
|
mockMvc = mockMvc,
|
||||||
statusCode(302)
|
endpoint = endpoint,
|
||||||
header("Location", containsString("/login"))
|
log = true
|
||||||
|
) {
|
||||||
|
status { is3xxRedirection() }
|
||||||
|
header {
|
||||||
|
string("Location", "/login")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
When("로그인 상태라면") {
|
When("로그인 상태라면") {
|
||||||
setUpUser()
|
loginAsUser()
|
||||||
every { memberRepository.findByIdOrNull(user.id!!) } returns user
|
|
||||||
|
|
||||||
Then("토큰의 존재 여부와 무관하게 토큰을 만료시킨다.") {
|
Then("토큰의 존재 여부와 무관하게 토큰을 만료시킨다.") {
|
||||||
runPostTest(endpoint) {
|
runPostTest(
|
||||||
log().all()
|
mockMvc = mockMvc,
|
||||||
statusCode(200)
|
endpoint = endpoint,
|
||||||
cookie("accessToken", "")
|
log = true
|
||||||
header("Set-Cookie", containsString("Max-Age=0"))
|
) {
|
||||||
|
status { isOk() }
|
||||||
|
header {
|
||||||
|
string("Set-Cookie", containsString("Max-Age=0"))
|
||||||
|
string("Set-Cookie", containsString("accessToken="))
|
||||||
|
string("Set-Cookie", containsString("Path=/"))
|
||||||
|
string("Set-Cookie", containsString("HttpOnly"))
|
||||||
|
string("Set-Cookie", containsString("Secure"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,158 +0,0 @@
|
|||||||
//package roomescape.common.dto.response
|
|
||||||
//
|
|
||||||
//import com.fasterxml.jackson.databind.ObjectMapper
|
|
||||||
//import com.ninjasquad.springmockk.MockkBean
|
|
||||||
//import com.ninjasquad.springmockk.SpykBean
|
|
||||||
//import io.kotest.core.spec.style.BehaviorSpec
|
|
||||||
//import org.hamcrest.CoreMatchers.equalTo
|
|
||||||
//import org.springframework.beans.factory.annotation.Autowired
|
|
||||||
//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
|
||||||
//import org.springframework.http.MediaType
|
|
||||||
//import org.springframework.test.web.servlet.MockMvc
|
|
||||||
//import org.springframework.test.web.servlet.get
|
|
||||||
//import org.springframework.test.web.servlet.post
|
|
||||||
//import org.springframework.web.bind.annotation.*
|
|
||||||
//import roomescape.auth.infrastructure.jwt.JwtHandler
|
|
||||||
//import roomescape.auth.web.support.AdminInterceptor
|
|
||||||
//import roomescape.auth.web.support.LoginInterceptor
|
|
||||||
//import roomescape.auth.web.support.MemberIdResolver
|
|
||||||
//import roomescape.common.exception.ErrorType
|
|
||||||
//import roomescape.member.business.MemberService
|
|
||||||
//import roomescape.member.infrastructure.persistence.MemberRepository
|
|
||||||
//
|
|
||||||
//@WebMvcTest(ApiResponseTestController::class)
|
|
||||||
//class RoomescapeApiResponseKTTest(
|
|
||||||
// @Autowired private val mockMvc: MockMvc
|
|
||||||
//) : BehaviorSpec() {
|
|
||||||
// @Autowired
|
|
||||||
// private lateinit var AdminInterceptor: AdminInterceptor
|
|
||||||
//
|
|
||||||
// @Autowired
|
|
||||||
// private lateinit var loginInterceptor: LoginInterceptor
|
|
||||||
//
|
|
||||||
// @Autowired
|
|
||||||
// private lateinit var memberIdResolver: MemberIdResolver
|
|
||||||
//
|
|
||||||
// @SpykBean
|
|
||||||
// private lateinit var memberService: MemberService
|
|
||||||
//
|
|
||||||
// @MockkBean
|
|
||||||
// private lateinit var memberRepository: MemberRepository
|
|
||||||
//
|
|
||||||
// @MockkBean
|
|
||||||
// private lateinit var jwtHandler: JwtHandler
|
|
||||||
//
|
|
||||||
// init {
|
|
||||||
// Given("성공 응답에") {
|
|
||||||
// val endpoint = "/success"
|
|
||||||
// When("객체 데이터를 담으면") {
|
|
||||||
// val id: Long = 1L
|
|
||||||
// val name = "name"
|
|
||||||
// Then("success=true, data={객체} 형태로 응답한다.") {
|
|
||||||
// mockMvc.post("$endpoint/$id/$name") {
|
|
||||||
// contentType = MediaType.APPLICATION_JSON
|
|
||||||
// }.andDo {
|
|
||||||
// print()
|
|
||||||
// }.andExpect {
|
|
||||||
// status { isOk() }
|
|
||||||
// jsonPath("$.success", equalTo(true))
|
|
||||||
// jsonPath("$.data.id", equalTo(id.toInt()))
|
|
||||||
// jsonPath("$.data.name", equalTo(name))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// When("문자열 데이터를 담으면") {
|
|
||||||
// val message: String = "Hello, World!"
|
|
||||||
//
|
|
||||||
// Then("success=true, data={문자열} 형태로 응답한다.") {
|
|
||||||
// mockMvc.get("/success/$message") {
|
|
||||||
// contentType = MediaType.APPLICATION_JSON
|
|
||||||
// }.andDo {
|
|
||||||
// print()
|
|
||||||
// }.andExpect {
|
|
||||||
// status { isOk() }
|
|
||||||
// jsonPath("$.success", equalTo(true))
|
|
||||||
// jsonPath("$.data", equalTo(message))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Given("실패 응답에") {
|
|
||||||
// val endpoint = "/fail"
|
|
||||||
// val objectMapper = ObjectMapper()
|
|
||||||
//
|
|
||||||
// When("errorType만 담으면") {
|
|
||||||
// Then("success=false, errorType={errorType}, message={errorType.description} 형태로 응답한다.") {
|
|
||||||
// mockMvc.post(endpoint) {
|
|
||||||
// contentType = MediaType.APPLICATION_JSON
|
|
||||||
// content = objectMapper.writeValueAsString(FailRequest(errorType = ErrorType.INTERNAL_SERVER_ERROR))
|
|
||||||
// }.andDo {
|
|
||||||
// print()
|
|
||||||
// }.andExpect {
|
|
||||||
// status { isOk() }
|
|
||||||
// jsonPath("$.success", equalTo(false))
|
|
||||||
// jsonPath("$.errorType", equalTo(ErrorType.INTERNAL_SERVER_ERROR.name))
|
|
||||||
// jsonPath("$.message", equalTo(ErrorType.INTERNAL_SERVER_ERROR.description))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// When("errorType과 message를 담으면") {
|
|
||||||
// val message: String = "An error occurred"
|
|
||||||
//
|
|
||||||
// Then("success=false, errorType={errorType}, message={message} 형태로 응답한다.") {
|
|
||||||
// mockMvc.post(endpoint) {
|
|
||||||
// contentType = MediaType.APPLICATION_JSON
|
|
||||||
// content = objectMapper.writeValueAsString(FailRequest(errorType = ErrorType.INTERNAL_SERVER_ERROR, message = message))
|
|
||||||
// }.andDo {
|
|
||||||
// print()
|
|
||||||
// }.andExpect {
|
|
||||||
// status { isOk() }
|
|
||||||
// jsonPath("$.success", equalTo(false))
|
|
||||||
// jsonPath("$.errorType", equalTo(ErrorType.INTERNAL_SERVER_ERROR.name))
|
|
||||||
// jsonPath("$.message", equalTo(message))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//data class SuccessResponse(
|
|
||||||
// val id: Long,
|
|
||||||
// val name: String
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//data class FailRequest(
|
|
||||||
// val errorType: ErrorType,
|
|
||||||
// val message: String? = null
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//@RestController
|
|
||||||
//class ApiResponseTestController {
|
|
||||||
//
|
|
||||||
// @GetMapping("/success/{message}")
|
|
||||||
// fun succeedToGet(
|
|
||||||
// @PathVariable message: String,
|
|
||||||
// ): RoomescapeApiResponseKT<String> =
|
|
||||||
// RoomescapeApiResponseKT.success(message)
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// @PostMapping("/success/{id}/{name}")
|
|
||||||
// fun succeedToPost(
|
|
||||||
// @PathVariable id: Long,
|
|
||||||
// @PathVariable name: String,
|
|
||||||
// ): RoomescapeApiResponseKT<SuccessResponse> =
|
|
||||||
// RoomescapeApiResponseKT.success(SuccessResponse(id, name))
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// @PostMapping("/fail")
|
|
||||||
// fun fail(
|
|
||||||
// @RequestBody request: FailRequest
|
|
||||||
// ): RoomescapeApiResponseKT<Unit> =
|
|
||||||
// request.message?.let {
|
|
||||||
// RoomescapeApiResponseKT.fail(request.errorType, it)
|
|
||||||
// } ?: RoomescapeApiResponseKT.fail(request.errorType)
|
|
||||||
//}
|
|
||||||
@ -1,17 +1,21 @@
|
|||||||
package roomescape.member.controller
|
package roomescape.member.controller
|
||||||
|
|
||||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
|
||||||
import io.kotest.assertions.assertSoftly
|
import io.kotest.assertions.assertSoftly
|
||||||
import io.kotest.matchers.collections.shouldContainAll
|
import io.kotest.matchers.collections.shouldContainAll
|
||||||
import io.kotest.matchers.shouldBe
|
import io.kotest.matchers.shouldBe
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.restassured.module.kotlin.extensions.Extract
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.hamcrest.Matchers.containsString
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||||
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
|
import roomescape.member.web.MemberController
|
||||||
|
import roomescape.member.web.MembersResponse
|
||||||
import roomescape.util.MemberFixture
|
import roomescape.util.MemberFixture
|
||||||
import roomescape.util.RoomescapeApiTest
|
import roomescape.util.RoomescapeApiTest
|
||||||
import roomescape.member.web.MembersResponse
|
|
||||||
|
|
||||||
class MemberControllerTest : RoomescapeApiTest() {
|
@WebMvcTest(controllers = [MemberController::class])
|
||||||
|
class MemberControllerTest(
|
||||||
|
@Autowired private val mockMvc: MockMvc
|
||||||
|
) : RoomescapeApiTest() {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
given("GET /members 요청을") {
|
given("GET /members 요청을") {
|
||||||
@ -24,16 +28,21 @@ class MemberControllerTest : RoomescapeApiTest() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
`when`("관리자가 보내면") {
|
`when`("관리자가 보내면") {
|
||||||
setUpAdmin()
|
loginAsAdmin()
|
||||||
|
|
||||||
then("성공한다.") {
|
then("성공한다.") {
|
||||||
val result: Any = runGetTest(endpoint) {
|
val result: String = runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
} Extract {
|
endpoint = endpoint,
|
||||||
path("data")
|
log = true
|
||||||
}
|
) {
|
||||||
|
status { isOk() }
|
||||||
|
}.andReturn().response.contentAsString
|
||||||
|
|
||||||
val response: MembersResponse = jacksonObjectMapper().convertValue(result, MembersResponse::class.java)
|
val response: MembersResponse = readValue(
|
||||||
|
responseJson = result,
|
||||||
|
valueType = MembersResponse::class.java
|
||||||
|
)
|
||||||
|
|
||||||
assertSoftly(response.members) {
|
assertSoftly(response.members) {
|
||||||
it.size shouldBe 3
|
it.size shouldBe 3
|
||||||
@ -44,20 +53,32 @@ class MemberControllerTest : RoomescapeApiTest() {
|
|||||||
|
|
||||||
`when`("관리자가 아니면 로그인 페이지로 이동한다.") {
|
`when`("관리자가 아니면 로그인 페이지로 이동한다.") {
|
||||||
then("비회원") {
|
then("비회원") {
|
||||||
setUpNotLoggedIn()
|
doNotLogin()
|
||||||
|
|
||||||
runGetTest(endpoint) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
body(containsString("<title>Login</title>"))
|
endpoint = endpoint,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { is3xxRedirection() }
|
||||||
|
header {
|
||||||
|
string("Location", "/login")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
then("일반 회원") {
|
then("일반 회원") {
|
||||||
setUpUser()
|
loginAsUser()
|
||||||
|
|
||||||
runGetTest(endpoint) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
body(containsString("<title>Login</title>"))
|
endpoint = endpoint,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { is3xxRedirection() }
|
||||||
|
header {
|
||||||
|
string("Location", "/login")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,8 @@ import roomescape.auth.web.LoginRequest
|
|||||||
import java.util.concurrent.atomic.AtomicLong
|
import java.util.concurrent.atomic.AtomicLong
|
||||||
|
|
||||||
object MemberFixture {
|
object MemberFixture {
|
||||||
|
const val NOT_LOGGED_IN_USERID: Long = 0
|
||||||
|
|
||||||
val idCounter: AtomicLong = AtomicLong(1L)
|
val idCounter: AtomicLong = AtomicLong(1L)
|
||||||
|
|
||||||
fun create(
|
fun create(
|
||||||
|
|||||||
@ -1,30 +1,40 @@
|
|||||||
package roomescape.util
|
package roomescape.util
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||||
import com.ninjasquad.springmockk.MockkBean
|
import com.ninjasquad.springmockk.MockkBean
|
||||||
|
import com.ninjasquad.springmockk.SpykBean
|
||||||
import io.kotest.core.spec.style.BehaviorSpec
|
import io.kotest.core.spec.style.BehaviorSpec
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.restassured.http.ContentType
|
|
||||||
import io.restassured.module.kotlin.extensions.Given
|
|
||||||
import io.restassured.module.kotlin.extensions.Then
|
|
||||||
import io.restassured.module.kotlin.extensions.When
|
|
||||||
import io.restassured.response.ValidatableResponse
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest
|
|
||||||
import org.springframework.boot.test.web.server.LocalServerPort
|
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
|
import org.springframework.http.HttpHeaders
|
||||||
import org.springframework.http.HttpStatus
|
import org.springframework.http.HttpStatus
|
||||||
import roomescape.member.infrastructure.persistence.Member
|
import org.springframework.http.MediaType
|
||||||
import roomescape.member.infrastructure.persistence.MemberRepository
|
import org.springframework.test.web.servlet.*
|
||||||
import roomescape.auth.infrastructure.jwt.JwtHandler
|
import roomescape.auth.infrastructure.jwt.JwtHandler
|
||||||
|
import roomescape.auth.web.support.AdminInterceptor
|
||||||
|
import roomescape.auth.web.support.LoginInterceptor
|
||||||
|
import roomescape.auth.web.support.MemberIdResolver
|
||||||
import roomescape.common.exception.ErrorType
|
import roomescape.common.exception.ErrorType
|
||||||
import roomescape.common.exception.RoomescapeException
|
import roomescape.common.exception.RoomescapeException
|
||||||
|
import roomescape.member.business.MemberService
|
||||||
|
import roomescape.member.infrastructure.persistence.Member
|
||||||
|
import roomescape.member.infrastructure.persistence.MemberRepository
|
||||||
|
import roomescape.util.MemberFixture.NOT_LOGGED_IN_USERID
|
||||||
|
|
||||||
const val NOT_LOGGED_IN_USERID: Long = 0;
|
abstract class RoomescapeApiTest : BehaviorSpec() {
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpykBean
|
||||||
@NoSqlInitialize
|
private lateinit var AdminInterceptor: AdminInterceptor
|
||||||
class RoomescapeApiTest(
|
|
||||||
@LocalServerPort val port: Int? = 9090,
|
@SpykBean
|
||||||
) : BehaviorSpec() {
|
private lateinit var loginInterceptor: LoginInterceptor
|
||||||
|
|
||||||
|
@SpykBean
|
||||||
|
private lateinit var memberIdResolver: MemberIdResolver
|
||||||
|
|
||||||
|
@SpykBean
|
||||||
|
lateinit var memberService: MemberService
|
||||||
|
|
||||||
@MockkBean
|
@MockkBean
|
||||||
lateinit var memberRepository: MemberRepository
|
lateinit var memberRepository: MemberRepository
|
||||||
@ -32,36 +42,42 @@ class RoomescapeApiTest(
|
|||||||
@MockkBean
|
@MockkBean
|
||||||
lateinit var jwtHandler: JwtHandler
|
lateinit var jwtHandler: JwtHandler
|
||||||
|
|
||||||
|
val objectMapper: ObjectMapper = jacksonObjectMapper()
|
||||||
val admin: Member = MemberFixture.admin()
|
val admin: Member = MemberFixture.admin()
|
||||||
val user: Member = MemberFixture.user()
|
val user: Member = MemberFixture.user()
|
||||||
|
|
||||||
fun runGetTest(endpoint: String, token: String? = "token", assert: ValidatableResponse.() -> Unit): ValidatableResponse {
|
fun runGetTest(
|
||||||
return Given {
|
mockMvc: MockMvc,
|
||||||
port(port!!)
|
endpoint: String,
|
||||||
header("Cookie", "accessToken=$token")
|
log: Boolean = false,
|
||||||
} When {
|
assert: MockMvcResultMatchersDsl.() -> Unit
|
||||||
get(endpoint)
|
): ResultActionsDsl = mockMvc.get(endpoint) {
|
||||||
} Then assert
|
header(HttpHeaders.COOKIE, "accessToken=token")
|
||||||
|
}.apply {
|
||||||
|
log.takeIf { it }?.let { this.andDo { print() } }
|
||||||
|
}.andExpect {
|
||||||
|
assert
|
||||||
}
|
}
|
||||||
|
|
||||||
fun runPostTest(
|
fun runPostTest(
|
||||||
|
mockMvc: MockMvc,
|
||||||
endpoint: String,
|
endpoint: String,
|
||||||
token: String? = "token",
|
|
||||||
body: Any? = null,
|
body: Any? = null,
|
||||||
assert: ValidatableResponse.() -> Unit
|
log: Boolean = false,
|
||||||
): ValidatableResponse {
|
assert: MockMvcResultMatchersDsl.() -> Unit
|
||||||
return Given {
|
): ResultActionsDsl = mockMvc.post(endpoint) {
|
||||||
port(port!!)
|
this.header(HttpHeaders.COOKIE, "accessToken=token")
|
||||||
contentType(ContentType.JSON)
|
body?.let {
|
||||||
body?.let { body(it) }
|
this.contentType = MediaType.APPLICATION_JSON
|
||||||
header("Cookie", "accessToken=$token")
|
this.content = objectMapper.writeValueAsString(it)
|
||||||
} When {
|
}
|
||||||
post(endpoint)
|
}.apply {
|
||||||
} Then assert
|
log.takeIf { it }?.let { this.andDo { print() } }
|
||||||
|
}.andExpect {
|
||||||
|
assert
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setUpAdmin() {
|
fun loginAsAdmin() {
|
||||||
every {
|
every {
|
||||||
jwtHandler.getMemberIdFromToken(any())
|
jwtHandler.getMemberIdFromToken(any())
|
||||||
} returns admin.id!!
|
} returns admin.id!!
|
||||||
@ -70,7 +86,7 @@ class RoomescapeApiTest(
|
|||||||
every { memberRepository.findByIdOrNull(admin.id!!) } returns admin
|
every { memberRepository.findByIdOrNull(admin.id!!) } returns admin
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setUpUser() {
|
fun loginAsUser() {
|
||||||
every {
|
every {
|
||||||
jwtHandler.getMemberIdFromToken(any())
|
jwtHandler.getMemberIdFromToken(any())
|
||||||
} returns user.id!!
|
} returns user.id!!
|
||||||
@ -79,15 +95,19 @@ class RoomescapeApiTest(
|
|||||||
every { memberRepository.findByIdOrNull(user.id!!) } returns user
|
every { memberRepository.findByIdOrNull(user.id!!) } returns user
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setUpNotLoggedIn() {
|
fun doNotLogin() {
|
||||||
every {
|
every {
|
||||||
jwtHandler.getMemberIdFromToken(any())
|
jwtHandler.getMemberIdFromToken(any())
|
||||||
} returns NOT_LOGGED_IN_USERID
|
} throws RoomescapeException(ErrorType.INVALID_TOKEN, HttpStatus.UNAUTHORIZED)
|
||||||
|
|
||||||
every { memberRepository.existsById(NOT_LOGGED_IN_USERID) } throws RoomescapeException(
|
every { memberRepository.existsById(NOT_LOGGED_IN_USERID) } returns false
|
||||||
ErrorType.LOGIN_REQUIRED,
|
|
||||||
HttpStatus.FORBIDDEN
|
|
||||||
)
|
|
||||||
every { memberRepository.findByIdOrNull(NOT_LOGGED_IN_USERID) } returns null
|
every { memberRepository.findByIdOrNull(NOT_LOGGED_IN_USERID) } returns null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> readValue(responseJson: String, valueType: Class<T>): T = objectMapper
|
||||||
|
.readTree(responseJson)["data"]
|
||||||
|
?.let { objectMapper.convertValue(it, valueType) }
|
||||||
|
?: throw RuntimeException("""
|
||||||
|
[Test] Exception occurred while reading response json: $responseJson with value type: $valueType
|
||||||
|
""".trimIndent())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +0,0 @@
|
|||||||
package roomescape.util
|
|
||||||
|
|
||||||
import org.springframework.test.context.TestPropertySource
|
|
||||||
|
|
||||||
@Target(AnnotationTarget.CLASS)
|
|
||||||
@Retention(AnnotationRetention.RUNTIME)
|
|
||||||
@TestPropertySource(properties = [
|
|
||||||
"spring.jpa.hibernate.ddl-auto=none",
|
|
||||||
"spring.sql.init.mode=never"
|
|
||||||
])
|
|
||||||
annotation class NoSqlInitialize
|
|
||||||
@ -1,35 +1,56 @@
|
|||||||
package roomescape.view
|
package roomescape.view
|
||||||
|
|
||||||
import org.hamcrest.Matchers
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||||
|
import org.springframework.test.web.servlet.MockMvc
|
||||||
import roomescape.util.RoomescapeApiTest
|
import roomescape.util.RoomescapeApiTest
|
||||||
|
|
||||||
class PageControllerTest() : RoomescapeApiTest() {
|
@WebMvcTest(controllers = [
|
||||||
|
AuthPageController::class,
|
||||||
|
AdminPageController::class,
|
||||||
|
ClientPageController::class
|
||||||
|
])
|
||||||
|
class PageControllerTest(
|
||||||
|
@Autowired private val mockMvc: MockMvc
|
||||||
|
) : RoomescapeApiTest() {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
listOf("/", "/login").forEach {
|
listOf("/", "/login").forEach {
|
||||||
given("GET $it 요청은") {
|
given("GET $it 요청은") {
|
||||||
`when`("로그인 및 권한 여부와 관계없이 성공한다.") {
|
`when`("로그인 및 권한 여부와 관계없이 성공한다.") {
|
||||||
then("비회원") {
|
then("비회원") {
|
||||||
setUpNotLoggedIn()
|
doNotLogin()
|
||||||
|
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
then("회원") {
|
then("회원") {
|
||||||
setUpUser()
|
loginAsUser()
|
||||||
|
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
then("관리자") {
|
then("관리자") {
|
||||||
setUpAdmin()
|
loginAsAdmin()
|
||||||
|
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,22 +60,32 @@ class PageControllerTest() : RoomescapeApiTest() {
|
|||||||
listOf("/admin", "/admin/reservation", "/admin/time", "/admin/theme", "/admin/waiting").forEach {
|
listOf("/admin", "/admin/reservation", "/admin/time", "/admin/theme", "/admin/waiting").forEach {
|
||||||
given("GET $it 요청을") {
|
given("GET $it 요청을") {
|
||||||
`when`("관리자가 보내면") {
|
`when`("관리자가 보내면") {
|
||||||
setUpAdmin()
|
loginAsAdmin()
|
||||||
|
|
||||||
then("성공한다.") {
|
then("성공한다.") {
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
`when`("회원이 보내면") {
|
`when`("회원이 보내면") {
|
||||||
setUpUser()
|
loginAsUser()
|
||||||
|
|
||||||
then("로그인 페이지로 이동한다.") {
|
then("로그인 페이지로 이동한다.") {
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
body(Matchers.containsString("<title>Login</title>"))
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { is3xxRedirection() }
|
||||||
|
header {
|
||||||
|
string("Location", "/login")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,28 +96,42 @@ class PageControllerTest() : RoomescapeApiTest() {
|
|||||||
given("GET $it 요청을") {
|
given("GET $it 요청을") {
|
||||||
`when`("로그인 된 회원이 보내면 성공한다.") {
|
`when`("로그인 된 회원이 보내면 성공한다.") {
|
||||||
then("회원") {
|
then("회원") {
|
||||||
setUpUser()
|
loginAsUser()
|
||||||
|
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
then("관리자") {
|
then("관리자") {
|
||||||
setUpAdmin()
|
loginAsAdmin()
|
||||||
|
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { isOk() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
`when`("로그인 없이 보내면") {
|
`when`("로그인 없이 보내면") {
|
||||||
then("로그인 페이지로 이동한다.") {
|
then("로그인 페이지로 이동한다.") {
|
||||||
setUpNotLoggedIn()
|
doNotLogin()
|
||||||
|
|
||||||
runGetTest(it) {
|
runGetTest(
|
||||||
statusCode(200)
|
mockMvc = mockMvc,
|
||||||
body(Matchers.containsString("<title>Login</title>"))
|
endpoint = it,
|
||||||
|
log = true
|
||||||
|
) {
|
||||||
|
status { is3xxRedirection() }
|
||||||
|
header {
|
||||||
|
string("Location", "/login")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user