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

Also allows post for the multi-fetch operation #4290

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MultiFetchRoutes(
pathPrefix("multi-fetch") {
pathPrefix("resources") {
extractCaller { implicit caller =>
(get & entity(as[MultiFetchRequest])) { request =>
((get | post) & entity(as[MultiFetchRequest])) { request =>
implicit val printer: Printer = selectPrinter(request)
emit(multiFetch(request).flatMap(_.asJson))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final class ResourcesTrialRoutes(
}

private def generateRoute: Route =
(get & pathPrefix("trial") & pathPrefix("resources")) {
(pathPrefix("trial") & pathPrefix("resources") & post) {
extractCaller { implicit caller =>
(resolveProjectRef & pathEndOrSingleSlash) { project =>
authorizeFor(project, Write).apply {
Expand All @@ -100,7 +100,6 @@ final class ResourcesTrialRoutes(
}
)
}

}

object ResourcesTrialRoutes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ class MultiFetchRoutesSpec extends BaseRouteSpec {
}
}

"return expected results as annotated source for a user with limited access" in {
"return expected results as annotated source for a user with limited access using post method" in {
val entity = request(ResourceRepresentation.AnnotatedSourceJson).toEntity
Get(endpoint, entity) ~> asAlice ~> routes ~> check {
Post(endpoint, entity) ~> asAlice ~> routes ~> check {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we allow both get and post here shouldn't both be tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I planned to push this "hotfix" first so as not to unblock Dinika and then complete the tests.
But I can add them now

status shouldEqual StatusCodes.OK
response.asJson shouldEqual jsonContentOf("multi-fetch/annotated-source-response.json")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur

"fail to generate a resource for a user without access" in {
val payload = json"""{ "resource": $validSource }"""
Get(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> routes ~> check {
Post(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> routes ~> check {
response.status shouldEqual StatusCodes.Forbidden
response.asJson shouldEqual jsonContentOf("errors/authorization-failed.json")
}
}

"generate a resource without passing a schema" in {
val payload = json"""{ "resource": $validSource }"""
Get(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
Post(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
response.status shouldEqual StatusCodes.OK
val jsonResponse = response.asJsonObject
jsonResponse("schema") shouldBe empty
Expand All @@ -146,9 +146,9 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur
}
}

"generate a resource passing a new schema" in {
"generate a resource passing a new schema and using post" in {
val payload = json"""{ "schema": $schemaSource, "resource": $validSource }"""
Get(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
Post(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
response.status shouldEqual StatusCodes.OK
val jsonResponse = response.asJsonObject
jsonResponse("schema") should not be empty
Expand All @@ -159,7 +159,7 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur

"fails to generate a resource when passing an invalid new schema" in {
val payload = json"""{ "schema": { "invalid": "xxx" }, "resource": $validSource }"""
Get(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
Post(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
response.status shouldEqual StatusCodes.BadRequest
response.asJson shouldEqual
json"""{
Expand All @@ -174,7 +174,7 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur

"fails to generate a resource when the resource payload is invalid and without passing a schema" in {
val payload = json"""{ "resource": $invalidSource }"""
Get(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
Post(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
response.status shouldEqual StatusCodes.OK
response.asJson shouldEqual
json"""
Expand All @@ -190,7 +190,7 @@ class ResourcesTrialRoutesSpec extends BaseRouteSpec with ResourceInstanceFixtur

"fail to generate a resource passing a new schema" in {
val payload = json"""{ "schema": $schemaSource, "resource": $invalidSource }"""
Get(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
Post(s"/v1/trial/resources/$projectRef/", payload.toEntity) ~> asAlice ~> routes ~> check {
response.status shouldEqual StatusCodes.OK
val jsonResponse = response.asJsonObject
jsonResponse("schema") should not be empty
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/paradox/docs/delta/api/multi-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Please visit @ref:[Authentication & authorization](authentication.md) section to
## Payload

```
GET /v1/multi-fetch/resources
GET|POST /v1/multi-fetch/resources

{
"format": {format}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/paradox/docs/delta/api/trial.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ It applies the same validation steps than the creation/update of resources, the
that nothing is persisted.

```
GET /v1/trial/resources/{org_label}/{project_label}
POST /v1/trial/resources/{org_label}/{project_label}

{
"schema": {schema},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class ResourcesTrialSpec extends BaseSpec with CirceEq {
def schema = root.schema.`@id`.string.getOption(_)

"fail for a user without access" in {
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payloadWithoutSchema, Alice)(expectForbidden)
deltaClient.post[Json](s"/trial/resources/$ref/", payloadWithoutSchema, Alice)(expectForbidden)
}

"fail for an unknown project" in {
deltaClient.getWithBody[Json](s"/trial/resources/$org/xxx/", payloadWithoutSchema, Alice)(expectForbidden)
deltaClient.post[Json](s"/trial/resources/$org/xxx/", payloadWithoutSchema, Alice)(expectForbidden)
}

"succeed for a payload without schema" in {
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payloadWithoutSchema, Bob) { (json, response) =>
deltaClient.post[Json](s"/trial/resources/$ref/", payloadWithoutSchema, Bob) { (json, response) =>
response.status shouldEqual StatusCodes.OK
resultId(json).value shouldEqual resourceId
schema(json) shouldBe empty
Expand All @@ -60,7 +60,7 @@ class ResourcesTrialSpec extends BaseSpec with CirceEq {
}

"succeed for a payload with an existing schema" in {
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payloadWithExistingSchema, Bob) { (json, response) =>
deltaClient.post[Json](s"/trial/resources/$ref/", payloadWithExistingSchema, Bob) { (json, response) =>
response.status shouldEqual StatusCodes.OK
resultId(json).value shouldEqual resourceId
schema(json) shouldBe empty
Expand All @@ -69,7 +69,7 @@ class ResourcesTrialSpec extends BaseSpec with CirceEq {
}

"succeed for a payload with a new schema" in {
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payloadWithNewSchema, Bob) { (json, response) =>
deltaClient.post[Json](s"/trial/resources/$ref/", payloadWithNewSchema, Bob) { (json, response) =>
response.status shouldEqual StatusCodes.OK
resultId(json).value shouldEqual resourceId
schema(json).value shouldBe newSchemaId
Expand All @@ -79,7 +79,7 @@ class ResourcesTrialSpec extends BaseSpec with CirceEq {

"fail for a resource with an invalid context without generating any schema" in {
val payload = json"""{ "resource": { "@context": [ "https://bbp.epfl.ch/unknown-context" ], "test": "fail" } }"""
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payload, Bob) { (json, response) =>
deltaClient.post[Json](s"/trial/resources/$ref/", payload, Bob) { (json, response) =>
response.status shouldEqual StatusCodes.OK
resultId(json) shouldBe empty
schema(json) shouldBe empty
Expand All @@ -90,7 +90,7 @@ class ResourcesTrialSpec extends BaseSpec with CirceEq {
"fail for a resource with an invalid context but also returning the generated schema" in {
val resourcePayload = json"""{ "@context": [ "https://bbp.epfl.ch/unknown-context" ], "test": "fail" }"""
val payload = json"""{ "schema": $newSchemaPayload, "resource": $resourcePayload }"""
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payload, Bob) { (json, response) =>
deltaClient.post[Json](s"/trial/resources/$ref/", payload, Bob) { (json, response) =>
response.status shouldEqual StatusCodes.OK
resultId(json) shouldBe empty
schema(json).value shouldBe newSchemaId
Expand All @@ -101,7 +101,7 @@ class ResourcesTrialSpec extends BaseSpec with CirceEq {
"fail for a resource when shacl validation fails returning the generated " in {
val resourcePayload = SimpleResource.sourcePayloadWithType("nxv:UnexpectedType", 99)
val payload = json"""{ "schema": $newSchemaPayload ,"resource": $resourcePayload }"""
deltaClient.getWithBody[Json](s"/trial/resources/$ref/", payload, Bob) { (json, response) =>
deltaClient.post[Json](s"/trial/resources/$ref/", payload, Bob) { (json, response) =>
response.status shouldEqual StatusCodes.OK
resultId(json) shouldBe empty
schema(json).value shouldBe newSchemaId
Expand Down