-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: Oauth2 네트워크 통신 트랜잭션 범위 밖으로 분리 * refactor: accessToken 생성 로직 Transaction 범위 밖으로 분리 * refactor: @mock 어노테이션 활용 * refactor: Facade 패턴 적용 * refactor: DTO 정적 팩토리 메서드 정의 * refactor: LoginMemberDto에 Member객체가 아닌 필드를 담도록 수정
- Loading branch information
Showing
10 changed files
with
203 additions
and
107 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
backend/src/main/java/com/festago/auth/application/AuthFacadeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.festago.auth.application; | ||
|
||
import com.festago.auth.domain.AuthPayload; | ||
import com.festago.auth.domain.AuthProvider; | ||
import com.festago.auth.domain.OAuth2Client; | ||
import com.festago.auth.domain.OAuth2Clients; | ||
import com.festago.auth.domain.Role; | ||
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; | ||
|
||
@Service | ||
public class AuthFacadeService { | ||
|
||
private final AuthService authService; | ||
private final OAuth2Clients oAuth2Clients; | ||
private final AuthProvider authProvider; | ||
|
||
public AuthFacadeService(AuthService authService, OAuth2Clients oAuth2Clients, | ||
AuthProvider authProvider) { | ||
this.authService = authService; | ||
this.oAuth2Clients = oAuth2Clients; | ||
this.authProvider = authProvider; | ||
} | ||
|
||
public LoginResponse login(LoginRequest request) { | ||
LoginMemberDto loginMember = authService.login(getUserInfo(request)); | ||
String accessToken = getAccessToken(loginMember.memberId()); | ||
return LoginResponse.of(accessToken, loginMember); | ||
} | ||
|
||
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()); | ||
} | ||
|
||
public void deleteMember(Long memberId) { | ||
authService.deleteMember(memberId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/com/festago/auth/dto/LoginMemberDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.festago.auth.dto; | ||
|
||
import com.festago.domain.Member; | ||
|
||
public record LoginMemberDto( | ||
boolean isNew, | ||
Long memberId, | ||
String nickname | ||
) { | ||
|
||
public static LoginMemberDto isNew(Member member) { | ||
return new LoginMemberDto(true, member.getId(), member.getNickname()); | ||
} | ||
|
||
public static LoginMemberDto isExists(Member member) { | ||
return new LoginMemberDto(false, member.getId(), member.getNickname()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
backend/src/test/java/com/festago/auth/application/AuthFacadeServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.festago.auth.application; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.mock; | ||
|
||
import com.festago.auth.domain.AuthPayload; | ||
import com.festago.auth.domain.AuthProvider; | ||
import com.festago.auth.domain.OAuth2Clients; | ||
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.domain.Member; | ||
import com.festago.support.MemberFixture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@DisplayNameGeneration(ReplaceUnderscores.class) | ||
@SuppressWarnings("NonAsciiCharacters") | ||
@ExtendWith(MockitoExtension.class) | ||
class AuthFacadeServiceTest { | ||
|
||
AuthFacadeService authFacadeService; | ||
|
||
AuthService authService; | ||
|
||
AuthProvider authProvider; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
authService = mock(AuthService.class); | ||
OAuth2Clients oAuth2Clients = OAuth2Clients.builder() | ||
.add(new FestagoOAuth2Client()) | ||
.build(); | ||
authProvider = mock(AuthProvider.class); | ||
|
||
authFacadeService = new AuthFacadeService(authService, oAuth2Clients, authProvider); | ||
} | ||
|
||
@Test | ||
void 로그인() { | ||
LoginRequest request = new LoginRequest(SocialType.FESTAGO, "1"); | ||
|
||
Member member = MemberFixture.member() | ||
.id(1L) | ||
.build(); | ||
|
||
given(authProvider.provide(any(AuthPayload.class))) | ||
.willReturn("Bearer token"); | ||
|
||
given(authService.login(any(UserInfo.class))) | ||
.willReturn(new LoginMemberDto(false, member.getId(), member.getNickname())); | ||
|
||
// when | ||
LoginResponse response = authFacadeService.login(request); | ||
|
||
// then | ||
assertThat(response) | ||
.isNotNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.