Skip to content

Commit

Permalink
[#54] test: AuthService, 로그인 성공/실패 단위 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
kwj1270 committed Nov 26, 2021
1 parent 8a73a1b commit 72ad48e
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void setUp() {
@Test
void 특정_사람을_언팔로우한다() {
final Login.Response loginResponse = 로그인_되어있음(user1.userEmail().userEmail());
팔로우_요청(loginResponse.accessToken(), user2.userName().userName());
final ExtractableResponse<Response> response = 언팔로우_요청(
loginResponse.accessToken(),
user2.userName().userName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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("패스워드가 일치하지 않습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}
@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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 72ad48e

Please sign in to comment.