Skip to content

Commit

Permalink
refactor: LoginService 와 MemberFCMService 를 분리한다
Browse files Browse the repository at this point in the history
  • Loading branch information
BGuga committed Sep 26, 2023
1 parent 1c77fc9 commit 5793da7
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.festago.auth.domain.AuthPayload;
import com.festago.auth.domain.Role;
import com.festago.auth.domain.SocialType;
import com.festago.auth.domain.UserInfo;
import com.festago.auth.dto.LoginMemberDto;
import com.festago.auth.dto.LoginRequest;
import com.festago.auth.dto.LoginResponse;
import org.springframework.stereotype.Service;

Expand All @@ -22,9 +22,9 @@ public AuthFacadeService(AuthService authService, OAuth2Clients oAuth2Clients,
this.authProvider = authProvider;
}

public LoginResponse login(LoginRequest request) {
UserInfo userInfo = getUserInfo(request);
LoginMemberDto loginMember = authService.login(userInfo, request.fcmToken());
public LoginResponse login(SocialType socialType, String oAuthToken) {
UserInfo userInfo = getUserInfo(socialType, oAuthToken);
LoginMemberDto loginMember = authService.login(userInfo);
String accessToken = getAccessToken(loginMember.memberId());
return LoginResponse.of(accessToken, loginMember);
}
Expand All @@ -33,9 +33,9 @@ private String getAccessToken(Long memberId) {
return authProvider.provide(new AuthPayload(memberId, Role.MEMBER));
}

private UserInfo getUserInfo(LoginRequest request) {
OAuth2Client oAuth2Client = oAuth2Clients.getClient(request.socialType());
return oAuth2Client.getUserInfo(request.accessToken());
private UserInfo getUserInfo(SocialType socialType, String oAuthToken) {
OAuth2Client oAuth2Client = oAuth2Clients.getClient(socialType);
return oAuth2Client.getUserInfo(oAuthToken);
}

public void deleteMember(Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import com.festago.auth.domain.UserInfo;
import com.festago.auth.dto.LoginMemberDto;
import com.festago.auth.dto.event.MemberDeleteEvent;
import com.festago.auth.dto.event.MemberLoginEvent;
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
import com.festago.member.domain.Member;
import com.festago.member.repository.MemberRepository;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,16 +19,13 @@ public class AuthService {
private static final Logger log = LoggerFactory.getLogger(AuthService.class);

private final MemberRepository memberRepository;
private final ApplicationEventPublisher publisher;

public AuthService(MemberRepository memberRepository, ApplicationEventPublisher publisher) {
public AuthService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
this.publisher = publisher;
}

public LoginMemberDto login(UserInfo userInfo, String fcmToken) {
public LoginMemberDto login(UserInfo userInfo) {
LoginMemberDto loginMemberDto = handleLoginRequest(userInfo);
publisher.publishEvent(new MemberLoginEvent(loginMemberDto.memberId(), fcmToken));
return loginMemberDto;
}

Expand All @@ -54,7 +48,6 @@ public void deleteMember(Long memberId) {
.orElseThrow(() -> new NotFoundException(ErrorCode.MEMBER_NOT_FOUND));
logDeleteMember(member);
memberRepository.delete(member);
publisher.publishEvent(new MemberDeleteEvent(member.getId()));
}

private void logDeleteMember(Member member) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.festago.fcm.application;

import com.festago.auth.application.AuthExtractor;
import com.festago.auth.domain.AuthPayload;
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.InternalServerException;
import com.festago.fcm.domain.MemberFCM;
Expand All @@ -8,31 +10,39 @@
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Profile({"prod", "dev"})
public class MemberFCMService {

private static final Logger logger = LoggerFactory.getLogger(MemberFCMService.class);
private static final Logger log = LoggerFactory.getLogger(MemberFCMService.class);
private static final int LEAST_MEMBER_FCM = 1;

private final MemberFCMRepository memberFCMRepository;
private final AuthExtractor authExtractor;

public MemberFCMService(MemberFCMRepository memberFCMRepository) {
public MemberFCMService(MemberFCMRepository memberFCMRepository, AuthExtractor authExtractor) {
this.memberFCMRepository = memberFCMRepository;
this.authExtractor = authExtractor;
}

@Transactional(readOnly = true)
public MemberFCMsResponse findMemberFCM(Long memberId) {
List<MemberFCM> memberFCM = memberFCMRepository.findByMemberId(memberId);
if (memberFCM.size() < LEAST_MEMBER_FCM) {
logger.error("member {} 의 FCM 코드가 발급되지 않았습니다.", memberId);
log.error("member {} 의 FCM 코드가 발급되지 않았습니다.", memberId);
throw new InternalServerException(ErrorCode.FCM_NOT_FOUND);
}
return MemberFCMsResponse.from(memberFCM);
}

@Async
public void saveMemberFCM(String accessToken, String fcmToken) {
AuthPayload authPayload = authExtractor.extract(accessToken);
Long memberId = authPayload.getMemberId();
memberFCMRepository.save(new MemberFCM(memberId, fcmToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.festago.auth.application.AuthFacadeService;
import com.festago.auth.dto.LoginRequest;
import com.festago.auth.dto.LoginResponse;
import com.festago.fcm.application.MemberFCMService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -20,15 +21,18 @@
public class AuthController {

private final AuthFacadeService authFacadeService;
private final MemberFCMService memberFCMService;

public AuthController(AuthFacadeService authFacadeService) {
public AuthController(AuthFacadeService authFacadeService, MemberFCMService memberFCMService) {
this.authFacadeService = authFacadeService;
this.memberFCMService = memberFCMService;
}

@PostMapping("/oauth2")
@Operation(description = "소셜 엑세스 토큰을 기반으로 로그인 요청을 보낸다.", summary = "OAuth2 로그인")
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
LoginResponse response = authFacadeService.login(request);
LoginResponse response = authFacadeService.login(request.socialType(), request.accessToken());
memberFCMService.saveMemberFCM(response.accessToken(), request.fcmToken());
return ResponseEntity.ok()
.body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.festago.auth.domain.SocialType;
import com.festago.auth.domain.UserInfo;
import com.festago.auth.dto.LoginMemberDto;
import com.festago.auth.dto.LoginRequest;
import com.festago.auth.dto.LoginResponse;
import com.festago.auth.infrastructure.FestagoOAuth2Client;
import com.festago.member.domain.Member;
Expand Down Expand Up @@ -45,20 +44,18 @@ void setUp() {

@Test
void 로그인() {
LoginRequest request = new LoginRequest(SocialType.FESTAGO, "1", "fcmToken");

Member member = MemberFixture.member()
.id(1L)
.build();

given(authProvider.provide(any(AuthPayload.class)))
.willReturn("Bearer token");

given(authService.login(any(UserInfo.class), any()))
given(authService.login(any(UserInfo.class)))
.willReturn(new LoginMemberDto(false, member.getId(), member.getNickname()));

// when
LoginResponse response = authFacadeService.login(request);
LoginResponse response = authFacadeService.login(SocialType.FESTAGO, "1");

// then
assertThat(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.festago.auth.domain.SocialType;
import com.festago.auth.domain.UserInfo;
import com.festago.auth.dto.LoginMemberDto;
import com.festago.auth.dto.event.MemberDeleteEvent;
import com.festago.auth.dto.event.MemberLoginEvent;
import com.festago.common.exception.NotFoundException;
import com.festago.member.domain.Member;
import com.festago.member.repository.MemberRepository;
Expand All @@ -27,7 +23,6 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;

@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
Expand All @@ -37,9 +32,6 @@ class AuthServiceTest {
@Mock
MemberRepository memberRepository;

@Mock
ApplicationEventPublisher publisher;

@InjectMocks
AuthService authService;

Expand All @@ -60,12 +52,11 @@ class 로그인 {
member.getProfileImage());

// when
LoginMemberDto response = authService.login(userInfo, "fcmToken");
LoginMemberDto response = authService.login(userInfo);

// then
assertThat(response.isNew())
.isTrue();
verify(publisher, times(1)).publishEvent(any(MemberLoginEvent.class));
}

@Test
Expand All @@ -80,12 +71,11 @@ class 로그인 {
member.getProfileImage());

// when
LoginMemberDto response = authService.login(userInfo, "fcmToken");
LoginMemberDto response = authService.login(userInfo);

// then
assertThat(response.isNew())
.isFalse();
verify(publisher, times(1)).publishEvent(any(MemberLoginEvent.class));
}
}

Expand Down Expand Up @@ -116,7 +106,6 @@ class 회원탈퇴 {
// when & then
assertThatNoException()
.isThrownBy(() -> authService.deleteMember(memberId));
verify(publisher, times(1)).publishEvent(any(MemberDeleteEvent.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.festago.application.integration.ApplicationIntegrationTest;
import com.festago.auth.application.AuthFacadeService;
import com.festago.auth.domain.SocialType;
import com.festago.auth.dto.LoginRequest;
import com.festago.member.domain.Member;
import com.festago.member.repository.MemberRepository;
import com.festago.support.MemberFixture;
Expand All @@ -33,13 +32,12 @@ class AuthFacadeServiceIntegrationTest extends ApplicationIntegrationTest {
@Test
void 회원이_탈퇴하고_재가입하면_새로운_계정으로_가입() {
// given
LoginRequest request = new LoginRequest(SocialType.FESTAGO, "1", "fcmToken");
authFacadeService.login(request);
authFacadeService.login(SocialType.FESTAGO, "1");
Member member = memberRepository.findBySocialIdAndSocialType("1", SocialType.FESTAGO).get();

// when
memberRepository.delete(member);
authFacadeService.login(request);
authFacadeService.login(SocialType.FESTAGO, "1");

// then
assertThat(memberRepository.count()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.festago.auth.domain.SocialType;
import com.festago.auth.dto.LoginRequest;
import com.festago.auth.dto.LoginResponse;
import com.festago.fcm.application.MemberFCMService;
import com.festago.presentation.AuthController;
import com.festago.support.CustomWebMvcTest;
import com.festago.support.WithMockAuth;
Expand All @@ -39,11 +40,14 @@ class AuthControllerTest {
@MockBean
AuthFacadeService authFacadeService;

@MockBean
MemberFCMService memberFCMService;

@Test
void OAuth2_로그인을_한다() throws Exception {
// given
LoginResponse expected = new LoginResponse("accesstoken", "nickname", true);
given(authFacadeService.login(any(LoginRequest.class)))
given(authFacadeService.login(any(), any()))
.willReturn(expected);
LoginRequest request = new LoginRequest(SocialType.FESTAGO, "code", "fcmToken");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.festago.fcm.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.festago.auth.application.AuthExtractor;
import com.festago.auth.domain.AuthPayload;
import com.festago.auth.domain.Role;
import com.festago.fcm.domain.MemberFCM;
import com.festago.fcm.dto.MemberFCMResponse;
import com.festago.fcm.repository.MemberFCMRepository;
Expand All @@ -21,6 +27,9 @@ class MemberFCMServiceTest {
@Mock
MemberFCMRepository memberFCMRepository;

@Mock
AuthExtractor authExtractor;

@InjectMocks
MemberFCMService memberFCMService;

Expand All @@ -44,4 +53,20 @@ class MemberFCMServiceTest {
// then
assertThat(actual).isEqualTo(expect);
}

@Test
void 유저의_FCM_저장() {
// given
String accessToken = "accessToken";
String fcmToken = "fcmToken";
given(authExtractor.extract(any()))
.willReturn(new AuthPayload(1L, Role.MEMBER));

// when
memberFCMService.saveMemberFCM(accessToken, fcmToken);

// then
verify(memberFCMRepository, times(1))
.save(any(MemberFCM.class));
}
}

0 comments on commit 5793da7

Please sign in to comment.