Skip to content

Commit

Permalink
Merge pull request #57 from coffee-meet/feat/#36
Browse files Browse the repository at this point in the history
Feat/#36 oauth 도메인 테스트 코드 작성
  • Loading branch information
yumyeonghan authored Oct 26, 2023
2 parents 4ffbd4a + f042e53 commit d497d4c
Show file tree
Hide file tree
Showing 20 changed files with 489 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package coffeemeet.server.oauth.client;

import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.user.domain.OAuthProvider;

public interface OAuthMemberClient {

OAuthProvider oAuthProvider();

OAuthInfoResponse fetch(String authCode);
OAuthInfoDto.Response fetch(String authCode);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package coffeemeet.server.oauth.client;

import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.user.domain.OAuthProvider;
import java.util.Map;
import java.util.Optional;
Expand All @@ -21,11 +21,11 @@ public OAuthMemberClientComposite(Set<OAuthMemberClient> clients) {
);
}

public OAuthInfoResponse fetch(OAuthProvider oAuthProvider, String authCode) {
public OAuthInfoDto.Response fetch(OAuthProvider oAuthProvider, String authCode) {
return getClient(oAuthProvider).fetch(authCode);
}

public OAuthMemberClient getClient(OAuthProvider oAuthProvider) {
private OAuthMemberClient getClient(OAuthProvider oAuthProvider) {
return Optional.ofNullable(mapping.get(oAuthProvider))
.orElseThrow(() -> new IllegalArgumentException(
String.format(INVALID_LOGIN_TYPE_MESSAGE, oAuthProvider))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/oauth2")
@RequestMapping("/api/v1/oauth2.0")
public class OAuthController {

private final OAuthService oAuthService;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/coffeemeet/server/oauth/dto/OAuthInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package coffeemeet.server.oauth.dto;

import coffeemeet.server.user.domain.OAuthProvider;

public sealed interface OAuthInfoDto permits OAuthInfoDto.Response {

record Response(
String name,
String profileImage,
String birthYear,
String birthDay,
String email,
OAuthProvider oAuthProvider,
String oAuthProviderId
) implements OAuthInfoDto {

public static OAuthInfoDto.Response of(
String name,
String profileImage,
String birthYear,
String birthDay,
String email,
OAuthProvider oAuthProvider,
String oAuthProviderId
) {
return new OAuthInfoDto.Response(
name,
profileImage,
birthYear,
birthDay,
email,
oAuthProvider,
oAuthProviderId
);
}
}

}
34 changes: 0 additions & 34 deletions src/main/java/coffeemeet/server/oauth/dto/OAuthInfoResponse.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package coffeemeet.server.oauth.infrastructure.kakao.client;

import coffeemeet.server.oauth.client.OAuthMemberClient;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoMemberResponse;
import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoTokens;
import coffeemeet.server.user.domain.OAuthProvider;
Expand All @@ -20,7 +20,7 @@ public OAuthProvider oAuthProvider() {
}

@Override
public OAuthInfoResponse fetch(String authCode) {
public OAuthInfoDto.Response fetch(String authCode) {
KakaoTokens tokenInfo = kakaoApiClient.fetchToken(authCode);
KakaoMemberResponse response = kakaoApiClient.fetchMember(tokenInfo.accessToken());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package coffeemeet.server.oauth.infrastructure.kakao.dto;

import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.user.domain.Birth;
import coffeemeet.server.user.domain.Email;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.user.domain.OAuthProvider;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
Expand All @@ -13,12 +11,13 @@ public record KakaoMemberResponse(
KakaoAccount kakaoAccount
) {

public OAuthInfoResponse toOAuthInfoResponse() {
return OAuthInfoResponse.of(
public OAuthInfoDto.Response toOAuthInfoResponse() {
return OAuthInfoDto.Response.of(
kakaoAccount.name,
kakaoAccount.profile.profileImageUrl,
new Birth(kakaoAccount.birthyear, kakaoAccount.birthday),
new Email(kakaoAccount.email),
kakaoAccount.birthyear,
kakaoAccount.birthday,
kakaoAccount.email,
OAuthProvider.KAKAO,
String.valueOf(id)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProviderComposite;
import coffeemeet.server.oauth.client.OAuthMemberClientComposite;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.user.domain.OAuthProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -18,7 +18,7 @@ public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) {
return authCodeRequestUrlProviderComposite.provide(oAuthProvider);
}

public OAuthInfoResponse getOAuthInfo(OAuthProvider oAuthProvider, String authCode) {
public OAuthInfoDto.Response getOAuthInfo(OAuthProvider oAuthProvider, String authCode) {
return oauthMemberClientComposite.fetch(oAuthProvider,
authCode);
}
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/coffeemeet/server/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import coffeemeet.server.interest.domain.Keyword;
import coffeemeet.server.interest.repository.InterestRepository;
import coffeemeet.server.interest.service.InterestService;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.oauth.service.OAuthService;
import coffeemeet.server.user.domain.Birth;
import coffeemeet.server.user.domain.Email;
import coffeemeet.server.user.domain.OAuthInfo;
import coffeemeet.server.user.domain.OAuthProvider;
import coffeemeet.server.user.domain.Profile;
Expand Down Expand Up @@ -92,24 +94,26 @@ public void updateProfileInfo(Long userId, String nickname, String name,

@Transactional
public AuthTokens signup(SignupRequest request) {
OAuthInfoResponse response = oAuthService.getOAuthInfo(request.oAuthProvider(),
OAuthInfoDto.Response response = oAuthService.getOAuthInfo(request.oAuthProvider(),
request.authCode());

checkDuplicatedUser(response);
checkDuplicatedNickname(request.nickname());
String profileImage = getProfileImageOrDefault(response.profileImage());

User user = new User(new OAuthInfo(response.oAuthProvider(), response.oAuthProviderId()),
Profile.builder().name(response.name()).nickname(request.nickname()).email(response.email())
.profileImageUrl(profileImage).birth(response.birth()).build());
Profile.builder().name(response.name()).nickname(request.nickname())
.email(new Email(response.email()))
.profileImageUrl(profileImage)
.birth(new Birth(response.birthYear(), response.birthDay())).build());

User newUser = userRepository.save(user);
saveInterests(request, newUser);
return authTokensGenerator.generate(newUser.getId());
}

public AuthTokens login(OAuthProvider oAuthProvider, String authCode) {
OAuthInfoResponse response = oAuthService.getOAuthInfo(oAuthProvider, authCode);
OAuthInfoDto.Response response = oAuthService.getOAuthInfo(oAuthProvider, authCode);
User foundUser = userRepository.getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId(
response.oAuthProvider(), response.oAuthProviderId()).orElseThrow(
() -> new IllegalArgumentException(
Expand All @@ -130,7 +134,7 @@ public void checkDuplicatedNickname(String nickname) {
}
}

public void checkDuplicatedUser(OAuthInfoResponse response) {
public void checkDuplicatedUser(OAuthInfoDto.Response response) {
if (userRepository.existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId(
response.oAuthProvider(), response.oAuthProviderId())) {
throw new IllegalArgumentException(ALREADY_REGISTERED_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package coffeemeet.server.common.fixture.dto;

import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoMemberResponse;
import org.instancio.Instancio;

public class KakaoMemberResponseFixture {

public static KakaoMemberResponse kakaoMemberResponse() {
return Instancio.of(KakaoMemberResponse.class)
.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package coffeemeet.server.common.fixture.dto;

import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoTokens;
import org.instancio.Instancio;

public class KakaoTokensFixture {

public static KakaoTokens kakaoTokens() {
return Instancio.of(KakaoTokens.class)
.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package coffeemeet.server.common.fixture.dto;

import coffeemeet.server.oauth.dto.OAuthInfoDto;
import org.instancio.Instancio;

public class OAuthInfoResponseFixture {

public static OAuthInfoDto.Response oAuthInfoResponse() {
return Instancio.of(OAuthInfoDto.Response.class)
.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package coffeemeet.server.oauth.authcode;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

import coffeemeet.server.user.domain.OAuthProvider;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AuthCodeRequestUrlProviderCompositeTest {

@Mock
private AuthCodeRequestUrlProvider provider;

@Mock
private Set<AuthCodeRequestUrlProvider> providers;

@InjectMocks
private AuthCodeRequestUrlProviderComposite composite;

@DisplayName("sns 의 redirect url 을 제공할 수 있다.")
@Test
void provideTest() {
// given
OAuthProvider oAuthProvider = OAuthProvider.KAKAO;
String resultUrl = "https://kakao.com/login/oauth/authorize?client_id=testClientId&redirect_uri=https://testClientId/redirect";

given(provider.oAuthProvider()).willReturn(OAuthProvider.KAKAO);
given(provider.provide()).willReturn(resultUrl);

providers = new HashSet<>(Collections.singletonList(provider));
composite = new AuthCodeRequestUrlProviderComposite(providers);

// when
String expectedUrl = composite.provide(oAuthProvider);

// then
assertThat(resultUrl).isEqualTo(expectedUrl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package coffeemeet.server.oauth.client;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

import coffeemeet.server.common.fixture.dto.OAuthInfoResponseFixture;
import coffeemeet.server.oauth.dto.OAuthInfoDto;
import coffeemeet.server.user.domain.OAuthProvider;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class OAuthMemberClientCompositeTest {

@Mock
private OAuthMemberClient client;

@Mock
private Set<OAuthMemberClient> clients;

@InjectMocks
private OAuthMemberClientComposite composite;

@DisplayName("sns 로부터 사용자 정보를 가져올 수 있다.")
@Test
void fetchTest() {
// given
String authCode = "authCode";
OAuthProvider oAuthProvider = OAuthProvider.KAKAO;
OAuthInfoDto.Response response = OAuthInfoResponseFixture.oAuthInfoResponse();

given(client.oAuthProvider()).willReturn(OAuthProvider.KAKAO);
given(client.fetch(authCode)).willReturn(response);

clients = new HashSet<>(Collections.singletonList(client));
composite = new OAuthMemberClientComposite(clients);

// when
OAuthInfoDto.Response expectedResponse = composite.fetch(oAuthProvider, authCode);

// then
assertThat(response).isEqualTo(expectedResponse);
}

}
Loading

0 comments on commit d497d4c

Please sign in to comment.