From 50e7ca9f595fb6de5eeef5b89066bd1bf50868bd Mon Sep 17 00:00:00 2001 From: jhon3242 Date: Thu, 24 Oct 2024 17:25:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EB=B0=A9=20=EB=82=98=EA=B0=88=20?= =?UTF-8?q?=EB=95=8C=20=EC=BF=A0=ED=82=A4=20=EC=82=AD=EC=A0=9C=20#371?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ddangkong/controller/room/RoomController.java | 9 ++++++++- .../controller/room/RoomMemberCookieEncryptor.java | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/ddangkong/controller/room/RoomController.java b/backend/src/main/java/ddangkong/controller/room/RoomController.java index 76e31a17..37ac8ffc 100644 --- a/backend/src/main/java/ddangkong/controller/room/RoomController.java +++ b/backend/src/main/java/ddangkong/controller/room/RoomController.java @@ -83,8 +83,10 @@ public RoomJoinResponse joinRoom(@PathVariable String uuid, @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping("/balances/rooms/{roomId}/members/{memberId}") public void leaveRoom(@PathVariable @Positive Long roomId, - @PathVariable @Positive Long memberId) { + @PathVariable @Positive Long memberId, + HttpServletResponse response) { roomFacade.leaveRoom(roomId, memberId); + deleteCookie(response); } @ResponseStatus(HttpStatus.NO_CONTENT) @@ -130,4 +132,9 @@ private void setEncryptCookie(HttpServletRequest request, ResponseCookie encodedCookie = roomMemberCookieEncryptor.getEncodedCookie(cookieValue, origin); response.addHeader(HttpHeaders.SET_COOKIE, encodedCookie.toString()); } + + private void deleteCookie(HttpServletResponse response) { + ResponseCookie deleteCookie = roomMemberCookieEncryptor.deleteCookie(); + response.addHeader(HttpHeaders.SET_COOKIE, deleteCookie.toString()); + } } diff --git a/backend/src/main/java/ddangkong/controller/room/RoomMemberCookieEncryptor.java b/backend/src/main/java/ddangkong/controller/room/RoomMemberCookieEncryptor.java index e313870e..4cefb8af 100644 --- a/backend/src/main/java/ddangkong/controller/room/RoomMemberCookieEncryptor.java +++ b/backend/src/main/java/ddangkong/controller/room/RoomMemberCookieEncryptor.java @@ -1,6 +1,5 @@ package ddangkong.controller.room; -import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.server.Cookie.SameSite; import org.springframework.http.ResponseCookie; @@ -9,6 +8,7 @@ @Component public class RoomMemberCookieEncryptor { + private static final String ROOT_PATH = "/"; private static final String DEFAULT_PATH = "/api/balances/rooms"; private static final String LOCALHOST = "http://localhost"; @@ -31,6 +31,15 @@ public ResponseCookie getEncodedCookie(Object value, String origin) { .build(); } + public ResponseCookie deleteCookie() { + return ResponseCookie.from(rejoinKey, null) + .httpOnly(true) + .secure(true) + .path(ROOT_PATH) + .maxAge(0) + .build(); + } + private String getSameSiteOption(String origin) { if (origin != null && origin.startsWith(LOCALHOST)) { return SameSite.NONE.attributeValue(); From 845116a05415709fe17203a8eaff5d6a9995689b Mon Sep 17 00:00:00 2001 From: jhon3242 Date: Thu, 24 Oct 2024 17:25:35 +0900 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20=EB=B0=A9=20=EB=82=98=EA=B0=80?= =?UTF-8?q?=EA=B8=B0=20=EC=BF=A0=ED=82=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20API=20=EB=AC=B8=EC=84=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20#371?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/docs/asciidoc/room.adoc | 4 +++ .../controller/room/RoomControllerTest.java | 30 +++++++++++++++++++ .../room/RoomDocumentationTest.java | 3 ++ 3 files changed, 37 insertions(+) diff --git a/backend/src/docs/asciidoc/room.adoc b/backend/src/docs/asciidoc/room.adoc index c45037af..b2c8bc05 100644 --- a/backend/src/docs/asciidoc/room.adoc +++ b/backend/src/docs/asciidoc/room.adoc @@ -98,6 +98,10 @@ include::{snippets}/room/leave/path-parameters.adoc[] include::{snippets}/room/leave/http-response.adoc[] +response cookies + +include::{snippets}/room/leave/response-cookies.adoc[] + ''' === 방 설정 변경 diff --git a/backend/src/test/java/ddangkong/controller/room/RoomControllerTest.java b/backend/src/test/java/ddangkong/controller/room/RoomControllerTest.java index 2be36775..194262ad 100644 --- a/backend/src/test/java/ddangkong/controller/room/RoomControllerTest.java +++ b/backend/src/test/java/ddangkong/controller/room/RoomControllerTest.java @@ -1,6 +1,7 @@ package ddangkong.controller.room; import static ddangkong.support.fixture.MemberFixture.PRIN; +import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; @@ -370,5 +371,34 @@ class 쿠키 { // then assertThat(body.nickname()).isEqualTo(roomJoinResponse.member().nickname()); } + + @Test + void 방을_나가면_쿠키를_삭제한다() { + // given + RoomJoinRequest body = new RoomJoinRequest("참가자"); + String cookie = RestAssured.given().log().all() + .contentType(ContentType.JSON) + .body(body) + .when().post("/api/balances/rooms") + .getCookie("test_cookie"); + + RoomJoinResponse roomJoinResponse = RestAssured.given().log().all() + .contentType(ContentType.JSON) + .cookie("test_cookie", cookie) + .when().get("/api/balances/rooms/member") + .then().contentType(ContentType.JSON).log().all() + .statusCode(200) + .extract().as(RoomJoinResponse.class); + + // when + String deleteCookie = RestAssured.given().log().all() + .pathParam("roomId", roomJoinResponse.roomId()) + .pathParam("memberId", roomJoinResponse.member().memberId()) + .cookie("test_cookie", cookie) + .when().delete("/api/balances/rooms/{roomId}/members/{memberId}") + .getCookie("test_cookie"); + + assertThat(deleteCookie).isBlank(); + } } } diff --git a/backend/src/test/java/ddangkong/documentation/room/RoomDocumentationTest.java b/backend/src/test/java/ddangkong/documentation/room/RoomDocumentationTest.java index 97249127..7ea92311 100644 --- a/backend/src/test/java/ddangkong/documentation/room/RoomDocumentationTest.java +++ b/backend/src/test/java/ddangkong/documentation/room/RoomDocumentationTest.java @@ -271,6 +271,9 @@ class 방_나가기 { pathParameters( parameterWithName("roomId").description("방 ID"), parameterWithName("memberId").description("멤버 ID") + ), + responseCookies( + cookieWithName("test_cookie").description("삭제 쿠키") ) )); }