|
21 | 21 |
|
22 | 22 | import java.time.LocalDate; |
23 | 23 |
|
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
24 | 26 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
25 | 27 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; |
26 | 28 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
@@ -330,4 +332,119 @@ void updateMyProfile_expiredAccessToken() throws Exception { |
330 | 332 | .andExpect(jsonPath("$.code").value("AUTH_004")) |
331 | 333 | .andExpect(jsonPath("$.message").value("만료된 액세스 토큰입니다.")); |
332 | 334 | } |
| 335 | + |
| 336 | + // ====================== 내 계정 삭제 테스트 ====================== |
| 337 | + |
| 338 | + @Test |
| 339 | + @DisplayName("회원 탈퇴 성공 → 200 OK") |
| 340 | + void deleteMyAccount_success() throws Exception { |
| 341 | + // given: 정상 유저 저장 |
| 342 | + User user = User. createUser( "deleteuser", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 343 | + user.setUserProfile(new UserProfile(user, "홍길동", "https://cdn.example.com/1.png", "소개글", LocalDate.of(1990, 1, 1), 100)); |
| 344 | + user.setUserStatus(UserStatus.ACTIVE); |
| 345 | + userRepository.save(user); |
| 346 | + |
| 347 | + String accessToken = generateAccessToken(user); |
| 348 | + |
| 349 | + // when & then |
| 350 | + mvc.perform(delete("/api/users/me") |
| 351 | + .header("Authorization", "Bearer " + accessToken)) |
| 352 | + .andDo(print()) |
| 353 | + .andExpect(status().isOk()) |
| 354 | + .andExpect(jsonPath("$.success").value(true)) |
| 355 | + .andExpect(jsonPath("$.code").value("SUCCESS_200")) |
| 356 | + .andExpect(jsonPath("$.message").value("회원 탈퇴가 완료되었습니다.")); |
| 357 | + |
| 358 | + // DB 반영 확인 |
| 359 | + User deleted = userRepository.findById(user.getId()).orElseThrow(); |
| 360 | + assertThat(deleted.getUserStatus()).isEqualTo(UserStatus.DELETED); |
| 361 | + assertThat(deleted.getUsername()).startsWith("deleted_"); |
| 362 | + assertThat(deleted.getEmail()).startsWith("deleted_"); |
| 363 | + assertThat(deleted.getProvider()).startsWith("deleted_"); |
| 364 | + assertThat(deleted.getProviderId()).startsWith("deleted_"); |
| 365 | + assertThat(deleted.getUserProfile().getNickname()).isEqualTo("탈퇴한 회원"); |
| 366 | + } |
| 367 | + |
| 368 | + @Test |
| 369 | + @DisplayName("이미 탈퇴한 계정 탈퇴 시도 → 410 Gone") |
| 370 | + void deleteMyAccount_alreadyDeleted() throws Exception { |
| 371 | + // given: DELETED 상태 유저 저장 |
| 372 | + User user = User. createUser( "alreadydeleted", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 373 | + user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0)); |
| 374 | + user.setUserStatus(UserStatus.DELETED); |
| 375 | + userRepository.save(user); |
| 376 | + |
| 377 | + String accessToken = generateAccessToken(user); |
| 378 | + |
| 379 | + // when & then |
| 380 | + mvc.perform(delete("/api/users/me") |
| 381 | + .header("Authorization", "Bearer " + accessToken)) |
| 382 | + .andDo(print()) |
| 383 | + .andExpect(status().isGone()) |
| 384 | + .andExpect(jsonPath("$.code").value("USER_009")) |
| 385 | + .andExpect(jsonPath("$.message").value("탈퇴한 계정입니다.")); |
| 386 | + } |
| 387 | + |
| 388 | + @Test |
| 389 | + @DisplayName("정지된 계정 탈퇴 시도 → 403 Forbidden") |
| 390 | + void deleteMyAccount_suspendedUser() throws Exception { |
| 391 | + // given: SUSPENDED 상태 유저 저장 |
| 392 | + User user = User. createUser( "suspendeddelete", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 393 | + user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0)); |
| 394 | + user.setUserStatus(UserStatus.SUSPENDED); |
| 395 | + userRepository.save(user); |
| 396 | + |
| 397 | + String accessToken = generateAccessToken(user); |
| 398 | + |
| 399 | + // when & then |
| 400 | + mvc.perform(delete("/api/users/me") |
| 401 | + .header("Authorization", "Bearer " + accessToken)) |
| 402 | + .andDo(print()) |
| 403 | + .andExpect(status().isForbidden()) |
| 404 | + .andExpect(jsonPath("$.code").value("USER_008")) |
| 405 | + .andExpect(jsonPath("$.message").value("정지된 계정입니다. 관리자에게 문의하세요.")); |
| 406 | + } |
| 407 | + |
| 408 | + @Test |
| 409 | + @DisplayName("AccessToken 없음으로 회원 탈퇴 시도 → 401 Unauthorized") |
| 410 | + void deleteMyAccount_noAccessToken() throws Exception { |
| 411 | + mvc.perform(delete("/api/users/me")) |
| 412 | + .andDo(print()) |
| 413 | + .andExpect(status().isUnauthorized()) |
| 414 | + .andExpect(jsonPath("$.code").value("AUTH_001")) |
| 415 | + .andExpect(jsonPath("$.message").value("인증이 필요합니다.")); |
| 416 | + } |
| 417 | + |
| 418 | + @Test |
| 419 | + @DisplayName("잘못된 AccessToken으로 회원 탈퇴 시도 → 401 Unauthorized (AUTH_002)") |
| 420 | + void deleteMyAccount_invalidAccessToken() throws Exception { |
| 421 | + mvc.perform(delete("/api/users/me") |
| 422 | + .header("Authorization", "Bearer invalidToken")) |
| 423 | + .andDo(print()) |
| 424 | + .andExpect(status().isUnauthorized()) |
| 425 | + .andExpect(jsonPath("$.code").value("AUTH_002")) |
| 426 | + .andExpect(jsonPath("$.message").value("유효하지 않은 액세스 토큰입니다.")); |
| 427 | + } |
| 428 | + |
| 429 | + @Test |
| 430 | + @DisplayName("만료된 AccessToken으로 회원 탈퇴 시도 → 401 Unauthorized (AUTH_004)") |
| 431 | + void deleteMyAccount_expiredAccessToken() throws Exception { |
| 432 | + // given |
| 433 | + User user = User. createUser( "expiredDelete", "[email protected]", passwordEncoder. encode( "P@ssw0rd!")); |
| 434 | + user.setUserProfile(new UserProfile(user, "닉네임", null, null, null, 0)); |
| 435 | + user.setUserStatus(UserStatus.ACTIVE); |
| 436 | + userRepository.save(user); |
| 437 | + |
| 438 | + String expiredToken = testJwtTokenProvider.createExpiredAccessToken( |
| 439 | + user.getId(), user.getUsername(), user.getRole().name() |
| 440 | + ); |
| 441 | + |
| 442 | + // when & then |
| 443 | + mvc.perform(delete("/api/users/me") |
| 444 | + .header("Authorization", "Bearer " + expiredToken)) |
| 445 | + .andDo(print()) |
| 446 | + .andExpect(status().isUnauthorized()) |
| 447 | + .andExpect(jsonPath("$.code").value("AUTH_004")) |
| 448 | + .andExpect(jsonPath("$.message").value("만료된 액세스 토큰입니다.")); |
| 449 | + } |
333 | 450 | } |
0 commit comments