Skip to content

Commit

Permalink
Merge pull request #8589 from IQSS/8365-ds-move-func-perms
Browse files Browse the repository at this point in the history
8365 ds move func perms
  • Loading branch information
kcondon committed Apr 19, 2022
2 parents 0f4ff80 + b16e1b3 commit 396b7f7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/sphinx-guides/source/admin/dataverses-datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Move a Dataset

Superusers can move datasets using the dashboard. See also :doc:`dashboard`.

Moves a dataset whose id is passed to a Dataverse collection whose alias is passed. If the moved dataset has a guestbook or a Dataverse collection link that is not compatible with the destination Dataverse collection, you will be informed and given the option to force the move (with ``forceMove=true`` as a query parameter) and remove the guestbook or link (or both). Only accessible to users with permission to publish the dataset in the original and destination Dataverse collection. ::
Moves a dataset whose id is passed to a Dataverse collection whose alias is passed. If the moved dataset has a guestbook or a Dataverse collection link that is not compatible with the destination Dataverse collection, you will be informed and given the option to force the move (with ``forceMove=true`` as a query parameter) and remove the guestbook or link (or both). Only accessible to users with permission to publish the dataset in the original and destination Dataverse collection. Note: any roles granted to users on the dataset will continue to be in effect after the dataset has been moved. ::

curl -H "X-Dataverse-key: $API_TOKEN" -X POST http://$SERVER/api/datasets/$id/move/$alias

Expand Down
100 changes: 100 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/MoveIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.path.json.JsonPath;
import static com.jayway.restassured.path.json.JsonPath.with;
import com.jayway.restassured.response.Response;
import edu.harvard.iq.dataverse.authorization.DataverseRole;
import java.io.StringReader;
import java.util.List;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonObject;
Expand All @@ -16,6 +18,7 @@
import org.hamcrest.CoreMatchers;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -308,5 +311,102 @@ public void testMoveLinkedDataset() {
Assert.assertEquals(0, linksAfterData.getJsonObject("data").getJsonArray("dataverses that link to dataset id " + datasetId).size());

}

@Test
public void testMoveDatasetsPerms() {

/*
Verify that permissions set on a dataset remain
after that dataaset is moved
*/
Response createCurator = UtilIT.createRandomUser();
createCurator.prettyPrint();
createCurator.then().assertThat()
.statusCode(OK.getStatusCode());
String curatorUsername = UtilIT.getUsernameFromResponse(createCurator);
String curatorApiToken = UtilIT.getApiTokenFromResponse(createCurator);

Response createRando = UtilIT.createRandomUser();
createCurator.prettyPrint();
createCurator.then().assertThat()
.statusCode(OK.getStatusCode());
String randoUsername = UtilIT.getUsernameFromResponse(createRando);
String randoApiToken = UtilIT.getApiTokenFromResponse(createRando);

Response createCuratorDataverse1 = UtilIT.createRandomDataverse(curatorApiToken);
createCuratorDataverse1.prettyPrint();
createCuratorDataverse1.then().assertThat()
.statusCode(CREATED.getStatusCode());
String curatorDataverseAlias1 = UtilIT.getAliasFromResponse(createCuratorDataverse1);

Response createAuthor = UtilIT.createRandomUser();
createAuthor.prettyPrint();
createAuthor.then().assertThat()
.statusCode(OK.getStatusCode());
String authorUsername = UtilIT.getUsernameFromResponse(createAuthor);
String authorApiToken = UtilIT.getApiTokenFromResponse(createAuthor);

Response grantAuthorAddDataset = UtilIT.grantRoleOnDataverse(curatorDataverseAlias1, DataverseRole.DS_CONTRIBUTOR.toString(), "@" + authorUsername, curatorApiToken);
grantAuthorAddDataset.prettyPrint();
grantAuthorAddDataset.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.assignee", equalTo("@" + authorUsername))
.body("data._roleAlias", equalTo("dsContributor"));

Response createDataset = UtilIT.createRandomDatasetViaNativeApi(curatorDataverseAlias1, authorApiToken);
createDataset.prettyPrint();
createDataset.then().assertThat()
.statusCode(CREATED.getStatusCode());

Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset);

Response datasetAsJson = UtilIT.nativeGet(datasetId, authorApiToken);
datasetAsJson.then().assertThat()
.statusCode(OK.getStatusCode());

String identifier = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.identifier");
assertEquals(10, identifier.length());

String protocol1 = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.protocol");
String authority1 = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.authority");
String identifier1 = JsonPath.from(datasetAsJson.getBody().asString()).getString("data.identifier");
String datasetPersistentId = protocol1 + ":" + authority1 + "/" + identifier1;

Response giveRandoPermission = UtilIT.grantRoleOnDataset(datasetPersistentId, DataverseRole.CURATOR, "@" + randoUsername, curatorApiToken);
giveRandoPermission.prettyPrint();
assertEquals(200, giveRandoPermission.getStatusCode());

Response createAuthorDataverse1 = UtilIT.createRandomDataverse(curatorApiToken);
createAuthorDataverse1.prettyPrint();
createAuthorDataverse1.then().assertThat()
.statusCode(CREATED.getStatusCode());
String authorDataverseAlias1 = UtilIT.getAliasFromResponse(createAuthorDataverse1);

Response createSuperuser = UtilIT.createRandomUser();
createSuperuser.then().assertThat()
.statusCode(OK.getStatusCode());
String superusername = UtilIT.getUsernameFromResponse(createSuperuser);
String superuserApiToken = UtilIT.getApiTokenFromResponse(createSuperuser);
Response makeSuperuser = UtilIT.makeSuperUser(superusername);
makeSuperuser.then().assertThat()
.statusCode(OK.getStatusCode());

Response moveDataset1 = UtilIT.moveDataset(datasetId.toString(), authorDataverseAlias1, superuserApiToken);
moveDataset1.prettyPrint();
moveDataset1.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data.message", equalTo("Dataset moved successfully."));

Response roleAssignments = UtilIT.getRoleAssignmentsOnDataset(datasetId.toString(), null, superuserApiToken);
roleAssignments.prettyPrint();

/*
make sure the rando assigned role continues on the moved dataset
*/
assertEquals(OK.getStatusCode(), roleAssignments.getStatusCode());
List<JsonObject> assignments = with(roleAssignments.body().asString()).param("curator", "curator").getJsonObject("data.findAll { data -> data._roleAlias == curator }");
assertEquals(1, assignments.size());

}

}

0 comments on commit 396b7f7

Please sign in to comment.