Skip to content

Commit

Permalink
JAMES-2214 Fix mailbox not found invalid lambda in SetMessages::create
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Nov 15, 2017
1 parent 903ead0 commit f85b5c6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Expand Up @@ -120,10 +120,13 @@ public abstract class SetMessagesMethodTest {
private static final String USERS_DOMAIN = "domain.tld";
private static final String USERNAME = "username@" + USERS_DOMAIN;
private static final String PASSWORD = "password";
private static final String BOB = "bob@" + USERS_DOMAIN;
private static final String BOB_PASSWORD = "bobPassword";
private static final MailboxPath USER_MAILBOX = MailboxPath.forUser(USERNAME, "mailbox");
private static final String NOT_UPDATED = ARGUMENTS + ".notUpdated";

private ConditionFactory calmlyAwait;
private AccessToken bobAccessToken;

protected abstract GuiceJamesServer createJmapServer();

Expand Down Expand Up @@ -158,8 +161,10 @@ public void setup() throws Throwable {

dataProbe.addDomain(USERS_DOMAIN);
dataProbe.addUser(USERNAME, PASSWORD);
dataProbe.addUser(BOB, BOB_PASSWORD);
mailboxProbe.createMailbox("#private", USERNAME, DefaultMailboxes.INBOX);
accessToken = HttpJmapAuthentication.authenticateJamesUser(baseUri(), USERNAME, PASSWORD);
bobAccessToken = HttpJmapAuthentication.authenticateJamesUser(baseUri(), BOB, BOB_PASSWORD);

mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, USERNAME, DefaultMailboxes.OUTBOX);
mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, USERNAME, DefaultMailboxes.TRASH);
Expand Down Expand Up @@ -1181,6 +1186,40 @@ public void setMessagesShouldReturnCreatedMessageWithEmptySubjectWhenSubjectIsEm
.body(ARGUMENTS + ".created[\""+messageCreationId+"\"].subject", equalTo(""));
}

@Test
public void setMessagesShouldReturnValidErrorWhenMailboxNotFound() {
String messageCreationId = "creationId1337";
String fromAddress = USERNAME;
String requestBody = "[" +
" [" +
" \"setMessages\","+
" {" +
" \"create\": { \"" + messageCreationId + "\" : {" +
" \"from\": { \"name\": \"Me\", \"email\": \"" + fromAddress + "\"}," +
" \"to\": [{ \"name\": \"BOB\", \"email\": \"someone@example.com\"}]," +
" \"subject\": \"\"," +
" \"mailboxIds\": [\"" + getOutboxId(accessToken) + "\"]" +
" }}" +
" }," +
" \"#0\"" +
" ]" +
"]";

given()
.header("Authorization", bobAccessToken.serialize())
.body(requestBody)
.when()
.post("/jmap")
.then()
.log().ifValidationFails()
.statusCode(200)
.body(NAME, equalTo("messagesSet"))
.body(ARGUMENTS + ".created", aMapWithSize(0))
.body(ARGUMENTS + ".notCreated", aMapWithSize(1))
.body(ARGUMENTS + ".notCreated." + messageCreationId + ".type", equalTo("error"))
.body(ARGUMENTS + ".notCreated." + messageCreationId + ".description", endsWith("can not be found"));
}

@Test
public void setMessagesShouldReturnCreatedMessageWithNonASCIICharactersInSubjectWhenPresent() {
String messageCreationId = "creationId1337";
Expand Down Expand Up @@ -1325,6 +1364,7 @@ public void setMessageWithCreatedMessageShouldSupportKeywordsForFlags() {
.body(ARGUMENTS + ".created[\""+messageCreationId+"\"].keywords.$Flagged", equalTo(true))
;
}

@Test
public void setMessagesShouldSupportArbitraryMessageId() {
String messageCreationId = "1717fcd1-603e-44a5-b2a6-1234dbcd5723";
Expand Down Expand Up @@ -1890,11 +1930,10 @@ public void setMessagesShouldSendMessageToBcc() throws Exception {
dataProbe.addUser(recipientAddress, password);
mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, recipientAddress, DefaultMailboxes.INBOX);

String bccAddress = "bob" + "@" + USERS_DOMAIN;
dataProbe.addUser(bccAddress, password);
String bccAddress = BOB;
mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, bccAddress, DefaultMailboxes.INBOX);
await();
AccessToken bccToken = HttpJmapAuthentication.authenticateJamesUser(baseUri(), bccAddress, password);
AccessToken bccToken = HttpJmapAuthentication.authenticateJamesUser(baseUri(), BOB, BOB_PASSWORD);

String messageCreationId = "creationId1337";
String fromAddress = USERNAME;
Expand Down
Expand Up @@ -57,6 +57,7 @@
import org.slf4j.LoggerFactory;

import com.github.fge.lambdas.Throwing;
import com.github.fge.lambdas.functions.FunctionChainer;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
Expand Down Expand Up @@ -105,9 +106,9 @@ public SetMessagesResponse process(SetMessagesRequest request, MailboxSession ma

private void handleCreate(CreationMessageEntry create, Builder responseBuilder, MailboxSession mailboxSession) {
try {
validateIsUserOwnerOfMailboxes(create, mailboxSession);
validateImplementedFeature(create, mailboxSession);
validateArguments(create, mailboxSession);
validateIsUserOwnerOfMailboxes(create, mailboxSession);
MessageWithId created = handleOutboxMessages(create, mailboxSession);
responseBuilder.created(created.getCreationId(), created.getValue());

Expand Down Expand Up @@ -190,9 +191,10 @@ private void validateArguments(CreationMessageEntry entry, MailboxSession sessio
}

private boolean containsMailboxNotOwn(List<String> mailboxIds, MailboxSession session) {
FunctionChainer<MailboxId, MessageManager> findMailbox = Throwing.function(mailboxId -> mailboxManager.getMailbox(mailboxId, session));
return mailboxIds.stream()
.map(mailboxIdFactory::fromString)
.map(Throwing.function(mailboxId -> mailboxManager.getMailbox(mailboxId, session)))
.map(findMailbox.sneakyThrow())
.map(Throwing.function(MessageManager::getMailboxPath))
.anyMatch(path -> !session.getUser().isSameUser(path.getUser()));
}
Expand Down
Expand Up @@ -62,6 +62,7 @@
import org.apache.james.mailbox.inmemory.InMemoryId;
import org.apache.james.mailbox.mock.MockMailboxSession;
import org.apache.james.mailbox.model.ComposedMessageId;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.model.MailboxId.Factory;
import org.apache.james.mailbox.model.MailboxPath;
import org.apache.james.mailbox.model.MessageId;
Expand Down Expand Up @@ -286,6 +287,8 @@ public void processShouldNotSpoolMailWhenNotSavingToOutbox() throws Exception {
creationMessageBuilder.mailboxId("any-id-but-outbox-id")
.build())
.build();
when(mockedMailboxManager.getMailbox(any(MailboxId.class), any()))
.thenReturn(outbox);

sut.process(notInOutboxCreationRequest, session);

Expand Down Expand Up @@ -320,6 +323,7 @@ public void processShouldNotSendWhenSavingToDrafts() throws Exception {
.create(
creationMessageId, creationMessageBuilder.mailboxId(DRAFTS_ID.serialize()).build())
.build();
when(mockedMailboxManager.getMailbox(any(MailboxId.class), any())).thenReturn(drafts);
sut.process(createMessageInDrafts, session);

// Then
Expand Down

0 comments on commit f85b5c6

Please sign in to comment.