Skip to content

Commit

Permalink
test: 누락된 테스트 케이스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
apptie committed Sep 29, 2023
1 parent fbb1fd7 commit 84b6e99
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.ddang.ddang.authentication.infrastructure.oauth2.Oauth2Type;
import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.image.application.exception.ImageNotFoundException;
import java.util.Optional;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
Expand All @@ -31,8 +32,8 @@ class AuthenticationServiceTest extends AuthenticationServiceFixture {

// when & then
assertThatThrownBy(() -> authenticationService.login(
지원하지_않는_소셜_로그인,
유효한_소셜_로그인_토큰,
지원하지_않는_소셜_로그인,
유효한_소셜_로그인_토큰,
디바이스_토큰)
).isInstanceOf(UnsupportedSocialLoginException.class)
.hasMessage("지원하는 소셜 로그인 기능이 아닙니다.");
Expand Down Expand Up @@ -62,15 +63,15 @@ class AuthenticationServiceTest extends AuthenticationServiceFixture {

// when
final TokenDto actual = authenticationService.login(
지원하는_소셜_로그인,
유효한_소셜_로그인_토큰,
지원하는_소셜_로그인,
유효한_소셜_로그인_토큰,
디바이스_토큰
);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual.accessToken()).isNotEmpty();
softAssertions.assertThat(actual.refreshToken()).isNotEmpty();
softAssertions.assertThat(actual.accessToken()).isNotEmpty().contains("Bearer ");
softAssertions.assertThat(actual.refreshToken()).isNotEmpty().contains("Bearer ");
});
}

Expand All @@ -81,7 +82,7 @@ class AuthenticationServiceTest extends AuthenticationServiceFixture {
given(소셜_회원_정보_제공자.findUserInformation(anyString())).willReturn(가입하지_않은_사용자_회원_정보);

// when & then
assertThatThrownBy(() -> authenticationService.login(지원하는_소셜_로그인, 이미지가_없는_사용자_액세스_토큰, 디바이스_토큰))
assertThatThrownBy(() -> profileImageAuthenticationService.login(지원하는_소셜_로그인, 이미지가_없는_사용자_액세스_토큰, 디바이스_토큰))
.isInstanceOf(ImageNotFoundException.class)
.hasMessage("기본 이미지를 찾을 수 없습니다.");
}
Expand Down Expand Up @@ -166,6 +167,7 @@ class AuthenticationServiceTest extends AuthenticationServiceFixture {

@Test
void 가입한_회원이_탈퇴하는_경우_정상처리한다() throws InvalidWithdrawalException {
// given
given(소셜_회원_정보_제공자_묶음.findProvider(지원하는_소셜_로그인)).willReturn(소셜_회원_정보_제공자);
given(소셜_회원_정보_제공자.findUserInformation(anyString())).willReturn(사용자_회원_정보);

Expand Down Expand Up @@ -196,9 +198,45 @@ class AuthenticationServiceTest extends AuthenticationServiceFixture {
.willThrow(new InvalidTokenException("401 Unauthorized"));

// when & then
assertThatThrownBy(
() -> authenticationService.withdrawal(Oauth2Type.KAKAO, 존재하지_않는_사용자_액세스_토큰, 유효한_리프레시_토큰))
assertThatThrownBy(() -> authenticationService.withdrawal(Oauth2Type.KAKAO, 존재하지_않는_사용자_액세스_토큰, 유효한_리프레시_토큰))
.isInstanceOf(InvalidWithdrawalException.class)
.hasMessage("탈퇴에 대한 권한 없습니다.");
}

@Test
void 탈퇴할_때_유효한_토큰이_아닌_경우_예외가_발생한다() {
// when & then
assertThatThrownBy(() -> authenticationService.withdrawal(Oauth2Type.KAKAO, 유효하지_않은_액세스_토큰, 유효한_리프레시_토큰))
.isInstanceOf(InvalidTokenException.class)
.hasMessage("유효한 토큰이 아닙니다.");
}

@Test
void 로그인할_때_이미지가_없는_사용자라면_예외가_발생한다() {
// given
given(소셜_회원_정보_제공자_묶음.findProvider(지원하는_소셜_로그인)).willReturn(소셜_회원_정보_제공자);
given(소셜_회원_정보_제공자.findUserInformation(anyString())).willReturn(가입하지_않은_사용자_회원_정보);
given(프로필_이미지_저장소.findByStoreName(anyString())).willReturn(Optional.empty());

// when & then
assertThatThrownBy(() -> profileImageAuthenticationService.login(Oauth2Type.KAKAO, 유효한_액세스_토큰, 디바이스_토큰))
.isInstanceOf(ImageNotFoundException.class)
.hasMessage("기본 이미지를 찾을 수 없습니다.");
}

@Test
void 로그인할_때_가입하지_않은_사용자라면_회원가입을_진행한다() {
// given
given(소셜_회원_정보_제공자_묶음.findProvider(지원하는_소셜_로그인)).willReturn(소셜_회원_정보_제공자);
given(소셜_회원_정보_제공자.findUserInformation(anyString())).willReturn(가입하지_않은_사용자_회원_정보);

// when
final TokenDto actual = authenticationService.login(Oauth2Type.KAKAO, 유효한_소셜_로그인_토큰, 디바이스_토큰);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual.accessToken()).isNotEmpty().contains("Bearer ");
softAssertions.assertThat(actual.refreshToken()).isNotEmpty().contains("Bearer ");
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ddang.ddang.authentication.application.fixture;

import static org.mockito.Mockito.mock;

import com.ddang.ddang.authentication.application.AuthenticationService;
import com.ddang.ddang.authentication.application.BlackListTokenService;
import com.ddang.ddang.authentication.domain.Oauth2UserInformationProviderComposite;
Expand All @@ -9,7 +11,6 @@
import com.ddang.ddang.authentication.domain.dto.UserInformationDto;
import com.ddang.ddang.authentication.infrastructure.oauth2.OAuth2UserInformationProvider;
import com.ddang.ddang.authentication.infrastructure.oauth2.Oauth2Type;
import com.ddang.ddang.configuration.IsolateDatabase;
import com.ddang.ddang.device.application.DeviceTokenService;
import com.ddang.ddang.device.domain.DeviceToken;
import com.ddang.ddang.device.infrastructure.persistence.JpaDeviceTokenRepository;
Expand All @@ -35,6 +36,8 @@ public class AuthenticationServiceFixture {
protected OAuth2UserInformationProvider 소셜_회원_정보_제공자;

protected AuthenticationService authenticationService;
protected AuthenticationService profileImageAuthenticationService;
protected JpaProfileImageRepository 프로필_이미지_저장소 = mock(JpaProfileImageRepository.class);

protected Oauth2Type 지원하는_소셜_로그인 = Oauth2Type.KAKAO;
protected Oauth2Type 지원하지_않는_소셜_로그인 = Oauth2Type.KAKAO;
Expand All @@ -47,12 +50,14 @@ public class AuthenticationServiceFixture {

protected User 사용자;
protected User 탈퇴한_사용자;
protected User 이미지가_없는_사용자;

protected UserInformationDto 사용자_회원_정보 = new UserInformationDto(12345L);
protected UserInformationDto 탈퇴한_사용자_회원_정보 = new UserInformationDto(54321L);
protected UserInformationDto 가입하지_않은_사용자_회원_정보 = new UserInformationDto(-99999L);

protected String 유효한_액세스_토큰;
protected String 유효하지_않은_액세스_토큰 = "Bearer invalidAccessToken";
protected String 탈퇴한_사용자_액세스_토큰;
protected String 이미지가_없는_사용자_액세스_토큰;
protected String 존재하지_않는_사용자_액세스_토큰;
Expand Down Expand Up @@ -94,6 +99,19 @@ void setUp() {
deviceTokenRepository
);

profileImageAuthenticationService = new AuthenticationService(
deviceTokenService,
소셜_회원_정보_제공자_묶음,
userRepository,
프로필_이미지_저장소,
tokenEncoder,
tokenDecoder,
blackListTokenService,
deviceTokenRepository
);

profileImageRepository.save(new ProfileImage("default_profile_image.png", "default_profile_image.png"));

사용자 = User.builder()
.name("kakao12345")
.profileImage(new ProfileImage("upload.png", "store.png"))
Expand All @@ -108,6 +126,13 @@ void setUp() {
.oauthId("12346")
.build();

이미지가_없는_사용자 = User.builder()
.name("kakao12347")
.profileImage(null)
.reliability(0.0d)
.oauthId("12347")
.build();

userRepository.save(사용자);
userRepository.save(탈퇴한_사용자);

Expand Down

0 comments on commit 84b6e99

Please sign in to comment.