Skip to content

Commit

Permalink
Merge pull request #10020 from bencomp/9619-guestbook-responses
Browse files Browse the repository at this point in the history
Lookup dataverse by alias or ID when downloading guestbook responses via API
  • Loading branch information
kcondon authored Oct 18, 2023
2 parents 7a2aae9 + 7d7d731 commit 75d19d5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -1173,8 +1173,9 @@ public Response getGroupByOwnerAndAliasInOwner(@Context ContainerRequestContext
public Response getGuestbookResponsesByDataverse(@Context ContainerRequestContext crc, @PathParam("identifier") String dvIdtf,
@QueryParam("guestbookId") Long gbId, @Context HttpServletResponse response) {

Dataverse dv;
try {
Dataverse dv = findDataverseOrDie(dvIdtf);
dv = findDataverseOrDie(dvIdtf);
User u = getRequestUser(crc);
DataverseRequest req = createDataverseRequest(u);
if (permissionSvc.request(req)
Expand All @@ -1194,16 +1195,14 @@ public Response getGuestbookResponsesByDataverse(@Context ContainerRequestContex
public void write(OutputStream os) throws IOException,
WebApplicationException {

Dataverse dv = dataverseService.findByAlias(dvIdtf);
Map<Integer, Object> customQandAs = guestbookResponseService.mapCustomQuestionAnswersAsStrings(dv.getId(), gbId);
Map<Integer, String> datasetTitles = guestbookResponseService.mapDatasetTitles(dv.getId());

List<Object[]> guestbookResults = guestbookResponseService.getGuestbookResults(dv.getId(), gbId);
os.write("Guestbook, Dataset, Dataset PID, Date, Type, File Name, File Id, File PID, User Name, Email, Institution, Position, Custom Questions\n".getBytes());
for (Object[] result : guestbookResults) {
StringBuilder sb = guestbookResponseService.convertGuestbookResponsesToCSV(customQandAs, datasetTitles, result);
os.write(sb.toString().getBytes());

}
}
};
Expand Down
46 changes: 43 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/DataversesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import static jakarta.ws.rs.core.Response.Status.CREATED;
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import jakarta.ws.rs.core.Response.Status;
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
import static jakarta.ws.rs.core.Response.Status.OK;
import static jakarta.ws.rs.core.Response.Status.CREATED;
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
import static jakarta.ws.rs.core.Response.Status.FORBIDDEN;
import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -144,6 +146,44 @@ public void testMinimalDataverse() throws FileNotFoundException {
deleteDataverse.then().assertThat().statusCode(OK.getStatusCode());
}

/**
* A regular user can create a Dataverse Collection and access its
* GuestbookResponses by DV alias or ID.
* A request for a non-existent Dataverse's GuestbookResponses returns
* Not Found.
* A regular user cannot access the guestbook responses for a Dataverse
* that they do not have permissions for, like the root Dataverse.
*/
@Test
public void testGetGuestbookResponses() {
Response createUser = UtilIT.createRandomUser();
createUser.prettyPrint();
String apiToken = UtilIT.getApiTokenFromResponse(createUser);

Response create = UtilIT.createRandomDataverse(apiToken);
create.prettyPrint();
create.then().assertThat().statusCode(CREATED.getStatusCode());
String alias = UtilIT.getAliasFromResponse(create);
Integer dvId = UtilIT.getDataverseIdFromResponse(create);

logger.info("Request guestbook responses for non-existent Dataverse");
Response getResponsesByBadAlias = UtilIT.getGuestbookResponses("-1", null, apiToken);
getResponsesByBadAlias.then().assertThat().statusCode(NOT_FOUND.getStatusCode());

logger.info("Request guestbook responses for existent Dataverse by alias");
Response getResponsesByAlias = UtilIT.getGuestbookResponses(alias, null, apiToken);
getResponsesByAlias.then().assertThat().statusCode(OK.getStatusCode());

logger.info("Request guestbook responses for existent Dataverse by ID");
Response getResponsesById = UtilIT.getGuestbookResponses(dvId.toString(), null, apiToken);
getResponsesById.then().assertThat().statusCode(OK.getStatusCode());

logger.info("Request guestbook responses for root Dataverse by alias");
getResponsesById = UtilIT.getGuestbookResponses("root", null, apiToken);
getResponsesById.prettyPrint();
getResponsesById.then().assertThat().statusCode(FORBIDDEN.getStatusCode());
}

@Test
public void testNotEnoughJson() {
Response createUser = UtilIT.createRandomUser();
Expand Down
13 changes: 11 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ static String getAliasFromResponse(Response createDataverseResponse) {
static Integer getDataverseIdFromResponse(Response createDataverseResponse) {
JsonPath createdDataverse = JsonPath.from(createDataverseResponse.body().asString());
int dataverseId = createdDataverse.getInt("data.id");
logger.info("Id found in create dataverse response: " + createdDataverse);
logger.info("Id found in create dataverse response: " + dataverseId);
return dataverseId;
}

Expand Down Expand Up @@ -397,7 +397,16 @@ static Response showDataverseContents(String alias, String apiToken) {
.header(API_TOKEN_HTTP_HEADER, apiToken)
.when().get("/api/dataverses/" + alias + "/contents");
}


static Response getGuestbookResponses(String dataverseAlias, Long guestbookId, String apiToken) {
RequestSpecification requestSpec = given()
.header(API_TOKEN_HTTP_HEADER, apiToken);
if (guestbookId != null) {
requestSpec.queryParam("guestbookId", guestbookId);
}
return requestSpec.get("/api/dataverses/" + dataverseAlias + "/guestbookResponses/");
}

static Response createRandomDatasetViaNativeApi(String dataverseAlias, String apiToken) {
return createRandomDatasetViaNativeApi(dataverseAlias, apiToken, false);
}
Expand Down

0 comments on commit 75d19d5

Please sign in to comment.