Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/#119 에러 코드 문서화 작업 및 리팩토링 #120

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -3,17 +3,17 @@
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum AboutExceptionCode implements ExceptionCode {
CATEGORY_NOT_MATCH(BAD_REQUEST, "메인 카테고리와 보조 카테고리가 맞지 않습니다."),
ABOUT_NOT_FOUND(NOT_FOUND, "해당 조건의 소개글을 찾을 수 없습니다.")
;
CATEGORY_NOT_MATCH(BAD_REQUEST, "메인 카테고리와 보조 카테고리가 일치하지 않습니다."),
ABOUT_NOT_FOUND(NOT_FOUND, "해당 소개글을 찾을 수 없습니다.");
minjo-on marked this conversation as resolved.
Show resolved Hide resolved

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import kgu.developers.api.auth.presentation.exception.InvalidPasswordException;
import kgu.developers.api.auth.presentation.request.LoginRequest;
import kgu.developers.api.auth.presentation.response.TokenResponse;
import kgu.developers.api.user.application.UserService;
Expand All @@ -29,9 +28,7 @@ public TokenResponse login(LoginRequest request) {
String password = request.password();

User user = userService.getUserById(userId);
if (!passwordEncoder.matches(password, user.getPassword())) {
throw new InvalidPasswordException();
}
user.isPasswordMatching(password, passwordEncoder);

String refreshToken = tokenProvider.generateToken(user.getId(), Duration.ofDays(7));
String accessToken = tokenProvider.generateToken(user.getId(), Duration.ofHours(2));
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package kgu.developers.api.comment.presentation.exception;

import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.NOT_FOUND;

@Getter
@AllArgsConstructor
public enum CommentExceptionCode implements ExceptionCode {
COMMENT_NOT_FOUND(NOT_FOUND, "해당 ID의 댓글이 없습니다.");
COMMENT_NOT_FOUND(NOT_FOUND, "해당 댓글을 찾을 수 없습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package kgu.developers.api.file.presentation.exception;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.*;

@Getter
@AllArgsConstructor
public enum FileExceptionCode implements ExceptionCode {
FILE_SAVING_ERROR(BAD_REQUEST, "파일 저장이 되지 않았습니다.")
;
FILE_SAVING_ERROR(BAD_REQUEST, "파일 저장에 실패하였습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Getter
@AllArgsConstructor
public enum LabExceptionCode implements ExceptionCode {
LAB_NOT_FOUND(NOT_FOUND, "해당 ID의 연구실이 없습니다.");
LAB_NOT_FOUND(NOT_FOUND, "해당 연구실을 찾을 수 없습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package kgu.developers.api.post.presentation.exception;

import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.NOT_FOUND;

@Getter
@AllArgsConstructor
public enum PostExceptionCode implements ExceptionCode {
POST_NOT_FOUND(NOT_FOUND, "해당 ID의 게시글이 없습니다.");
POST_NOT_FOUND(NOT_FOUND, "해당 게시글을 찾을 수 없습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum ProfessorExceptionCode implements ExceptionCode {
PROFESSOR_NOT_FOUND(NOT_FOUND, "해당 ID의 교수가 없습니다.");
PROFESSOR_NOT_FOUND(NOT_FOUND, "해당 교수를 찾을 수 없습니다.");

private final HttpStatus status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package kgu.developers.api.user.presentation.exception;

import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.FORBIDDEN;

@Getter
@AllArgsConstructor
public enum UserExceptionCode implements ExceptionCode {
USER_ID_DUPLICATED(CONFLICT, "이미 동일한 학번으로 가입이 되어있습니다."),
USER_EMAIL_ID_DUPLICATED(CONFLICT, "이미 동일한 이메일로 가입이 되어있습니다."),
USER_PHONE_NUMBER_DUPLICATED(CONFLICT, "이미 동일한 전화번호로 가입이 되어있습니다."),
USER_NOT_AUTHENTICATED(FORBIDDEN, "회원 인증에 실패하였습니다."),
USER_NOT_AUTHENTICATED(UNAUTHORIZED, "회원 인증에 실패하였습니다."),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package kgu.developers.api.user.presentation.exception;

import static kgu.developers.api.user.presentation.exception.UserExceptionCode.USER_ID_DUPLICATED;

import kgu.developers.common.exception.CustomException;

public class UserIdDuplicateException extends CustomException {
public UserIdDuplicateException() {
super(UserExceptionCode.USER_ID_DUPLICATED);
super(USER_ID_DUPLICATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import kgu.developers.api.auth.application.AuthService;
import kgu.developers.api.auth.presentation.exception.InvalidPasswordException;
import kgu.developers.api.auth.presentation.request.LoginRequest;
import kgu.developers.api.user.application.UserService;
import kgu.developers.common.auth.jwt.JwtProperties;
import kgu.developers.common.auth.jwt.TokenProvider;
import kgu.developers.domain.user.domain.Major;
import kgu.developers.domain.user.domain.User;
import kgu.developers.domain.user.exception.InvalidPasswordException;
import mock.FakeUserRepository;

public class AuthServiceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import java.util.Collections;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -22,13 +26,11 @@
import kgu.developers.domain.post.domain.Post;
import kgu.developers.domain.user.exception.DeptCodeNotValidException;
import kgu.developers.domain.user.exception.EmailDomainNotValidException;
import kgu.developers.domain.user.exception.InvalidPasswordException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Getter
Expand Down Expand Up @@ -126,4 +128,10 @@ private static boolean isValidEmailDomain(String email) {
return ACCESSIBLE_EMAIL_DOMAINS.stream()
.anyMatch(email::endsWith);
}

public void isPasswordMatching(String rawPassword, PasswordEncoder passwordEncoder) {
if (!passwordEncoder.matches(rawPassword, this.password)) {
throw new InvalidPasswordException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kgu.developers.domain.user.exception;

import static kgu.developers.domain.user.exception.UserDomainExceptionCode.INVALID_PASSWORD;

import kgu.developers.common.exception.CustomException;

public class InvalidPasswordException extends CustomException {
public InvalidPasswordException() {
super(INVALID_PASSWORD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import kgu.developers.common.exception.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum UserDomainExceptionCode implements ExceptionCode {
USER_NOT_FOUND(NOT_FOUND, "해당 회원을 찾을 수 없습니다."),
EMAIL_NOT_VALID(BAD_REQUEST, "학교 이메일 형식이 아닙니다."),
DEPT_CODE_NOT_VALID(BAD_REQUEST, "학번별 학과 코드가 일치하지 않습니다.")
DEPT_CODE_NOT_VALID(BAD_REQUEST, "학번별 학과 코드가 일치하지 않습니다."),
INVALID_PASSWORD(BAD_REQUEST, "비밀번호가 일치하지 않습니다."),
;

private final HttpStatus status;
Expand Down
26 changes: 26 additions & 0 deletions aics-domain/src/testFixtures/java/user/domain/UserDomainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import kgu.developers.domain.user.domain.Major;
import kgu.developers.domain.user.domain.Role;
import kgu.developers.domain.user.domain.User;
import kgu.developers.domain.user.exception.DeptCodeNotValidException;
import kgu.developers.domain.user.exception.EmailDomainNotValidException;
import kgu.developers.domain.user.exception.InvalidPasswordException;

public class UserDomainTest {

Expand Down Expand Up @@ -75,4 +78,27 @@ public void createUser_InvalidDeptCode_ThrowsException() {
User.create(id, password, name, email, phone, major);
}).isInstanceOf(DeptCodeNotValidException.class);
}

@Test
@DisplayName("비밀번호가 일치하지 않을 시 InvalidPasswordException이 발생 한다")
public void isPasswordMatching_InvalidPassword_ThrowsException() {
// given
String rawPassword = "invalidPassword";
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

User user = User.create(
"202411345",
passwordEncoder.encode("correctPassword"),
"홍길동",
"valid@kgu.ac.kr",
"010-1234-5678",
Major.CSE
);

// when
// then
assertThatThrownBy(() -> {
user.isPasswordMatching(rawPassword, passwordEncoder);
}).isInstanceOf(InvalidPasswordException.class);
}
}