You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2020/10/19 03:59:05 UTC
[james-project] 02/02: JAMES-3414 MailboxIds partial updates patch
validation
This is an automated email from the ASF dual-hosted git repository.
rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 572546736945e5ea3ecc45c15302ae2ed2aab0c2
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Oct 15 14:41:26 2020 +0700
JAMES-3414 MailboxIds partial updates patch validation
---
.../rfc8621/contract/EmailSetMethodContract.scala | 199 +++++++++++++++++++++
.../james/jmap/json/EmailSetSerializer.scala | 1 +
.../scala/org/apache/james/jmap/json/package.scala | 4 +-
.../org/apache/james/jmap/mail/EmailSet.scala | 36 ++--
4 files changed, 219 insertions(+), 21 deletions(-)
diff --git a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala
index 1bbfc8f..758d6ac 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSetMethodContract.scala
@@ -514,6 +514,205 @@ trait EmailSetMethodContract {
}
@Test
+ def invalidPatchPropertyShouldFail(server: GuiceJamesServer): Unit = {
+ val mailboxProbe = server.getProbe(classOf[MailboxProbeImpl])
+ val mailboxId1: MailboxId = mailboxProbe.createMailbox(MailboxPath.inbox(BOB))
+
+ val messageId: MessageId = mailboxProbe
+ .appendMessage(BOB.asString, MailboxPath.inbox(BOB),
+ AppendCommand.from(
+ buildTestMessage))
+ .getMessageId
+
+ val request =
+ s"""{
+ | "using": ["urn:ietf:params:jmap:core","urn:ietf:params:jmap:mail"],
+ | "methodCalls": [
+ | ["Email/set", {
+ | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+ | "update": {
+ | "${messageId.serialize}": {
+ | "invalid": "value"
+ | }
+ | }
+ | }, "c1"]
+ | ]
+ |}""".stripMargin
+
+ val response = `given`
+ .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+ .body(request)
+ .when
+ .post
+ .`then`
+ .statusCode(SC_OK)
+ .contentType(JSON)
+ .extract
+ .body
+ .asString
+
+ assertThatJson(response)
+ .inPath("methodResponses[0][1].notUpdated")
+ .isEqualTo(
+ s"""{
+ | "1": {
+ | "type": "invalidPatch",
+ | "description": "Message 1 update is invalid: List((,List(JsonValidationError(List(invalid is an invalid entry in an Email/set update patch),ArraySeq()))))"
+ | }
+ |}""".stripMargin)
+ }
+
+ @Test
+ def invalidMailboxPartialUpdatePropertyShouldFail(server: GuiceJamesServer): Unit = {
+ val mailboxProbe = server.getProbe(classOf[MailboxProbeImpl])
+ val mailboxId1: MailboxId = mailboxProbe.createMailbox(MailboxPath.inbox(BOB))
+
+ val messageId: MessageId = mailboxProbe
+ .appendMessage(BOB.asString, MailboxPath.inbox(BOB),
+ AppendCommand.from(
+ buildTestMessage))
+ .getMessageId
+
+ val request =
+ s"""{
+ | "using": ["urn:ietf:params:jmap:core","urn:ietf:params:jmap:mail"],
+ | "methodCalls": [
+ | ["Email/set", {
+ | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+ | "update": {
+ | "${messageId.serialize}": {
+ | "mailboxIds/invalid": "value"
+ | }
+ | }
+ | }, "c1"]
+ | ]
+ |}""".stripMargin
+
+ val response = `given`
+ .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+ .body(request)
+ .when
+ .post
+ .`then`
+ .statusCode(SC_OK)
+ .contentType(JSON)
+ .extract
+ .body
+ .asString
+
+ assertThatJson(response)
+ .inPath("methodResponses[0][1].notUpdated")
+ .isEqualTo(
+ s"""{
+ | "1": {
+ | "type": "invalidPatch",
+ | "description": "Message 1 update is invalid: List((,List(JsonValidationError(List(mailboxIds/invalid is an invalid entry in an Email/set update patch: For input string: \\"invalid\\"),ArraySeq()))))"
+ | }
+ |}""".stripMargin)
+ }
+
+ @Test
+ def invalidMailboxPartialUpdateValueShouldFail(server: GuiceJamesServer): Unit = {
+ val mailboxProbe = server.getProbe(classOf[MailboxProbeImpl])
+ val mailboxId1: MailboxId = mailboxProbe.createMailbox(MailboxPath.inbox(BOB))
+
+ val messageId: MessageId = mailboxProbe
+ .appendMessage(BOB.asString, MailboxPath.inbox(BOB),
+ AppendCommand.from(
+ buildTestMessage))
+ .getMessageId
+
+ val request =
+ s"""{
+ | "using": ["urn:ietf:params:jmap:core","urn:ietf:params:jmap:mail"],
+ | "methodCalls": [
+ | ["Email/set", {
+ | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+ | "update": {
+ | "${messageId.serialize}": {
+ | "mailboxIds/${mailboxId1.serialize}": false
+ | }
+ | }
+ | }, "c1"]
+ | ]
+ |}""".stripMargin
+
+ val response = `given`
+ .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+ .body(request)
+ .when
+ .post
+ .`then`
+ .statusCode(SC_OK)
+ .contentType(JSON)
+ .extract
+ .body
+ .asString
+
+ assertThatJson(response)
+ .inPath("methodResponses[0][1].notUpdated")
+ .isEqualTo(
+ s"""{
+ | "1": {
+ | "type": "invalidPatch",
+ | "description": "Message 1 update is invalid: List((,List(JsonValidationError(List(Value associated with mailboxIds/1 is invalid: MailboxId partial updates requires a JsBoolean(true) (set) or a JsNull (unset)),ArraySeq()))))"
+ | }
+ |}""".stripMargin)
+ }
+
+ @Test
+ def mixingResetAndPartialUpdatesShouldFail(server: GuiceJamesServer): Unit = {
+ val mailboxProbe = server.getProbe(classOf[MailboxProbeImpl])
+ val mailboxId1: MailboxId = mailboxProbe.createMailbox(MailboxPath.inbox(BOB))
+
+ val messageId: MessageId = mailboxProbe
+ .appendMessage(BOB.asString, MailboxPath.inbox(BOB),
+ AppendCommand.from(
+ buildTestMessage))
+ .getMessageId
+
+ val request =
+ s"""{
+ | "using": ["urn:ietf:params:jmap:core","urn:ietf:params:jmap:mail"],
+ | "methodCalls": [
+ | ["Email/set", {
+ | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+ | "update": {
+ | "${messageId.serialize}": {
+ | "mailboxIds/${mailboxId1.serialize}": true,
+ | "mailboxIds" : {
+ | "${mailboxId1.serialize}": true
+ | }
+ | }
+ | }
+ | }, "c1"]
+ | ]
+ |}""".stripMargin
+
+ val response = `given`
+ .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+ .body(request)
+ .when
+ .post
+ .`then`
+ .statusCode(SC_OK)
+ .contentType(JSON)
+ .extract
+ .body
+ .asString
+
+ assertThatJson(response)
+ .inPath("methodResponses[0][1].notUpdated")
+ .isEqualTo(
+ s"""{
+ | "1": {
+ | "type": "invalidPatch",
+ | "description": "Message 1 update is invalid: Partial update and reset specified"
+ | }
+ |}""".stripMargin)
+ }
+
+ @Test
def emailSetDestroySuccessAndFailureCanBeMixed(server: GuiceJamesServer): Unit = {
val mailboxProbe = server.getProbe(classOf[MailboxProbeImpl])
mailboxProbe.createMailbox(MailboxPath.inbox(BOB))
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailSetSerializer.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailSetSerializer.scala
index 34d2f07..556f194 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailSetSerializer.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailSetSerializer.scala
@@ -82,6 +82,7 @@ class EmailSetSerializer @Inject()(messageIdFactory: MessageId.Factory, mailboxI
case JsNull => MailboxRemoval(id)
case _ => InvalidPatchEntryValue(property, "MailboxId partial updates requires a JsBoolean(true) (set) or a JsNull (unset)")
})
+ case _ => InvalidPatchEntryName(property)
}
}
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/package.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/package.scala
index c3dd3d8..05eaeab 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/package.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/package.scala
@@ -52,8 +52,8 @@ package object json {
refinedKey match {
case Left(error) => Left(JsError(error))
case scala.util.Right(unparsedK) =>
- val transformValue: JsResult[V] = valueReads.reads(keyValue._2)
- transformValue.fold(
+ val transformedValue: JsResult[V] = valueReads.reads(keyValue._2)
+ transformedValue.fold(
error => Left(JsError(error)),
v => scala.util.Right(validatedAcc + (unparsedK -> v)))
}
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
index 29d1275..ab692e5 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
@@ -59,25 +59,23 @@ case class EmailSetResponse(accountId: AccountId,
case class EmailSetUpdate(mailboxIds: Option[MailboxIds],
mailboxIdsToAdd: Option[MailboxIds],
mailboxIdsToRemove: Option[MailboxIds]) {
- def validate: Either[IllegalArgumentException, ValidatedEmailSetUpdate] = {
- if (mailboxIds.isDefined && (mailboxIdsToAdd.isDefined || mailboxIdsToRemove.isDefined)) {
- Left(new IllegalArgumentException("Partial update for mailboxIds"))
- } else {
- val identity: Function[MailboxIds, MailboxIds] = ids => ids
- val mailboxIdsAddition: Function[MailboxIds, MailboxIds] = mailboxIdsToAdd
- .map(toBeAdded => (ids: MailboxIds) => ids ++ toBeAdded)
- .getOrElse(identity)
- val mailboxIdsRemoval: Function[MailboxIds, MailboxIds] = mailboxIdsToRemove
- .map(toBeRemoved => (ids: MailboxIds) => ids -- toBeRemoved)
- .getOrElse(identity)
- val mailboxIdsReset: Function[MailboxIds, MailboxIds] = mailboxIds
- .map(toReset => (_: MailboxIds) => toReset)
- .getOrElse(identity)
- val mailboxIdsTransformation: Function[MailboxIds, MailboxIds] = mailboxIdsAddition
- .compose(mailboxIdsRemoval)
- .compose(mailboxIdsReset)
- scala.Right(ValidatedEmailSetUpdate(mailboxIdsTransformation))
- }
+ def validate: Either[IllegalArgumentException, ValidatedEmailSetUpdate] = if (mailboxIds.isDefined && (mailboxIdsToAdd.isDefined || mailboxIdsToRemove.isDefined)) {
+ Left(new IllegalArgumentException("Partial update and reset specified"))
+ } else {
+ val identity: Function[MailboxIds, MailboxIds] = ids => ids
+ val mailboxIdsAddition: Function[MailboxIds, MailboxIds] = mailboxIdsToAdd
+ .map(toBeAdded => (ids: MailboxIds) => ids ++ toBeAdded)
+ .getOrElse(identity)
+ val mailboxIdsRemoval: Function[MailboxIds, MailboxIds] = mailboxIdsToRemove
+ .map(toBeRemoved => (ids: MailboxIds) => ids -- toBeRemoved)
+ .getOrElse(identity)
+ val mailboxIdsReset: Function[MailboxIds, MailboxIds] = mailboxIds
+ .map(toReset => (_: MailboxIds) => toReset)
+ .getOrElse(identity)
+ val mailboxIdsTransformation: Function[MailboxIds, MailboxIds] = mailboxIdsAddition
+ .compose(mailboxIdsRemoval)
+ .compose(mailboxIdsReset)
+ scala.Right(ValidatedEmailSetUpdate(mailboxIdsTransformation))
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org