Skip to content

Commit

Permalink
Add test to ensure filenames are encoded correctly in S3 (#4955)
Browse files Browse the repository at this point in the history
* Add test to ensure filenames are encoded correctly in S3

* Add test for location

* Add test that there is a file on S3 at the location
  • Loading branch information
shinyhappydan authored May 14, 2024
1 parent bde1796 commit 505647e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.epfl.bluebrain.nexus.testkit.scalatest

import ch.epfl.bluebrain.nexus.testkit.scalatest.JsonMatchers.field
import ch.epfl.bluebrain.nexus.testkit.scalatest.JsonMatchers.{field, fieldThatEndsWith}
import io.circe.Json
import io.circe.syntax.KeyOps
import org.scalatest.matchers.HavePropertyMatcher
Expand All @@ -22,4 +22,7 @@ object FileMatchers {
field("_digest", Json.obj("_algorithm" := algo, "_value" := value))

def filename(expected: String): HavePropertyMatcher[Json, String] = field("_filename", expected)

def locationWithFilename(expected: String): HavePropertyMatcher[Json, String] =
fieldThatEndsWith("_location", expected)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,15 @@ object JsonMatchers {
actual.orNull
)
}

def fieldThatEndsWith(key: String, expectedEnding: String): HavePropertyMatcher[Json, String] = HavePropertyMatcher {
json =>
val actual = json.hcursor.downField(key).as[String].toOption
HavePropertyMatchResult(
actual.exists(_.endsWith(expectedEnding)),
key,
"ends with " + expectedEnding,
actual.orNull
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import akka.http.scaladsl.model.{ContentTypes, StatusCodes}
import akka.util.ByteString
import cats.effect.IO
import cats.implicits.toTraverseOps
import ch.epfl.bluebrain.nexus.testkit.scalatest.FileMatchers.{digest => digestField, filename => filenameField, mediaType => mediaTypeField}
import ch.epfl.bluebrain.nexus.delta.kernel.utils.UrlUtils
import ch.epfl.bluebrain.nexus.testkit.scalatest.FileMatchers.{digest => digestField, filename => filenameField, locationWithFilename, mediaType => mediaTypeField}
import ch.epfl.bluebrain.nexus.testkit.scalatest.ShouldMatchers.convertToAnyShouldWrapper
import ch.epfl.bluebrain.nexus.tests.HttpClient.acceptAll
import ch.epfl.bluebrain.nexus.tests.Identity.storages.Coyote
import ch.epfl.bluebrain.nexus.tests.Optics.{error, filterMetadataKeys}
Expand All @@ -24,7 +26,7 @@ import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3AsyncClient
import software.amazon.awssdk.services.s3.model._

import java.net.URI
import java.net.{URI, URLEncoder}
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import java.security.MessageDigest
Expand Down Expand Up @@ -206,6 +208,46 @@ class S3StorageSpec extends StorageSpec {
}
}

"Filenames with url-encodable characters" should {
"have an appropriate filename in S3" in {

val name = "name with spaces.txt"

val location = uploadAFileWithName(name)
location should endWith(UrlUtils.encode(name))

assertThereIsAFileInS3WithAtLocation(location)
}
}

private def uploadAFileWithName(name: String): String = {
val id = genId()
val (json, response) = deltaClient
.uploadFileWithMetadata(
s"/files/$projectRef/$id?storage=nxv:$storageId",
"file contents",
ContentTypes.`text/plain(UTF-8)`,
name,
Coyote,
None,
None,
Map.empty
)
.accepted

response.status shouldEqual StatusCodes.Created
json should have(filenameField(name))
json.hcursor.downField("_location").as[String].getOrElse(fail("file has no _location field"))
}

private def assertThereIsAFileInS3WithAtLocation(location: String): Assertion = {
s3Client
.listObjectsV2(ListObjectsV2Request.builder.bucket(bucket).prefix(s"myprefix/$projectRef/files").build)
.map(_.contents.asScala.map(_.key()))
.map(keys => keys should contain(location))
.accepted
}

private def registrationResponse(id: String, digestValue: String, location: String): Json =
jsonContentOf(
"kg/files/registration-metadata.json",
Expand Down

0 comments on commit 505647e

Please sign in to comment.