-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* refactor: User 코드 포맷팅 * refactor: Dept 코드로 축약 * feat: UserDomainTest 테스트 코드 구현 * refactor: coderabbit yaml 삭제 * feat: 통합 테스트를 위한 testFixtures 적용 * feat: UserServiceTest 구현 * feat: AuthServiceTest 구현 * feat: AuthControllerTest 구현 * feat: 중형 테스트 설명 * feat: AtomicLong 사용 예시 * Update aics-common/src/main/java/kgu/developers/common/auth/jwt/JwtProperties.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * fix: JwtProperties 컴파일 에러 해결 --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5634485
commit 7bea2da
Showing
25 changed files
with
526 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
aics-api/src/testFixtures/java/auth/application/AuthServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package auth.application; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
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 mock.FakeUserRepository; | ||
|
||
public class AuthServiceTest { | ||
private AuthService authService; | ||
|
||
@BeforeEach | ||
public void init() { | ||
FakeUserRepository fakeUserRepository = new FakeUserRepository(); | ||
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); | ||
|
||
this.authService = AuthService.builder() | ||
.userService( | ||
UserService.builder() | ||
.userRepository(fakeUserRepository) | ||
.bCryptPasswordEncoder(bCryptPasswordEncoder) | ||
.build() | ||
) | ||
.passwordEncoder(bCryptPasswordEncoder) | ||
.tokenProvider( | ||
TokenProvider.builder() | ||
.jwtProperties(new JwtProperties("testIssuer", "testSecretKey")) | ||
.build() | ||
) | ||
.build(); | ||
|
||
fakeUserRepository.save(User.builder() | ||
.id("202411345") | ||
.password(bCryptPasswordEncoder.encode("password1234")) | ||
.name("홍길동") | ||
.email("test@kyonggi.ac.kr") | ||
.phone("010-1234-5678") | ||
.major(Major.CSE) | ||
.build()); | ||
} | ||
|
||
@Test | ||
@DisplayName("login은 토큰을 발급할 수 있다") | ||
public void login_Success() { | ||
// given | ||
String userId = "202411345"; | ||
String password = "password1234"; | ||
|
||
// when | ||
// then | ||
assertThatCode(() -> { | ||
authService.login(LoginRequest.builder() | ||
.userId(userId) | ||
.password(password) | ||
.build() | ||
); | ||
}).doesNotThrowAnyException(); | ||
} | ||
|
||
@Test | ||
@DisplayName("login은 비밀번호가 틀리면 InvalidPasswordException을 발생시킨다") | ||
public void login_InvalidPassword_ThrowsException() { | ||
// given | ||
String userId = "202411345"; | ||
String password = "wrongPassword"; | ||
|
||
// when | ||
// then | ||
assertThatThrownBy(() -> { | ||
authService.login(LoginRequest.builder() | ||
.userId(userId) | ||
.password(password) | ||
.build() | ||
); | ||
}).isInstanceOf(InvalidPasswordException.class); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
aics-api/src/testFixtures/java/auth/presentation/AuthControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package auth.presentation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Objects; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import kgu.developers.api.auth.presentation.request.LoginRequest; | ||
import kgu.developers.api.auth.presentation.response.TokenResponse; | ||
import kgu.developers.api.user.presentation.request.UserCreateRequest; | ||
import kgu.developers.domain.user.domain.Major; | ||
import mock.TestContainer; | ||
/* | ||
* 추후 Controller 테스트는 medium test로 전환할 예정입니다. | ||
* medium test는 Controller / Service / Repository 계층을 함께 테스트합니다. | ||
*/ | ||
public class AuthControllerTest { | ||
|
||
@Test | ||
@DisplayName("로그인 성공 후 200 상태 코드와 토큰을 정상적으로 발급받는다") | ||
public void login_Success() { | ||
// given | ||
TestContainer testContainer = new TestContainer(); | ||
testContainer.userService.createUser(UserCreateRequest.builder() | ||
.userId("202411345") | ||
.password("password0000") | ||
.name("김철수") | ||
.email("kim@kyonggi.ac.kr") | ||
.phone("010-0000-0000") | ||
.major(Major.CSE) | ||
.build()); | ||
|
||
// when | ||
ResponseEntity<TokenResponse> result = testContainer.authController.login(LoginRequest.builder() | ||
.userId("202411345") | ||
.password("password0000") | ||
.build()); | ||
|
||
// then | ||
assertThat(result.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(200)); | ||
assertThat(Objects.requireNonNull(result.getBody()).accessToken()).isNotNull(); | ||
assertThat(Objects.requireNonNull(result.getBody()).refreshToken()).isNotNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package mock; | ||
|
||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
import kgu.developers.api.auth.application.AuthService; | ||
import kgu.developers.api.auth.presentation.AuthController; | ||
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.UserRepository; | ||
|
||
public class TestContainer { | ||
public final UserRepository userRepository; | ||
public final UserService userService; | ||
public final AuthService authService; | ||
public final AuthController authController; | ||
|
||
public TestContainer() { | ||
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); | ||
this.userRepository = new FakeUserRepository(); | ||
this.userService = UserService.builder() | ||
.userRepository(this.userRepository) | ||
.bCryptPasswordEncoder(bCryptPasswordEncoder) | ||
.build(); | ||
this.authService = AuthService.builder() | ||
.userService(this.userService) | ||
.passwordEncoder(bCryptPasswordEncoder) | ||
.tokenProvider(TokenProvider.builder() | ||
.jwtProperties(new JwtProperties("testIssuer", "testSecretKey")) | ||
.build() | ||
) | ||
.build(); | ||
this.authController = AuthController.builder() | ||
.authService(this.authService) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.