Skip to content

Commit

Permalink
[fix #163] 추가 정보 기입 시 닉네임 2차 검증 누락 해결 (#165)
Browse files Browse the repository at this point in the history
* [fix] 추가정보 닉네임 2차 validation

* [feat] 닉네임 관련 에러코드 추가

* [test] 추가정보 닉네임 Validation 단위테스트 추가

* [style] 미사용 패턴 제거

* [style] 컨벤션에 따른 닉네임 검증 메서드 네이밍 변경
  • Loading branch information
dudxo authored Dec 4, 2024
1 parent 7d62df5 commit 8e0d75f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/dnd/gongmuin/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.Duration;
import java.util.Date;
import java.util.Objects;
import java.util.regex.Pattern;

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class AuthService {
private static final String LOGOUT = "logout";
private static final String DELETE = "delete";
private static final String ANONYMOUS = "ROLE_ANONYMOUS";
private static final Pattern nicknamePattern = Pattern.compile("^[a-zA-Z0-9가-힣]+$");
private final TokenProvider tokenProvider;
private final MemberRepository memberRepository;
private final CookieUtil cookieUtil;
Expand Down Expand Up @@ -121,13 +123,25 @@ public SignUpResponse signUp(AdditionalInfoRequest request, String email, HttpSe
throw new NotFoundException(MemberErrorCode.NOT_FOUND_NEW_MEMBER);
}

updateAdditionalInfo(request, foundMember);
checkNickname(request.nickname());

updateAdditionalInfo(request, foundMember);
cookieUtil.deleteCookie(response);

return new SignUpResponse(foundMember.getNickname());
}

private void checkNickname(String nickname) {
boolean isDuplicated = memberRepository.existsByNickname(nickname);
if (isDuplicated) {
throw new NotFoundException(MemberErrorCode.DUPLICATED_NICKNAME);
}

if (!nicknamePattern.matcher(nickname).matches()) {
throw new NotFoundException(MemberErrorCode.INVALID_NICKNAME);
}
}

public LogoutResponse logout(HttpServletRequest request, HttpServletResponse response) {
String accessToken = cookieUtil.getCookieValue(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public enum MemberErrorCode implements ErrorCode {
QUESTION_POSTS_BY_MEMBER_FAILED("마이페이지 게시글 목록을 불러오는데 실패했습니다", "MEMBER_006"),
NOT_FOUND_JOB_GROUP("직군을 올바르게 입력해주세요.", "MEMBER_007"),
NOT_FOUND_JOB_CATEGORY("직렬을 올바르게 입력해주세요.", "MEMBER_008"),
DELETE_FAILED("회원탈퇴를 실패했습니다.", "MEMBER_009");
DELETE_FAILED("회원탈퇴를 실패했습니다.", "MEMBER_009"),
DUPLICATED_NICKNAME("중복된 닉네임입니다.", "MEMBER_010"),
INVALID_NICKNAME("유효하지 않은 닉네임입니다.", "MEMBER_011");

private final String message;
private final String code;
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/com/dnd/gongmuin/auth/service/AuthServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.dnd.gongmuin.member.domain.JobCategory;
import com.dnd.gongmuin.member.domain.JobGroup;
import com.dnd.gongmuin.member.domain.Member;
import com.dnd.gongmuin.member.exception.MemberErrorCode;
import com.dnd.gongmuin.member.repository.MemberRepository;
import com.dnd.gongmuin.notification.repository.NotificationRepository;
import com.dnd.gongmuin.post_interaction.repository.InteractionRepository;
Expand Down Expand Up @@ -129,6 +130,63 @@ void signUp() {
);
}

@DisplayName("추가정보 업데이트할 때 닉네임이 중복이라면 예외가 발생한다.")
@Test
void throwExceptionWhenSignUpWithDuplicatedNickName() {
// given
AdditionalInfoRequest request = new AdditionalInfoRequest("[email protected]", "김신규", "공업", "일반기계");
MockHttpServletResponse mockResponse = new MockHttpServletResponse();

Member member = MemberFixture.member3();

given(memberRepository.findBySocialEmail(member.getSocialEmail())).willReturn(
Optional.of(member));
given(memberRepository.existsByNickname(request.nickname())).willReturn(Boolean.TRUE);

// when // then
assertThatThrownBy(() -> authService.signUp(request, member.getSocialEmail(), mockResponse))
.isInstanceOf(RuntimeException.class)
.hasMessage(MemberErrorCode.DUPLICATED_NICKNAME.getMessage());
}

@DisplayName("추가정보 업데이트할 때 공백이 포함된 닉네임이라면 예외가 발생한다.")
@Test
void throwExceptionWhenSignUpWithWhiteSpaceNickName() {
// given
AdditionalInfoRequest request = new AdditionalInfoRequest("[email protected]", " 김신규", "공업", "일반기계");
MockHttpServletResponse mockResponse = new MockHttpServletResponse();

Member member = MemberFixture.member3();

given(memberRepository.findBySocialEmail(member.getSocialEmail())).willReturn(
Optional.of(member));
given(memberRepository.existsByNickname(request.nickname())).willReturn(Boolean.FALSE);

// when // then
assertThatThrownBy(() -> authService.signUp(request, member.getSocialEmail(), mockResponse))
.isInstanceOf(RuntimeException.class)
.hasMessage(MemberErrorCode.INVALID_NICKNAME.getMessage());
}

@DisplayName("추가정보 업데이트할 때 유효하지 않은 닉네임이라면 예외가 발생한다.")
@Test
void throwExceptionWhenSignUpWithInvalidNickName() {
// given
AdditionalInfoRequest request = new AdditionalInfoRequest("[email protected]", "abcㄱㄴ123", "공업", "일반기계");
MockHttpServletResponse mockResponse = new MockHttpServletResponse();

Member member = MemberFixture.member3();

given(memberRepository.findBySocialEmail(member.getSocialEmail())).willReturn(
Optional.of(member));
given(memberRepository.existsByNickname(request.nickname())).willReturn(Boolean.FALSE);

// when // then
assertThatThrownBy(() -> authService.signUp(request, member.getSocialEmail(), mockResponse))
.isInstanceOf(RuntimeException.class)
.hasMessage(MemberErrorCode.INVALID_NICKNAME.getMessage());
}

@DisplayName("로그인 회원은 로그아웃 할 수 있다.")
@Test
void logout() {
Expand Down

0 comments on commit 8e0d75f

Please sign in to comment.