Skip to content

Commit

Permalink
JAMES-1721 Rename a mailbox to a system Mailbox is not possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Laura-Royet committed Apr 25, 2016
1 parent 80c6001 commit 39928fd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Expand Up @@ -1476,4 +1476,40 @@ public void setMailboxesShouldReturnNotUpdatedWhenRenamingSystemMailbox() {
hasEntry(equalTo("type"), equalTo("invalidArguments")),
hasEntry(equalTo("description"), equalTo("Cannot update a system mailbox.")))));
}

@Test
public void setMailboxesShouldReturnNotUpdatedWhenRenameToSystemMailboxName() {

jmapServer.serverProbe().createMailbox("#private", username, "myBox");
Mailbox<?> mailboxMyBox = jmapServer.serverProbe().getMailbox("#private", username, "myBox");
String mailboxIdMyBox = mailboxMyBox.getMailboxId().serialize();

String requestBody =
"[" +
" [ \"setMailboxes\"," +
" {" +
" \"update\": {" +
" \"" + mailboxIdMyBox + "\" : {" +
" \"name\" : \"outbox\"" +
" }" +
" }" +
" }," +
" \"#0\"" +
" ]" +
"]";

given()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.header("Authorization", this.accessToken.serialize())
.body(requestBody)
.when()
.post("/jmap")
.then()
.statusCode(200)
.body(NAME, equalTo("mailboxesSet"))
.body(ARGUMENTS + ".notUpdated", hasEntry(equalTo(mailboxIdMyBox), Matchers.allOf(
hasEntry(equalTo("type"), equalTo("invalidArguments")),
hasEntry(equalTo("description"), equalTo("The mailbox 'outbox' is a system mailbox.")))));
}
}
Expand Up @@ -130,11 +130,26 @@ private Mailbox getMailbox(String mailboxId, MailboxSession mailboxSession) thro

private void validateMailboxName(MailboxUpdateRequest updateRequest, MailboxSession mailboxSession) throws MailboxNameException {
char pathDelimiter = mailboxSession.getPathDelimiter();
if (updateRequest.getName()
.filter(name -> name.contains(String.valueOf(pathDelimiter)))
.isPresent()) {

if (nameContainsPathDelimiter(updateRequest, pathDelimiter)) {
throw new MailboxNameException(String.format("The mailbox '%s' contains an illegal character: '%c'", updateRequest.getName().get(), pathDelimiter));
}
if (nameMatchesSystemMailbox(updateRequest)) {
throw new MailboxNameException(String.format("The mailbox '%s' is a system mailbox.", updateRequest.getName().get()));
}
}

private boolean nameMatchesSystemMailbox(MailboxUpdateRequest updateRequest) {
return updateRequest.getName()
.flatMap(Role::from)
.filter(Role::isSystemRole)
.isPresent();
}

private boolean nameContainsPathDelimiter(MailboxUpdateRequest updateRequest, char pathDelimiter) {
return updateRequest.getName()
.filter(name -> name.contains(String.valueOf(pathDelimiter)))
.isPresent() ;
}

private void validateParent(Mailbox mailbox, MailboxUpdateRequest updateRequest, MailboxSession mailboxSession) throws MailboxException, MailboxHasChildException {
Expand Down

0 comments on commit 39928fd

Please sign in to comment.