Skip to content

Commit

Permalink
Add check for tags to ResourcesRoutesSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
dantb committed Oct 7, 2023
1 parent 50de1ad commit 66dafb1
Showing 1 changed file with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -104,26 +106,33 @@ 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,
resolverContextResolution,
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}"""

Expand Down Expand Up @@ -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
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 66dafb1

Please sign in to comment.