From 72a6df1560001b77e6b9579dba1a9b2414baa59d Mon Sep 17 00:00:00 2001 From: Michael Spector Date: Thu, 11 Apr 2024 10:54:55 -0400 Subject: [PATCH 1/3] basic working Cromwell -> ECM contract test --- .../workbench/cromwell/consumer/Helper.scala | 22 +++++++ .../consumer/EcmServiceContractSpec.scala | 62 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala diff --git a/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala b/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala index 1405ec20f87..fc48ea5a675 100644 --- a/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala +++ b/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala @@ -122,6 +122,28 @@ object PactHelper { .headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) .body(responseBody) + def buildInteraction(builder: PactDslWithProvider, + state: String, + stateParams: Map[String, Any], + uponReceiving: String, + method: String, + path: String, + requestHeaders: Seq[(String, String)], + status: Int, + responseHeaders: Seq[(String, String)], + responseString: String + ): PactDslResponse = + builder + .`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) + .uponReceiving(uponReceiving) + .method(method) + .path(path) + .headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) + .willRespondWith() + .status(status) + .headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) + .body(responseString) + def buildInteraction[A](builder: PactDslWithProvider, state: String, uponReceiving: String, diff --git a/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala b/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala new file mode 100644 index 00000000000..24524d9f784 --- /dev/null +++ b/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala @@ -0,0 +1,62 @@ +package org.broadinstitute.dsde.workbench.cromwell.consumer + +import au.com.dius.pact.consumer.dsl._ +import au.com.dius.pact.consumer.{ConsumerPactBuilder, PactTestExecutionContext} +import au.com.dius.pact.core.model.RequestResponsePact +import cromwell.core.TestKitSuite +import cromwell.services.auth.GithubAuthVending.{GithubToken, TerraToken} +import cromwell.services.auth.ecm.EcmService +import org.scalatest.flatspec.AnyFlatSpecLike + +import scala.concurrent.{Await, ExecutionContextExecutor} +import org.scalatest.matchers.should.Matchers +import pact4s.scalatest.RequestResponsePactForger + +import scala.concurrent.duration.DurationInt + +class EcmServiceContractSpec extends TestKitSuite with AnyFlatSpecLike with Matchers with RequestResponsePactForger { + + implicit val ec: ExecutionContextExecutor = system.dispatcher + + /* + Define the folder that the pact contracts get written to upon completion of this test suite. + */ + override val pactTestExecutionContext: PactTestExecutionContext = + new PactTestExecutionContext( + "./target/pacts" + ) + + val consumerPactBuilder: ConsumerPactBuilder = ConsumerPactBuilder + .consumer("cromwell") + + val pactProvider: PactDslWithProvider = consumerPactBuilder + .hasPactWith("ecm") + + var testUser: String = "cromwell_test_user@test.com" + var testBearerToken: String = "cromwellBearerToken" + var testGithubToken: String = "githubToken" + + var pactDslResponse: PactDslResponse = pactProvider + .`given`("a user is registered", Map[String, String]( + "userEmail" -> testUser, + "bearerToken" -> testBearerToken, + ) + ) + .uponReceiving("a github token request") + .method("GET") + .path("/api/oauth/v1/github/access-token") + .headers(Map[String, String] ("Authorization" -> s"Bearer $testBearerToken")) + .willRespondWith() + .status(200) + .bodyMatchingContentType("text/plain", testGithubToken) + + override val pact: RequestResponsePact = pactDslResponse.toPact + + it should "get a github token" in { + Await.result( + new EcmService(mockServer.getUrl) + .getGithubAccessToken(TerraToken(testBearerToken)), + 10.seconds + ) shouldBe GithubToken(testGithubToken) + } +} From b0a849266ab4191d6e2846fa2b93d9b8701d8acd Mon Sep 17 00:00:00 2001 From: Michael Spector Date: Thu, 11 Apr 2024 17:46:24 -0400 Subject: [PATCH 2/3] scalafmt --- .../dsde/workbench/cromwell/consumer/Helper.scala | 2 +- .../cromwell/consumer/EcmServiceContractSpec.scala | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala b/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala index fc48ea5a675..00a2ee1fa4b 100644 --- a/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala +++ b/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala @@ -132,7 +132,7 @@ object PactHelper { status: Int, responseHeaders: Seq[(String, String)], responseString: String - ): PactDslResponse = + ): PactDslResponse = builder .`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) .uponReceiving(uponReceiving) diff --git a/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala b/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala index 24524d9f784..e8101f66927 100644 --- a/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala +++ b/pact4s/src/test/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/EcmServiceContractSpec.scala @@ -37,15 +37,16 @@ class EcmServiceContractSpec extends TestKitSuite with AnyFlatSpecLike with Matc var testGithubToken: String = "githubToken" var pactDslResponse: PactDslResponse = pactProvider - .`given`("a user is registered", Map[String, String]( - "userEmail" -> testUser, - "bearerToken" -> testBearerToken, - ) + .`given`("a user is registered", + Map[String, String]( + "userEmail" -> testUser, + "bearerToken" -> testBearerToken + ) ) .uponReceiving("a github token request") .method("GET") .path("/api/oauth/v1/github/access-token") - .headers(Map[String, String] ("Authorization" -> s"Bearer $testBearerToken")) + .headers(Map[String, String]("Authorization" -> s"Bearer $testBearerToken")) .willRespondWith() .status(200) .bodyMatchingContentType("text/plain", testGithubToken) @@ -55,7 +56,7 @@ class EcmServiceContractSpec extends TestKitSuite with AnyFlatSpecLike with Matc it should "get a github token" in { Await.result( new EcmService(mockServer.getUrl) - .getGithubAccessToken(TerraToken(testBearerToken)), + .getGithubAccessToken(TerraToken(testBearerToken)), 10.seconds ) shouldBe GithubToken(testGithubToken) } From 2e687ab0664617e77d1f01a8c432b1e1db87a6e5 Mon Sep 17 00:00:00 2001 From: Michael Spector Date: Thu, 11 Apr 2024 18:02:28 -0400 Subject: [PATCH 3/3] revert unused changes --- .../workbench/cromwell/consumer/Helper.scala | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala b/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala index 00a2ee1fa4b..1405ec20f87 100644 --- a/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala +++ b/pact4s/src/main/scala/org/broadinstitute/dsde/workbench/cromwell/consumer/Helper.scala @@ -122,28 +122,6 @@ object PactHelper { .headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) .body(responseBody) - def buildInteraction(builder: PactDslWithProvider, - state: String, - stateParams: Map[String, Any], - uponReceiving: String, - method: String, - path: String, - requestHeaders: Seq[(String, String)], - status: Int, - responseHeaders: Seq[(String, String)], - responseString: String - ): PactDslResponse = - builder - .`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) - .uponReceiving(uponReceiving) - .method(method) - .path(path) - .headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) - .willRespondWith() - .status(status) - .headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) - .body(responseString) - def buildInteraction[A](builder: PactDslWithProvider, state: String, uponReceiving: String,