Skip to content

Commit

Permalink
Merge pull request #423 from woowacourse-teams/refactor/#416
Browse files Browse the repository at this point in the history
[refactor] Test Fixture 적용 (#416)
  • Loading branch information
greeng00se authored Sep 29, 2023
2 parents 9f3472c + 649c88e commit 6ec3784
Show file tree
Hide file tree
Showing 49 changed files with 889 additions and 1,348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import dev.tripdraw.post.domain.PostRepository;
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import java.util.List;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;

@Component
public class PostCreateEventHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import static dev.tripdraw.test.fixture.AuthFixture.테스트_REFRESH_TOKEN_설정;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;

import dev.tripdraw.auth.exception.AuthException;
Expand All @@ -34,7 +34,7 @@ class AuthExtractorTest {
String accessToken = jwtTokenProvider.generateAccessToken("1");
HttpServletRequest request = mock(HttpServletRequest.class);
String encoded = "Bearer " + accessToken;
when(request.getHeader(AUTHORIZATION)).thenReturn(encoded);
given(request.getHeader(AUTHORIZATION)).willReturn(encoded);

// when
LoginUser loginUser = authExtractor.extract(request);
Expand All @@ -48,7 +48,7 @@ class AuthExtractorTest {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
String encoded = "Basic aGVsbG86d29ybGQ=";
when(request.getHeader(AUTHORIZATION)).thenReturn(encoded);
given(request.getHeader(AUTHORIZATION)).willReturn(encoded);

// expect
assertThatThrownBy(() -> authExtractor.extract(request))
Expand All @@ -60,7 +60,7 @@ class AuthExtractorTest {
void 요청_헤더에_인증_정보가_없을_경우_예외를_발생시킨다() {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(AUTHORIZATION)).thenReturn(null);
given(request.getHeader(AUTHORIZATION)).willReturn(null);

// expect
assertThatThrownBy(() -> authExtractor.extract(request))
Expand All @@ -73,7 +73,7 @@ class AuthExtractorTest {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
String notEncoded = "Bearer wrong.long.token";
when(request.getHeader(AUTHORIZATION)).thenReturn(notEncoded);
given(request.getHeader(AUTHORIZATION)).willReturn(notEncoded);

// expect
Assertions.assertThatThrownBy(() -> authExtractor.extract(request))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev.tripdraw.auth.application;

import static dev.tripdraw.common.auth.OauthType.KAKAO;
import static dev.tripdraw.test.fixture.MemberFixture.OAUTH_아이디;
import static dev.tripdraw.test.fixture.MemberFixture.닉네임이_없는_사용자;
import static dev.tripdraw.test.fixture.MemberFixture.사용자;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.BDDMockito.given;

Expand Down Expand Up @@ -52,8 +55,8 @@ void setUp() {
@Test
void 가입된_회원이_카카오_소셜_로그인하면_토큰이_포함된_응답을_반환한다() {
// given
memberRepository.save(new Member("통후추", "kakaoId", KAKAO));
OauthRequest oauthRequest = new OauthRequest(KAKAO, "oauth.kakao.token");
memberRepository.save(사용자());
OauthRequest oauthRequest = new OauthRequest(KAKAO, OAUTH_아이디);

// when
OauthResponse response = authFacadeService.login(oauthRequest);
Expand All @@ -68,7 +71,7 @@ void setUp() {
@Test
void 신규_회원이_로그인하면_회원을_저장하고_빈_토큰이_포함된_응답을_반환한다() {
// given
OauthRequest oauthRequest = new OauthRequest(KAKAO, "oauth.kakao.token");
OauthRequest oauthRequest = new OauthRequest(KAKAO, OAUTH_아이디);

// when
OauthResponse response = authFacadeService.login(oauthRequest);
Expand All @@ -83,10 +86,8 @@ void setUp() {
@Test
void 신규_회원의_닉네임을_등록하면_토큰이_포함된_응답을_반환한다() {
// given
Member member = Member.of("kakaoId", KAKAO);
memberRepository.save(member);

RegisterRequest registerRequest = new RegisterRequest("통후추", KAKAO, "oauth.kakao.token");
memberRepository.save(닉네임이_없는_사용자());
RegisterRequest registerRequest = new RegisterRequest("통후추", KAKAO, OAUTH_아이디);

// when
OauthResponse response = authFacadeService.register(registerRequest);
Expand All @@ -101,7 +102,7 @@ void setUp() {
@Test
void Refresh_토큰을_입력받아_Access_토큰과_Refresh_토큰을_재발급한다() {
// given
Member member = memberRepository.save(new Member("통후추", "kakaoId", KAKAO));
Member member = memberRepository.save(사용자());
String refreshToken = jwtTokenProvider.generateRefreshToken();
refreshTokenRepository.save(new RefreshToken(member.id(), refreshToken));
TokenRefreshRequest tokenRefreshRequest = new TokenRefreshRequest(refreshToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package dev.tripdraw.auth.application;

import static dev.tripdraw.common.auth.OauthType.KAKAO;
import static dev.tripdraw.member.exception.MemberExceptionType.DUPLICATE_NICKNAME;
import static dev.tripdraw.member.exception.MemberExceptionType.MEMBER_NOT_FOUND;
import static dev.tripdraw.test.fixture.AuthFixture.OAuth_정보;
import static dev.tripdraw.test.fixture.MemberFixture.사용자;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;

import dev.tripdraw.auth.dto.OauthInfo;
import dev.tripdraw.common.auth.OauthType;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.member.domain.MemberRepository;
Expand All @@ -37,16 +36,14 @@ class AuthServiceTest {
@Mock
private MemberRepository memberRepository;

private final OauthInfo oauthInfo = new OauthInfo("id", KAKAO);

@Test
void 신규_회원이_로그인하면_회원을_저장_후_빈_회원을_반환한다() {
// given
given(memberRepository.findByOauthIdAndOauthType(any(String.class), any(OauthType.class)))
.willReturn(Optional.empty());

// when
Optional<Member> member = authService.login(oauthInfo);
Optional<Member> member = authService.login(OAuth_정보());

// then
assertSoftly(softly -> {
Expand All @@ -58,12 +55,11 @@ class AuthServiceTest {
@Test
void 기존의_회원이_로그인하면_회원_정보를_반환한다() {
// given
Member 사용자 = 사용자();
given(memberRepository.findByOauthIdAndOauthType(any(String.class), any(OauthType.class)))
.willReturn(Optional.of(사용자));
.willReturn(Optional.of(사용자()));

// when
Optional<Member> member = authService.login(oauthInfo);
Optional<Member> member = authService.login(OAuth_정보());

// then
assertThat(member).isPresent();
Expand All @@ -76,7 +72,7 @@ class AuthServiceTest {
.willReturn(Optional.of(사용자()));

// when
Optional<Member> member = authService.register(oauthInfo, "통후추");
Optional<Member> member = authService.register(OAuth_정보(), "통후추");

// then
assertThat(member).isPresent();
Expand All @@ -89,7 +85,7 @@ class AuthServiceTest {
.willReturn(Optional.empty());

// expect
assertThatThrownBy(() -> authService.register(oauthInfo, "통후추"))
assertThatThrownBy(() -> authService.register(OAuth_정보(), "통후추"))
.isInstanceOf(MemberException.class)
.hasMessage(MEMBER_NOT_FOUND.message());
}
Expand All @@ -101,7 +97,7 @@ class AuthServiceTest {
.willReturn(true);

// expect
assertThatThrownBy(() -> authService.register(oauthInfo, "통후추"))
assertThatThrownBy(() -> authService.register(OAuth_정보(), "통후추"))
.isInstanceOf(MemberException.class)
.hasMessage(DUPLICATE_NICKNAME.message());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.tripdraw.auth.application;

import static dev.tripdraw.common.auth.OauthType.KAKAO;
import static dev.tripdraw.test.fixture.AuthFixture.OAUTH_TOKEN;
import static dev.tripdraw.test.fixture.MemberFixture.OAUTH_아이디;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.given;

import dev.tripdraw.auth.dto.OauthInfo;
import dev.tripdraw.auth.oauth.OauthClientProvider;
Expand All @@ -29,20 +31,16 @@ class OAuthServiceTest {

@BeforeEach
void setUp() {
when(oauthClientProvider.provide(KAKAO)).thenReturn(new TestKakaoApiClient());
given(oauthClientProvider.provide(KAKAO)).willReturn(new TestKakaoApiClient());
}

@Test
void Oauth_공통_정보를_반환한다() {
// given
String oauthToken = "oauth.kakao.token";
// expect
OauthInfo oauthInfo = oAuthService.request(KAKAO, OAUTH_TOKEN);

// when
OauthInfo oauthInfo = oAuthService.request(KAKAO, oauthToken);

// then
assertSoftly(softly -> {
softly.assertThat(oauthInfo.oauthId()).isEqualTo("kakaoId");
softly.assertThat(oauthInfo.oauthId()).isEqualTo(OAUTH_아이디);
softly.assertThat(oauthInfo.oauthType()).isEqualTo(KAKAO);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.BDDMockito.then;
import static org.mockito.BDDMockito.times;

import dev.tripdraw.auth.domain.RefreshToken;
import dev.tripdraw.auth.domain.RefreshTokenRepository;
Expand Down Expand Up @@ -54,7 +54,9 @@ class TokenGenerateServiceTest {
assertSoftly(softly -> {
softly.assertThat(result.accessToken()).isNotEmpty();
softly.assertThat(result.refreshToken()).isNotEmpty();
verify(refreshTokenRepository, times(1)).save(any(RefreshToken.class));
then(refreshTokenRepository)
.should(times(1))
.save(any(RefreshToken.class));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RefreshTokenRepositoryTest {
@Test
void 토큰을_입력받아_refreshToken_객체를_반환한다() {
// given
long memberId = 1L;
Long memberId = 1L;
RefreshToken refreshToken = new RefreshToken(memberId, "refreshToken");
refreshTokenRepository.save(refreshToken);

Expand All @@ -41,7 +41,7 @@ class RefreshTokenRepositoryTest {
@Test
void 사용자_아이디를_입력받아_해당되는_모든_RefreshToken을_제거한다() {
// given
long memberId = 1L;
Long memberId = 1L;
refreshTokenRepository.save(new RefreshToken(memberId, "refreshToken"));
refreshTokenRepository.save(new RefreshToken(memberId, "refreshToken"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package dev.tripdraw.auth.dto;

import static dev.tripdraw.common.auth.OauthType.KAKAO;
import static java.time.LocalDateTime.now;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import java.time.LocalDateTime;
import org.junit.jupiter.api.Test;

class KakaoInfoResponseTest {

@Test
void Oauth_공통_정보를_반환한다() {
// given
KakaoInfoResponse kakaoInfoResponse = new KakaoInfoResponse(
"kakaoId",
true,
LocalDateTime.now(),
null
);
KakaoInfoResponse kakaoInfoResponse = new KakaoInfoResponse("kakaoId", true, now(), null);

// when
OauthInfo oauthInfo = kakaoInfoResponse.toOauthInfo();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dev.tripdraw.auth.oauth;

import static dev.tripdraw.common.auth.OauthType.KAKAO;
import static java.time.LocalDateTime.now;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.given;

import dev.tripdraw.auth.dto.KakaoInfoResponse;
import dev.tripdraw.auth.dto.OauthInfo;
import dev.tripdraw.common.auth.OauthType;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand Down Expand Up @@ -45,14 +45,8 @@ void setUp() {
// given
String accessToken = "good.access.token";
KakaoApiClient kakaoApiClient = new KakaoApiClient("url", restTemplate);

when(restTemplate.postForObject(any(String.class), any(Object.class), any()))
.thenReturn(new KakaoInfoResponse(
"kakaoId",
true,
LocalDateTime.now(),
null
));
KakaoInfoResponse kakaoInfoResponse = new KakaoInfoResponse("kakaoId", true, now(), null);
given(restTemplate.postForObject(any(String.class), any(Object.class), any())).willReturn(kakaoInfoResponse);

// when
OauthInfo oauthInfo = kakaoApiClient.requestOauthInfo(accessToken);
Expand Down
Loading

0 comments on commit 6ec3784

Please sign in to comment.