|
| 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