Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@RequestMapping("/auth")
@RequiredArgsConstructor
@Tag(name = "MemberController", description = "ํšŒ์› ์ปจํŠธ๋กค๋Ÿฌ")
public class MemberController {
public class MemberController {
private final MemberService memberService;
private final Rq rq;
private final EmailVerificationService emailVerificationService;
Expand Down Expand Up @@ -80,7 +80,7 @@ public RsData<Void> login(@RequestBody LoginRequest request) {
public RsData<Void> logout() {
rq.deleteCookie("accessToken");
rq.deleteCookie("refreshToken");
return new RsData<>("200-1", "๋กœ๊ทธ์•„์›ƒ ์„ฑ๊ณต");
return new RsData<>("200-8", "๋กœ๊ทธ์•„์›ƒ ์„ฑ๊ณต");
}

@GetMapping("/me")
Expand All @@ -100,4 +100,17 @@ public RsData<Void> refresh() {

return new RsData<>("200-6", "ํ† ํฐ ๊ฐฑ์‹  ์„ฑ๊ณต");
}

@DeleteMapping("/me")
@Operation(summary = "ํšŒ์› ํƒˆํ‡ด")
public RsData<Void> deleteMember() {
Member currentUser = rq.getActor();
memberService.deleteMember(currentUser);

// ํƒˆํ‡ด ํ›„ ์ฟ ํ‚ค ์‚ญ์ œ
rq.deleteCookie("accessToken");
rq.deleteCookie("refreshToken");

return new RsData<>("200-7", "ํšŒ์› ํƒˆํ‡ด๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ public boolean isValidToken(String token) {
return authTokenService.isValidToken(token);
}

@Transactional
public void deleteMember(Member currentUser) {
if (currentUser == null) {
throw new ServiceException("401-1", "๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.");
}

Member member = memberRepository.findById(currentUser.getId())
.orElseThrow(() -> new ServiceException("404-1", "์กด์žฌํ•˜์ง€ ์•Š๋Š” ํšŒ์›์ž…๋‹ˆ๋‹ค."));

// ๊ด€๋ จ ์—”ํ‹ฐํ‹ฐ๋“ค ๋จผ์ € ์‚ญ์ œ
menteeRepository.findByMemberId(member.getId()).ifPresent(menteeRepository::delete);
mentorRepository.findByMemberId(member.getId()).ifPresent(mentorRepository::delete);

memberRepository.delete(member);
}

public boolean isRefreshToken(String token) {
return authTokenService.isRefreshToken(token);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
Expand Down Expand Up @@ -368,7 +369,7 @@ void t9() throws Exception {

result
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$.resultCode").value("200-1"))
.andExpect(jsonPath("$.resultCode").value("200-8"))
.andExpect(jsonPath("$.msg").value("๋กœ๊ทธ์•„์›ƒ ์„ฑ๊ณต"))
.andExpect(cookie().maxAge("accessToken", 0))
.andExpect(cookie().maxAge("refreshToken", 0));
Expand Down Expand Up @@ -443,4 +444,56 @@ void t11() throws Exception {
.andExpect(jsonPath("$.msg").value("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋‹‰๋„ค์ž„์ž…๋‹ˆ๋‹ค."));
}

@Test
@DisplayName("ํšŒ์› ํƒˆํ‡ด ์„ฑ๊ณต")
void t12() throws Exception {
// ๋ฉ˜ํ‹ฐ ํšŒ์›๊ฐ€์ž…
String email = "[email protected]";
memberService.joinMentee(email, "ํƒˆํ‡ด์‚ฌ์šฉ์ž", "ํƒˆํ‡ด๋‹‰๋„ค์ž„", "password123", "Backend");

// ๋กœ๊ทธ์ธํ•˜์—ฌ ์ฟ ํ‚ค ๋ฐ›๊ธฐ
ResultActions loginResult = mvc.perform(
post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(String.format("""
{
"email": "%s",
"password": "password123"
}
""", email))
);

Cookie accessToken = loginResult.andReturn().getResponse().getCookie("accessToken");

// ํšŒ์› ํƒˆํ‡ด ์š”์ฒญ
ResultActions result = mvc
.perform(
delete("/auth/me")
.cookie(accessToken)
)
.andDo(print());

result
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("$.resultCode").value("200-7"))
.andExpect(jsonPath("$.msg").value("ํšŒ์› ํƒˆํ‡ด๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."))
.andExpect(cookie().maxAge("accessToken", 0))
.andExpect(cookie().maxAge("refreshToken", 0));

// ํƒˆํ‡ด ํ›„ ํ•ด๋‹น ์ด๋ฉ”์ผ๋กœ ์กฐํšŒํ–ˆ์„ ๋•Œ ์—†์–ด์•ผ ํ•จ
assertThat(memberService.findByEmail(email)).isEmpty();
}

@Test
@DisplayName("๋กœ๊ทธ์ธํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ํšŒ์› ํƒˆํ‡ด ์‹œ๋„ - ์‹คํŒจ")
void t13() throws Exception {
// ๋กœ๊ทธ์ธ ์—†์ด ํšŒ์› ํƒˆํ‡ด ์‹œ๋„
ResultActions result = mvc
.perform(delete("/auth/me"))
.andDo(print());

result
.andExpect(status().isUnauthorized());
}

}