From 66dafb1176499a2e062c74d1d5db0e525ae21b7e Mon Sep 17 00:00:00 2001 From: dantb Date: Sat, 7 Oct 2023 16:24:06 +0200 Subject: [PATCH] Add check for tags to ResourcesRoutesSpec --- .../delta/routes/ResourcesRoutesSpec.scala | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutesSpec.scala b/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutesSpec.scala index 98efdc14ad..d67808a7d5 100644 --- a/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutesSpec.scala +++ b/delta/app/src/test/scala/ch/epfl/bluebrain/nexus/delta/routes/ResourcesRoutesSpec.scala @@ -16,7 +16,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.generators.{ProjectGen, ResourceResolut import ch.epfl.bluebrain.nexus.delta.sdk.identities.IdentitiesDummy import ch.epfl.bluebrain.nexus.delta.sdk.identities.model.Caller import ch.epfl.bluebrain.nexus.delta.sdk.implicits._ -import ch.epfl.bluebrain.nexus.delta.sdk.model.ResourceUris +import ch.epfl.bluebrain.nexus.delta.sdk.model.{ResourceUris, Tags} import ch.epfl.bluebrain.nexus.delta.sdk.permissions.Permissions.{events, resources} import ch.epfl.bluebrain.nexus.delta.sdk.projects.FetchContextDummy import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ApiMappings @@ -29,6 +29,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.resources.{Resources, ResourcesConfig, import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.Schema import ch.epfl.bluebrain.nexus.delta.sdk.utils.BaseRouteSpec import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.{Anonymous, Authenticated, Group, Subject} +import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag import ch.epfl.bluebrain.nexus.delta.sourcing.model.{ProjectRef, ResourceRef} import ch.epfl.bluebrain.nexus.testkit.bio.IOFromMap import io.circe.{Json, Printer} @@ -62,6 +63,7 @@ class ResourcesRoutesSpec extends BaseRouteSpec with IOFromMap { private val schemaSource = jsonContentOf("resources/schema.json").addContext(contexts.shacl, contexts.schemasMetadata) private val schema1 = SchemaGen.schema(nxv + "myschema", project.value.ref, schemaSource.removeKeys(keywords.id)) private val schema2 = SchemaGen.schema(schema.Person, project.value.ref, schemaSource.removeKeys(keywords.id)) + private val tag = UserTag.unsafe("mytag") private val myId = nxv + "myid" // Resource created against no schema with id present on the payload private val myId2 = nxv + "myid2" // Resource created against schema1 with id present on the payload @@ -104,7 +106,7 @@ class ResourcesRoutesSpec extends BaseRouteSpec with IOFromMap { (_, _, _) => IO.raiseError(ResourceResolutionReport()) ) - private def routesWithDecodingOption(implicit decodingOption: DecodingOption) = { + private def routesWithDecodingOption(implicit decodingOption: DecodingOption): (Route, Resources) = { val resources = ResourcesImpl( validator, fetchContext, @@ -112,18 +114,25 @@ class ResourcesRoutesSpec extends BaseRouteSpec with IOFromMap { ResourcesConfig(eventLogConfig, decodingOption), xas ) - Route.seal( - ResourcesRoutes( - IdentitiesDummy(caller), - aclCheck, - resources, - DeltaSchemeDirectives(fetchContext, ioFromMap(uuid -> projectRef.organization), ioFromMap(uuid -> projectRef)), - IndexingAction.noop - ) + ( + Route.seal( + ResourcesRoutes( + IdentitiesDummy(caller), + aclCheck, + resources, + DeltaSchemeDirectives( + fetchContext, + ioFromMap(uuid -> projectRef.organization), + ioFromMap(uuid -> projectRef) + ), + IndexingAction.noop + ) + ), + resources ) } - private lazy val routes = routesWithDecodingOption(DecodingOption.Strict) + private lazy val routes = routesWithDecodingOption(DecodingOption.Strict)._1 private val payloadUpdated = payload deepMerge json"""{"name": "Alice", "address": null}""" @@ -158,15 +167,17 @@ class ResourcesRoutesSpec extends BaseRouteSpec with IOFromMap { } "create a tagged resource" in { - val endpoints = List( + val endpoints = List( ("/v1/resources/myorg/myproject?tag=mytag", myId6, schemas.resources), ("/v1/resources/myorg/myproject/myschema?tag=mytag", myId7, schema1.id) ) + val (routes, resources) = routesWithDecodingOption(DecodingOption.Strict) forAll(endpoints) { case (endpoint, id, schema) => val payload = jsonContentOf("resources/resource.json", "id" -> id) Post(endpoint, payload.toEntity) ~> routes ~> check { status shouldEqual StatusCodes.Created response.asJson shouldEqual resourceMetadata(projectRef, id, schema, (nxv + "Custom").toString) + resourceTags(resources, id).contains(tag) shouldBe true } } } @@ -187,20 +198,25 @@ class ResourcesRoutesSpec extends BaseRouteSpec with IOFromMap { } "create a tagged resource with an authenticated user and provided id" in { - val endpoints = List( + val endpoints = List( ("/v1/resources/myorg/myproject/_/myid8?tag=mytag", myId8, schemas.resources), ("/v1/resources/myorg/myproject/myschema/myid9?tag=mytag", myId9, schema1.id) ) + val (routes, resources) = routesWithDecodingOption(DecodingOption.Strict) forAll(endpoints) { case (endpoint, id, schema) => val payload = jsonContentOf("resources/resource.json", "id" -> id) Put(endpoint, payload.toEntity) ~> asAlice ~> routes ~> check { status shouldEqual StatusCodes.Created response.asJson shouldEqual resourceMetadata(projectRef, id, schema, (nxv + "Custom").toString, createdBy = alice, updatedBy = alice) + resourceTags(resources, id).contains(tag) shouldBe true } } } + def resourceTags(resources: Resources, id: Iri): Tags = + resources.fetch(ResourceRef(id), projectRef).accepted.value.tags + "reject the creation of a resource which already exists" in { Put("/v1/resources/myorg/myproject/_/myid", payload.toEntity) ~> routes ~> check { status shouldEqual StatusCodes.Conflict @@ -246,7 +262,7 @@ class ResourcesRoutesSpec extends BaseRouteSpec with IOFromMap { } "succeed if underscore fields are present but the decoding is set to lenient" in { - val lenientDecodingRoutes = routesWithDecodingOption(DecodingOption.Lenient) + val lenientDecodingRoutes = routesWithDecodingOption(DecodingOption.Lenient)._1 Post("/v1/resources/myorg/myproject/_/", payloadWithUnderscoreFields.toEntity) ~> lenientDecodingRoutes ~> check { response.status shouldEqual StatusCodes.Created