From 91ef4ec0a810ed347702bbe49b470505eb3c3067 Mon Sep 17 00:00:00 2001 From: irinaschubert Date: Tue, 14 Sep 2021 14:13:19 +0200 Subject: [PATCH] refactor(ontology)!: use patch instead of delete for deleting cardinalities (DSP-1700) (#1903) * use put instead of delete for deleting cardinalities * use patch instead of put for deleting cardinalities * update r2r test * add PATCH to list of allowed actions * Update CORSSupportE2ESpec.scala * Update ontology-information.md * Remove unused code --- docs/03-apis/api-v2/ontology-information.md | 4 +-- webapi/src/main/resources/application.conf | 2 +- .../webapi/routing/v2/OntologiesRouteV2.scala | 2 +- .../knora/webapi/e2e/CORSSupportE2ESpec.scala | 30 ++++++++----------- .../webapi/e2e/v2/OntologyV2R2RSpec.scala | 2 +- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/docs/03-apis/api-v2/ontology-information.md b/docs/03-apis/api-v2/ontology-information.md index 62fc3ac2a1..94ac39a4cb 100644 --- a/docs/03-apis/api-v2/ontology-information.md +++ b/docs/03-apis/api-v2/ontology-information.md @@ -1598,7 +1598,7 @@ property a cardinality refers to, is not used inside the data. Also, the propert isn't allowed to be used inside the data in any subclasses of this class. ``` -HTTP DELETE to http://host/v2/ontologies/cardinalities +HTTP PATCH to http://host/v2/ontologies/cardinalities ``` ```jsonld @@ -1645,7 +1645,7 @@ definition (but not any of the other entities in the ontology). To check whether a class's cardinality can be deleted: ``` -HTTP POST to http://host/v2/ontologies/candeletedinalities +HTTP POST to http://host/v2/ontologies/candeletecardinalities ``` The response will look like this: diff --git a/webapi/src/main/resources/application.conf b/webapi/src/main/resources/application.conf index 01b0a7718c..09a07bd7fe 100644 --- a/webapi/src/main/resources/application.conf +++ b/webapi/src/main/resources/application.conf @@ -250,7 +250,7 @@ akka-http-cors { # # The preflight request will be rejected if the `Access-Control-Request-Method` # header's method is not part of the list. - allowed-methods = ["GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS"] + allowed-methods = ["GET", "PUT", "POST", "DELETE", "PATCH", "HEAD", "OPTIONS"] # List of headers (other than simple response headers) that browsers are allowed to access. # If not empty, this list is returned as part of the `Access-Control-Expose-Headers` diff --git a/webapi/src/main/scala/org/knora/webapi/routing/v2/OntologiesRouteV2.scala b/webapi/src/main/scala/org/knora/webapi/routing/v2/OntologiesRouteV2.scala index ea05706df0..1eb232809f 100644 --- a/webapi/src/main/scala/org/knora/webapi/routing/v2/OntologiesRouteV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/routing/v2/OntologiesRouteV2.scala @@ -526,7 +526,7 @@ class OntologiesRouteV2(routeData: KnoraRouteData) extends KnoraRoute(routeData) // not used in resources. private def deleteCardinalitiesFromClass(featureFactoryConfig: FeatureFactoryConfig): Route = path(OntologiesBasePath / "cardinalities") { - delete { + patch { entity(as[String]) { jsonRequest => requestContext => { val requestMessageFuture: Future[DeleteCardinalitiesFromClassRequestV2] = for { diff --git a/webapi/src/test/scala/org/knora/webapi/e2e/CORSSupportE2ESpec.scala b/webapi/src/test/scala/org/knora/webapi/e2e/CORSSupportE2ESpec.scala index 2fc58a7128..8df6c7313b 100644 --- a/webapi/src/test/scala/org/knora/webapi/e2e/CORSSupportE2ESpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/e2e/CORSSupportE2ESpec.scala @@ -36,8 +36,8 @@ object CORSSupportE2ESpec { } /** - * End-to-end test specification for testing [[CORSSupport]]. - */ + * End-to-end test specification for testing [[CORSSupport]]. + */ class CORSSupportE2ESpec extends E2ESpec(CORSSupportE2ESpec.config) { implicit def default(implicit system: ActorSystem) = RouteTestTimeout(settings.defaultTimeout) @@ -51,30 +51,23 @@ class CORSSupportE2ESpec extends E2ESpec(CORSSupportE2ESpec.config) { "A Route with enabled CORS support" should { "accept valid pre-flight requests" in { - val request = Options(baseApiUrl + s"/admin/projects") ~> Origin(exampleOrigin) ~> `Access-Control-Request-Method`( - GET) + val request = + Options(baseApiUrl + s"/admin/projects") ~> Origin(exampleOrigin) ~> `Access-Control-Request-Method`(GET) val response: HttpResponse = singleAwaitingRequest(request) response.status shouldBe StatusCodes.OK response.headers should contain allElementsOf Seq( `Access-Control-Allow-Origin`(exampleOrigin), - `Access-Control-Allow-Methods`(List(GET, PUT, POST, DELETE, HEAD, OPTIONS)), + `Access-Control-Allow-Methods`(List(GET, PUT, POST, DELETE, PATCH, HEAD, OPTIONS)), `Access-Control-Max-Age`(1800), `Access-Control-Allow-Credentials`(true) ) } - "reject requests with invalid method" in { - val request = Options(baseApiUrl + s"/admin/projects") ~> Origin(exampleOrigin) ~> `Access-Control-Request-Method`( - PATCH) - val response: HttpResponse = singleAwaitingRequest(request) - responseToString(response) shouldEqual "CORS: invalid method 'PATCH'" - response.status shouldBe StatusCodes.BadRequest - } - "send `Access-Control-Allow-Origin` header when the Knora resource is found " in { val request = Get( - baseApiUrl + "/v1/resources/" + java.net.URLEncoder.encode("http://rdfh.ch/0001/55UrkgTKR2SEQgnsLWI9mg", - "utf-8")) ~> Origin(exampleOrigin) + baseApiUrl + "/v1/resources/" + java.net.URLEncoder + .encode("http://rdfh.ch/0001/55UrkgTKR2SEQgnsLWI9mg", "utf-8") + ) ~> Origin(exampleOrigin) val response = singleAwaitingRequest(request) response.status should equal(StatusCodes.OK) response.headers should contain allElementsOf Seq( @@ -83,8 +76,9 @@ class CORSSupportE2ESpec extends E2ESpec(CORSSupportE2ESpec.config) { } "send `Access-Control-Allow-Origin` header when the Knora resource is NOT found " in { - val request = Get(baseApiUrl + "/v1/resources/" + java.net.URLEncoder.encode("http://rdfh.ch/0803/nonexistent", - "utf-8")) ~> Origin(exampleOrigin) + val request = Get( + baseApiUrl + "/v1/resources/" + java.net.URLEncoder.encode("http://rdfh.ch/0803/nonexistent", "utf-8") + ) ~> Origin(exampleOrigin) val response = singleAwaitingRequest(request) response.status should equal(StatusCodes.NotFound) response.headers should contain allElementsOf Seq( @@ -93,7 +87,7 @@ class CORSSupportE2ESpec extends E2ESpec(CORSSupportE2ESpec.config) { } "send `Access-Control-Allow-Origin` header when the api endpoint route is NOT found " in { - val request = Get(baseApiUrl + "/NotFound") ~> Origin(exampleOrigin) + val request = Get(baseApiUrl + "/NotFound") ~> Origin(exampleOrigin) val response = singleAwaitingRequest(request) response.status should equal(StatusCodes.NotFound) response.headers should contain allElementsOf Seq( diff --git a/webapi/src/test/scala/org/knora/webapi/e2e/v2/OntologyV2R2RSpec.scala b/webapi/src/test/scala/org/knora/webapi/e2e/v2/OntologyV2R2RSpec.scala index a6a335be76..ee16ff4bcb 100644 --- a/webapi/src/test/scala/org/knora/webapi/e2e/v2/OntologyV2R2RSpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/e2e/v2/OntologyV2R2RSpec.scala @@ -2911,7 +2911,7 @@ class OntologyV2R2RSpec extends R2RSpec { } // Successfully remove the (unused) text value cardinality from the class. - Delete("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials( + Patch("/v2/ontologies/cardinalities", HttpEntity(RdfMediaTypes.`application/ld+json`, params)) ~> addCredentials( BasicHttpCredentials(anythingUsername, password) ) ~> ontologiesPath ~> check { val responseStr = responseAs[String]