Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-1042 implement public asset/get method #1060

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tmail-backend/apps/distributed/src/main/conf/jmap.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ authentication.strategy.rfc8621=JWTAuthenticationStrategy,BasicAuthenticationStr
calendarEvent.reply.mailTemplateLocation=file://eml-template/

# The supported languages for replying to CalendarEvent emails
calendarEvent.reply.supportedLanguages=en,fr
calendarEvent.reply.supportedLanguages=en,fr

url.prefix=http://localhost
4 changes: 3 additions & 1 deletion tmail-backend/apps/memory/src/main/conf/jmap.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ jwt.privatekeypem.url=file://conf/jwt_privatekey
calendarEvent.reply.mailTemplateLocation=file://eml-template/

# The supported languages for replying to CalendarEvent emails
calendarEvent.reply.supportedLanguages=en,fr
calendarEvent.reply.supportedLanguages=en,fr

url.prefix=http://localhost
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/****************************************************************
* 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 com.linagora.tmail.james;

import org.apache.james.JamesServerBuilder;
import org.apache.james.JamesServerExtension;
import org.apache.james.SearchConfiguration;
import org.apache.james.backends.redis.RedisExtension;
import org.apache.james.modules.AwsS3BlobStoreExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.linagora.tmail.blob.blobid.list.BlobStoreConfiguration;
import com.linagora.tmail.james.app.CassandraExtension;
import com.linagora.tmail.james.app.DistributedJamesConfiguration;
import com.linagora.tmail.james.app.DistributedServer;
import com.linagora.tmail.james.app.EventBusKeysChoice;
import com.linagora.tmail.james.app.RabbitMQExtension;
import com.linagora.tmail.james.common.PublicAssetGetMethodContract;
import com.linagora.tmail.james.common.probe.PublicAssetProbeModule;
import com.linagora.tmail.module.LinagoraTestJMAPServerModule;

public class DistributedPublicAssetGetMethodTest implements PublicAssetGetMethodContract {
@RegisterExtension
static JamesServerExtension testExtension = new JamesServerBuilder<DistributedJamesConfiguration>(tmpDir ->
DistributedJamesConfiguration.builder()
.workingDirectory(tmpDir)
.configurationFromClasspath()
.blobStore(BlobStoreConfiguration.builder()
.disableCache()
.deduplication()
.noCryptoConfig()
.disableSingleSave())
.eventBusKeysChoice(EventBusKeysChoice.REDIS)
.searchConfiguration(SearchConfiguration.openSearchDisabled())
.build())
.extension(new CassandraExtension())
.extension(new RabbitMQExtension())
.extension(new RedisExtension())
.extension(new AwsS3BlobStoreExtension())
.server(configuration -> DistributedServer.createServer(configuration)
.overrideWith(new LinagoraTestJMAPServerModule())
.overrideWith(new PublicAssetProbeModule()))
.build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
package com.linagora.tmail.james.common

import java.io.ByteArrayInputStream

import com.linagora.tmail.james.common.PublicAssetGetMethodContract.{CREATION_REQUEST, IDENTITY_ID}
import com.linagora.tmail.james.common.probe.PublicAssetProbe
import com.linagora.tmail.james.jmap.publicAsset.ImageContentType.ImageContentType
import com.linagora.tmail.james.jmap.publicAsset.{ImageContentType, PublicAssetCreationRequest, PublicAssetIdFactory}
import io.netty.handler.codec.http.HttpHeaderNames.ACCEPT
import io.restassured.RestAssured.{`given`, requestSpecification}
import io.restassured.http.ContentType.JSON
import net.javacrumbs.jsonunit.JsonMatchers.jsonEquals
import net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER
import net.javacrumbs.jsonunit.core.internal.Options
import org.apache.http.HttpStatus.SC_OK
import org.apache.james.GuiceJamesServer
import org.apache.james.jmap.api.model.Size.Size
import org.apache.james.jmap.api.model.{IdentityId, Size}
import org.apache.james.jmap.core.ResponseObject.SESSION_STATE
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.model.ContentType
import org.apache.james.utils.DataProbeImpl
import org.junit.jupiter.api.{BeforeEach, Test}

object PublicAssetGetMethodContract {
val CONTENT_TYPE: ContentType = ContentType.of("image/png")
val IMAGE_CONTENT_TYPE: ImageContentType = ImageContentType.from(CONTENT_TYPE).toOption.get
val ASSET_CONTENT: Array[Byte] = Array[Byte](1, 2, 3)
val SIZE: Size = Size.sanitizeSize(ASSET_CONTENT.length)
val IDENTITY_ID = IdentityId.generate
val IDENTITY_IDS: Seq[IdentityId] = Seq(IDENTITY_ID)
val CREATION_REQUEST: PublicAssetCreationRequest = PublicAssetCreationRequest(
size = SIZE,
contentType = IMAGE_CONTENT_TYPE,
content = () => new ByteArrayInputStream(ASSET_CONTENT),
identityIds = IDENTITY_IDS)
}

trait PublicAssetGetMethodContract {
@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 missingPublicAssetCapabilityShouldFail(): Unit =
`given`
.body(
s"""{
| "using": ["urn:ietf:params:jmap:core"],
| "methodCalls": [
| [
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "ids": null
| },
| "c1"
| ]
| ]
|}""".stripMargin)
.when
.post
.`then`
.statusCode(SC_OK)
.contentType(JSON)
.body("", jsonEquals(
s"""{
| "sessionState": "${SESSION_STATE.value}",
| "methodResponses": [
| [
| "error",
| {
| "type": "unknownMethod",
| "description": "Missing capability(ies): com:linagora:params:jmap:public:assets"
| },
| "c1"
| ]
| ]
|}""".stripMargin))


@Test
def getShouldReturnEmptyAssetsByDefault(): Unit =
`given`
.body(
s"""{
| "using": ["urn:ietf:params:jmap:core", "com:linagora:params:jmap:public:assets"],
| "methodCalls": [
| [
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "ids": null
| },
| "c1"
| ]
| ]
|}""".stripMargin)
.when
.post
.`then`
.statusCode(SC_OK)
.contentType(JSON)
.body("methodResponses[0]", jsonEquals(
s"""[
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "state": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
| "list": [],
| "notFound": []
| },
| "c1"
|]""".stripMargin))

@Test
def fetchNullIdsShouldReturnAllAssets(server: GuiceJamesServer): Unit = {
hungphan227 marked this conversation as resolved.
Show resolved Hide resolved
val publicAsset = server.getProbe(classOf[PublicAssetProbe]).create(BOB, CREATION_REQUEST)
val publicAsset2 = server.getProbe(classOf[PublicAssetProbe]).create(BOB, CREATION_REQUEST)

`given`
.body(
s"""{
| "using": ["urn:ietf:params:jmap:core", "com:linagora:params:jmap:public:assets"],
| "methodCalls": [
| [
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "ids": null
| },
| "c1"
| ]
| ]
|}""".stripMargin)
.when
.post
.`then`
.statusCode(SC_OK)
.contentType(JSON)
.body("methodResponses[0]", jsonEquals(
hungphan227 marked this conversation as resolved.
Show resolved Hide resolved
s"""[
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "state": "$${json-unit.ignore}",
| "list": [
| {
| "id": "${publicAsset.id.value}",
| "publicURI": "${publicAsset.publicURI.value}",
| "size": 3,
| "contentType": "image/png",
| "identityIds": [ "${IDENTITY_ID.id}" ]
| },
| {
| "id": "${publicAsset2.id.value}",
| "publicURI": "${publicAsset2.publicURI.value}",
| "size": 3,
| "contentType": "image/png",
| "identityIds": [ "${IDENTITY_ID.id}" ]
| }
| ],
| "notFound": []
| },
| "c1"
|]""".stripMargin).withOptions(new Options(IGNORING_ARRAY_ORDER)))
}

@Test
def fetchIdsShouldReturnSpecificAssets(server: GuiceJamesServer): Unit = {
val publicAsset = server.getProbe(classOf[PublicAssetProbe]).create(BOB, CREATION_REQUEST)

`given`
.body(
s"""{
| "using": ["urn:ietf:params:jmap:core", "com:linagora:params:jmap:public:assets"],
| "methodCalls": [
| [
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "ids": [ "${publicAsset.id.value}" ]
| },
| "c1"
| ]
| ]
|}""".stripMargin)
.when
.post
.`then`
.statusCode(SC_OK)
.contentType(JSON)
.body("methodResponses[0]", jsonEquals(
s"""[
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "state": "$${json-unit.ignore}",
| "list": [
| {
| "id": "${publicAsset.id.value}",
| "publicURI": "${publicAsset.publicURI.value}",
| "size": 3,
| "contentType": "image/png",
| "identityIds": [ "${IDENTITY_ID.id}" ]
| }
| ],
| "notFound": []
| },
| "c1"
|]""".stripMargin))
}

@Test
def mixedFoundAndNotFoundCase(server: GuiceJamesServer): Unit = {
val publicAsset = server.getProbe(classOf[PublicAssetProbe]).create(BOB, CREATION_REQUEST)
val nonExistedAssetId = PublicAssetIdFactory.generate()

`given`
.body(
s"""{
| "using": ["urn:ietf:params:jmap:core", "com:linagora:params:jmap:public:assets"],
| "methodCalls": [
| [
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "ids": [
| "${publicAsset.id.value}",
| "${nonExistedAssetId.value}",
| "notFound"
| ]
| },
| "c1"
| ]
| ]
|}""".stripMargin)
.when
.post
.`then`
.statusCode(SC_OK)
.contentType(JSON)
.body("methodResponses[0]", jsonEquals(
s"""[
| "PublicAsset/get",
| {
| "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
| "state": "$${json-unit.ignore}",
| "list": [
| {
| "id": "${publicAsset.id.value}",
| "publicURI": "${publicAsset.publicURI.value}",
| "size": 3,
| "contentType": "image/png",
| "identityIds": [ "${IDENTITY_ID.id}" ]
| }
| ],
| "notFound": [ "notFound", "${nonExistedAssetId.value}" ]
| },
| "c1"
|]""".stripMargin))
}

@Test
def shouldFailWhenWrongAccountId(): Unit =
`given`
.body(
s"""{
| "using": ["urn:ietf:params:jmap:core", "com:linagora:params:jmap:public:assets"],
| "methodCalls": [
| [
| "PublicAsset/get",
| {
| "accountId": "unknownAccountId",
| "ids": null
| },
| "c1"
| ]
| ]
|}""".stripMargin)
.when
.post
.`then`
.statusCode(SC_OK)
.contentType(JSON)
.body("", jsonEquals(
s"""{
| "sessionState": "${SESSION_STATE.value}",
| "methodResponses": [
| ["error", {
| "type": "accountNotFound"
| }, "c1"]
| ]
|}""".stripMargin))
}
Loading