Arsnael commented on code in PR #2177: URL: https://github.com/apache/james-project/pull/2177#discussion_r1558770897
########## server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/ImapKeywordsConsistencyContract.scala: ########## @@ -0,0 +1,614 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.rfc8621.contract + +import java.nio.charset.StandardCharsets +import java.util +import java.util.concurrent.TimeUnit + +import io.netty.handler.codec.http.HttpHeaderNames.ACCEPT +import io.restassured.RestAssured +import io.restassured.RestAssured.`given` +import io.restassured.http.ContentType.JSON +import net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson +import org.apache.http.HttpStatus.SC_OK +import org.apache.james.GuiceJamesServer +import org.apache.james.jmap.JMAPTestingConstants.{DOMAIN, LOCALHOST_IP} +import org.apache.james.jmap.http.UserCredential +import org.apache.james.jmap.rfc8621.contract.Fixture.{ACCEPT_RFC8621_VERSION_HEADER, ACCOUNT_ID, ANDRE, BOB, BOB_PASSWORD, authScheme, baseRequestSpecBuilder} +import org.apache.james.jmap.rfc8621.contract.ImapKeywordsConsistencyContract.bobInboxPath +import org.apache.james.mailbox.DefaultMailboxes +import org.apache.james.mailbox.MessageManager.AppendCommand +import org.apache.james.mailbox.model.{MailboxConstants, MailboxPath, MessageId} +import org.apache.james.mime4j.dom.Message +import org.apache.james.modules.MailboxProbeImpl +import org.apache.james.modules.protocols.ImapGuiceProbe +import org.apache.james.utils.{DataProbeImpl, TestIMAPClient} +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility +import org.awaitility.Durations.ONE_HUNDRED_MILLISECONDS +import org.junit.jupiter.api.{BeforeEach, Disabled, Test} +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +import scala.jdk.CollectionConverters._ + +object ImapKeywordsConsistencyContract { + private val bobInboxPath = MailboxPath.forUser(BOB, DefaultMailboxes.INBOX) +} + +trait ImapKeywordsConsistencyContract { + private lazy val slowPacedPollInterval = ONE_HUNDRED_MILLISECONDS + private lazy val calmlyAwait = Awaitility.`with` + .pollInterval(slowPacedPollInterval) + .and.`with`.pollDelay(slowPacedPollInterval) + .await + private lazy val awaitAtMostOneMinute = calmlyAwait.atMost(1, TimeUnit.MINUTES) + + def imapClient: TestIMAPClient + + @BeforeEach + def setUp(server: GuiceJamesServer): Unit = { + server.getProbe(classOf[DataProbeImpl]) + .fluent + .addDomain(DOMAIN) + .addUser(BOB.asString, BOB_PASSWORD) + + val mailboxProbe = server.getProbe(classOf[MailboxProbeImpl]) + mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, BOB.asString, DefaultMailboxes.INBOX) + mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, BOB.asString, DefaultMailboxes.ARCHIVE) + mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, BOB.asString, DefaultMailboxes.TRASH) + + RestAssured.requestSpecification = baseRequestSpecBuilder(server) + .setAuth(authScheme(UserCredential(BOB, BOB_PASSWORD))) + .build + } + + @ParameterizedTest + @ValueSource(strings = Array( + DefaultMailboxes.INBOX, + DefaultMailboxes.ARCHIVE + )) + def emailGetShouldUnionKeywordsWhenInconsistencyCreatedViaImap(mailbox: String, server: GuiceJamesServer): Unit = { + // Given the user has a message "m1" in "inbox" mailbox with subject "My awesome subject", content "This is the content" + val messageId = appendMessageToInbox(server) + + // And bob copies "m1" from mailbox "inbox" to mailbox "archive" + copyMessageFromInboxToArchive(server, messageId) + awaitAtMostOneMinute.until(() => listMessageIdsArchive(server).size == 1) + + // And the user has an open IMAP connection with mailbox "<mailbox>" selected + imapClient.connect(LOCALHOST_IP, server.getProbe(classOf[ImapGuiceProbe]).getImapPort) + .login(BOB, BOB_PASSWORD) + .select(mailbox) + + // And the user set flags via IMAP to "(\Flagged)" for all messages in mailbox "<mailbox>" + imapClient.setFlagsForAllMessagesInMailbox("\\Flagged") + + // When the user ask for message "m1" + // Then no error is returned + // And the list should contain 1 message + // And the id of the message is "m1" + // And the keywords of the message is "(\Flagged)" + val ids = listMessageIds().asScala.toList + + assertThat(ids.size).isEqualTo(1) + assertThat(ids).isEqualTo(List(messageId.serialize)) + + val idString = concatMessageIds(ids) + val response = getMessagesByIds(idString) + + assertThatJson(response) + .inPath("methodResponses[0][1].list[0].keywords") + .isEqualTo("{\"$flagged\": true}") + } + + @Disabled("No intersection on JMAP RFC, all keywords are just unioned") + @ParameterizedTest + @ValueSource(strings = Array( + DefaultMailboxes.INBOX, + DefaultMailboxes.ARCHIVE + )) + def emailGetShouldIntersectDraftWhenInconsistencyCreatedViaImap(mailbox: String, server: GuiceJamesServer): Unit = { + // Given the user has a message "m1" in "inbox" mailbox with subject "My awesome subject", content "This is the content" + val messageId = appendMessageToInbox(server) + + // And user copies "m1" from mailbox "inbox" to mailbox "archive" + copyMessageFromInboxToArchive(server, messageId) + awaitAtMostOneMinute.until(() => listMessageIdsArchive(server).size == 1) + + // And the user has an open IMAP connection with mailbox "<mailbox>" selected + imapClient.connect(LOCALHOST_IP, server.getProbe(classOf[ImapGuiceProbe]).getImapPort) + .login(BOB, BOB_PASSWORD) + .select(mailbox) + + // And the user set flags via IMAP to "(\Draft)" for all messages in mailbox "<mailbox>" + imapClient.setFlagsForAllMessagesInMailbox("\\Draft") + + // When the user ask for message "m1" + // Then no error is returned + // And the list should contain 1 message + // And the id of the message is "m1" + // And the keywords of the message is <keyword> + val ids = listMessageIds().asScala.toList + + assertThat(ids.size).isEqualTo(1) + assertThat(ids).isEqualTo(List(messageId.serialize)) + + val idString = concatMessageIds(ids) + val response = getMessagesByIds(idString) + + assertThatJson(response) + .inPath("methodResponses[0][1].list[0].keywords") + .isEqualTo("") + } + + @Test + def emailQueryShouldReturnMatchingMessageIdWhenMatchingInAtLeastOneMailbox(server: GuiceJamesServer): Unit = { + // Given the user has a message "m1" in "inbox" mailbox with subject "My awesome subject", content "This is the content" + val messageId = appendMessageToInbox(server) + + // And user copies "m1" from mailbox "inbox" to mailbox "archive" + copyMessageFromInboxToArchive(server, messageId) + awaitAtMostOneMinute.until(() => listMessageIdsArchive(server).size == 1) + + // And the user has an open IMAP connection with mailbox "archive" selected + imapClient.connect(LOCALHOST_IP, server.getProbe(classOf[ImapGuiceProbe]).getImapPort) + .login(BOB, BOB_PASSWORD) + .select(DefaultMailboxes.ARCHIVE) + + // And the user set flags via IMAP to "(\Flagged)" for all messages in mailbox "archive" + imapClient.setFlagsForAllMessagesInMailbox("\\Flagged") + + // When the user asks for message list with flag "$Flagged" + val ids = listMessageIdsBykeyword("$Flagged").asScala.toList + + // Then the message list has size 1 + // And the message list contains "m1" + assertThat(ids.size).isEqualTo(1) + assertThat(ids).isEqualTo(List(messageId.serialize)) + } + + @Test + def emailQueryInSpecificMailboxShouldReturnMessageIdWhenMatching(server: GuiceJamesServer): Unit = { + // Given the user has a message "m1" in "inbox" mailbox with subject "My awesome subject", content "This is the content" + val messageId = appendMessageToInbox(server) + + // And user copies "m1" from mailbox "inbox" to mailbox "archive" + copyMessageFromInboxToArchive(server, messageId) + awaitAtMostOneMinute.until(() => listMessageIdsArchive(server).size == 1) + + // And the user has an open IMAP connection with mailbox "archive" selected + imapClient.connect(LOCALHOST_IP, server.getProbe(classOf[ImapGuiceProbe]).getImapPort) + .login(BOB, BOB_PASSWORD) + .select(DefaultMailboxes.ARCHIVE) + + // And the user set flags via IMAP to "(\Flagged)" for all messages in mailbox "archive" + imapClient.setFlagsForAllMessagesInMailbox("\\Flagged") + + // When user asks for message list in mailbox "archive" with flag "$Flagged" + val ids = listMessageIdsByMailboxAndKeyword(server, DefaultMailboxes.ARCHIVE, "$Flagged").asScala.toList + + // Then the message list has size 1 + // And the message list contains "m1" + assertThat(ids.size).isEqualTo(1) + assertThat(ids).isEqualTo(List(messageId.serialize)) + } + + @Test + def emailQueryInSpecificMailboxShouldSkipMessageIdWhenNotMatching(server: GuiceJamesServer): Unit = { + // Given the user has a message "m1" in "inbox" mailbox with subject "My awesome subject", content "This is the content" + val messageId = appendMessageToInbox(server) + + // And user copies "m1" from mailbox "inbox" to mailbox "archive" + copyMessageFromInboxToArchive(server, messageId) + awaitAtMostOneMinute.until(() => listMessageIdsArchive(server).size == 1) + + // And the user has an open IMAP connection with mailbox "archive" selected + imapClient.connect(LOCALHOST_IP, server.getProbe(classOf[ImapGuiceProbe]).getImapPort) + .login(BOB, BOB_PASSWORD) + .select(DefaultMailboxes.ARCHIVE) + + // And the user set flags via IMAP to "(\Flagged)" for all messages in mailbox "archive" + imapClient.setFlagsForAllMessagesInMailbox("\\Flagged") + + // When user asks for message list in mailbox "inbox" with flag "$Flagged" + val ids = listMessageIdsByMailboxAndKeyword(server, DefaultMailboxes.INBOX, "$Flagged").asScala.toList + + // Then the message list is empty + assertThat(ids.size).isEqualTo(0) + } + + @Disabled("TODO: Fix!") + @Test + def emailSetShouldSucceedToSolveKeywordsConflictsIntroducedViaImapUponFlagsAddition(server: GuiceJamesServer): Unit = { Review Comment: That one is a tricky one, tried to solve it quickly but every time it created problems elsewhere... I think it's better to leave it disabled for now, and fix it in a following PR, so we can move on with the jmap draft removal -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
