Skip to content

Commit

Permalink
add tests for direct (and non-direct) download #6783
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Nov 20, 2023
1 parent bbf6789 commit 20bbc70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ services:
-Ddataverse.files.localstack1.bucket-name=mybucket
-Ddataverse.files.localstack1.path-style-access=true
-Ddataverse.files.localstack1.upload-redirect=true
-Ddataverse.files.localstack1.download-redirect=true
-Ddataverse.files.localstack1.access-key=default
-Ddataverse.files.localstack1.secret-key=default
-Ddataverse.files.minio1.type=s3
Expand All @@ -39,6 +40,7 @@ services:
-Ddataverse.files.minio1.bucket-name=mybucket
-Ddataverse.files.minio1.path-style-access=true
-Ddataverse.files.minio1.upload-redirect=false
-Ddataverse.files.minio1.download-redirect=false
-Ddataverse.files.minio1.access-key=minioadmin
-Ddataverse.files.minio1.secret-key=minioadmin
ports:
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/S3AccessIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.amazonaws.services.s3.model.HeadBucketRequest;
import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
import io.restassured.http.Header;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
Expand Down Expand Up @@ -176,6 +177,13 @@ public void testNonDirectUpload() {
// The file uploaded above only contains the character "a".
assertEquals("a".trim(), s3Object.trim());

System.out.println("non-direct download...");
Response downloadFile = UtilIT.downloadFile(Integer.valueOf(fileId), apiToken);
downloadFile.then().assertThat().statusCode(200);

String contentsOfDownloadedFile = downloadFile.getBody().asString();
assertEquals("a\n", contentsOfDownloadedFile);

Response deleteFile = UtilIT.deleteFileApi(Integer.parseInt(fileId), apiToken);
deleteFile.prettyPrint();
deleteFile.then().assertThat().statusCode(200);
Expand Down Expand Up @@ -344,6 +352,28 @@ public void testDirectUpload() {
// assertEquals(contentsOfFile.trim(), s3Object.trim());
assertEquals(contentsOfFile, s3Object);

System.out.println("direct download...");
Response getHeaders = downloadFileNoRedirect(Integer.valueOf(fileId), apiToken);
for (Header header : getHeaders.getHeaders()) {
System.out.println("direct download header: " + header);
}
getHeaders.then().assertThat().statusCode(303);

String urlFromResponse = getHeaders.getHeader("Location");
String localhostDownloadUrl = urlFromResponse.replace("localstack", "localhost");
String decodedDownloadUrl = null;
try {
decodedDownloadUrl = URLDecoder.decode(localhostDownloadUrl, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException ex) {
}

Response downloadFile = downloadFromUrl(decodedDownloadUrl);
downloadFile.prettyPrint();
downloadFile.then().assertThat().statusCode(200);

String contentsOfDownloadedFile = downloadFile.getBody().asString();
assertEquals(contentsOfFile, contentsOfDownloadedFile);

Response deleteFile = UtilIT.deleteFileApi(Integer.parseInt(fileId), apiToken);
deleteFile.prettyPrint();
deleteFile.then().assertThat().statusCode(200);
Expand Down Expand Up @@ -402,4 +432,13 @@ static Response uploadFileDirect(String url, InputStream inputStream) {
.put(url);
}

static Response downloadFileNoRedirect(Integer fileId, String apiToken) {
return given().when().redirects().follow(false)
.get("/api/access/datafile/" + fileId + "?key=" + apiToken);
}

static Response downloadFromUrl(String url) {
return given().get(url);
}

}

0 comments on commit 20bbc70

Please sign in to comment.