Skip to content

Commit

Permalink
test: enable remote envs testing in integration tests (#801)
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Baliasnikov <[email protected]>
  • Loading branch information
Anton Baliasnikov authored Nov 30, 2023
1 parent e5b1b14 commit c49c813
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
13 changes: 7 additions & 6 deletions tests/integration-tests/src/test/kotlin/common/ListenToEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +31,7 @@ open class ListenToEvents(
var presentationEvents: MutableList<PresentationEvent> = mutableListOf()
var didEvents: MutableList<DidEvent> = mutableListOf()

fun route(application: Application) {
private fun route(application: Application) {
application.routing {
post("/") {
val eventString = call.receiveText()
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/integration-tests/src/test/kotlin/config/Webhook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -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",
Expand Down
44 changes: 40 additions & 4 deletions tests/integration-tests/src/test/kotlin/features/Init.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ fun initActors() {
if (actor.recall<String>("BEARER_TOKEN") != null) {
spec.addHeader("Authorization", "Bearer ${actor.recall<String>("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()
Expand All @@ -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())
}
Expand All @@ -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<String>("AUTH_KEY") != null) {
spec.addHeader(actor.recall("AUTH_HEADER"), actor.recall("AUTH_KEY"))
}
if (actor.recall<String>("BEARER_TOKEN") != null) {
spec.addHeader("Authorization", "Bearer ${actor.recall<String>("BEARER_TOKEN")}")
}
RestAssured
.given().spec(spec.build())
.delete("/events/webhooks/${actor.recall<String>("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()
Expand All @@ -120,7 +156,7 @@ fun init() {

@AfterAll
fun clearStage() {
OnStage.drawTheCurtain()
destroyActors()
config.agents?.forEach { agent ->
agent.stop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c49c813

Please sign in to comment.