Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 방 나갈 때 쿠키 삭제 추가 #374

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/src/docs/asciidoc/room.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]

'''

=== 방 설정 변경
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ class 방_나가기 {
pathParameters(
parameterWithName("roomId").description("방 ID"),
parameterWithName("memberId").description("멤버 ID")
),
responseCookies(
cookieWithName("test_cookie").description("삭제 쿠키")
)
));
}
Expand Down