|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.oauth.flows; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.JsonNode; |
| 11 | +import com.google.common.collect.ImmutableMap; |
| 12 | +import io.airbyte.commons.json.Jsons; |
| 13 | +import io.airbyte.config.SourceOAuthParameter; |
| 14 | +import io.airbyte.config.persistence.ConfigNotFoundException; |
| 15 | +import io.airbyte.config.persistence.ConfigRepository; |
| 16 | +import io.airbyte.oauth.OAuthFlowImplementation; |
| 17 | +import io.airbyte.validation.json.JsonValidationException; |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.http.HttpClient; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.UUID; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +public class IntercomOAuthFlowIntegrationTest extends OAuthFlowIntegrationTest { |
| 29 | + |
| 30 | + protected static final Path CREDENTIALS_PATH = Path.of("secrets/intercom.json"); |
| 31 | + protected static final String REDIRECT_URL = "http://localhost:8000/code"; |
| 32 | + protected static final int SERVER_LISTENING_PORT = 8000; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Path getCredentialsPath() { |
| 36 | + return CREDENTIALS_PATH; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected OAuthFlowImplementation getFlowImplementation(ConfigRepository configRepository, HttpClient httpClient) { |
| 41 | + return new IntercomOAuthFlow(configRepository, httpClient); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected int getServerListeningPort() { |
| 46 | + return SERVER_LISTENING_PORT; |
| 47 | + } |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + public void setup() throws IOException { |
| 51 | + super.setup(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFullIntercomOAuthFlow() throws InterruptedException, ConfigNotFoundException, IOException, JsonValidationException { |
| 56 | + int limit = 20; |
| 57 | + final UUID workspaceId = UUID.randomUUID(); |
| 58 | + final UUID definitionId = UUID.randomUUID(); |
| 59 | + final String fullConfigAsString = new String(Files.readAllBytes(CREDENTIALS_PATH)); |
| 60 | + final JsonNode credentialsJson = Jsons.deserialize(fullConfigAsString); |
| 61 | + when(configRepository.listSourceOAuthParam()).thenReturn(List.of(new SourceOAuthParameter() |
| 62 | + .withOauthParameterId(UUID.randomUUID()) |
| 63 | + .withSourceDefinitionId(definitionId) |
| 64 | + .withWorkspaceId(workspaceId) |
| 65 | + .withConfiguration(Jsons.jsonNode( |
| 66 | + Map.of("authorization", |
| 67 | + ImmutableMap.builder() |
| 68 | + .put("client_id", credentialsJson.get("client_id").asText()) |
| 69 | + .put("client_secret", credentialsJson.get("client_secret").asText()) |
| 70 | + .build()))))); |
| 71 | + |
| 72 | + final String url = flow.getSourceConsentUrl(workspaceId, definitionId, REDIRECT_URL); |
| 73 | + LOGGER.info("Waiting for user consent at: {}", url); |
| 74 | + |
| 75 | + // TODO: To automate, start a selenium job to navigate to the Consent URL and click on allowing |
| 76 | + // access... |
| 77 | + while (!serverHandler.isSucceeded() && limit > 0) { |
| 78 | + Thread.sleep(1000); |
| 79 | + limit -= 1; |
| 80 | + } |
| 81 | + assertTrue(serverHandler.isSucceeded(), "Failed to get User consent on time"); |
| 82 | + final Map<String, Object> params = flow.completeSourceOAuth(workspaceId, definitionId, |
| 83 | + Map.of("code", serverHandler.getParamValue()), REDIRECT_URL); |
| 84 | + LOGGER.info("Response from completing OAuth Flow is: {}", params.toString()); |
| 85 | + assertTrue(params.containsKey("access_token")); |
| 86 | + assertTrue(params.get("access_token").toString().length() > 0); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments