From 8f1016600a7eaccb4989f10f02552f385c0f0832 Mon Sep 17 00:00:00 2001 From: dantb Date: Mon, 10 Jun 2024 11:51:41 +0200 Subject: [PATCH] Fix / test metadata and mediaType (#5024) --- .../delta/plugins/storage/files/Files.scala | 2 +- .../files/routes/DelegateFilesRoutes.scala | 12 +++++-- .../files/routes/TokenIssuerSuite.scala | 2 +- .../kg/files/registration-metadata.json | 2 +- .../nexus/tests/kg/files/S3StorageSpec.scala | 34 +++++++++++++++---- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala index e3f99bce7e..9b928d8da6 100644 --- a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala +++ b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/Files.scala @@ -157,7 +157,7 @@ final class Files( test(CreateFile(iri, projectRef, testStorageRef, testStorageType, testAttributes, caller.subject, tag = None)) (_, storage) <- fetchAndValidateActiveStorage(storageId, projectRef, pc) metadata <- fileOperations.delegate(storage, description.filename) - } yield DelegationResponse(metadata.bucket, iri, metadata.path, description.metadata) + } yield DelegationResponse(metadata.bucket, iri, metadata.path, description.metadata, description.mediaType) }.span("delegate") /** diff --git a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/DelegateFilesRoutes.scala b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/DelegateFilesRoutes.scala index c8b697962a..09f4cb1a9a 100644 --- a/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/DelegateFilesRoutes.scala +++ b/delta/plugins/storage/src/main/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/DelegateFilesRoutes.scala @@ -1,7 +1,7 @@ package ch.epfl.bluebrain.nexus.delta.plugins.storage.files.routes import akka.http.scaladsl.model.StatusCodes.{Created, OK} -import akka.http.scaladsl.model.Uri +import akka.http.scaladsl.model.{ContentType, Uri} import akka.http.scaladsl.server._ import cats.effect.IO import cats.syntax.all._ @@ -104,7 +104,7 @@ final class DelegateFilesRoutes( delegationResponse.metadata, delegationResponse.path.path, None, - None + delegationResponse.mediaType ) _ <- index(project, fileResource, mode) } yield fileResource @@ -113,7 +113,13 @@ final class DelegateFilesRoutes( object DelegateFilesRoutes { - final case class DelegationResponse(bucket: String, id: Iri, path: Uri, metadata: Option[FileCustomMetadata]) + final case class DelegationResponse( + bucket: String, + id: Iri, + path: Uri, + metadata: Option[FileCustomMetadata], + mediaType: Option[ContentType] + ) object DelegationResponse { implicit val enc: Encoder[DelegationResponse] = deriveEncoder diff --git a/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/TokenIssuerSuite.scala b/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/TokenIssuerSuite.scala index 3cc007aae5..a5bf9591c6 100644 --- a/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/TokenIssuerSuite.scala +++ b/delta/plugins/storage/src/test/scala/ch/epfl/bluebrain/nexus/delta/plugins/storage/files/routes/TokenIssuerSuite.scala @@ -41,7 +41,7 @@ class TokenIssuerSuite extends CatsEffectSuite with Generators { test("JWS verification fails if token is expired") { val tokenIssuer = new TokenIssuer(rsaJWK, tokenValidity = 0.seconds) - val returnedPayload = DelegationResponse(genString(), iri"potato", Uri(genString()), None) + val returnedPayload = DelegationResponse(genString(), iri"potato", Uri(genString()), None, None) val program = for { jwsPayload <- tokenIssuer.issueJWSPayload(returnedPayload.asJson) diff --git a/tests/src/test/resources/kg/files/registration-metadata.json b/tests/src/test/resources/kg/files/registration-metadata.json index 8c867feae5..0e07d5c0ab 100644 --- a/tests/src/test/resources/kg/files/registration-metadata.json +++ b/tests/src/test/resources/kg/files/registration-metadata.json @@ -17,7 +17,7 @@ }, "_filename": "{{filename}}", "_location": "{{location}}", - "_mediaType": "image/png", + "_mediaType": "{{mediaType}}", "_origin" : "External", "_incoming": "{{self}}/incoming", "_outgoing": "{{self}}/outgoing", diff --git a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/S3StorageSpec.scala b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/S3StorageSpec.scala index fb2c5abbee..476e432f71 100644 --- a/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/S3StorageSpec.scala +++ b/tests/src/test/scala/ch/epfl/bluebrain/nexus/tests/kg/files/S3StorageSpec.scala @@ -237,7 +237,13 @@ class S3StorageSpec extends StorageSpec { .accepted } - private def registrationResponse(id: String, digestValue: String, location: String, filename: String): Json = + private def registrationResponse( + id: String, + digestValue: String, + location: String, + filename: String, + mediaType: String + ): Json = jsonContentOf( "kg/files/registration-metadata.json", replacements( @@ -248,7 +254,8 @@ class S3StorageSpec extends StorageSpec { "projId" -> s"$projectRef", "digestValue" -> digestValue, "location" -> location, - "filename" -> filename + "filename" -> filename, + "mediaType" -> mediaType ): _* ) @@ -282,7 +289,8 @@ class S3StorageSpec extends StorageSpec { fullId, logoSha256HexDigest, location = path, - filename = logoFilename + filename = logoFilename, + mediaType = "image/png" ) } } yield assertion @@ -372,8 +380,18 @@ class S3StorageSpec extends StorageSpec { s"Delegate S3 file upload" should { "succeed using JWS protocol with flattened serialization" in { - val filename = genString() - val payload = Json.obj("filename" -> Json.fromString(filename)) + val filename = genString() + val (name, desc, keywords) = (genString(), genString(), Json.obj(genString() := genString())) + val metadata = + json""" + { + "name": "$name", + "description": "$desc", + "keywords": $keywords + } + """ + val payload = + Json.obj("filename" -> Json.fromString(filename), "metadata" -> metadata, "mediaType" := "image/dan") for { jwsPayload <- @@ -390,14 +408,16 @@ class S3StorageSpec extends StorageSpec { } encodedId = UrlUtils.encode(id) filename = path.split("/").last + expectedMetadata = Json.obj("name" := name, "description" := desc, "_keywords" := keywords) assertion <- deltaClient.get[Json](s"/files/$projectRef/$encodedId", Coyote) { (json, response) => response.status shouldEqual StatusCodes.OK val expected = registrationResponse( id, logoSha256HexDigest, location = path, - filename = filename - ) + filename = filename, + mediaType = "image/dan" + ).deepMerge(expectedMetadata) val actual = filterMetadataKeys(json) actual shouldEqual expected }