From 7dc4d31aedb0f6d8ff5a60a5f2aac5c190615998 Mon Sep 17 00:00:00 2001 From: hongwei Date: Mon, 30 Mar 2020 09:57:51 +0200 Subject: [PATCH] added the test for deleteDynamicEndpoint --- .../scala/code/api/v4_0_0/APIMethods400.scala | 2 +- .../api/v4_0_0/DynamicendPointsTest.scala | 86 +++++++++++++++++-- 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala index 55d2320a4..a077eb3f9 100644 --- a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala +++ b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala @@ -2931,7 +2931,7 @@ trait APIMethods400 { for { deleted <- NewStyle.function.deleteDynamicEndpoint(dynamicEndpointId, cc.callContext) } yield { - (deleted, HttpCode.`200`(cc.callContext)) + (deleted, HttpCode.`204`(cc.callContext)) } } } diff --git a/obp-api/src/test/scala/code/api/v4_0_0/DynamicendPointsTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/DynamicendPointsTest.scala index 5af5cdb82..a6d9cc83f 100644 --- a/obp-api/src/test/scala/code/api/v4_0_0/DynamicendPointsTest.scala +++ b/obp-api/src/test/scala/code/api/v4_0_0/DynamicendPointsTest.scala @@ -25,6 +25,7 @@ class DynamicEndpointsTest extends V400ServerSetup { object ApiEndpoint1 extends Tag(nameOf(Implementations4_0_0.createDynamicEndpoint)) object ApiEndpoint2 extends Tag(nameOf(Implementations4_0_0.getDynamicEndpoints)) object ApiEndpoint3 extends Tag(nameOf(Implementations4_0_0.getDynamicEndpoint)) + object ApiEndpoint4 extends Tag(nameOf(Implementations4_0_0.deleteDynamicEndpoint)) feature(s"test $ApiEndpoint1 version $VersionOfApi - Unauthorized access") { @@ -80,7 +81,7 @@ class DynamicEndpointsTest extends V400ServerSetup { } feature(s"test $ApiEndpoint2 version $VersionOfApi - Unauthorized access") { - scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) { + scenario("We will call the endpoint without user credentials", ApiEndpoint2, VersionOfApi) { When("We make a request v4.0.0") val request400 = (v4_0_0_Request / "management" / "dynamic-endpoints").GET val response400 = makeGetRequest(request400) @@ -91,7 +92,7 @@ class DynamicEndpointsTest extends V400ServerSetup { } feature(s"test $ApiEndpoint2 version $VersionOfApi - authorized access- missing role") { - scenario("We will call the endpoint with user credentials", ApiEndpoint1, VersionOfApi) { + scenario("We will call the endpoint with user credentials", ApiEndpoint2, VersionOfApi) { When("We make a request v4.0.0") val request = (v4_0_0_Request / "management" / "dynamic-endpoints").GET<@ (user1) val response = makeGetRequest(request) @@ -102,7 +103,7 @@ class DynamicEndpointsTest extends V400ServerSetup { } feature(s"test $ApiEndpoint2 version $VersionOfApi - authorized access - with role - should be success!") { - scenario("We will call the endpoint with user credentials", ApiEndpoint1, VersionOfApi) { + scenario("We will call the endpoint with user credentials", ApiEndpoint2, VersionOfApi) { When("We make a request v4.0.0") val postDynamicEndpointRequestBodyExample = ExampleValue.dynamicEndpointRequestBodyExample @@ -144,7 +145,7 @@ class DynamicEndpointsTest extends V400ServerSetup { } feature(s"test $ApiEndpoint3 version $VersionOfApi - Unauthorized access") { - scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) { + scenario("We will call the endpoint without user credentials", ApiEndpoint3, VersionOfApi) { When("We make a request v4.0.0") val request400 = (v4_0_0_Request / "management" / "dynamic-endpoints"/ "some-id").GET val response400 = makeGetRequest(request400) @@ -155,7 +156,7 @@ class DynamicEndpointsTest extends V400ServerSetup { } feature(s"test $ApiEndpoint3 version $VersionOfApi - authorized access- missing role") { - scenario("We will call the endpoint with user credentials", ApiEndpoint1, VersionOfApi) { + scenario("We will call the endpoint with user credentials", ApiEndpoint3, VersionOfApi) { When("We make a request v4.0.0") val request = (v4_0_0_Request / "management" / "dynamic-endpoints" /"some-id").GET<@ (user1) val response = makeGetRequest(request) @@ -166,7 +167,7 @@ class DynamicEndpointsTest extends V400ServerSetup { } feature(s"test $ApiEndpoint3 version $VersionOfApi - authorized access - with role - should be success!") { - scenario("We will call the endpoint with user credentials", ApiEndpoint1, VersionOfApi) { + scenario("We will call the endpoint with user credentials", ApiEndpoint3, VersionOfApi) { When("We make a request v4.0.0") val postDynamicEndpointRequestBodyExample = ExampleValue.dynamicEndpointRequestBodyExample @@ -208,5 +209,76 @@ class DynamicEndpointsTest extends V400ServerSetup { response400.body.toString contains("Example Company") should be (true) } - } + } + + feature(s"test $ApiEndpoint4 version $VersionOfApi - Unauthorized access") { + scenario("We will call the endpoint without user credentials", ApiEndpoint4, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "management" / "dynamic-endpoints"/ "some-id").DELETE + val response400 = makeDeleteRequest(request400) + Then("We should get a 400") + response400.code should equal(400) + response400.body.extract[ErrorMessage].message should equal(UserNotLoggedIn) + } + } + + feature(s"test $ApiEndpoint4 version $VersionOfApi - authorized access- missing role") { + scenario("We will call the endpoint with user credentials", ApiEndpoint4, VersionOfApi) { + When("We make a request v4.0.0") + val request = (v4_0_0_Request / "management" / "dynamic-endpoints" /"some-id").DELETE<@ (user1) + val response = makeDeleteRequest(request) + Then("We should get a 400") + response.code should equal(403) + response.body.extract[ErrorMessage].message.toString contains (UserHasMissingRoles) should be (true) + } + } + + feature(s"test $ApiEndpoint4 version $VersionOfApi - authorized access - with role - should be success!") { + scenario("We will call the endpoint with user credentials", ApiEndpoint4, VersionOfApi) { + When("We make a request v4.0.0") + val postDynamicEndpointRequestBodyExample = ExampleValue.dynamicEndpointRequestBodyExample + + When("We make a request v4.0.0") + val request = (v4_0_0_Request / "management" / "dynamic-endpoints").POST<@ (user1) + val response = makePostRequest(request, write(postDynamicEndpointRequestBodyExample)) + Then("We should get a 403") + response.code should equal(403) + response.body.extract[ErrorMessage].message.toString contains (UserHasMissingRoles) should be (true) + + Then("We grant the role to the user1") + Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetDynamicEndpoint.toString) + Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanCreateDynamicEndpoint.toString) + Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanDeleteDynamicEndpoint.toString) + + val newSwagger = postDynamicEndpointRequestBodyExample.transformField { + case JField(name, value) if name.startsWith("/") => JField(s"$name/def2", value) + } + + val responseWithRole = makePostRequest(request, write(newSwagger)) + Then("We should get a 201") + responseWithRole.code should equal(201) + + + val id = responseWithRole.body.\\("dynamic_endpoint_id").values.get("dynamic_endpoint_id").head.toString + + val request400 = (v4_0_0_Request / "management" / "dynamic-endpoints" /id).GET<@ (user1) + val response400 = makeGetRequest(request400) + response400.code should be (200) + response400.body.toString contains("dynamic_endpoint_id") should be (true) + response400.body.toString contains("swagger_string") should be (true) + response400.body.toString contains("Example Title") should be (true) + response400.body.toString contains("Example Description") should be (true) + response400.body.toString contains("Example Company") should be (true) + + + val requestDelete = (v4_0_0_Request / "management" / "dynamic-endpoints" /id).DELETE<@ (user1) + val responseDelete = makeDeleteRequest(requestDelete) + responseDelete.code should be (204) + + val responseGetAgain = makeGetRequest(request400) + responseGetAgain.code should be (404) + + + } + } }