Skip to content

Commit

Permalink
add REST Assured tests for access facet #3776
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Jun 29, 2017
1 parent b4a5654 commit e4f10fa
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
76 changes: 73 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

}


Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit e4f10fa

Please sign in to comment.