From e8f0a821d34a0e8c2ee0657be93a83384e8b3f7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=8F=84=EB=AA=A8?= Date: Mon, 12 Feb 2024 20:15:14 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=B3=B8=EC=9D=B8=EC=9D=B4=20=EB=B3=B8?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=ED=8C=94=EB=A1=9C=EC=9A=B0=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20Validation=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(#319)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 본인을 팔로우하는 경우 validation 추가 * 본인 팔로우 validation 테스트 코드 추가 --- .../domain/follow/application/FollowService.java | 7 +++++++ .../depromeet/global/error/exception/ErrorCode.java | 1 + .../follow/application/FollowServiceTest.java | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/main/java/com/depromeet/domain/follow/application/FollowService.java b/src/main/java/com/depromeet/domain/follow/application/FollowService.java index 791383809..ecb6eef42 100644 --- a/src/main/java/com/depromeet/domain/follow/application/FollowService.java +++ b/src/main/java/com/depromeet/domain/follow/application/FollowService.java @@ -38,6 +38,7 @@ public class FollowService { public void createFollow(FollowCreateRequest request) { final Member currentMember = memberUtil.getCurrentMember(); + validateSelfFollow(currentMember.getId(), request.targetId()); Member targetMember = getTargetMember(request.targetId()); boolean existMemberRelation = @@ -177,6 +178,12 @@ && isToday(record.getStartedAt())) return result; } + private void validateSelfFollow(Long expectedId, Long actualId) { + if (expectedId.equals(actualId)) { + throw new CustomException(ErrorCode.FOLLOW_SELF_NOT_ALLOWED); + } + } + private boolean isToday(LocalDateTime dateTime) { LocalDateTime today = LocalDateTime.now(); return dateTime.toLocalDate().isEqual(today.toLocalDate()); diff --git a/src/main/java/com/depromeet/global/error/exception/ErrorCode.java b/src/main/java/com/depromeet/global/error/exception/ErrorCode.java index f4a49109f..69d9f7906 100644 --- a/src/main/java/com/depromeet/global/error/exception/ErrorCode.java +++ b/src/main/java/com/depromeet/global/error/exception/ErrorCode.java @@ -49,6 +49,7 @@ public enum ErrorCode { FOLLOW_TARGET_MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "타겟 유저을 찾을 수 없습니다."), FOLLOW_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "이미 팔로우 중인 회원입니다."), FOLLOW_NOT_EXIST(HttpStatus.BAD_REQUEST, "팔로우 중인 회원만 팔로우 취소가 가능합니다."), + FOLLOW_SELF_NOT_ALLOWED(HttpStatus.CONFLICT, "본인을 팔로우 할 수 없습니다."), // Image IMAGE_KEY_NOT_FOUND(HttpStatus.NOT_FOUND, "이미지 키를 찾을 수 없습니다."), diff --git a/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java b/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java index c64bbb0df..7fb5a3cf1 100644 --- a/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java +++ b/src/test/java/com/depromeet/domain/follow/application/FollowServiceTest.java @@ -68,6 +68,19 @@ class 팔로우를_추가할_때 { .hasMessage(ErrorCode.MEMBER_NOT_FOUND.getMessage()); } + @Test + void 본인을_팔로우_할_경우_예외를_발생시킨다() { + FollowCreateRequest request = new FollowCreateRequest(1L); + memberRepository.save( + Member.createNormalMember( + Profile.createProfile("testNickname1", "testImageUrl1"))); + + // when, then + assertThatThrownBy(() -> followService.createFollow(request)) + .isInstanceOf(CustomException.class) + .hasMessage(ErrorCode.FOLLOW_SELF_NOT_ALLOWED.getMessage()); + } + @Test void 타겟회원이_존재하지_않는다면_예외를_발생시킨다() { // given