You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2021/04/22 09:21:10 UTC

[GitHub] [james-project] Arsnael commented on a change in pull request #385: JAMES-3520 MDN/send

Arsnael commented on a change in pull request #385:
URL: https://github.com/apache/james-project/pull/385#discussion_r618200304



##########
File path: server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MDNSendMethodContract.scala
##########
@@ -0,0 +1,1796 @@
+/****************************************************************
+ * 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 io.netty.handler.codec.http.HttpHeaderNames.ACCEPT
+import io.restassured.RestAssured._
+import io.restassured.builder.ResponseSpecBuilder
+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.core.Username
+import org.apache.james.jmap.core.ResponseObject.SESSION_STATE
+import org.apache.james.jmap.draft.MessageIdProbe
+import org.apache.james.jmap.http.UserCredential
+import org.apache.james.jmap.rfc8621.contract.Fixture.{BOB, BOB_PASSWORD, DOMAIN, authScheme, baseRequestSpecBuilder, _}
+import org.apache.james.jmap.rfc8621.contract.MDNSendMethodContract.TAG_MDN_MESSAGE_FORMAT
+import org.apache.james.mailbox.MessageManager.AppendCommand
+import org.apache.james.mailbox.model.{MailboxId, MailboxPath, MessageId, MultimailboxesSearchQuery, SearchQuery}
+import org.apache.james.mime4j.codec.DecodeMonitor
+import org.apache.james.mime4j.dom.{Message, Multipart}
+import org.apache.james.mime4j.message.DefaultMessageBuilder
+import org.apache.james.mime4j.stream.{MimeConfig, RawField}
+import org.apache.james.modules.MailboxProbeImpl
+import org.apache.james.utils.DataProbeImpl
+import org.awaitility.Awaitility
+import org.awaitility.Durations.ONE_HUNDRED_MILLISECONDS
+import org.awaitility.core.ConditionFactory
+import org.junit.jupiter.api.{BeforeEach, Tag, Test}
+
+import java.nio.charset.StandardCharsets
+import java.time.Duration
+import java.util.concurrent.TimeUnit
+import scala.jdk.CollectionConverters._
+
+object MDNSendMethodContract {
+  val TAG_MDN_MESSAGE_FORMAT: "MDN_MESSAGE_FORMAT" = "MDN_MESSAGE_FORMAT"
+}
+
+trait MDNSendMethodContract {
+  private lazy val slowPacedPollInterval: Duration = ONE_HUNDRED_MILLISECONDS
+
+  private lazy val calmlyAwait: ConditionFactory = Awaitility.`with`
+    .pollInterval(slowPacedPollInterval)
+    .and.`with`.pollDelay(slowPacedPollInterval)
+    .await
+
+  private lazy val awaitAtMostTenSeconds: ConditionFactory = calmlyAwait.atMost(10, TimeUnit.SECONDS)
+
+  private def getFirstMessageInMailBox(guiceJamesServer: GuiceJamesServer, username: Username): Option[Message] = {
+    val searchByRFC822MessageId: MultimailboxesSearchQuery = MultimailboxesSearchQuery.from(SearchQuery.of(SearchQuery.all())).build
+    val defaultMessageBuilder: DefaultMessageBuilder = new DefaultMessageBuilder
+    defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE)
+    defaultMessageBuilder.setDecodeMonitor(DecodeMonitor.SILENT)
+
+    guiceJamesServer.getProbe(classOf[MailboxProbeImpl]).searchMessage(searchByRFC822MessageId, username.asString(), 100)
+      .asScala.headOption
+      .flatMap(messageId => guiceJamesServer.getProbe(classOf[MessageIdProbe]).getMessages(messageId, username).asScala.headOption)
+      .map(messageResult => defaultMessageBuilder.parseMessage(messageResult.getFullContent.getInputStream))
+  }
+
+  private def buildOriginalMessage(tag : String) :Message =
+    Message.Builder
+      .of
+      .setSubject(s"Subject of original message$tag")
+      .setSender(BOB.asString)
+      .setFrom(BOB.asString)
+      .setTo(ANDRE.asString)
+      .addField(new RawField("Disposition-Notification-To", s"Bob <${BOB.asString()}>"))
+      .setBody(s"Body of mail$tag, that mdn related", StandardCharsets.UTF_8)
+      .build
+
+  def randomMessageId: MessageId
+
+  @BeforeEach
+  def setUp(server: GuiceJamesServer): Unit = {
+    server.getProbe(classOf[DataProbeImpl])
+      .fluent()
+      .addDomain(DOMAIN.asString())
+      .addUser(BOB.asString(), BOB_PASSWORD)
+      .addUser(ANDRE.asString, ANDRE_PASSWORD)
+      .addUser(DAVID.asString, DAVID.asString())
+
+    requestSpecification = baseRequestSpecBuilder(server)
+      .setAuth(authScheme(UserCredential(BOB, BOB_PASSWORD)))
+      .build()
+  }
+
+  @Test
+  def mdnSendShouldBeSuccessAndSendMailSuccessfully(guiceJamesServer: GuiceJamesServer): Unit = {
+    val mailboxProbe: MailboxProbeImpl = guiceJamesServer.getProbe(classOf[MailboxProbeImpl])
+
+    val bobMailBoxPath: MailboxPath = MailboxPath.inbox(BOB)
+    val bobInboxId: MailboxId = mailboxProbe.createMailbox(bobMailBoxPath)
+
+    val andreMailBoxPath: MailboxPath = MailboxPath.inbox(ANDRE)
+    mailboxProbe.createMailbox(andreMailBoxPath)
+
+    val emailIdRelated: MessageId = mailboxProbe
+      .appendMessage(ANDRE.asString(), andreMailBoxPath, AppendCommand.builder()
+        .build(buildOriginalMessage("1")))
+      .getMessageId
+
+    val mdnSendRequest: String =
+      s"""{
+         |  "using": [
+         |    "urn:ietf:params:jmap:core",
+         |    "urn:ietf:params:jmap:mail",
+         |    "urn:ietf:params:jmap:mdn"
+         |  ],
+         |  "methodCalls": [
+         |    [
+         |      "MDN/send",
+         |      {
+         |        "accountId": "$ANDRE_ACCOUNT_ID",
+         |        "identityId": "I64588216",
+         |        "send": {
+         |          "k1546": {
+         |            "forEmailId": "${emailIdRelated.serialize()}",
+         |            "subject": "Read receipt for: World domination",
+         |            "textBody": "This receipt shows that the email has been displayed on your recipient's computer. ",
+         |            "reportingUA": "joes-pc.cs.example.com; Foomail 97.1",
+         |            "disposition": {
+         |              "actionMode": "manual-action",
+         |              "sendingMode": "mdn-sent-manually",
+         |              "type": "displayed"
+         |            },
+         |            "extensionFields": {
+         |              "X-EXTENSION-EXAMPLE": "example.com"
+         |            }
+         |          }
+         |        },
+         |        "onSuccessUpdateEmail": {
+         |          "#k1546": {
+         |            "keywords/$$mdnsent": true
+         |          }
+         |        }
+         |      },
+         |      "c1"
+         |    ]
+         |  ]
+         |}""".stripMargin
+
+   val mdnSendResponse: String =
+      `given`(
+        baseRequestSpecBuilder(guiceJamesServer)
+          .setAuth(authScheme(UserCredential(ANDRE, ANDRE_PASSWORD)))
+          .addHeader(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+          .setBody(mdnSendRequest)
+          .build, new ResponseSpecBuilder().build)
+        .post
+      .`then`
+        .statusCode(SC_OK)
+        .contentType(JSON)
+        .extract
+        .body
+        .asString
+
+    assertThatJson(mdnSendResponse)
+      .whenIgnoringPaths("methodResponses[1][1].newState",
+        "methodResponses[1][1].oldState")
+      .isEqualTo(
+        s"""{
+           |    "sessionState": "${SESSION_STATE.value}",
+           |    "methodResponses": [
+           |        [
+           |            "MDN/send",
+           |            {
+           |                "accountId": "$ANDRE_ACCOUNT_ID",
+           |                "sent": {
+           |                    "k1546": {
+           |                        "finalRecipient": "rfc822; ${BOB.asString}",
+           |                        "includeOriginalMessage": false,
+           |                        "originalRecipient": "rfc822; ${BOB.asString()}"
+           |                    }
+           |                }
+           |            },
+           |            "c1"
+           |        ],
+           |        [
+           |            "Email/set",
+           |            {
+           |                "accountId": "$ANDRE_ACCOUNT_ID",
+           |                "oldState": "23",
+           |                "newState": "42",
+           |                "updated": {
+           |                    "${emailIdRelated.serialize()}": null
+           |                }
+           |            },
+           |            "c1"
+           |        ]
+           |    ]
+           |}""".stripMargin)
+
+    val requestQueryMDNMessage: String =
+      s"""{
+         |  "using": ["urn:ietf:params:jmap:core","urn:ietf:params:jmap:mail"],
+         |  "methodCalls": [[
+         |    "Email/query",
+         |    {
+         |      "accountId": "$ACCOUNT_ID",
+         |      "filter": {"inMailbox": "${bobInboxId.serialize}"}
+         |    },
+         |    "c1"]]
+         |}""".stripMargin
+
+    awaitAtMostTenSeconds.untilAsserted { () =>
+      val response: String =
+        `given`(
+          baseRequestSpecBuilder(guiceJamesServer)
+            .addHeader(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+            .setBody(requestQueryMDNMessage)
+            .build, new ResponseSpecBuilder().build)

Review comment:
       We only need to redefine the `baseRequestSpecBuilder` when we want to call jmap with an other user than the one defined in the setup for the requestSpecification (here BOB). 
   
   You can do here for example: 
   ```
   `given`()
       .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
       .body(requestQueryMDNMessage)
       .post()
   .then()
   [...]
   ```

##########
File path: server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/MDNSendMethodContract.scala
##########
@@ -0,0 +1,1796 @@
+/****************************************************************
+ * 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 io.netty.handler.codec.http.HttpHeaderNames.ACCEPT
+import io.restassured.RestAssured._
+import io.restassured.builder.ResponseSpecBuilder
+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.core.Username
+import org.apache.james.jmap.core.ResponseObject.SESSION_STATE
+import org.apache.james.jmap.draft.MessageIdProbe
+import org.apache.james.jmap.http.UserCredential
+import org.apache.james.jmap.rfc8621.contract.Fixture.{BOB, BOB_PASSWORD, DOMAIN, authScheme, baseRequestSpecBuilder, _}
+import org.apache.james.jmap.rfc8621.contract.MDNSendMethodContract.TAG_MDN_MESSAGE_FORMAT
+import org.apache.james.mailbox.MessageManager.AppendCommand
+import org.apache.james.mailbox.model.{MailboxId, MailboxPath, MessageId, MultimailboxesSearchQuery, SearchQuery}
+import org.apache.james.mime4j.codec.DecodeMonitor
+import org.apache.james.mime4j.dom.{Message, Multipart}
+import org.apache.james.mime4j.message.DefaultMessageBuilder
+import org.apache.james.mime4j.stream.{MimeConfig, RawField}
+import org.apache.james.modules.MailboxProbeImpl
+import org.apache.james.utils.DataProbeImpl
+import org.awaitility.Awaitility
+import org.awaitility.Durations.ONE_HUNDRED_MILLISECONDS
+import org.awaitility.core.ConditionFactory
+import org.junit.jupiter.api.{BeforeEach, Tag, Test}
+
+import java.nio.charset.StandardCharsets
+import java.time.Duration
+import java.util.concurrent.TimeUnit
+import scala.jdk.CollectionConverters._
+
+object MDNSendMethodContract {
+  val TAG_MDN_MESSAGE_FORMAT: "MDN_MESSAGE_FORMAT" = "MDN_MESSAGE_FORMAT"
+}
+
+trait MDNSendMethodContract {
+  private lazy val slowPacedPollInterval: Duration = ONE_HUNDRED_MILLISECONDS
+
+  private lazy val calmlyAwait: ConditionFactory = Awaitility.`with`
+    .pollInterval(slowPacedPollInterval)
+    .and.`with`.pollDelay(slowPacedPollInterval)
+    .await
+
+  private lazy val awaitAtMostTenSeconds: ConditionFactory = calmlyAwait.atMost(10, TimeUnit.SECONDS)
+
+  private def getFirstMessageInMailBox(guiceJamesServer: GuiceJamesServer, username: Username): Option[Message] = {
+    val searchByRFC822MessageId: MultimailboxesSearchQuery = MultimailboxesSearchQuery.from(SearchQuery.of(SearchQuery.all())).build
+    val defaultMessageBuilder: DefaultMessageBuilder = new DefaultMessageBuilder
+    defaultMessageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE)
+    defaultMessageBuilder.setDecodeMonitor(DecodeMonitor.SILENT)
+
+    guiceJamesServer.getProbe(classOf[MailboxProbeImpl]).searchMessage(searchByRFC822MessageId, username.asString(), 100)
+      .asScala.headOption
+      .flatMap(messageId => guiceJamesServer.getProbe(classOf[MessageIdProbe]).getMessages(messageId, username).asScala.headOption)
+      .map(messageResult => defaultMessageBuilder.parseMessage(messageResult.getFullContent.getInputStream))
+  }
+
+  private def buildOriginalMessage(tag : String) :Message =
+    Message.Builder
+      .of
+      .setSubject(s"Subject of original message$tag")
+      .setSender(BOB.asString)
+      .setFrom(BOB.asString)
+      .setTo(ANDRE.asString)
+      .addField(new RawField("Disposition-Notification-To", s"Bob <${BOB.asString()}>"))
+      .setBody(s"Body of mail$tag, that mdn related", StandardCharsets.UTF_8)
+      .build
+
+  def randomMessageId: MessageId
+
+  @BeforeEach
+  def setUp(server: GuiceJamesServer): Unit = {
+    server.getProbe(classOf[DataProbeImpl])
+      .fluent()
+      .addDomain(DOMAIN.asString())
+      .addUser(BOB.asString(), BOB_PASSWORD)
+      .addUser(ANDRE.asString, ANDRE_PASSWORD)
+      .addUser(DAVID.asString, DAVID.asString())
+
+    requestSpecification = baseRequestSpecBuilder(server)
+      .setAuth(authScheme(UserCredential(BOB, BOB_PASSWORD)))

Review comment:
       Why not putting ANDRE user here actually after thinking?
   
   It seems you are making calls with the ANDRE user in all your tests, while you make maybe only one or two calls with BOB.
   
   Then you could apply my above comment a bit everywhere, allowing you reduce a bit the number of lines here




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org