From 1ddf812d1c6d6ee1122f1f163bfb134f5b0eb198 Mon Sep 17 00:00:00 2001 From: pricelees Date: Sat, 13 Sep 2025 11:48:57 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20RestAssuredUtils=EC=97=90=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EC=A0=84=EC=9A=A9=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=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 --- .../roomescape/util/RestAssuredUtils.kt | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/roomescape/util/RestAssuredUtils.kt b/src/test/kotlin/roomescape/util/RestAssuredUtils.kt index 79e7bc43..bfe87932 100644 --- a/src/test/kotlin/roomescape/util/RestAssuredUtils.kt +++ b/src/test/kotlin/roomescape/util/RestAssuredUtils.kt @@ -7,7 +7,9 @@ import io.restassured.module.kotlin.extensions.When import io.restassured.response.Response import io.restassured.response.ValidatableResponse import io.restassured.specification.RequestSpecification +import org.hamcrest.CoreMatchers.equalTo import org.springframework.data.repository.findByIdOrNull +import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus import org.springframework.http.MediaType import roomescape.admin.infrastructure.persistence.AdminEntity @@ -16,9 +18,9 @@ import roomescape.auth.web.LoginRequest import roomescape.auth.web.LoginRequestV2 import roomescape.common.config.next import roomescape.common.dto.PrincipalType +import roomescape.common.exception.ErrorCode import roomescape.member.infrastructure.persistence.* import roomescape.member.web.UserCreateRequest -import roomescape.member.web.toEntity class AuthUtil( private val memberRepository: MemberRepository, @@ -87,10 +89,11 @@ class AuthUtil( if (adminRepository.findByAccount(admin.account) == null) { adminRepository.save(admin) } + val requestBody = LoginRequestV2(admin.account, admin.password, PrincipalType.ADMIN) return Given { contentType(MediaType.APPLICATION_JSON_VALUE) - body(LoginRequestV2(account = admin.account, password = admin.password, principalType = PrincipalType.ADMIN)) + body(requestBody) } When { post("/auth/login") } Then { @@ -139,6 +142,37 @@ fun runTest( } } +fun runExceptionTest( + token: String? = null, + method: HttpMethod, + requestBody: Any? = null, + endpoint: String, + expectedErrorCode: ErrorCode +): ValidatableResponse { + return runTest( + token = token, + using = { + requestBody?.let { body(requestBody) } ?: this + }, + on = { + when (method) { + HttpMethod.GET -> get(endpoint) + HttpMethod.POST -> post(endpoint) + HttpMethod.PATCH -> patch(endpoint) + HttpMethod.DELETE -> delete(endpoint) + + else -> { + throw AssertionError("Unsupported HTTP method: $method") + } + } + }, + expect = { + statusCode(expectedErrorCode.httpStatus.value()) + body("code", equalTo(expectedErrorCode.errorCode)) + } + ) +} + /** * @param props: RestAssured 응답 Body 에서 존재 & Null 여부를 확인할 프로퍼티 이름 */