From 83aa008574d3a0d00ad020f15243e82698636920 Mon Sep 17 00:00:00 2001 From: James Netherton Date: Thu, 25 Aug 2022 09:15:27 +0100 Subject: [PATCH] Adapt Dropbox tests to new authentication mechanism Fixes #4021 --- integration-tests/dropbox/README.adoc | 9 +++++ .../component/dropbox/it/DropboxResource.java | 35 +++++++++---------- .../component/dropbox/it/DropboxTest.java | 4 +++ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/integration-tests/dropbox/README.adoc b/integration-tests/dropbox/README.adoc index 25cfaca01d1f..67ce1e17b444 100644 --- a/integration-tests/dropbox/README.adoc +++ b/integration-tests/dropbox/README.adoc @@ -3,10 +3,19 @@ To run the Dropbox integration tests, you need a valid Dropbox https://www.dropbox.com/developers[developer account]. Then from the developer console, create a new app and make a note of the access token. +You will also require a refresh token for authentication. If you're planning on repeatedly performing tests, then it's a good idea to +generate a long-lived token. Some instructions for how to do that are here: + +https://dropbox.tech/developers/migrating-app-permissions-and-access-tokens#implement-refresh-tokens + Then set the following environment variables. Note that `DROPBOX_CLIENT_IDENTIFIER` should be set to the name of your Dropbox app: [source,shell] ---- export DROPBOX_ACCESS_TOKEN=your-access-token +export DROPBOX_API_KEY=your-app-api-key +export DROPBOX_API_SECRET=your-app-api-secret export DROPBOX_CLIENT_IDENTIFIER=your-client-identifier +export DROPBOX_REFRESH_TOKEN=your-refresh-token +export DROPBOX_REFRESH_TOKEN_EXPIRES_IN=your-refresh-token-expiry ---- diff --git a/integration-tests/dropbox/src/main/java/org/apache/camel/quarkus/component/dropbox/it/DropboxResource.java b/integration-tests/dropbox/src/main/java/org/apache/camel/quarkus/component/dropbox/it/DropboxResource.java index c2d504dd5306..282eb7aba86f 100644 --- a/integration-tests/dropbox/src/main/java/org/apache/camel/quarkus/component/dropbox/it/DropboxResource.java +++ b/integration-tests/dropbox/src/main/java/org/apache/camel/quarkus/component/dropbox/it/DropboxResource.java @@ -53,16 +53,10 @@ public class DropboxResource { @Produces(MediaType.TEXT_PLAIN) public Response createFile() throws Exception { java.nio.file.Path path = Files.write(Paths.get("target", FILE_NAME), FILE_CONTENT.getBytes(StandardCharsets.UTF_8)); - String result = producerTemplate.requestBodyAndHeader( - "dropbox://put?uploadMode=add&accessToken={{DROPBOX_ACCESS_TOKEN}}&clientIdentifier={{DROPBOX_CLIENT_IDENTIFIER}}&localPath=" - + path.toString() - + "&remotePath=" + REMOTE_PATH, - null, - HEADER_PUT_FILE_NAME, FILE_NAME, String.class); - return Response - .created(new URI("https://camel.apache.org/")) - .entity(result) - .build(); + String result = producerTemplate.requestBodyAndHeader("dropbox://put?" + getCredentialsUriOptions() + + "&uploadMode=add&localPath=" + path + "&remotePath=" + REMOTE_PATH, null, HEADER_PUT_FILE_NAME, FILE_NAME, + String.class); + return Response.created(new URI("https://camel.apache.org/")).entity(result).build(); } @Path("/read") @@ -71,10 +65,7 @@ public Response createFile() throws Exception { public Response readFile() { try { String content = producerTemplate.requestBody( - "dropbox://get?accessToken={{DROPBOX_ACCESS_TOKEN}}&clientIdentifier={{DROPBOX_CLIENT_IDENTIFIER}}&remotePath=" - + REMOTE_PATH - + FILE_NAME, - null, + "dropbox://get?" + getCredentialsUriOptions() + "&remotePath=" + REMOTE_PATH + FILE_NAME, null, String.class); if (content != null) { return Response.ok(content).build(); @@ -93,11 +84,17 @@ public Response readFile() { @Path("/delete") @DELETE public Response deleteFile() { - producerTemplate - .requestBody( - "dropbox://del?accessToken={{DROPBOX_ACCESS_TOKEN}}&clientIdentifier={{DROPBOX_CLIENT_IDENTIFIER}}&remotePath=" - + REMOTE_PATH + FILE_NAME, - (Object) null); + producerTemplate.requestBody("dropbox://del?" + getCredentialsUriOptions() + "&remotePath=" + REMOTE_PATH + FILE_NAME, + (Object) null); return Response.status(Response.Status.NO_CONTENT).build(); } + + private String getCredentialsUriOptions() { + return "accessToken={{DROPBOX_ACCESS_TOKEN}}" + + "&clientIdentifier={{DROPBOX_CLIENT_IDENTIFIER}}" + + "&refreshToken={{DROPBOX_REFRESH_TOKEN}}" + + "&apiKey={{DROPBOX_API_KEY}}" + + "&apiSecret={{DROPBOX_API_SECRET}}" + + "&expireIn={{DROPBOX_REFRESH_TOKEN_EXPIRES_IN}}"; + } } diff --git a/integration-tests/dropbox/src/test/java/org/apache/camel/quarkus/component/dropbox/it/DropboxTest.java b/integration-tests/dropbox/src/test/java/org/apache/camel/quarkus/component/dropbox/it/DropboxTest.java index b9e4f5a414c1..264a5796630f 100644 --- a/integration-tests/dropbox/src/test/java/org/apache/camel/quarkus/component/dropbox/it/DropboxTest.java +++ b/integration-tests/dropbox/src/test/java/org/apache/camel/quarkus/component/dropbox/it/DropboxTest.java @@ -25,7 +25,11 @@ import static org.hamcrest.Matchers.is; @EnabledIfEnvironmentVariable(named = "DROPBOX_ACCESS_TOKEN", matches = ".+") +@EnabledIfEnvironmentVariable(named = "DROPBOX_API_KEY", matches = ".+") +@EnabledIfEnvironmentVariable(named = "DROPBOX_API_SECRET", matches = ".+") @EnabledIfEnvironmentVariable(named = "DROPBOX_CLIENT_IDENTIFIER", matches = ".+") +@EnabledIfEnvironmentVariable(named = "DROPBOX_REFRESH_TOKEN", matches = ".+") +@EnabledIfEnvironmentVariable(named = "DROPBOX_REFRESH_TOKEN_EXPIRES_IN", matches = ".+") @QuarkusTest class DropboxTest {