Skip to content

Commit

Permalink
Test: OauthServiceTest에 리팩토링 사항 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaewon-pro committed Oct 25, 2024
1 parent 0d1b679 commit 38ef7a1
Showing 1 changed file with 22 additions and 49 deletions.
71 changes: 22 additions & 49 deletions src/test/java/com/dnd/runus/application/oauth/OauthServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.dnd.runus.auth.oidc.provider.OidcProviderRegistry;
import com.dnd.runus.auth.token.TokenProviderModule;
import com.dnd.runus.auth.token.dto.AuthTokenDto;
import com.dnd.runus.domain.member.*;
import com.dnd.runus.domain.member.Member;
import com.dnd.runus.domain.member.SocialProfile;
import com.dnd.runus.global.constant.MemberRole;
import com.dnd.runus.global.constant.SocialType;
import com.dnd.runus.global.exception.BusinessException;
import com.dnd.runus.global.exception.NotFoundException;
import com.dnd.runus.global.exception.type.ErrorType;
import com.dnd.runus.presentation.v1.oauth.dto.request.SignInRequest;
import com.dnd.runus.presentation.v1.oauth.dto.request.SignUpRequest;
Expand All @@ -27,7 +27,6 @@
import org.springframework.context.ApplicationEventPublisher;

import java.time.OffsetDateTime;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand All @@ -46,17 +45,11 @@ class OauthServiceTest {
private OidcProvider oidcProvider;

@Mock
private SocialProfileRepository socialProfileRepository;

@Mock
private MemberRepository memberRepository;
private SocialProfileService socialProfileService;

@Mock
private TokenProviderModule tokenProviderModule;

@Mock
private MemberLevelRepository memberLevelRepository;

@Mock
private ApplicationEventPublisher eventPublisher;

Expand Down Expand Up @@ -98,8 +91,8 @@ void socialProfile_exist_then_signIn_success() {
given(oidcProvider.getClaimsBy(idToken)).willReturn(claims);
given(claims.getSubject()).willReturn(oauthId);
given(claims.get("email")).willReturn(email);
given(socialProfileRepository.findBySocialTypeAndOauthId(socialType, oauthId))
.willReturn(Optional.of(new SocialProfile(1L, member, socialType, oauthId, email)));
given(socialProfileService.findOrThrow(socialType, oauthId, email))
.willReturn(new SocialProfile(1L, member, socialType, oauthId, email));
AuthTokenDto tokenDto = new AuthTokenDto("access-token", "refresh-token", "bearer");
given(tokenProviderModule.generate(String.valueOf(member.memberId())))
.willReturn(tokenDto);
Expand All @@ -123,8 +116,8 @@ void socialProfile_not_exist_then_signIn_fail() {
given(oidcProvider.getClaimsBy(idToken)).willReturn(claims);
given(claims.getSubject()).willReturn(oauthId);
given(claims.get("email")).willReturn(email);
given(socialProfileRepository.findBySocialTypeAndOauthId(socialType, oauthId))
.willReturn(Optional.empty());
given(socialProfileService.findOrThrow(socialType, oauthId, email))
.willThrow(new BusinessException(ErrorType.USER_NOT_FOUND));

// when, then
BusinessException exception = assertThrows(BusinessException.class, () -> oauthService.signIn(request));
Expand All @@ -141,8 +134,8 @@ void socialProfile_not_exist_then_signUp_success() {
given(claims.getSubject()).willReturn(oauthId);
given(claims.get("email")).willReturn(email);
SocialProfile socialProfile = new SocialProfile(1L, member, socialType, oauthId, email);
given(socialProfileRepository.findBySocialTypeAndOauthId(socialType, oauthId))
.willReturn(Optional.of(socialProfile));
given(socialProfileService.findOrCreate(socialType, oauthId, email, request.nickname()))
.willReturn(socialProfile);
AuthTokenDto tokenDto = new AuthTokenDto("access-token", "refresh-token", "bearer");
given(tokenProviderModule.generate(String.valueOf(member.memberId())))
.willReturn(tokenDto);
Expand All @@ -158,21 +151,21 @@ void socialProfile_not_exist_then_signUp_success() {
}

@Test
@DisplayName("sign-up 시 socialProfile이 없다면 socialProfileRepository.save() 호출")
@DisplayName("sign-up 시 socialProfile이 없다면 socialProfile을 생성")
void socialProfile_not_exist_then_signUp_save_social_profile() {
// given
SignUpRequest request = new SignUpRequest(socialType, idToken, "nickname");
given(oidcProviderRegistry.getOidcProviderBy(socialType)).willReturn(oidcProvider);
given(oidcProvider.getClaimsBy(idToken)).willReturn(claims);
given(claims.getSubject()).willReturn(oauthId);
given(claims.get("email")).willReturn(email);
given(socialProfileRepository.findBySocialTypeAndOauthId(socialType, oauthId))
.willReturn(Optional.empty());

Member newMember =
new Member(2L, MemberRole.USER, request.nickname(), OffsetDateTime.now(), OffsetDateTime.now());
SocialProfile socialProfile = new SocialProfile(1L, newMember, socialType, oauthId, email);
given(socialProfileRepository.save(any(SocialProfile.class))).willReturn(socialProfile);

given(socialProfileService.findOrCreate(socialType, oauthId, email, request.nickname()))
.willReturn(socialProfile);

AuthTokenDto tokenDto = new AuthTokenDto("access-token", "refresh-token", "bearer");
given(tokenProviderModule.generate(String.valueOf(newMember.memberId())))
Expand All @@ -187,7 +180,7 @@ void socialProfile_not_exist_then_signUp_save_social_profile() {
assertEquals(email, signResponse.email());
assertEquals(tokenDto.accessToken(), signResponse.accessToken());

then(socialProfileRepository).should().save(any(SocialProfile.class));
then(socialProfileService).should().findOrCreate(socialType, oauthId, email, request.nickname());
}

@DisplayName("소셜 로그인 연동 해제: 성공")
Expand All @@ -196,14 +189,12 @@ void revokeOauth_Success() throws InterruptedException {

// given
WithdrawRequest request = new WithdrawRequest(socialType, authorizationCode, idToken);
SocialProfile socialProfileMock = new SocialProfile(1L, member, request.socialType(), oauthId, email);

given(memberRepository.findById(member.memberId())).willReturn(Optional.of(member));
given(oidcProviderRegistry.getOidcProviderBy(request.socialType())).willReturn(oidcProvider);
given(oidcProvider.getClaimsBy(request.idToken())).willReturn(claims);
given(claims.getSubject()).willReturn(oauthId);
given(socialProfileRepository.findBySocialTypeAndOauthId(request.socialType(), oauthId))
.willReturn(Optional.of(socialProfileMock));
given(socialProfileService.isSocialMemberExists(request.socialType(), oauthId, member.memberId()))
.willReturn(true);

CountDownLatch latch = new CountDownLatch(2);

Expand All @@ -228,28 +219,17 @@ void revokeOauth_Success() throws InterruptedException {
assertTrue(completed);
}

@DisplayName("소셜 로그인 연동 해제: member_id가 없을 경우 NotFoundException을 발생한다.")
@Test
void revokeOauth_NotFound_MemberID() {
// given
WithdrawRequest request = new WithdrawRequest(socialType, authorizationCode, idToken);
given(memberRepository.findById(member.memberId())).willReturn(Optional.empty());

// when, then
assertThrows(NotFoundException.class, () -> oauthService.revokeOauth(member.memberId(), request));
}

@DisplayName("소셜 로그인 연동 해제: oauthId와 socialType에 해당하는 socialProfile 없을 경우 AuthException을 발생한다.")
@Test
void revokeOauth_NotFound_SocialProfile() {
// given
WithdrawRequest request = new WithdrawRequest(socialType, authorizationCode, idToken);
given(memberRepository.findById(member.memberId())).willReturn(Optional.of(member));

given(oidcProviderRegistry.getOidcProviderBy(request.socialType())).willReturn(oidcProvider);
given(oidcProvider.getClaimsBy(request.idToken())).willReturn(claims);
given(claims.getSubject()).willReturn(oauthId);
given(socialProfileRepository.findBySocialTypeAndOauthId(request.socialType(), oauthId))
.willReturn(Optional.empty());
given(socialProfileService.isSocialMemberExists(request.socialType(), oauthId, member.memberId()))
.willReturn(false);

// when, then
assertThrows(AuthException.class, () -> oauthService.revokeOauth(member.memberId(), request));
Expand All @@ -260,19 +240,12 @@ void revokeOauth_NotFound_SocialProfile() {
void revokeOauth_MissMatch_socialProfileAndMemberId() {
// given
WithdrawRequest request = new WithdrawRequest(socialType, authorizationCode, idToken);
SocialProfile socialProfileMock = new SocialProfile(
1L,
new Member(2L, MemberRole.USER, "nickname", OffsetDateTime.now(), OffsetDateTime.now()),
request.socialType(),
oauthId,
email);

given(memberRepository.findById(member.memberId())).willReturn(Optional.of(member));

given(oidcProviderRegistry.getOidcProviderBy(request.socialType())).willReturn(oidcProvider);
given(oidcProvider.getClaimsBy(request.idToken())).willReturn(claims);
given(claims.getSubject()).willReturn(oauthId);
given(socialProfileRepository.findBySocialTypeAndOauthId(request.socialType(), oauthId))
.willReturn(Optional.of(socialProfileMock));
given(socialProfileService.isSocialMemberExists(request.socialType(), oauthId, member.memberId()))
.willReturn(false);

// when, then
assertThrows(AuthException.class, () -> oauthService.revokeOauth(member.memberId(), request));
Expand Down

0 comments on commit 38ef7a1

Please sign in to comment.