Skip to content

Commit 36fb4d8

Browse files
committed
test: 테스트 코드 작성
1 parent ef9a857 commit 36fb4d8

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.example.solidconnection.auth.service.oauth;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatCode;
5+
import static org.junit.jupiter.api.Assertions.assertAll;
6+
import static org.mockito.BDDMockito.given;
7+
import static org.mockito.Mockito.mock;
8+
9+
import com.example.solidconnection.common.exception.CustomException;
10+
import com.example.solidconnection.common.exception.ErrorCode;
11+
import com.example.solidconnection.siteuser.domain.AuthType;
12+
import java.util.List;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
16+
@DisplayName("OAuthClientMap 테스트")
17+
class OAuthClientMapTest {
18+
19+
@Test
20+
void AuthType에_해당하는_Client를_반환한다() {
21+
// given
22+
OAuthClient appleClient = mock(OAuthClient.class);
23+
OAuthClient kakaoClient = mock(OAuthClient.class);
24+
given(appleClient.getAuthType()).willReturn(AuthType.APPLE);
25+
given(kakaoClient.getAuthType()).willReturn(AuthType.KAKAO);
26+
27+
OAuthClientMap oAuthClientMap = new OAuthClientMap(
28+
List.of(appleClient, kakaoClient)
29+
);
30+
31+
// when & then
32+
assertAll(
33+
() -> assertThat(oAuthClientMap.getOAuthClient(AuthType.APPLE)).isEqualTo(appleClient),
34+
() -> assertThat(oAuthClientMap.getOAuthClient(AuthType.KAKAO)).isEqualTo(kakaoClient)
35+
);
36+
}
37+
38+
@Test
39+
void AuthType에_매칭되는_Client가_없으면_예외가_발생한다() {
40+
// given
41+
OAuthClient appleClient = mock(OAuthClient.class);
42+
given(appleClient.getAuthType()).willReturn(AuthType.APPLE);
43+
44+
OAuthClientMap oAuthClientMap = new OAuthClientMap(
45+
List.of(appleClient)
46+
);
47+
48+
// when & then
49+
assertThatCode(() -> oAuthClientMap.getOAuthClient(AuthType.KAKAO))
50+
.isInstanceOf(CustomException.class)
51+
.hasMessageContaining(ErrorCode.NOT_DEFINED_ERROR.getMessage());
52+
}
53+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.example.solidconnection.auth.service.oauth;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertAll;
5+
import static org.mockito.BDDMockito.given;
6+
import static org.mockito.Mockito.mock;
7+
8+
import com.example.solidconnection.auth.dto.oauth.OAuthCodeRequest;
9+
import com.example.solidconnection.auth.dto.oauth.OAuthResponse;
10+
import com.example.solidconnection.auth.dto.oauth.OAuthSignInResponse;
11+
import com.example.solidconnection.auth.dto.oauth.OAuthUserInfoDto;
12+
import com.example.solidconnection.auth.dto.oauth.SignUpPrepareResponse;
13+
import com.example.solidconnection.siteuser.domain.AuthType;
14+
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
15+
import com.example.solidconnection.support.TestContainerSpringBootTest;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.DisplayName;
18+
import org.junit.jupiter.api.Test;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.test.mock.mockito.MockBean;
21+
22+
@DisplayName("OAuth 서비스 테스트")
23+
@TestContainerSpringBootTest
24+
class OAuthServiceTest {
25+
26+
@Autowired
27+
private OAuthService oAuthService;
28+
29+
@Autowired
30+
private SiteUserFixture siteUserFixture;
31+
32+
@MockBean
33+
private OAuthClientMap oauthClientMap;
34+
35+
private final AuthType authType = AuthType.KAKAO;
36+
private final String oauthCode = "code";
37+
private final String email = "test@test.com";
38+
private final String profileImageUrl = "profile.jpg";
39+
private final String nickname = "testUser";
40+
41+
@BeforeEach
42+
void setUp() { // 실제 client 호출하지 않도록 mocking
43+
OAuthUserInfoDto oauthUserInfoDto = mock(OAuthUserInfoDto.class);
44+
given(oauthUserInfoDto.getEmail()).willReturn(email);
45+
given(oauthUserInfoDto.getProfileImageUrl()).willReturn(profileImageUrl);
46+
given(oauthUserInfoDto.getNickname()).willReturn(nickname);
47+
48+
OAuthClient oAuthClient = mock(OAuthClient.class);
49+
given(oauthClientMap.getOAuthClient(authType)).willReturn(oAuthClient);
50+
given(oAuthClient.getAuthType()).willReturn(authType);
51+
given(oAuthClient.getUserInfo(oauthCode)).willReturn(oauthUserInfoDto);
52+
}
53+
54+
@Test
55+
void 기존_회원이라면_로그인한다() {
56+
// given
57+
siteUserFixture.사용자(email, authType);
58+
59+
// when
60+
OAuthResponse response = oAuthService.processOAuth(authType, new OAuthCodeRequest(oauthCode));
61+
62+
// then
63+
assertThat(response).isInstanceOf(OAuthSignInResponse.class);
64+
OAuthSignInResponse signInResponse = (OAuthSignInResponse) response;
65+
assertAll(
66+
() -> assertThat(signInResponse.isRegistered()).isTrue(),
67+
() -> assertThat(signInResponse.accessToken()).isNotBlank(),
68+
() -> assertThat(signInResponse.refreshToken()).isNotBlank()
69+
);
70+
}
71+
72+
@Test
73+
void 신규_회원이라면_회원가입에_필요한_정보를_응답한다() {
74+
// when
75+
OAuthResponse response = oAuthService.processOAuth(authType, new OAuthCodeRequest(oauthCode));
76+
77+
// then
78+
assertThat(response).isInstanceOf(SignUpPrepareResponse.class);
79+
SignUpPrepareResponse signUpPrepareResponse = (SignUpPrepareResponse) response;
80+
assertAll(
81+
() -> assertThat(signUpPrepareResponse.isRegistered()).isFalse(),
82+
() -> assertThat(signUpPrepareResponse.signUpToken()).isNotBlank(),
83+
() -> assertThat(signUpPrepareResponse.email()).isEqualTo(email),
84+
() -> assertThat(signUpPrepareResponse.profileImageUrl()).isEqualTo(profileImageUrl),
85+
() -> assertThat(signUpPrepareResponse.nickname()).isEqualTo(nickname)
86+
);
87+
}
88+
}

0 commit comments

Comments
 (0)