Arsnael commented on code in PR #1275: URL: https://github.com/apache/james-project/pull/1275#discussion_r1009148929
########## server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/QuotaChangesMethodContract.scala: ########## @@ -0,0 +1,529 @@ +/**************************************************************** + * 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.{`given`, requestSpecification} +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.quota.QuotaCountLimit +import org.apache.james.jmap.core.ResponseObject.SESSION_STATE +import org.apache.james.jmap.core.UuidState.INSTANCE +import org.apache.james.jmap.http.UserCredential +import org.apache.james.jmap.rfc8621.contract.Fixture.{ACCEPT_RFC8621_VERSION_HEADER, ANDRE, ANDRE_PASSWORD, BOB, BOB_PASSWORD, DOMAIN, authScheme, baseRequestSpecBuilder} +import org.apache.james.mailbox.MessageManager.AppendCommand +import org.apache.james.mailbox.model.MailboxACL.Right.Read +import org.apache.james.mailbox.model.{MailboxACL, MailboxPath} +import org.apache.james.mime4j.dom.Message +import org.apache.james.modules.{ACLProbeImpl, MailboxProbeImpl, QuotaProbesImpl} +import org.apache.james.utils.DataProbeImpl +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility +import org.junit.jupiter.api.{BeforeEach, Test} + +import java.nio.charset.StandardCharsets +import java.time.Duration +import java.util.concurrent.TimeUnit + + +trait QuotaChangesMethodContract { + + private lazy val awaitAtMostTenSeconds = Awaitility.`with` + .await + .pollInterval(Duration.ofMillis(100)) + .atMost(10, TimeUnit.SECONDS) + + @BeforeEach + def setUp(server: GuiceJamesServer): Unit = { + server.getProbe(classOf[DataProbeImpl]) + .fluent + .addDomain(DOMAIN.asString) + .addUser(BOB.asString, BOB_PASSWORD) + .addUser(ANDRE.asString, ANDRE_PASSWORD) + + requestSpecification = baseRequestSpecBuilder(server) + .setAuth(authScheme(UserCredential(BOB, BOB_PASSWORD))) + .addHeader(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER) + .build + } + + @Test + def quotaChangeShouldReturnCorrectResponse(server: GuiceJamesServer): Unit = { + val quotaProbe = server.getProbe(classOf[QuotaProbesImpl]) + val bobQuotaRoot = quotaProbe.getQuotaRoot(MailboxPath.inbox(BOB)) + quotaProbe.setMaxMessageCount(bobQuotaRoot, QuotaCountLimit.count(100L)) + + val response = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [ + | [ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "oldState": "${INSTANCE.value}", + | "newState": "84c40a2e-76a1-3f84-a1e8-862104c7a697", + | "hasMoreChanges": false, + | "updatedProperties": null, + | "created": [], + | "updated": ["08417be420b6dd6fa77d48fb2438e0d19108cd29424844bb109b52d356fab528"], + | "destroyed": [] + | }, + | "c1" + | ] + | ] + |}""".stripMargin) + } + + @Test + def quotaChangeShouldReturnSameResponseWhenSameRequest(server: GuiceJamesServer): Unit = { + val quotaProbe = server.getProbe(classOf[QuotaProbesImpl]) + val bobQuotaRoot = quotaProbe.getQuotaRoot(MailboxPath.inbox(BOB)) + quotaProbe.setMaxMessageCount(bobQuotaRoot, QuotaCountLimit.count(100L)) + + val response1 = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + val response2 = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response1).isEqualTo(response2) + } + + @Test + def hasMoreChangesShouldIsFalseWhenNoQuotaChanges(server: GuiceJamesServer): Unit = { + val quotaProbe = server.getProbe(classOf[QuotaProbesImpl]) + val bobQuotaRoot = quotaProbe.getQuotaRoot(MailboxPath.inbox(BOB)) + quotaProbe.setMaxMessageCount(bobQuotaRoot, QuotaCountLimit.count(100L)) + + val newState: String = getLastState() + + val response = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "${newState}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [ + | [ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "oldState": "${newState}", + | "newState": "${newState}", + | "hasMoreChanges": false, + | "updatedProperties": null, + | "created": [], + | "updated": [], + | "destroyed": [] + | }, + | "c1" + | ] + | ] + |}""".stripMargin) + } + + + @Test + def stateShouldBeChangedWhenQuotaIsUpdated(server: GuiceJamesServer): Unit = { + val quotaProbe = server.getProbe(classOf[QuotaProbesImpl]) + val bobQuotaRoot = quotaProbe.getQuotaRoot(MailboxPath.inbox(BOB)) + quotaProbe.setMaxMessageCount(bobQuotaRoot, QuotaCountLimit.count(100L)) + + val newState: String = getLastState() + + // update quota usage + server.getProbe(classOf[MailboxProbeImpl]).createMailbox(MailboxPath.inbox(BOB)) + server.getProbe(classOf[MailboxProbeImpl]) + .appendMessage(BOB.asString(), MailboxPath.inbox(BOB), AppendCommand.from(Message.Builder + .of + .setSubject("test") + .setBody("testmail", StandardCharsets.UTF_8) + .build)) + .getMessageId.serialize() + + awaitAtMostTenSeconds.untilAsserted(() => assertThat(getLastState()) + .isNotEqualTo(newState)) + } + + private def getLastState(): String = { + `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract() + .jsonPath() + .get("methodResponses[0][1].newState") + } + + @Test + def quotaChangesShouldFailWhenWrongAccountId(): Unit = { + val response = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "unknownAccountId", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [ + | ["error", { + | "type": "accountNotFound" + | }, "c1"] + | ] + |}""".stripMargin) + } + + @Test + def quotaChangesShouldFailWhenMissSinceState(): Unit = { + val response = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [ + | [ + | "error", + | { + | "type": "invalidArguments", + | "description": "{\\"errors\\":[{\\"path\\":\\"obj.sinceState\\",\\"messages\\":[\\"error.path.missing\\"]}]}" + | }, + | "c1" + | ] + | ] + |}""".stripMargin) + } + + @Test + def quotaChangesShouldFailWhenSinceStateIsInvalid(): Unit = { + val response = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "invaLid@" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [ + | [ + | "error", + | { + | "type": "invalidArguments", + | "description": "{\\"errors\\":[{\\"path\\":\\"obj.sinceState\\",\\"messages\\":[\\"error.expected.uuid\\"]}]}" + | }, + | "c1" + | ] + | ] + |}""".stripMargin) + } + + @Test + def quotaChangeShouldFailWhenOmittingOneCapability(): Unit = { + val response = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "unknownAccountId", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [[ + | "error", + | { + | "type": "unknownMethod", + | "description":"Missing capability(ies): urn:ietf:params:jmap:quota" + | }, + | "c1"]] + |}""".stripMargin) + } + + @Test + def quotaChangesShouldFailWhenOmittingAllCapability(): Unit = { + val response = `given` + .body( + s"""{ + | "using": [], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "unknownAccountId", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract + .body + .asString + + assertThatJson(response).isEqualTo( + s"""{ + | "sessionState": "${SESSION_STATE.value}", + | "methodResponses": [[ + | "error", + | { + | "type": "unknownMethod", + | "description":"Missing capability(ies): urn:ietf:params:jmap:quota, urn:ietf:params:jmap:core" + | }, + | "c1"]] + |}""".stripMargin) + } + + @Test + def quotaChangesShouldReturnDifferenceStateWhenProvideDelegatedMailbox(server: GuiceJamesServer): Unit = { + val quotaProbe = server.getProbe(classOf[QuotaProbesImpl]) + val bobQuotaRoot = quotaProbe.getQuotaRoot(MailboxPath.inbox(BOB)) + quotaProbe.setMaxMessageCount(bobQuotaRoot, QuotaCountLimit.count(100L)) + + val stateWithOutShareCapability: String = `given` + .body( + s"""{ + | "using": [ + | "urn:ietf:params:jmap:core", + | "urn:ietf:params:jmap:quota"], + | "methodCalls": [[ + | "Quota/changes", + | { + | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6", + | "sinceState": "${INSTANCE.value}" + | }, + | "c1"]] + |}""".stripMargin) + .when + .post + .`then` + .statusCode(SC_OK) + .contentType(JSON) + .extract() + .jsonPath() + .get("methodResponses[0][1].newState") + + // setup delegated Mailbox + val andreMailbox = MailboxPath.forUser(ANDRE, "mailbox") + val mailboxId = server.getProbe(classOf[MailboxProbeImpl]).createMailbox(andreMailbox) + server.getProbe(classOf[ACLProbeImpl]) + .replaceRights(andreMailbox, BOB.asString, new MailboxACL.Rfc4314Rights(Read)) + + quotaProbe.setMaxMessageCount(quotaProbe.getQuotaRoot(andreMailbox), QuotaCountLimit.count(88L)) Review Comment: I mean create everything you need first (including the shared mailbox and its quota) before you get bob quota/changes without and with shared maiboxes. If you get bob quota/changes without shared mailboxes, but before creating the mailbox, how can you be so sure you don't have an issue in the code if for example we return the changes in shared mailboxes even if not asked? :) -- 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]
