Skip to content

Commit

Permalink
Adapt Dropbox tests to new authentication mechanism
Browse files Browse the repository at this point in the history
Fixes #4021
  • Loading branch information
jamesnetherton committed Aug 25, 2022
1 parent 74736d4 commit 83aa008
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
9 changes: 9 additions & 0 deletions integration-tests/dropbox/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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();
Expand All @@ -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}}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 83aa008

Please sign in to comment.