From e942d772dce7ea5d5ecc7223462a8d36616276e4 Mon Sep 17 00:00:00 2001 From: pricelees Date: Tue, 15 Jul 2025 16:13:44 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=83=88=EB=A1=9C=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98=ED=95=9C=20API=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A5=BC=20=EB=B0=94=ED=83=95?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20PageControllerTest=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../roomescape/view/PageControllerTest.kt | 101 +++++++++++++----- 1 file changed, 73 insertions(+), 28 deletions(-) diff --git a/src/test/java/roomescape/view/PageControllerTest.kt b/src/test/java/roomescape/view/PageControllerTest.kt index 273d9235..a9f10075 100644 --- a/src/test/java/roomescape/view/PageControllerTest.kt +++ b/src/test/java/roomescape/view/PageControllerTest.kt @@ -1,35 +1,56 @@ 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 -class PageControllerTest() : RoomescapeApiTest() { +@WebMvcTest(controllers = [ + AuthPageController::class, + AdminPageController::class, + ClientPageController::class +]) +class PageControllerTest( + @Autowired private val mockMvc: MockMvc +) : RoomescapeApiTest() { init { listOf("/", "/login").forEach { given("GET $it 요청은") { `when`("로그인 및 권한 여부와 관계없이 성공한다.") { then("비회원") { - setUpNotLoggedIn() + doNotLogin() - runGetTest(it) { - statusCode(200) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { isOk() } } } then("회원") { - setUpUser() + loginAsUser() - runGetTest(it) { - statusCode(200) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { isOk() } } } then("관리자") { - setUpAdmin() + loginAsAdmin() - runGetTest(it) { - statusCode(200) + runGetTest( + 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 { given("GET $it 요청을") { `when`("관리자가 보내면") { - setUpAdmin() + loginAsAdmin() then("성공한다.") { - runGetTest(it) { - statusCode(200) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { isOk() } } } } `when`("회원이 보내면") { - setUpUser() + loginAsUser() then("로그인 페이지로 이동한다.") { - runGetTest(it) { - statusCode(200) - body(Matchers.containsString("Login")) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { is3xxRedirection() } + header { + string("Location", "/login") + } } } } @@ -65,28 +96,42 @@ class PageControllerTest() : RoomescapeApiTest() { given("GET $it 요청을") { `when`("로그인 된 회원이 보내면 성공한다.") { then("회원") { - setUpUser() + loginAsUser() - runGetTest(it) { - statusCode(200) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { isOk() } } } then("관리자") { - setUpAdmin() + loginAsAdmin() - runGetTest(it) { - statusCode(200) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { isOk() } } } } `when`("로그인 없이 보내면") { then("로그인 페이지로 이동한다.") { - setUpNotLoggedIn() + doNotLogin() - runGetTest(it) { - statusCode(200) - body(Matchers.containsString("Login")) + runGetTest( + mockMvc = mockMvc, + endpoint = it, + log = true + ) { + status { is3xxRedirection() } + header { + string("Location", "/login") + } } } }