-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOAuthServiceTest.java
More file actions
92 lines (78 loc) · 3.95 KB
/
OAuthServiceTest.java
File metadata and controls
92 lines (78 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.example.solidconnection.auth.service.oauth;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import com.example.solidconnection.auth.dto.oauth.OAuthCodeRequest;
import com.example.solidconnection.auth.dto.oauth.OAuthResponse;
import com.example.solidconnection.auth.dto.oauth.OAuthResult;
import com.example.solidconnection.auth.dto.oauth.OAuthSignInResponse;
import com.example.solidconnection.auth.dto.oauth.OAuthUserInfoDto;
import com.example.solidconnection.auth.dto.oauth.SignUpPrepareResponse;
import com.example.solidconnection.siteuser.domain.AuthType;
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
import com.example.solidconnection.support.TestContainerSpringBootTest;
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.test.context.bean.override.mockito.MockitoBean;
@DisplayName("OAuth 서비스 테스트")
@TestContainerSpringBootTest
class OAuthServiceTest {
@Autowired
private OAuthService oAuthService;
@Autowired
private SiteUserFixture siteUserFixture;
@MockitoBean
private OAuthClientMap oauthClientMap;
private final AuthType authType = AuthType.KAKAO;
private final String oauthCode = "code";
private final String email = "test@test.com";
private final String profileImageUrl = "profile.jpg";
private final String nickname = "testUser";
@BeforeEach
void setUp() { // 실제 client 호출하지 않도록 mocking
OAuthUserInfoDto oauthUserInfoDto = mock(OAuthUserInfoDto.class);
given(oauthUserInfoDto.getEmail()).willReturn(email);
given(oauthUserInfoDto.getProfileImageUrl()).willReturn(profileImageUrl);
given(oauthUserInfoDto.getNickname()).willReturn(nickname);
OAuthClient oAuthClient = mock(OAuthClient.class);
given(oauthClientMap.getOAuthClient(authType)).willReturn(oAuthClient);
given(oAuthClient.getAuthType()).willReturn(authType);
given(oAuthClient.getUserInfo(oauthCode)).willReturn(oauthUserInfoDto);
}
@Test
void 기존_회원이라면_로그인한다() {
// given
siteUserFixture.사용자(email, authType);
// when
OAuthResult oAuthResult = oAuthService.processOAuth(authType, new OAuthCodeRequest(oauthCode));
// then
OAuthResponse response = oAuthResult.response();
assertThat(response).isInstanceOf(OAuthSignInResponse.class);
OAuthSignInResponse signInResponse = (OAuthSignInResponse) response;
assertAll(
() -> assertThat(signInResponse.isRegistered()).isTrue(),
() -> assertThat(signInResponse.accessToken()).isNotBlank(),
() -> assertThat(oAuthResult.refreshToken()).isNotBlank()
);
}
@Test
void 신규_회원이라면_회원가입에_필요한_정보를_응답한다() {
// when
OAuthResult oAuthResult = oAuthService.processOAuth(authType, new OAuthCodeRequest(oauthCode));
// then
OAuthResponse response = oAuthResult.response();
assertThat(response).isInstanceOf(SignUpPrepareResponse.class);
SignUpPrepareResponse signUpPrepareResponse = (SignUpPrepareResponse) response;
assertAll(
() -> assertThat(signUpPrepareResponse.isRegistered()).isFalse(),
() -> assertThat(signUpPrepareResponse.signUpToken()).isNotBlank(),
() -> assertThat(signUpPrepareResponse.email()).isEqualTo(email),
() -> assertThat(signUpPrepareResponse.profileImageUrl()).isEqualTo(profileImageUrl),
() -> assertThat(signUpPrepareResponse.nickname()).isEqualTo(nickname),
() -> assertThat(oAuthResult.refreshToken()).isNull()
);
}
}