|
| 1 | +package apptive.team5.user.service; |
| 2 | + |
| 3 | +import apptive.team5.jwt.component.JWTUtil; |
| 4 | +import apptive.team5.jwt.dto.TokenResponse; |
| 5 | +import apptive.team5.oauth2.dto.GoogleOAuth2Rep; |
| 6 | +import apptive.team5.user.domain.UserEntity; |
| 7 | +import apptive.team5.util.TestUtil; |
| 8 | +import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; |
| 9 | +import org.assertj.core.api.SoftAssertions; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.InjectMocks; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 16 | + |
| 17 | +import static org.mockito.BDDMockito.*; |
| 18 | + |
| 19 | + |
| 20 | +@ExtendWith(SpringExtension.class) |
| 21 | +class UserServiceTest { |
| 22 | + |
| 23 | + @InjectMocks |
| 24 | + private UserService userService; |
| 25 | + |
| 26 | + @Mock |
| 27 | + private UserLowService userLowService; |
| 28 | + |
| 29 | + @Mock |
| 30 | + private JWTUtil jwtUtil; |
| 31 | + |
| 32 | + @Test |
| 33 | + @DisplayName("소셜 로그인 - 존재하는 회원이면 로그인") |
| 34 | + void socialLoginCase1() { |
| 35 | + // given |
| 36 | + UserEntity user = TestUtil.makeUserEntity(); |
| 37 | + |
| 38 | + given(userLowService.existsByIdentifier(any())) |
| 39 | + .willReturn(true); |
| 40 | + |
| 41 | + given(userLowService.findByIdentifier(any())) |
| 42 | + .willReturn(user); |
| 43 | + |
| 44 | + given(jwtUtil.createJWT(any(), any(), any())) |
| 45 | + .willReturn("accessToken", "refreshToken"); |
| 46 | + |
| 47 | + TokenResponse tokenResponse = userService.socialLogin(socialLoginCase()); |
| 48 | + |
| 49 | + SoftAssertions.assertSoftly(softly -> { |
| 50 | + softly.assertThat(tokenResponse.accessToken()).isEqualTo("accessToken"); |
| 51 | + softly.assertThat(tokenResponse.refreshToken()).isEqualTo("refreshToken"); |
| 52 | + }); |
| 53 | + verify(userLowService).existsByIdentifier(any()); |
| 54 | + verify(userLowService).findByIdentifier(any()); |
| 55 | + verify(jwtUtil, times(2)).createJWT(any(), any(), any()); |
| 56 | + verifyNoMoreInteractions(userLowService, jwtUtil); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("소셜 로그인 - 존재하지 않는 회원이면 회원가입") |
| 61 | + void socialLoginCase2() { |
| 62 | + // given |
| 63 | + UserEntity user = TestUtil.makeUserEntity(); |
| 64 | + |
| 65 | + given(userLowService.existsByIdentifier(any())) |
| 66 | + .willReturn(false); |
| 67 | + |
| 68 | + given(userLowService.save(any())) |
| 69 | + .willReturn(user); |
| 70 | + |
| 71 | + given(jwtUtil.createJWT(any(), any(), any())) |
| 72 | + .willReturn("accessToken", "refreshToken"); |
| 73 | + |
| 74 | + TokenResponse tokenResponse = userService.socialLogin(socialLoginCase()); |
| 75 | + |
| 76 | + SoftAssertions.assertSoftly(softly -> { |
| 77 | + softly.assertThat(tokenResponse.accessToken()).isEqualTo("accessToken"); |
| 78 | + softly.assertThat(tokenResponse.refreshToken()).isEqualTo("refreshToken"); |
| 79 | + }); |
| 80 | + verify(userLowService).existsByIdentifier(any()); |
| 81 | + verify(userLowService).save(any()); |
| 82 | + verify(jwtUtil, times(2)).createJWT(any(), any(), any()); |
| 83 | + verifyNoMoreInteractions(userLowService, jwtUtil); |
| 84 | + } |
| 85 | + |
| 86 | + private GoogleOAuth2Rep socialLoginCase() { |
| 87 | + GoogleIdToken.Payload payload = new GoogleIdToken.Payload(); |
| 88 | + |
| 89 | + payload.setSubject(TestUtil.userIdentifier); |
| 90 | + payload.set("name", "이진원"); |
| 91 | + payload. set( "email", "[email protected]"); |
| 92 | + |
| 93 | + return new GoogleOAuth2Rep(payload); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments