From e4f10fa0f80f7931a04b274cfac59a75d630dd20 Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Thu, 29 Jun 2017 15:46:13 -0400 Subject: [PATCH] add REST Assured tests for access facet #3776 --- .../edu/harvard/iq/dataverse/api/FilesIT.java | 76 ++++++++++++++++++- .../edu/harvard/iq/dataverse/api/UtilIT.java | 9 +++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java b/src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java index 295593a1ea5..2f2bb326205 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java @@ -21,6 +21,7 @@ import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.OK; import static junit.framework.Assert.assertEquals; +import org.hamcrest.CoreMatchers; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.nullValue; import org.hamcrest.Matchers; @@ -33,6 +34,12 @@ public class FilesIT { @BeforeClass public static void setUpClass() { RestAssured.baseURI = UtilIT.getRestAssuredBaseUri(); + + Response removeSearchApiNonPublicAllowed = UtilIT.deleteSetting(SettingsServiceBean.Key.SearchApiNonPublicAllowed); + removeSearchApiNonPublicAllowed.prettyPrint(); + removeSearchApiNonPublicAllowed.then().assertThat() + .statusCode(200); + } @@ -957,12 +964,75 @@ public void testRestrictAddedFile() { .body("data.files[0].dataFile.contentType", equalTo("image/png")) .body("data.files[0].label", equalTo("favicondataverse.png")) .statusCode(OK.getStatusCode()); - - //reset public install + + //reset public install UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, publicInstall); } - + + @Test + public void testAccessFacet() { + msgt("testRestrictFile"); + + UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, "true"); + + String apiToken = createUserGetToken(); + + // Create Dataverse + String dataverseAlias = createDataverseGetAlias(apiToken); + + // Create Dataset + Integer datasetId = createDatasetGetId(dataverseAlias, apiToken); + + // ------------------------- + // Add initial file + // ------------------------- + msg("Add initial file"); + String pathToFile = "src/main/webapp/resources/images/favicondataverse.png"; + Response addResponse = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile, apiToken); + + String successMsgAdd = ResourceBundle.getBundle("Bundle").getString("file.addreplace.success.add"); + + addResponse.then().assertThat() + .body("data.files[0].dataFile.contentType", equalTo("image/png")) + .body("data.files[0].label", equalTo("favicondataverse.png")) + .statusCode(OK.getStatusCode()); + + long fileId = JsonPath.from(addResponse.body().asString()).getLong("data.files[0].dataFile.id"); + + Response searchShouldFindNothingBecauseUnpublished = UtilIT.search("id:datafile_" + fileId + "_draft", apiToken); + searchShouldFindNothingBecauseUnpublished.prettyPrint(); + searchShouldFindNothingBecauseUnpublished.then().assertThat() + // This is normal. "limited to published data" http://guides.dataverse.org/en/4.7/api/search.html + .body("data.total_count", equalTo(0)) + .statusCode(OK.getStatusCode()); + // Let's temporarily allow searching of drafts. + Response enableNonPublicSearch = UtilIT.enableSetting(SettingsServiceBean.Key.SearchApiNonPublicAllowed); + enableNonPublicSearch.then().assertThat() + .statusCode(OK.getStatusCode()); + + Response searchResponse = UtilIT.searchAndShowFacets("id:datafile_" + fileId + "_draft", apiToken); + searchResponse.prettyPrint(); + searchResponse.then().assertThat() + // Now we can search unpublished data. Just for testing! + .body("data.total_count", equalTo(1)) + .body("data.items[0].name", equalTo("favicondataverse.png")) + .body("data.items[0].checksum.type", equalTo("MD5")) + .body("data.facets", CoreMatchers.not(equalTo(null))) + // No "fileAccess" facet because :PublicInstall is set to true. + .body("data.facets[0].publicationStatus", CoreMatchers.not(equalTo(null))) + .statusCode(OK.getStatusCode()); + + Response removeSearchApiNonPublicAllowed = UtilIT.deleteSetting(SettingsServiceBean.Key.SearchApiNonPublicAllowed); + removeSearchApiNonPublicAllowed.prettyPrint(); + removeSearchApiNonPublicAllowed.then().assertThat() + .statusCode(200); + + //reset public install + UtilIT.setSetting(SettingsServiceBean.Key.PublicInstall, "false"); + + } + private void msg(String m){ System.out.println(m); } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java index 05a2659475e..14348780133 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -740,6 +740,15 @@ static Response search(String query, String apiToken) { return requestSpecification.get("/api/search?q=" + query); } + static Response searchAndShowFacets(String query, String apiToken) { + RequestSpecification requestSpecification = given(); + if (apiToken != null) { + requestSpecification = given() + .header(UtilIT.API_TOKEN_HTTP_HEADER, apiToken); + } + return requestSpecification.get("/api/search?q=" + query + "&show_facets=true"); + } + static Response indexClear() { return given() .get("/api/admin/index/clear");