From c49c8136451ae9d0de30c19a5724b4ec1b17ad62 Mon Sep 17 00:00:00 2001 From: Anton Baliasnikov Date: Thu, 30 Nov 2023 08:27:14 +0000 Subject: [PATCH] test: enable remote envs testing in integration tests (#801) Signed-off-by: Anton Baliasnikov --- .../src/test/kotlin/common/ListenToEvents.kt | 13 +++--- .../src/test/kotlin/config/Webhook.kt | 1 + .../src/test/kotlin/config/services/Agent.kt | 2 + .../src/test/kotlin/features/Init.kt | 44 +++++++++++++++++-- .../src/test/resources/containers/agent.yml | 2 +- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/tests/integration-tests/src/test/kotlin/common/ListenToEvents.kt b/tests/integration-tests/src/test/kotlin/common/ListenToEvents.kt index 3efae51c49..3410f9c908 100644 --- a/tests/integration-tests/src/test/kotlin/common/ListenToEvents.kt +++ b/tests/integration-tests/src/test/kotlin/common/ListenToEvents.kt @@ -17,7 +17,8 @@ import java.net.URL import java.time.OffsetDateTime open class ListenToEvents( - private val url: URL + private val url: URL, + webhookPort: Int? ) : Ability, HasTeardown { private val server: ApplicationEngine @@ -30,7 +31,7 @@ open class ListenToEvents( var presentationEvents: MutableList = mutableListOf() var didEvents: MutableList = mutableListOf() - fun route(application: Application) { + private fun route(application: Application) { application.routing { post("/") { val eventString = call.receiveText() @@ -52,8 +53,8 @@ open class ListenToEvents( } companion object { - fun at(url: URL): ListenToEvents { - return ListenToEvents(url) + fun at(url: URL, webhookPort: Int?): ListenToEvents { + return ListenToEvents(url, webhookPort) } fun `as`(actor: Actor): ListenToEvents { @@ -64,8 +65,8 @@ open class ListenToEvents( init { server = embeddedServer( Netty, - port = url.port, - host = if (url.host == "host.docker.internal") "0.0.0.0" else url.host, + port = webhookPort ?: url.port, + host = "0.0.0.0", module = { route(this) } ) .start(wait = false) diff --git a/tests/integration-tests/src/test/kotlin/config/Webhook.kt b/tests/integration-tests/src/test/kotlin/config/Webhook.kt index 80aba3778f..c1b174329e 100644 --- a/tests/integration-tests/src/test/kotlin/config/Webhook.kt +++ b/tests/integration-tests/src/test/kotlin/config/Webhook.kt @@ -5,5 +5,6 @@ import java.net.URL data class Webhook( val url: URL, + @ConfigAlias("local_port") val localPort: Int? = null, @ConfigAlias("init_required") val initRequired: Boolean = true ) diff --git a/tests/integration-tests/src/test/kotlin/config/services/Agent.kt b/tests/integration-tests/src/test/kotlin/config/services/Agent.kt index f0c639aa5e..08034eefac 100644 --- a/tests/integration-tests/src/test/kotlin/config/services/Agent.kt +++ b/tests/integration-tests/src/test/kotlin/config/services/Agent.kt @@ -9,6 +9,7 @@ data class Agent( val version: String, @ConfigAlias("http_port") val httpPort: Int, @ConfigAlias("didcomm_port") val didcommPort: Int, + @ConfigAlias("didcomm_service_url") val didcommServiceUrl: String?, @ConfigAlias("auth_enabled") val authEnabled: Boolean, @ConfigAlias("prism_node") val prismNode: PrismNode?, val keycloak: Keycloak?, @@ -23,6 +24,7 @@ data class Agent( "OPEN_ENTERPRISE_AGENT_VERSION" to version, "API_KEY_ENABLED" to authEnabled.toString(), "AGENT_DIDCOMM_PORT" to didcommPort.toString(), + "DIDCOMM_SERVICE_URL" to (didcommServiceUrl ?: "http://host.docker.internal:${didcommPort}"), "AGENT_HTTP_PORT" to httpPort.toString(), "PRISM_NODE_PORT" to (prismNode?.httpPort?.toString() ?: ""), "SECRET_STORAGE_BACKEND" to if (vault != null) "vault" else "postgres", diff --git a/tests/integration-tests/src/test/kotlin/features/Init.kt b/tests/integration-tests/src/test/kotlin/features/Init.kt index 62300490d6..1035d14736 100644 --- a/tests/integration-tests/src/test/kotlin/features/Init.kt +++ b/tests/integration-tests/src/test/kotlin/features/Init.kt @@ -72,11 +72,13 @@ fun initActors() { if (actor.recall("BEARER_TOKEN") != null) { spec.addHeader("Authorization", "Bearer ${actor.recall("BEARER_TOKEN")}") } - RestAssured + val response = RestAssured .given().spec(spec.build()) .body(CreateWebhookNotification(url = webhookUrl)) .post("/events/webhooks") - .then().statusCode(HttpStatus.SC_OK) + .thenReturn() + response.then().statusCode(HttpStatus.SC_OK) + actor.remember("WEBHOOK_ID", response.body.jsonPath().getString("id")) } val cast = Cast() @@ -103,7 +105,7 @@ fun initActors() { actor.remember("AUTH_HEADER", role.authHeader) } if (role.webhook != null) { - actor.whoCan(ListenToEvents.at(role.webhook.url)) + actor.whoCan(ListenToEvents.at(role.webhook.url, role.webhook.localPort)) if (role.webhook.initRequired) { registerWebhook(actor, role.webhook.url.toExternalForm()) } @@ -112,6 +114,40 @@ fun initActors() { OnStage.setTheStage(cast) } +/** + * This function destroys all actors and clears the stage. + */ +fun destroyActors() { + /** + * This function deletes a webhook for an actor. + * + * @param actor The actor for which the webhook should be deleted. + */ + fun deleteWebhook(actor: Actor) { + val spec = RequestSpecBuilder() + .setBaseUri(actor.usingAbilityTo(CallAnApi::class.java).resolve("/")) + if (actor.recall("AUTH_KEY") != null) { + spec.addHeader(actor.recall("AUTH_HEADER"), actor.recall("AUTH_KEY")) + } + if (actor.recall("BEARER_TOKEN") != null) { + spec.addHeader("Authorization", "Bearer ${actor.recall("BEARER_TOKEN")}") + } + RestAssured + .given().spec(spec.build()) + .delete("/events/webhooks/${actor.recall("WEBHOOK_ID")}") + .then().statusCode(HttpStatus.SC_OK) + } + + // Delete webhooks + config.roles.forEach { role -> + val actor = OnStage.theActorCalled(role.name) + if (role.webhook != null && role.webhook.initRequired) { + deleteWebhook(actor) + } + } + OnStage.drawTheCurtain() +} + @BeforeAll fun init() { initServices() @@ -120,7 +156,7 @@ fun init() { @AfterAll fun clearStage() { - OnStage.drawTheCurtain() + destroyActors() config.agents?.forEach { agent -> agent.stop() } diff --git a/tests/integration-tests/src/test/resources/containers/agent.yml b/tests/integration-tests/src/test/resources/containers/agent.yml index 72a208ff55..45d209b0d9 100644 --- a/tests/integration-tests/src/test/resources/containers/agent.yml +++ b/tests/integration-tests/src/test/resources/containers/agent.yml @@ -37,7 +37,7 @@ services: # Configuration parameters AGENT_DIDCOMM_PORT: AGENT_HTTP_PORT: - DIDCOMM_SERVICE_URL: "http://host.docker.internal:${AGENT_DIDCOMM_PORT}" + DIDCOMM_SERVICE_URL: REST_SERVICE_URL: "http://host.docker.internal:${AGENT_HTTP_PORT}" API_KEY_ENABLED: # Secret storage configuration