Skip to content

Commit

Permalink
fix: 본인이 본인을 팔로우하는 경우 Validation 추가 (#319)
Browse files Browse the repository at this point in the history
* fix: 본인을 팔로우하는 경우 validation 추가

* 본인 팔로우 validation 테스트 코드 추가
  • Loading branch information
kdomo authored Feb 12, 2024
1 parent 0a973c8 commit e8f0a82
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "이미지 키를 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e8f0a82

Please sign in to comment.