Skip to content

Commit

Permalink
70402: Tests for replace /metadata PATCH of own metadata at /eperson/…
Browse files Browse the repository at this point in the history
…epersons/<id>
  • Loading branch information
MarieVerdonck committed Apr 15, 2020
1 parent 5727d09 commit bb1d5bd
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.dspace.app.rest.matcher.EPersonMatcher;
import org.dspace.app.rest.matcher.GroupMatcher;
import org.dspace.app.rest.matcher.HalMatcher;
import org.dspace.app.rest.matcher.MetadataMatcher;
import org.dspace.app.rest.model.EPersonRest;
import org.dspace.app.rest.model.MetadataRest;
import org.dspace.app.rest.model.MetadataValueRest;
Expand Down Expand Up @@ -1516,6 +1517,102 @@ private void runPatchMetadataTests(EPerson asUser, int expectedStatus) throws Ex
new MetadataPatchSuite().runWith(getClient(token), "/api/eperson/epersons/" + ePerson.getID(), expectedStatus);
}

@Test
public void patchMetadataByAdmin() throws Exception {

context.turnOffAuthorisationSystem();

EPerson ePerson = EPersonBuilder.createEPerson(context)
.withNameInMetadata("John", "Doe")
.withEmail("Johndoe@fake-email.com")
.build();

String newName = "JohnReplace";

List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation replaceOperation = new ReplaceOperation("/metadata/eperson.firstname", newName);
ops.add(replaceOperation);
String patchBody = getPatchContent(ops);

String token = getAuthToken(admin.getEmail(), password);

// should be allowed, and eperson.firstname should be replaced.
getClient(token).perform(patch("/api/eperson/epersons/" + ePerson.getID())
.content(patchBody)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.metadata", Matchers.allOf(
MetadataMatcher.matchMetadata("eperson.firstname", newName))));
}

@Test
public void patchOwnMetadataByNonAdminUser() throws Exception {

context.turnOffAuthorisationSystem();

EPerson ePerson = EPersonBuilder.createEPerson(context)
.withNameInMetadata("John", "Doe")
.withEmail("Johndoe@fake-email.com")
.withPassword(password)
.build();

String newName = "JohnReplace";

context.restoreAuthSystemState();

List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation replaceOperation = new ReplaceOperation("/metadata/eperson.firstname", newName);
ops.add(replaceOperation);
String patchBody = getPatchContent(ops);

String token = getAuthToken(ePerson.getEmail(), password);

// updates password
getClient(token).perform(patch("/api/eperson/epersons/" + ePerson.getID())
.content(patchBody)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.metadata", Matchers.allOf(
MetadataMatcher.matchMetadata("eperson.firstname", newName))));

}

@Test
public void patchNotOwnMetadataByNonAdminUser() throws Exception {

context.turnOffAuthorisationSystem();

EPerson ePerson = EPersonBuilder.createEPerson(context)
.withNameInMetadata("John", "Doe")
.withEmail("Johndoe@fake-email.com")
.withPassword(password)
.build();

EPerson ePerson2 = EPersonBuilder.createEPerson(context)
.withNameInMetadata("Jane", "Smith")
.withEmail("Janesmith@fake-email.com")
.withPassword(password)
.build();

String newName = "JohnReplace";

context.restoreAuthSystemState();

List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation replaceOperation = new ReplaceOperation("/metadata/eperson.firstname", newName);
ops.add(replaceOperation);
String patchBody = getPatchContent(ops);

String token = getAuthToken(ePerson2.getEmail(), password);

// updates password
getClient(token).perform(patch("/api/eperson/epersons/" + ePerson.getID())
.content(patchBody)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isForbidden());

}

@Test
public void newlyCreatedAccountHasNoGroups() throws Exception {
context.turnOffAuthorisationSystem();
Expand Down

0 comments on commit bb1d5bd

Please sign in to comment.