From 72ad48e3f0fe9a302289993097b81639b43f2a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9A=B0=EC=9E=AC?= Date: Fri, 26 Nov 2021 11:36:27 +0900 Subject: [PATCH] =?UTF-8?q?[#54]=20test:=20AuthService,=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EC=84=B1=EA=B3=B5/=EC=8B=A4=ED=8C=A8=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../acceptance/FollowAcceptanceTest.java | 1 + .../acceptance/UserAcceptanceTest.java | 1 + .../auth/application/AuthServiceTest.java | 57 +++++++++ .../application/FollowCommandServiceTest.java | 21 +++- .../application/FollowQueryServiceTest.java | 109 +++++++++++++++++- .../follow/domain/FollowRepositoryTest.java | 6 +- .../domain/follow/domain/FollowTest.java | 6 +- .../domain/user/domain/persist/UserTest.java | 3 +- 8 files changed, 191 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/study/realworld/domain/auth/application/AuthServiceTest.java diff --git a/src/test/java/com/study/realworld/acceptance/FollowAcceptanceTest.java b/src/test/java/com/study/realworld/acceptance/FollowAcceptanceTest.java index da0ea659..7494f099 100644 --- a/src/test/java/com/study/realworld/acceptance/FollowAcceptanceTest.java +++ b/src/test/java/com/study/realworld/acceptance/FollowAcceptanceTest.java @@ -74,6 +74,7 @@ public void setUp() { @Test void 특정_사람을_언팔로우한다() { final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail()); + 팔로우_요청(loginResponse.accessToken(), user2.userName().userName()); final ExtractableResponse response = 언팔로우_요청( loginResponse.accessToken(), user2.userName().userName()); diff --git a/src/test/java/com/study/realworld/acceptance/UserAcceptanceTest.java b/src/test/java/com/study/realworld/acceptance/UserAcceptanceTest.java index 62c7e065..79364cdd 100644 --- a/src/test/java/com/study/realworld/acceptance/UserAcceptanceTest.java +++ b/src/test/java/com/study/realworld/acceptance/UserAcceptanceTest.java @@ -76,6 +76,7 @@ class UserAcceptanceTest extends AcceptanceTest { assertAll( () -> assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value()), + () -> assertThat(userUpdateResponse.userName()).isEqualTo(UserName.from(userEmail.split("@")[0])), () -> assertThat(userUpdateResponse.userEmail()).isEqualTo(OTHER_USER_EMAIL), () -> assertThat(userUpdateResponse.userBio()).isEqualTo(OTHER_USER_BIO), () -> assertThat(userUpdateResponse.userImage()).isEqualTo(OTHER_USER_IMAGE) diff --git a/src/test/java/com/study/realworld/domain/auth/application/AuthServiceTest.java b/src/test/java/com/study/realworld/domain/auth/application/AuthServiceTest.java new file mode 100644 index 00000000..a67f503f --- /dev/null +++ b/src/test/java/com/study/realworld/domain/auth/application/AuthServiceTest.java @@ -0,0 +1,57 @@ +package com.study.realworld.domain.auth.application; + +import com.study.realworld.domain.user.application.UserQueryService; +import com.study.realworld.domain.user.domain.persist.User; +import com.study.realworld.domain.user.domain.vo.util.TestPasswordEncoder; +import com.study.realworld.domain.user.error.exception.PasswordMissMatchException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.crypto.password.PasswordEncoder; + +import static com.study.realworld.domain.user.domain.persist.UserTest.testDefaultUser; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willReturn; + +@DisplayName("인증 서비스(AuthService)") +@ExtendWith(MockitoExtension.class) +class AuthServiceTest { + + @Mock + private UserQueryService userQueryService; + + private PasswordEncoder passwordEncoder; + + private AuthService authService; + + @BeforeEach + void setUp() { + passwordEncoder = TestPasswordEncoder.initialize(); + authService = new AuthService(userQueryService, passwordEncoder); + } + + @Test + void 로그인_성공() { + final User user = testDefaultUser().encode(passwordEncoder); + willReturn(user).given(userQueryService).findByMemberEmail(any()); + + final User login = authService.login(user.userEmail(), testDefaultUser().userPassword()); + assertThat(login).isEqualTo(user); + } + + @Test + void 로그인_실패() { + final User user = testDefaultUser(); + willReturn(user).given(userQueryService).findByMemberEmail(any()); + + assertThatThrownBy(() -> authService.login(user.userEmail(), user.userPassword())) + .isExactlyInstanceOf(PasswordMissMatchException.class) + .hasMessage("패스워드가 일치하지 않습니다."); + } +} \ No newline at end of file diff --git a/src/test/java/com/study/realworld/domain/follow/application/FollowCommandServiceTest.java b/src/test/java/com/study/realworld/domain/follow/application/FollowCommandServiceTest.java index 25f3b342..f4947619 100644 --- a/src/test/java/com/study/realworld/domain/follow/application/FollowCommandServiceTest.java +++ b/src/test/java/com/study/realworld/domain/follow/application/FollowCommandServiceTest.java @@ -4,6 +4,7 @@ import com.study.realworld.domain.follow.domain.FollowRepository; import com.study.realworld.domain.follow.dto.FollowResponse; import com.study.realworld.domain.follow.dto.UnFollowResponse; +import com.study.realworld.domain.follow.error.exception.DuplicatedFollowException; import com.study.realworld.domain.user.application.UserQueryService; import com.study.realworld.domain.user.domain.persist.User; import com.study.realworld.domain.user.domain.vo.UserBio; @@ -16,9 +17,10 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import static com.study.realworld.domain.follow.domain.FollowTest.testFollower; +import static com.study.realworld.domain.follow.domain.FollowTest.testFollow; import static com.study.realworld.domain.user.domain.persist.UserTest.testUser; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertAll; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.willReturn; @@ -43,7 +45,7 @@ class FollowCommandServiceTest { void 팔로워_아이덴티티와_팔로위_이름을_입력하면_팔로우_할_수_있다() { final User follower = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); final User followee = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); - final Follow follow = testFollower(followee, follower); + final Follow follow = testFollow(followee, follower); willReturn(follower).given(userQueryService).findById(any()); willReturn(followee).given(userQueryService).findByUserName(any()); willReturn(false).given(followQueryService).existsByFolloweeAndFollower(any(), any()); @@ -58,11 +60,24 @@ class FollowCommandServiceTest { ); } + @Test + void 이미_팔로우_정보가_있다면_팔로우_할_수_없다() { + final User follower = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); + final User followee = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); + willReturn(follower).given(userQueryService).findById(any()); + willReturn(followee).given(userQueryService).findByUserName(any()); + willReturn(true).given(followQueryService).existsByFolloweeAndFollower(any(), any()); + + assertThatThrownBy(() -> followCommandService.follow(1L, UserName.from("user2"))) + .isExactlyInstanceOf(DuplicatedFollowException.class) + .hasMessage("팔로우 정보가 이미 있습니다"); + } + @Test void 팔로워_아이덴티티와_팔로위_이름을_입력하면_언팔로우_할_수_있다() { final User follower = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); final User followee = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); - final Follow follow = testFollower(followee, follower); + final Follow follow = testFollow(followee, follower); willReturn(follower).given(userQueryService).findById(any()); willReturn(followee).given(userQueryService).findByUserName(any()); willReturn(follow).given(followQueryService).findByFolloweeAndFollower(any(), any()); diff --git a/src/test/java/com/study/realworld/domain/follow/application/FollowQueryServiceTest.java b/src/test/java/com/study/realworld/domain/follow/application/FollowQueryServiceTest.java index 8905c15a..e3d66554 100644 --- a/src/test/java/com/study/realworld/domain/follow/application/FollowQueryServiceTest.java +++ b/src/test/java/com/study/realworld/domain/follow/application/FollowQueryServiceTest.java @@ -1,7 +1,112 @@ package com.study.realworld.domain.follow.application; -import static org.junit.jupiter.api.Assertions.*; +import com.study.realworld.domain.follow.domain.Follow; +import com.study.realworld.domain.follow.domain.FollowQueryDSLRepository; +import com.study.realworld.domain.follow.domain.FollowRepository; +import com.study.realworld.domain.follow.domain.FollowTest; +import com.study.realworld.domain.follow.dto.ProfileResponse; +import com.study.realworld.domain.follow.error.exception.FollowNotFoundException; +import com.study.realworld.domain.user.application.UserQueryService; +import com.study.realworld.domain.user.domain.persist.User; +import com.study.realworld.domain.user.domain.vo.UserBio; +import com.study.realworld.domain.user.domain.vo.UserImage; +import com.study.realworld.domain.user.domain.vo.UserName; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Optional; + +import static com.study.realworld.domain.user.domain.persist.UserTest.testUser; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.willReturn; + +@DisplayName("팔로우 쿼리 서비스(FollowQueryService)") +@ExtendWith(MockitoExtension.class) class FollowQueryServiceTest { -} \ No newline at end of file + @Mock + private UserQueryService userQueryService; + + @Mock + private FollowQueryDSLRepository followQueryDSLRepository; + + @Mock + private FollowRepository followRepository; + + @InjectMocks + private FollowQueryService followQueryService; + + @Test + void 특정_유저_정보_및_팔로우_여부_조회() { + final User me = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); + final User target = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); + willReturn(me).given(userQueryService).findById(any()); + willReturn(target).given(userQueryService).findByUserName(any()); + willReturn(false).given(followRepository).existsByFolloweeAndFollower(any(), any()); + + final ProfileResponse profileResponse = followQueryService.profile(1L, UserName.from("user2")); + assertAll( + () -> assertThat(profileResponse.userName()).isEqualTo(UserName.from("user2")), + () -> assertThat(profileResponse.userBio()).isEqualTo(UserBio.from("bio2")), + () -> assertThat(profileResponse.userImage()).isEqualTo(UserImage.from("image2")), + () -> assertThat(profileResponse.isFollowing()).isFalse() + ); + } + + @Test + void 팔로우_여부를_반환한다() { + final User user1 = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); + final User user2 = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); + willReturn(false).given(followRepository).existsByFolloweeAndFollower(any(), any()); + + final boolean actual = followQueryService.existsByFolloweeAndFollower(user1, user2); + assertThat(actual).isFalse(); + } + + @Test + void 팔로우_정보를_찾는다() { + final User followee = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); + final User follower = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); + final Follow follow = FollowTest.testFollow(follower, followee); + willReturn(Optional.of(follow)).given(followRepository).findByFolloweeAndFollower(any(), any()); + + final Follow findFollow = followQueryService.findByFolloweeAndFollower(followee, follower); + assertThat(follow).isEqualTo(findFollow); + } + + @Test + void 팔로우_정보가_없다면_예외를_발생시킨다() { + final User followee = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); + final User follower = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); + willReturn(Optional.empty()).given(followRepository).findByFolloweeAndFollower(any(), any()); + + assertThatThrownBy(() -> followQueryService.findByFolloweeAndFollower(followee, follower)) + .isExactlyInstanceOf(FollowNotFoundException.class) + .hasMessage("팔로우 정보를 찾을 수 없습니다"); + } + + @Test + void QueryDSL_로_특정_유저_정보_및_팔로우_여부_조회() { + final User me = testUser("user1@gmail.com", "user1", "password1", "bio1", "image1"); + final User target = testUser("user2@gmail.com", "user2", "password2", "bio2", "image2"); + final ProfileResponse profileResponse = new ProfileResponse(target, false); + willReturn(me).given(userQueryService).findById(any()); + willReturn(target).given(userQueryService).findByUserName(any()); + willReturn(profileResponse).given(followQueryDSLRepository).userInfoAndFollowable(any(), any()); + + final ProfileResponse findProfileResponse = followQueryService.profile2(1L, UserName.from("user2")); + assertAll( + () -> assertThat(findProfileResponse.userName()).isEqualTo(UserName.from("user2")), + () -> assertThat(findProfileResponse.userBio()).isEqualTo(UserBio.from("bio2")), + () -> assertThat(findProfileResponse.userImage()).isEqualTo(UserImage.from("image2")), + () -> assertThat(findProfileResponse.isFollowing()).isFalse() + ); + } +} diff --git a/src/test/java/com/study/realworld/domain/follow/domain/FollowRepositoryTest.java b/src/test/java/com/study/realworld/domain/follow/domain/FollowRepositoryTest.java index 3bcf1580..52d54030 100644 --- a/src/test/java/com/study/realworld/domain/follow/domain/FollowRepositoryTest.java +++ b/src/test/java/com/study/realworld/domain/follow/domain/FollowRepositoryTest.java @@ -7,7 +7,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; -import static com.study.realworld.domain.follow.domain.FollowTest.testFollower; +import static com.study.realworld.domain.follow.domain.FollowTest.testFollow; import static com.study.realworld.domain.user.domain.persist.UserTest.testUser; import static com.study.realworld.domain.user.domain.vo.util.UserVOFixture.*; import static org.assertj.core.api.Assertions.assertThat; @@ -33,7 +33,7 @@ void afterEach() { testEntityManager.persist(follower); testEntityManager.persist(followee); - testEntityManager.persist(testFollower(followee, follower)); + testEntityManager.persist(testFollow(followee, follower)); assertThat(followRepository.existsByFolloweeAndFollower(followee, follower)).isTrue(); } @@ -56,7 +56,7 @@ void afterEach() { testEntityManager.persist(follower); testEntityManager.persist(followee); - testEntityManager.persist(testFollower(followee, follower)); + testEntityManager.persist(testFollow(followee, follower)); final Follow follow = followRepository.findByFolloweeAndFollower(followee, follower).get(); assertAll( diff --git a/src/test/java/com/study/realworld/domain/follow/domain/FollowTest.java b/src/test/java/com/study/realworld/domain/follow/domain/FollowTest.java index 6854c485..6d27931b 100644 --- a/src/test/java/com/study/realworld/domain/follow/domain/FollowTest.java +++ b/src/test/java/com/study/realworld/domain/follow/domain/FollowTest.java @@ -17,7 +17,7 @@ public class FollowTest { void 두명의_유저들을_토대로_객체를_생성할_수_있다() { final User user = testUser(USER_EMAIL, USER_NAME, USER_PASSWORD, USER_BIO, USER_IMAGE); final User other = testUser(OTHER_USER_EMAIL, OTHER_USER_NAME, OTHER_USER_PASSWORD, OTHER_USER_BIO, OTHER_USER_IMAGE); - final Follow follow = testFollower(other, user); + final Follow follow = testFollow(other, user); assertAll( () -> assertThat(follow).isNotNull(), @@ -29,7 +29,7 @@ public class FollowTest { void 팔로위_팔로워_정보를_반환한다() { final User follower = testUser(USER_EMAIL, USER_NAME, USER_PASSWORD, USER_BIO, USER_IMAGE); final User followee = testUser(OTHER_USER_EMAIL, OTHER_USER_NAME, OTHER_USER_PASSWORD, OTHER_USER_BIO, OTHER_USER_IMAGE); - final Follow follow = testFollower(followee, follower); + final Follow follow = testFollow(followee, follower); ReflectionTestUtils.setField(follow, "id", 1L); assertAll( @@ -39,7 +39,7 @@ public class FollowTest { ); } - public static Follow testFollower(final User other, final User user) { + public static Follow testFollow(final User other, final User user) { return Follow.builder() .followee(other) .follower(user) diff --git a/src/test/java/com/study/realworld/domain/user/domain/persist/UserTest.java b/src/test/java/com/study/realworld/domain/user/domain/persist/UserTest.java index f369f245..c1750f42 100644 --- a/src/test/java/com/study/realworld/domain/user/domain/persist/UserTest.java +++ b/src/test/java/com/study/realworld/domain/user/domain/persist/UserTest.java @@ -32,8 +32,7 @@ public class UserTest { @Test void 패스워드를_인코딩_할_수_있다() { final PasswordEncoder passwordEncoder = TestPasswordEncoder.initialize(); - final User user = testUser(USER_EMAIL, USER_NAME, USER_PASSWORD, USER_BIO, USER_IMAGE) - .encode(passwordEncoder); + final User user = testUser(USER_EMAIL, USER_NAME, USER_PASSWORD, USER_BIO, USER_IMAGE).encode(passwordEncoder); assertAll( () -> assertThat(user.userPassword()).isNotEqualTo(USER_PASSWORD),