Skip to content

Commit

Permalink
test: 애플 로그인 흐름 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingyum-Kim committed Oct 19, 2024
1 parent 8c47a3d commit 292c373
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ private String handleNewUser(String user, String identifier) {
return accessTokenProvider.provide(joinedMember);
}

private Member join(String identifier, String user) {
String name = userInfoProvider.getName(user);
return joinManager.join(name, OauthType.APPLE, identifier);
}

private String handleExistingUser(String identifier) {
Member member = memberFinder.getByIdentifier(identifier);
if (member != null) {
Expand All @@ -43,9 +48,4 @@ private String handleExistingUser(String identifier) {
}
throw new AuthException(HttpStatus.BAD_REQUEST, AuthErrorMessage.CANNOT_FIND_APPLE_MEMBER);
}

public Member join(String identifier, String user) {
String name = userInfoProvider.getName(user);
return joinManager.join(name, OauthType.APPLE, identifier);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Objects;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
Expand All @@ -14,6 +15,7 @@ public class LoginDetail {
@Enumerated(EnumType.STRING)
private OauthType oauthType;

@Column(unique = true)
private String identifier;

protected LoginDetail() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package mouda.backend.auth.business;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import mouda.backend.member.implement.MemberFinder;
import mouda.backend.auth.implement.AppleUserInfoProvider;
import mouda.backend.common.fixture.MemberFixture;
import mouda.backend.member.domain.Member;
import mouda.backend.member.domain.MemberStatus;
import mouda.backend.member.infrastructure.MemberRepository;

@SpringBootTest
Expand All @@ -13,44 +24,65 @@ class AppleAuthServiceTest {
@Autowired
private AppleAuthService appleAuthService;

@MockBean
private AppleOauthManager appleOauthManager;
@Autowired
private MemberRepository memberRepository;

@MockBean
private MemberFinder memberFinder;
private AppleUserInfoProvider userInfoProvider;

@Autowired
private MemberRepository memberRepository;
private final String identifier = "123";
private final String name = "김민겸";

// @DisplayName("애플 로그인 요청을 보내면 access token을 반환한다.")
// @Test
// @Disabled("실제 Resource Server에게 요청을 보내는 테스트이다. 프론트 서버를 켜서 코드르 발급 받아 필요할 때만 테스트한다.")
// void oauthLogin() {
// String code = "";
// AppleOauthRequest oauthRequest = new AppleOauthRequest(1L, "nonce");
//
// LoginResponse loginResponse = appleAuthService.oauthLogin(oauthRequest);
//
// assertThat(loginResponse).isNotNull();
// }

/*
@DisplayName("사용자 전환을 시도하면 새로운 회원을 추가하지 않고 기존 회원의 정보를 수정한다.")
@BeforeEach
void setUp() {
when(userInfoProvider.getName(anyString())).thenReturn(name);
when(userInfoProvider.getIdentifier(anyString())).thenReturn(identifier);
}

@DisplayName("최초 로그인인 경우 회원 가입과 로그인을 진행한다.")
@Test
void oauthLoginConvertingMember() {
String appleSocialLoginId = "appleSocialLoginId";
when(appleOauthManager.getSocialLoginId(anyString())).thenReturn(appleSocialLoginId);
Member anna = memberRepository.save(MemberFixture.getAnna());
when(memberFinder.findBySocialId(anyString())).thenReturn(anna);
Long kakaoMemberId = anna.getId();
LoginResponse loginResponse = appleAuthService.oauthLogin(
new AppleOauthRequest(kakaoMemberId, "nonce"));
assertThat(loginResponse).isNotNull();
Optional<Member> memberOptional = memberRepository.findById(kakaoMemberId);
assertThat(memberOptional.isPresent()).isTrue();
LoginDetail expected = new LoginDetail(OauthType.APPLE, appleSocialLoginId);
assertThat(memberOptional.get().getLoginDetail()).isEqualTo(expected);
}*/
void joinAndLogin() {
// when
String accessToken = appleAuthService.login("idToken", "user");

// then
assertThat(accessToken).isNotNull();
Optional<Member> member = memberRepository.findByLoginDetail_Identifier(identifier);
assertThat(member.isPresent()).isTrue();
assertThat(member.get().getName()).isEqualTo(name);
}

@DisplayName("회원가입 이력이 있는 경우 회원가입 없이 로그인을 진행한다.")
@Test
void login() {
// given
Member anna = MemberFixture.getAnna(identifier);
memberRepository.save(anna);

// when
String accessToken = appleAuthService.login("idToken", null);

// then
assertThat(accessToken).isNotNull();
Optional<Member> member = memberRepository.findByLoginDetail_Identifier(identifier);
assertThat(member.isPresent()).isTrue();
}

@DisplayName("회원 탈퇴 이력이 있는 경우 재가입 후 로그인을 진행한다.")
@Test
void rejoinAndLogin() {
// given
Member anna = MemberFixture.getAnna(identifier);
anna.withdraw();
memberRepository.save(anna);

// when
String accessToken = appleAuthService.login("idToken", null);

// then
assertThat(accessToken).isNotNull();
Optional<Member> member = memberRepository.findByLoginDetail_Identifier(identifier);
assertThat(member.isPresent()).isTrue();
assertThat(member.get().getMemberStatus()).isEqualTo(MemberStatus.ACTIVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class OauthManagerTest {
class AppleUserInfoProviderTest {

@Autowired
private AppleOauthManager oauthManager;
private AppleUserInfoProvider userInfoProvider;

@DisplayName("Resource Server받아온 Identity Token으로 사용자 정보를 추출한다.")
@Test
@Disabled("실제 Resource Server에게 요청을 보내는 테스트이다. 프론트 서버를 켜서 코드르 발급 받아 필요할 때만 테스트한다.")
void getUserInfo() {
String code = "";

String identifier = oauthManager.getIdentifier(code);
String identifier = userInfoProvider.getIdentifier(code);
assertThat(identifier).isNotNull();
}
}

This file was deleted.

0 comments on commit 292c373

Please sign in to comment.