|
| 1 | +package com.jobtracker.unit; |
| 2 | + |
| 3 | +import com.jobtracker.config.GoogleDriveProperties; |
| 4 | +import com.jobtracker.entity.GoogleDriveBaseResume; |
| 5 | +import com.jobtracker.entity.GoogleDriveConnection; |
| 6 | +import com.jobtracker.entity.GoogleDriveOAuthState; |
| 7 | +import com.jobtracker.entity.User; |
| 8 | +import com.jobtracker.repository.GoogleDriveConnectionRepository; |
| 9 | +import com.jobtracker.repository.GoogleDriveOAuthStateRepository; |
| 10 | +import com.jobtracker.service.GoogleDriveApiClient; |
| 11 | +import com.jobtracker.service.GoogleDriveOAuthService; |
| 12 | +import com.jobtracker.util.SecurityUtils; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 16 | +import org.mockito.ArgumentCaptor; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 19 | + |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.mockito.ArgumentMatchers.any; |
| 28 | +import static org.mockito.Mockito.*; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +class GoogleDriveOAuthServiceTest { |
| 32 | + |
| 33 | + private static final UUID USER_ID = UUID.fromString("00000000-0000-0000-0000-000000000001"); |
| 34 | + private static final String VALID_STATE = "valid-state-token"; |
| 35 | + private static final String AUTH_CODE = "auth-code-123"; |
| 36 | + |
| 37 | + @Mock private GoogleDriveApiClient googleDriveApiClient; |
| 38 | + @Mock private GoogleDriveConnectionRepository connectionRepository; |
| 39 | + @Mock private GoogleDriveOAuthStateRepository oauthStateRepository; |
| 40 | + @Mock private SecurityUtils securityUtils; |
| 41 | + |
| 42 | + private GoogleDriveProperties googleDriveProperties; |
| 43 | + private GoogleDriveOAuthService oauthService; |
| 44 | + |
| 45 | + private User user; |
| 46 | + private GoogleDriveOAuthState validOAuthState; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() { |
| 50 | + googleDriveProperties = new GoogleDriveProperties( |
| 51 | + "client-id", |
| 52 | + "client-secret", |
| 53 | + "http://localhost:8080/api/v1/google-drive/oauth/callback", |
| 54 | + "http://localhost:5173/settings/google-drive/callback", |
| 55 | + "https://accounts.google.com/o/oauth2/v2/auth", |
| 56 | + "https://oauth2.googleapis.com/token" |
| 57 | + ); |
| 58 | + oauthService = new GoogleDriveOAuthService( |
| 59 | + googleDriveApiClient, |
| 60 | + googleDriveProperties, |
| 61 | + connectionRepository, |
| 62 | + oauthStateRepository, |
| 63 | + securityUtils |
| 64 | + ); |
| 65 | + |
| 66 | + user = new User(); |
| 67 | + user.setId(USER_ID); |
| 68 | + user.setEmail("user@example.com"); |
| 69 | + |
| 70 | + validOAuthState = new GoogleDriveOAuthState(); |
| 71 | + validOAuthState.setUser(user); |
| 72 | + validOAuthState.setStateToken(VALID_STATE); |
| 73 | + validOAuthState.setExpiresAt(LocalDateTime.now().plusMinutes(5)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void handleCallback_shouldRedirectWithError_whenGoogleReturnsError() { |
| 78 | + String result = oauthService.handleCallback(VALID_STATE, null, "access_denied"); |
| 79 | + |
| 80 | + assertThat(result).contains("status=error"); |
| 81 | + assertThat(result).contains("Google"); |
| 82 | + assertThat(result).contains("OAuth"); |
| 83 | + verifyNoInteractions(googleDriveApiClient); |
| 84 | + verifyNoInteractions(connectionRepository); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void handleCallback_shouldRedirectWithError_whenStateIsExpired() { |
| 89 | + validOAuthState.setExpiresAt(LocalDateTime.now().minusMinutes(1)); |
| 90 | + when(oauthStateRepository.findByStateToken(VALID_STATE)).thenReturn(Optional.of(validOAuthState)); |
| 91 | + |
| 92 | + String result = oauthService.handleCallback(VALID_STATE, AUTH_CODE, null); |
| 93 | + |
| 94 | + assertThat(result).contains("status=error"); |
| 95 | + assertThat(result).containsIgnoringCase("expired"); |
| 96 | + verifyNoInteractions(googleDriveApiClient); |
| 97 | + verifyNoInteractions(connectionRepository); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void handleCallback_shouldRedirectWithError_whenStateIsInvalid() { |
| 102 | + when(oauthStateRepository.findByStateToken("unknown-state")).thenReturn(Optional.empty()); |
| 103 | + |
| 104 | + String result = oauthService.handleCallback("unknown-state", AUTH_CODE, null); |
| 105 | + |
| 106 | + assertThat(result).contains("status=error"); |
| 107 | + assertThat(result).contains("Invalid"); |
| 108 | + verifyNoInteractions(googleDriveApiClient); |
| 109 | + verifyNoInteractions(connectionRepository); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void handleCallback_shouldRedirectWithError_whenMissingState() { |
| 114 | + String result = oauthService.handleCallback(null, AUTH_CODE, null); |
| 115 | + |
| 116 | + assertThat(result).contains("status=error"); |
| 117 | + assertThat(result).contains("Missing"); |
| 118 | + verifyNoInteractions(googleDriveApiClient); |
| 119 | + verifyNoInteractions(connectionRepository); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void handleCallback_shouldRedirectWithError_whenMissingCode() { |
| 124 | + String result = oauthService.handleCallback(VALID_STATE, null, null); |
| 125 | + |
| 126 | + assertThat(result).contains("status=error"); |
| 127 | + assertThat(result).contains("Missing"); |
| 128 | + verifyNoInteractions(googleDriveApiClient); |
| 129 | + verifyNoInteractions(connectionRepository); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void handleCallback_shouldCreateNewConnection_onFirstConnect() { |
| 134 | + when(oauthStateRepository.findByStateToken(VALID_STATE)).thenReturn(Optional.of(validOAuthState)); |
| 135 | + when(googleDriveApiClient.exchangeAuthorizationCode(AUTH_CODE)).thenReturn( |
| 136 | + new GoogleDriveApiClient.OAuthTokens("access-token", "refresh-token", |
| 137 | + LocalDateTime.now().plusHours(1), "https://www.googleapis.com/auth/drive") |
| 138 | + ); |
| 139 | + when(googleDriveApiClient.getCurrentAccount("access-token")).thenReturn( |
| 140 | + new GoogleDriveApiClient.GoogleDriveAccountProfile("perm-1", "user@gmail.com", "User") |
| 141 | + ); |
| 142 | + when(connectionRepository.findByUserId(USER_ID)).thenReturn(Optional.empty()); |
| 143 | + when(connectionRepository.save(any(GoogleDriveConnection.class))).thenAnswer(inv -> inv.getArgument(0)); |
| 144 | + |
| 145 | + String result = oauthService.handleCallback(VALID_STATE, AUTH_CODE, null); |
| 146 | + |
| 147 | + assertThat(result).contains("status=success"); |
| 148 | + ArgumentCaptor<GoogleDriveConnection> captor = ArgumentCaptor.forClass(GoogleDriveConnection.class); |
| 149 | + verify(connectionRepository).save(captor.capture()); |
| 150 | + GoogleDriveConnection saved = captor.getValue(); |
| 151 | + assertThat(saved.getGoogleEmail()).isEqualTo("user@gmail.com"); |
| 152 | + assertThat(saved.getRefreshToken()).isEqualTo("refresh-token"); |
| 153 | + verify(oauthStateRepository).delete(validOAuthState); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void handleCallback_shouldClearRootFolderAndBaseResumes_whenDifferentAccountConnects() { |
| 158 | + GoogleDriveConnection existingConnection = new GoogleDriveConnection(); |
| 159 | + existingConnection.setGoogleAccountId("old-account-id"); |
| 160 | + existingConnection.setRootFolderId("old-root-folder"); |
| 161 | + existingConnection.setRootFolderName("Old Root"); |
| 162 | + existingConnection.setBaseResumes(new ArrayList<>(List.of(new GoogleDriveBaseResume()))); |
| 163 | + |
| 164 | + when(oauthStateRepository.findByStateToken(VALID_STATE)).thenReturn(Optional.of(validOAuthState)); |
| 165 | + when(googleDriveApiClient.exchangeAuthorizationCode(AUTH_CODE)).thenReturn( |
| 166 | + new GoogleDriveApiClient.OAuthTokens("new-access", "new-refresh", |
| 167 | + LocalDateTime.now().plusHours(1), "https://www.googleapis.com/auth/drive") |
| 168 | + ); |
| 169 | + when(googleDriveApiClient.getCurrentAccount("new-access")).thenReturn( |
| 170 | + new GoogleDriveApiClient.GoogleDriveAccountProfile("new-account-id", "new@gmail.com", "New User") |
| 171 | + ); |
| 172 | + when(connectionRepository.findByUserId(USER_ID)).thenReturn(Optional.of(existingConnection)); |
| 173 | + when(connectionRepository.save(any(GoogleDriveConnection.class))).thenAnswer(inv -> inv.getArgument(0)); |
| 174 | + |
| 175 | + String result = oauthService.handleCallback(VALID_STATE, AUTH_CODE, null); |
| 176 | + |
| 177 | + assertThat(result).contains("status=success"); |
| 178 | + ArgumentCaptor<GoogleDriveConnection> captor = ArgumentCaptor.forClass(GoogleDriveConnection.class); |
| 179 | + verify(connectionRepository).save(captor.capture()); |
| 180 | + GoogleDriveConnection saved = captor.getValue(); |
| 181 | + assertThat(saved.getRootFolderId()).isNull(); |
| 182 | + assertThat(saved.getRootFolderName()).isNull(); |
| 183 | + assertThat(saved.getBaseResumes()).isEmpty(); |
| 184 | + assertThat(saved.getGoogleEmail()).isEqualTo("new@gmail.com"); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void handleCallback_shouldPreserveRootFolder_whenSameAccountReconnects() { |
| 189 | + GoogleDriveConnection existingConnection = new GoogleDriveConnection(); |
| 190 | + existingConnection.setGoogleAccountId("same-account-id"); |
| 191 | + existingConnection.setRootFolderId("existing-root-folder"); |
| 192 | + existingConnection.setRootFolderName("Job Tracker Root"); |
| 193 | + existingConnection.setBaseResumes(new ArrayList<>()); |
| 194 | + |
| 195 | + when(oauthStateRepository.findByStateToken(VALID_STATE)).thenReturn(Optional.of(validOAuthState)); |
| 196 | + when(googleDriveApiClient.exchangeAuthorizationCode(AUTH_CODE)).thenReturn( |
| 197 | + new GoogleDriveApiClient.OAuthTokens("new-access", "new-refresh", |
| 198 | + LocalDateTime.now().plusHours(1), "https://www.googleapis.com/auth/drive") |
| 199 | + ); |
| 200 | + when(googleDriveApiClient.getCurrentAccount("new-access")).thenReturn( |
| 201 | + new GoogleDriveApiClient.GoogleDriveAccountProfile("same-account-id", "user@gmail.com", "User") |
| 202 | + ); |
| 203 | + when(connectionRepository.findByUserId(USER_ID)).thenReturn(Optional.of(existingConnection)); |
| 204 | + when(connectionRepository.save(any(GoogleDriveConnection.class))).thenAnswer(inv -> inv.getArgument(0)); |
| 205 | + |
| 206 | + String result = oauthService.handleCallback(VALID_STATE, AUTH_CODE, null); |
| 207 | + |
| 208 | + assertThat(result).contains("status=success"); |
| 209 | + ArgumentCaptor<GoogleDriveConnection> captor = ArgumentCaptor.forClass(GoogleDriveConnection.class); |
| 210 | + verify(connectionRepository).save(captor.capture()); |
| 211 | + GoogleDriveConnection saved = captor.getValue(); |
| 212 | + assertThat(saved.getRootFolderId()).isEqualTo("existing-root-folder"); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + void handleCallback_shouldDeleteStateEvenOnError() { |
| 217 | + validOAuthState.setExpiresAt(LocalDateTime.now().minusMinutes(1)); |
| 218 | + when(oauthStateRepository.findByStateToken(VALID_STATE)).thenReturn(Optional.of(validOAuthState)); |
| 219 | + |
| 220 | + oauthService.handleCallback(VALID_STATE, AUTH_CODE, null); |
| 221 | + |
| 222 | + verify(oauthStateRepository).delete(validOAuthState); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void disconnect_shouldDeleteExistingConnection() { |
| 227 | + GoogleDriveConnection connection = new GoogleDriveConnection(); |
| 228 | + when(securityUtils.getCurrentUserId()).thenReturn(USER_ID); |
| 229 | + when(connectionRepository.findByUserId(USER_ID)).thenReturn(Optional.of(connection)); |
| 230 | + |
| 231 | + oauthService.disconnect(); |
| 232 | + |
| 233 | + verify(connectionRepository).delete(connection); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + void disconnect_shouldSucceed_whenNoConnectionExists() { |
| 238 | + when(securityUtils.getCurrentUserId()).thenReturn(USER_ID); |
| 239 | + when(connectionRepository.findByUserId(USER_ID)).thenReturn(Optional.empty()); |
| 240 | + |
| 241 | + oauthService.disconnect(); |
| 242 | + |
| 243 | + verify(connectionRepository, never()).delete(any()); |
| 244 | + } |
| 245 | +} |
0 commit comments