From a24cdddc9f40f86fc31b0a95a65ca28a947c4776 Mon Sep 17 00:00:00 2001 From: Paul Cleary Date: Mon, 21 Oct 2019 12:02:27 -0400 Subject: [PATCH] Build optimizations (#883) A few specific build optimizations: 1. Consolidated `dockerComposeUp` to only use a single `root/docker/docker-compose.yml` instead of each module having its own docker-compose files. This eliminates additional waits for docker containers to startup and stop, as well as reduces memory consumption during the build 2. Cleaned up `VinylDNSSpec` - I noticed that this spec was taking 3 minutes to run! I discovered that the way we were mocking the `WSClient` was largely to blame. Was able to get tests to run in **16 SECONDS** using a library called `mock-ws`. This is where we see most savings. 3. Added back `dynamodb-local` instead of running it in `localstack`. Integration tests for dynamodb were very slow in localstack. This added an additional 20-30 second improvement. After doing several tests locally running the following command... ``` > SECONDS=0; sbt verify; echo "DURATION = $SECONDS SECONDS" ``` Current master took 535 seconds to run; with these optimizations it took **211 SECONDS** - that is a 60% improvement. The initial Travis builds reported a run time of 13 minutes as opposed to 19 minutes; this would save some 6 minutes off of Travis build times (or 30% improvement). --- build.sbt | 18 +- docker/.env | 2 +- docker/api/docker.conf | 2 + docker/docker-compose-func-test.yml | 15 +- docker/docker-compose.yml | 19 +- docker/functest/run.sh | 13 +- modules/dynamodb/docker/docker-compose.yml | 9 - .../src/it/resources/application.conf | 22 +- .../repository/DynamoDBIntegrationSpec.scala | 2 +- modules/mysql/docker/conf/config-file.cnf | 3 - modules/mysql/docker/docker-compose.yml | 11 - .../mysql/src/it/resources/application.conf | 12 +- .../test/controllers/VinylDNSSpec.scala | 2514 +++++++---------- modules/sqs/docker/custom.conf | 21 - modules/sqs/docker/docker-compose.yml | 9 - modules/sqs/src/it/resources/application.conf | 2 +- ...sMessageQueueProviderIntegrationSpec.scala | 10 +- project/Dependencies.scala | 3 +- 18 files changed, 1042 insertions(+), 1645 deletions(-) delete mode 100644 modules/dynamodb/docker/docker-compose.yml delete mode 100644 modules/mysql/docker/conf/config-file.cnf delete mode 100644 modules/mysql/docker/docker-compose.yml delete mode 100644 modules/sqs/docker/custom.conf delete mode 100644 modules/sqs/docker/docker-compose.yml diff --git a/build.sbt b/build.sbt index 5e4528d75..1353b1c5b 100644 --- a/build.sbt +++ b/build.sbt @@ -136,7 +136,6 @@ lazy val apiDockerSettings = Seq( ExecCmd("RUN", "apk", "add", "--update", "--no-cache", "netcat-openbsd", "bash"), Cmd("USER", "1001:0") // switch back to the daemon user ), - composeFile := baseDirectory.value.getAbsolutePath + "/../../docker/docker-compose.yml" ) lazy val portalDockerSettings = Seq( @@ -219,7 +218,7 @@ lazy val allApiSettings = Revolver.settings ++ Defaults.itSettings ++ scalaStyleSettings lazy val api = (project in file("modules/api")) - .enablePlugins(JavaAppPackaging, DockerComposePlugin, AutomateHeaderPlugin) + .enablePlugins(JavaAppPackaging, AutomateHeaderPlugin) .configs(IntegrationTest) .settings(allApiSettings) .settings(headerSettings(IntegrationTest)) @@ -230,7 +229,7 @@ lazy val api = (project in file("modules/api")) .dependsOn(sqs % "compile->compile;it->it") val killDocker = TaskKey[Unit]("killDocker", "Kills all vinyldns docker containers") -lazy val root = (project in file(".")).enablePlugins(AutomateHeaderPlugin) +lazy val root = (project in file(".")).enablePlugins(DockerComposePlugin, AutomateHeaderPlugin) .configs(IntegrationTest) .settings(headerSettings(IntegrationTest)) .settings(sharedSettings) @@ -290,7 +289,7 @@ lazy val core = (project in file("modules/core")).enablePlugins(AutomateHeaderPl ) lazy val dynamodb = (project in file("modules/dynamodb")) - .enablePlugins(DockerComposePlugin, AutomateHeaderPlugin) + .enablePlugins(AutomateHeaderPlugin) .configs(IntegrationTest) .settings(sharedSettings) .settings(headerSettings(IntegrationTest)) @@ -308,7 +307,7 @@ lazy val dynamodb = (project in file("modules/dynamodb")) .settings(name := "dynamodb") lazy val mysql = (project in file("modules/mysql")) - .enablePlugins(DockerComposePlugin, AutomateHeaderPlugin) + .enablePlugins(AutomateHeaderPlugin) .configs(IntegrationTest) .settings(sharedSettings) .settings(headerSettings(IntegrationTest)) @@ -324,7 +323,7 @@ lazy val mysql = (project in file("modules/mysql")) .settings(name := "mysql") lazy val sqs = (project in file("modules/sqs")) - .enablePlugins(DockerComposePlugin, AutomateHeaderPlugin) + .enablePlugins(AutomateHeaderPlugin) .configs(IntegrationTest) .settings(sharedSettings) .settings(headerSettings(IntegrationTest)) @@ -520,12 +519,9 @@ addCommandAlias("validate", "; root/clean; " + "root/compile;root/test:compile;root/it:compile" ) -addCommandAlias("verify", "; project root; killDocker; " + - "project api; dockerComposeUp; project dynamodb; dockerComposeUp; project mysql; dockerComposeUp; " + - "project sqs; dockerComposeUp;" + +addCommandAlias("verify", "; project root; killDocker; dockerComposeUp; " + "project root; coverage; " + - "all core/test dynamodb/test mysql/test api/test dynamodb/it:test mysql/it:test api/it:test portal/test " + - "sqs/test sqs/it:test; " + + "all test it:test; " + "project root; coverageReport; coverageAggregate; killDocker" ) diff --git a/docker/.env b/docker/.env index 454f5fc24..027ff2bed 100644 --- a/docker/.env +++ b/docker/.env @@ -11,7 +11,7 @@ PORTAL_PORT=9001 PLAY_HTTP_SECRET_KEY=change-this-for-prod VINYLDNS_BACKEND_URL=http://vinyldns-api:9000 SQS_ENDPOINT=http://vinyldns-localstack:19007 -DYNAMODB_ENDPOINT=http://vinyldns-localstack:19000 +DYNAMODB_ENDPOINT=http://vinyldns-dynamodb:8000 MYSQL_ENDPOINT=vinyldns-mysql:3306 USER_TABLE_NAME=users USER_CHANGE_TABLE_NAME=userChange diff --git a/docker/api/docker.conf b/docker/api/docker.conf index cccecee2f..9636644f3 100644 --- a/docker/api/docker.conf +++ b/docker/api/docker.conf @@ -232,6 +232,8 @@ vinyldns { scheduled-changes-enabled = true + multi-record-batch-change-enabled = true + global-acl-rules = [ { group-ids: ["global-acl-group-id"], diff --git a/docker/docker-compose-func-test.yml b/docker/docker-compose-func-test.yml index bddcdaa06..081d548df 100644 --- a/docker/docker-compose-func-test.yml +++ b/docker/docker-compose-func-test.yml @@ -24,16 +24,27 @@ services: logging: driver: none + dynamodb-local: + image: cnadiminti/dynamodb-local:2017-02-16 + container_name: "vinyldns-dynamodb" + env_file: + .env + logging: + driver: none + ports: + - "19000:8000" + command: "--sharedDb --inMemory" + localstack: image: localstack/localstack:0.10.4 container_name: "vinyldns-localstack" ports: - - "19000:19000" - "19006:19006" - "19007:19007" environment: - - SERVICES=dynamodb:19000,sns:19006,sqs:19007 + - SERVICES=sns:19006,sqs:19007 - START_WEB=0 + - HOSTNAME_EXTERNAL=vinyldns-localstack # this file is copied into the target directory to get the jar! won't run in place as is! api: diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index f157fcdf3..ba71c010b 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,14 +1,14 @@ version: "3.0" services: mysql: - image: "mysql:5.7" + image: mysql:5.7 env_file: .env ports: - "19002:3306" bind9: - image: "vinyldns/bind9:0.0.4" + image: vinyldns/bind9:0.0.4 env_file: .env ports: @@ -18,24 +18,31 @@ services: - ./bind9/etc:/var/cache/bind/config - ./bind9/zones:/var/cache/bind/zones + dynamodb-local: + image: cnadiminti/dynamodb-local:2017-02-16 + env_file: + .env + ports: + - "19000:8000" + command: "--sharedDb --inMemory" + localstack: image: localstack/localstack:0.10.4 ports: - - "19000:19000" - "19006:19006" - "19007:19007" environment: - - SERVICES=dynamodb:19000,sns:19006,sqs:19007 + - SERVICES=sns:19006,sqs:19007 - START_WEB=0 mail: - image: flaviovs/mock-smtp + image: flaviovs/mock-smtp:0.0.2 ports: - "19025:25" volumes: - ./email:/var/lib/mock-smtp ldap: - image: rroemhild/test-openldap + image: rroemhild/test-openldap:latest ports: - "19008:389" diff --git a/docker/functest/run.sh b/docker/functest/run.sh index 8790d1929..bbf8b75fc 100644 --- a/docker/functest/run.sh +++ b/docker/functest/run.sh @@ -47,25 +47,32 @@ cd /app find . -name "*.pyc" -delete find . -name "__pycache__" -delete +result=0 # If PROD_ENV is not true, we are in a local docker environment so do not skip anything if [ "${PROD_ENV}" = "true" ]; then # -m plays havoc with -k, using variables is a headache, so doing this by hand # run parallel tests first (not serial) echo "./run-tests.py live_tests -n2 -v -m \"not skip_production and not serial and not multi_record_enabled\" -v --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=False" ./run-tests.py live_tests -n2 -v -m "not skip_production and not serial and not multi_record_enabled" --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=False - if [ $? -eq 0 ]; then + result=$? + if [ $result -eq 0 ]; then # run serial tests second (serial marker) echo "./run-tests.py live_tests -n0 -v -m \"not skip_production and serial and not multi_record_enabled\" -v --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=True" ./run-tests.py live_tests -n0 -v -m "not skip_production and serial and not multi_record_enabled" --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=True + result=$? fi else # run parallel tests first (not serial) echo "./run-tests.py live_tests -n2 -v -m \"not serial and not multi_record_disabled\" --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=False" ./run-tests.py live_tests -n2 -v -m "not serial and not multi_record_disabled" --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=False - - if [ $? -eq 0 ]; then + result=$? + if [ $result -eq 0 ]; then # run serial tests second (serial marker) echo "./run-tests.py live_tests -n0 -v -m \"serial and not multi_record_disabled\" --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=True" ./run-tests.py live_tests -n0 -v -m "serial and not multi_record_disabled" --url=${VINYLDNS_URL} --dns-ip=${DNS_IP} ${TEST_PATTERN} --teardown=True + result=$? fi fi + +exit $result + diff --git a/modules/dynamodb/docker/docker-compose.yml b/modules/dynamodb/docker/docker-compose.yml deleted file mode 100644 index 5d418edd3..000000000 --- a/modules/dynamodb/docker/docker-compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -version: "3.0" -services: - localstack: - image: localstack/localstack:0.10.4 - ports: - - "19003:19003" - environment: - - SERVICES=dynamodb:19003 - - START_WEB=0 diff --git a/modules/dynamodb/src/it/resources/application.conf b/modules/dynamodb/src/it/resources/application.conf index 566315c70..a5e441ade 100644 --- a/modules/dynamodb/src/it/resources/application.conf +++ b/modules/dynamodb/src/it/resources/application.conf @@ -6,35 +6,35 @@ dynamodb { settings { key = "vinyldnsTest" secret = "notNeededForDynamoDbLocal" - endpoint = "http://127.0.0.1:19003" + endpoint = "http://127.0.0.1:19000" region = "us-east-1" } repositories { record-change { table-name = "recordchange-startup-test" - provisioned-reads = 40 - provisioned-writes = 30 + provisioned-reads = 100 + provisioned-writes = 100 } zone-change { table-name = "zonechange-startup-test" - provisioned-reads = 40 - provisioned-writes = 30 + provisioned-reads = 100 + provisioned-writes = 100 } group { table-name = "groups-startup-test" - provisioned-reads = 40 - provisioned-writes = 30 + provisioned-reads = 100 + provisioned-writes = 100 } group-change { table-name = "groupchanges-startup-test" - provisioned-reads = 40 - provisioned-writes = 30 + provisioned-reads = 100 + provisioned-writes = 100 } membership { table-name = "memberships-startup-test" - provisioned-reads = 40 - provisioned-writes = 30 + provisioned-reads = 100 + provisioned-writes = 100 } } } diff --git a/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBIntegrationSpec.scala b/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBIntegrationSpec.scala index 7b7008323..9e853860f 100644 --- a/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBIntegrationSpec.scala +++ b/modules/dynamodb/src/it/scala/vinyldns/dynamodb/repository/DynamoDBIntegrationSpec.scala @@ -28,7 +28,7 @@ trait DynamoDBIntegrationSpec with Inspectors { // port is defined in the docker/docker-compose.yml file for dynamodb - val dynamoIntegrationConfig: DynamoDBDataStoreSettings = getDynamoConfig(19003) + val dynamoIntegrationConfig: DynamoDBDataStoreSettings = getDynamoConfig(19000) val logger: Logger = LoggerFactory.getLogger("DynamoDBIntegrationSpec") // only used for teardown diff --git a/modules/mysql/docker/conf/config-file.cnf b/modules/mysql/docker/conf/config-file.cnf deleted file mode 100644 index 9f7395c2e..000000000 --- a/modules/mysql/docker/conf/config-file.cnf +++ /dev/null @@ -1,3 +0,0 @@ -[mysqld] -# Turn this on to record queries -general_log = 1 diff --git a/modules/mysql/docker/docker-compose.yml b/modules/mysql/docker/docker-compose.yml deleted file mode 100644 index 14a5a57d2..000000000 --- a/modules/mysql/docker/docker-compose.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: "3.0" -services: - mysql-module: - image: "mysql:5.7" - environment: - - MYSQL_ROOT_PASSWORD=pass # do not use quotes around the environment variables!!! - - MYSQL_ROOT_HOST=% # this is required as mysql is currently locked down to localhost - ports: - - "19004:3306" - volumes: - - ./conf:/etc/mysql/conf.d diff --git a/modules/mysql/src/it/resources/application.conf b/modules/mysql/src/it/resources/application.conf index d25fef09b..f99941f85 100644 --- a/modules/mysql/src/it/resources/application.conf +++ b/modules/mysql/src/it/resources/application.conf @@ -5,10 +5,10 @@ mysql { # JDBC Settings, these are all values in scalikejdbc-config, not our own # these must be overridden to use MYSQL for production use # assumes a docker or mysql instance running locally - name = "vinyldns" + name = "vinyldns2" driver = "org.mariadb.jdbc.Driver" - migration-url = "jdbc:mariadb://localhost:19004/" - url = "jdbc:mariadb://localhost:19004/vinyldns" + migration-url = "jdbc:mariadb://localhost:19002/" + url = "jdbc:mariadb://localhost:19002/vinyldns2" user = "root" password = "pass" @@ -40,10 +40,10 @@ queue { messages-per-poll = 10 settings = { - name = "vinyldns" + name = "vinyldns2" driver = "org.mariadb.jdbc.Driver" - migration-url = "jdbc:mariadb://localhost:19004/?user=root&password=pass" - url = "jdbc:mariadb://localhost:19004/vinyldns?user=root&password=pass" + migration-url = "jdbc:mariadb://localhost:19002/?user=root&password=pass" + url = "jdbc:mariadb://localhost:19002/vinyldns2?user=root&password=pass" user = "root" password = "pass" diff --git a/modules/portal/test/controllers/VinylDNSSpec.scala b/modules/portal/test/controllers/VinylDNSSpec.scala index 128e0b471..587c0b4e2 100644 --- a/modules/portal/test/controllers/VinylDNSSpec.scala +++ b/modules/portal/test/controllers/VinylDNSSpec.scala @@ -19,6 +19,7 @@ package controllers import actions.{LegacySecuritySupport, SecuritySupport} import cats.effect.IO import controllers.VinylDNS.Alert +import mockws.MockWS import org.junit.runner._ import org.specs2.mock.Mockito import org.specs2.mutable._ @@ -29,8 +30,7 @@ import play.api.libs.ws.WSClient import play.api.mvc._ import play.api.test.Helpers._ import play.api.test._ -import play.api.{Configuration, Environment, Mode} -import play.core.server.{Server, ServerConfig} +import play.api.{Configuration, Environment} import vinyldns.core.crypto.{CryptoAlgebra, NoOpCrypto} import vinyldns.core.domain.membership._ @@ -38,13 +38,6 @@ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future /* these verbs are renamed to avoid collisions with the verb identifiers in the standard values library file */ -import play.api.routing.sird.{ - DELETE => backendDELETE, - GET => backendGET, - POST => backendPOST, - PUT => backendPUT, - _ -} @RunWith(classOf[JUnitRunner]) class VinylDNSSpec extends Specification with Mockito with TestApplicationData with BeforeEach { @@ -572,824 +565,630 @@ class VinylDNSSpec extends Specification with Mockito with TestApplicationData w } ".newGroup" should { + tag("slow") "return the group description on create - status ok (200)" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/groups") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.newGroup()( - FakeRequest(POST, "/groups") - .withJsonBody(hobbitGroupRequest) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(OK) - hasCacheHeaders(result) - contentAsJson(result) must beEqualTo(hobbitGroup) - } + val client = MockWS { + case (POST, "http://localhost:9001/groups") => + defaultActionBuilder { Results.Ok(hobbitGroup) } } + + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.newGroup()( + FakeRequest(POST, "/groups") + .withJsonBody(hobbitGroupRequest) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(OK) + hasCacheHeaders(result) + contentAsJson(result) must beEqualTo(hobbitGroup) } "return bad request (400) if the request is not properly made" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/groups") => - defaultActionBuilder { - Results.BadRequest("user id not found") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.newGroup()( - FakeRequest(POST, "/groups") - .withJsonBody(invalidHobbitGroup) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(BAD_REQUEST) - hasCacheHeaders(result) - } + val client = MockWS { + case (POST, "http://localhost:9001/groups") => + defaultActionBuilder { Results.BadRequest("user id not found") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.newGroup()( + FakeRequest(POST, "/groups") + .withJsonBody(invalidHobbitGroup) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(BAD_REQUEST) + hasCacheHeaders(result) } "return authentication failed (401) when auth fails in the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/groups") => - defaultActionBuilder { - Results.Unauthorized("Invalid credentials") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.newGroup()( - FakeRequest(POST, s"/groups") - .withJsonBody(hobbitGroupRequest) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(UNAUTHORIZED) - hasCacheHeaders(result) - } + val client = MockWS { + case (POST, "http://localhost:9001/groups") => + defaultActionBuilder { Results.Unauthorized("Invalid credentials") } } + + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.newGroup()( + FakeRequest(POST, s"/groups") + .withJsonBody(hobbitGroupRequest) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(UNAUTHORIZED) + hasCacheHeaders(result) } "return conflict (409) when the group exists already" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/groups") => - defaultActionBuilder { - Results.Conflict("A group named 'hobbits' already exists") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.newGroup()( - FakeRequest(POST, "/groups") - .withJsonBody(hobbitGroupRequest) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(CONFLICT) - hasCacheHeaders(result) - } + val client = MockWS { + case (POST, "http://localhost:9001/groups") => + defaultActionBuilder { Results.Conflict("A group named 'hobbits' already exists") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.newGroup()( + FakeRequest(POST, "/groups") + .withJsonBody(hobbitGroupRequest) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(CONFLICT) + hasCacheHeaders(result) } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/groups") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.newGroup()( - FakeRequest(POST, "/groups") - .withJsonBody(hobbitGroupRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.newGroup()( + FakeRequest(POST, "/groups") + .withJsonBody(hobbitGroupRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 403 + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") + hasCacheHeaders(result) } "return unauthorized (401) if user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/groups") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.newGroup()(FakeRequest(POST, "/groups") - .withJsonBody(hobbitGroupRequest)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.newGroup()( + FakeRequest(POST, "/groups") + .withJsonBody(hobbitGroupRequest)) - status(result) must beEqualTo(401) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) must beEqualTo(401) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } } ".getGroup" should { + tag("slow") "return the group description if it is found - status ok (200)" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/${hobbitGroupId}") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.getGroup(hobbitGroupId)(FakeRequest(GET, s"/groups/$hobbitGroupId") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(OK) - hasCacheHeaders(result) - contentAsJson(result) must beEqualTo(hobbitGroup) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups/${hobbitGroupId}" => + defaultActionBuilder { Results.Ok(hobbitGroup) } } + + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.getGroup(hobbitGroupId)( + FakeRequest(GET, s"/groups/$hobbitGroupId") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(OK) + hasCacheHeaders(result) + contentAsJson(result) must beEqualTo(hobbitGroup) } "return authentication failed (401) when auth fails in the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.Unauthorized("Invalid credentials") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.getGroup(hobbitGroupId)(FakeRequest(GET, s"/groups/$hobbitGroupId") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(UNAUTHORIZED) - hasCacheHeaders(result) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups/${hobbitGroupId}" => + defaultActionBuilder { Results.Unauthorized("Invalid credentials") } } - } - "return a not found (404) if the group does not exist" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/not-hobbits") => - defaultActionBuilder { - Results.NotFound - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.getGroup("not-hobbits")(FakeRequest(GET, "/groups/not-hobbits") + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.getGroup(hobbitGroupId)( + FakeRequest(GET, s"/groups/$hobbitGroupId") .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - status(result) must beEqualTo(NOT_FOUND) - hasCacheHeaders(result) - } + status(result) must beEqualTo(UNAUTHORIZED) + hasCacheHeaders(result) + } + "return a not found (404) if the group does not exist" in new WithApplication(app) { + val client = MockWS { + case (GET, u) if u == "http://localhost:9001/groups/not-hobbits" => + defaultActionBuilder { Results.NotFound } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.getGroup("not-hobbits")( + FakeRequest(GET, "/groups/not-hobbits") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(NOT_FOUND) + hasCacheHeaders(result) } "return status forbidden (403) if the user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/${hobbitGroupId}") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = - underTest.getGroup(hobbitGroupId)( - FakeRequest(GET, s"/groups/$hobbitGroupId") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = + underTest.getGroup(hobbitGroupId)( + FakeRequest(GET, s"/groups/$hobbitGroupId") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } "return unauthorized (401) if user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/${hobbitGroupId}") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.getGroup(hobbitGroupId)(FakeRequest(GET, s"/groups/$hobbitGroupId")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.getGroup(hobbitGroupId)(FakeRequest(GET, s"/groups/$hobbitGroupId")) - status(result) must beEqualTo(401) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) must beEqualTo(401) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } } ".deleteGroup" should { "return ok with no content (204) when delete is successful" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.NoContent - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.deleteGroup(hobbitGroupId)(FakeRequest(DELETE, s"/groups/$hobbitGroupId") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(NO_CONTENT) - hasCacheHeaders(result) - } + val client = MockWS { + case (DELETE, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.NoContent } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.deleteGroup(hobbitGroupId)( + FakeRequest(DELETE, s"/groups/$hobbitGroupId") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(NO_CONTENT) + hasCacheHeaders(result) } "return unauthorized (401) when user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.NoContent - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.deleteGroup(hobbitGroupId)(FakeRequest(DELETE, s"/groups/$hobbitGroupId")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.deleteGroup(hobbitGroupId)(FakeRequest(DELETE, s"/groups/$hobbitGroupId")) - status(result) mustEqual 401 - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 401 + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } "return forbidden (403) when user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.NoContent - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = - underTest.deleteGroup(hobbitGroupId)( - FakeRequest(DELETE, s"/groups/$hobbitGroupId") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = + underTest.deleteGroup(hobbitGroupId)( + FakeRequest(DELETE, s"/groups/$hobbitGroupId") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } "return authentication failed (401) when authentication fails in the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.Unauthorized("Invalid credentials") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.deleteGroup(hobbitGroupId)(FakeRequest(DELETE, s"/groups/$hobbitGroupId") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(UNAUTHORIZED) - hasCacheHeaders(result) - } + val client = MockWS { + case (DELETE, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.Unauthorized("Invalid credentials") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.deleteGroup(hobbitGroupId)( + FakeRequest(DELETE, s"/groups/$hobbitGroupId") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(UNAUTHORIZED) + hasCacheHeaders(result) } "return forbidden (403) when authorization fails in the backend" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/groups/$hobbitGroupId") => + val client = MockWS { + case (DELETE, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => defaultActionBuilder { Results.Forbidden("You do not have access to delete this group") } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - - val underTest = withClient(client) - val result = - underTest.deleteGroup(hobbitGroupId)(FakeRequest(DELETE, s"/groups/$hobbitGroupId") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(FORBIDDEN) - hasCacheHeaders(result) - } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + + val underTest = withClient(client) + val result = + underTest.deleteGroup(hobbitGroupId)( + FakeRequest(DELETE, s"/groups/$hobbitGroupId") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(FORBIDDEN) + hasCacheHeaders(result) } "return a not found (404) if the group does not exist" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/groups/not-hobbits") => - defaultActionBuilder { - Results.NotFound - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.deleteGroup("not-hobbits")(FakeRequest(DELETE, "/groups/not-hobbits") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(NOT_FOUND) - hasCacheHeaders(result) - } + val client = MockWS { + case (DELETE, "http://localhost:9001/groups/not-hobbits") => + defaultActionBuilder { Results.NotFound } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.deleteGroup("not-hobbits")( + FakeRequest(DELETE, "/groups/not-hobbits") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(NOT_FOUND) + hasCacheHeaders(result) } } ".updateGroup" should { "return the new group description if it is saved successfully - Ok (200)" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.updateGroup(hobbitGroupId)( - FakeRequest(PUT, s"/groups/$hobbitGroupId") - .withJsonBody(hobbitGroup) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(OK) - contentAsJson(result) must beEqualTo(hobbitGroup) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.Ok(hobbitGroup) } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.updateGroup(hobbitGroupId)( + FakeRequest(PUT, s"/groups/$hobbitGroupId") + .withJsonBody(hobbitGroup) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(OK) + contentAsJson(result) must beEqualTo(hobbitGroup) + hasCacheHeaders(result) } "return unauthorized (401) if the user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.updateGroup(hobbitGroupId)(FakeRequest(PUT, s"/groups/$hobbitGroupId") - .withJsonBody(hobbitGroup)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.updateGroup(hobbitGroupId)( + FakeRequest(PUT, s"/groups/$hobbitGroupId") + .withJsonBody(hobbitGroup)) - status(result) mustEqual 401 - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 401 + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } "return forbidden (403) if the user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.Ok(hobbitGroup) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.updateGroup(hobbitGroupId)( - FakeRequest(PUT, s"/groups/$hobbitGroupId") - .withJsonBody(hobbitGroup) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.updateGroup(hobbitGroupId)( + FakeRequest(PUT, s"/groups/$hobbitGroupId") + .withJsonBody(hobbitGroup) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 403 + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") + hasCacheHeaders(result) } "return bad request (400) when the request is rejected by the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.BadRequest("Unknown user") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.updateGroup(hobbitGroupId)( - FakeRequest(PUT, s"/groups/$hobbitGroupId") - .withJsonBody(invalidHobbitGroup) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(BAD_REQUEST) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.BadRequest("Unknown user") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.updateGroup(hobbitGroupId)( + FakeRequest(PUT, s"/groups/$hobbitGroupId") + .withJsonBody(invalidHobbitGroup) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(BAD_REQUEST) + hasCacheHeaders(result) } "return unauthorized (401) when request fails authentication" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/$hobbitGroupId") => - defaultActionBuilder { - Results.Unauthorized("Authentication failed, bad signature") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.updateGroup(hobbitGroupId)( - FakeRequest(PUT, s"/groups/$hobbitGroupId") - .withJsonBody(hobbitGroup) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(UNAUTHORIZED) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.Unauthorized("Authentication failed, bad signature") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.updateGroup(hobbitGroupId)( + FakeRequest(PUT, s"/groups/$hobbitGroupId") + .withJsonBody(hobbitGroup) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(UNAUTHORIZED) + hasCacheHeaders(result) } "return forbidden (403) when request fails permissions in the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/${hobbitGroupId}") => - defaultActionBuilder { - Results.Forbidden("Authentication failed, bad signature") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.updateGroup(hobbitGroupId)( - FakeRequest(PUT, s"/groups/$hobbitGroupId") - .withJsonBody(hobbitGroup) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(FORBIDDEN) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.Forbidden("Authentication failed, bad signature") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.updateGroup(hobbitGroupId)( + FakeRequest(PUT, s"/groups/$hobbitGroupId") + .withJsonBody(hobbitGroup) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(FORBIDDEN) + hasCacheHeaders(result) } "return not found (404) when the group is not found in the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/groups/not-hobbits") => - defaultActionBuilder { - Results.NotFound - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.updateGroup("not-hobbits")( - FakeRequest(PUT, "/groups/not-hobbits") - .withJsonBody(hobbitGroup) - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(NOT_FOUND) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/groups/$hobbitGroupId" => + defaultActionBuilder { Results.NotFound } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.updateGroup("not-hobbits")( + FakeRequest(PUT, "/groups/not-hobbits") + .withJsonBody(hobbitGroup) + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(NOT_FOUND) + hasCacheHeaders(result) } } ".getMemberList" should { "return a list of members of the group when requested - Ok (200)" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId/members") => - defaultActionBuilder { - Results.Ok(hobbitGroupMembers) - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.getMemberList(hobbitGroupId)( - FakeRequest(GET, s"/data/groups/$hobbitGroupId/members") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(OK) - hasCacheHeaders(result) - contentAsJson(result) must beEqualTo(hobbitGroupMembers) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups/$hobbitGroupId/members" => + defaultActionBuilder { Results.Ok(hobbitGroupMembers) } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.getMemberList(hobbitGroupId)( + FakeRequest(GET, s"/data/groups/$hobbitGroupId/members") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(OK) + hasCacheHeaders(result) + contentAsJson(result) must beEqualTo(hobbitGroupMembers) } "return unauthorized (401) if the user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId/members") => - defaultActionBuilder { - Results.Ok(hobbitGroupMembers) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.getMemberList(hobbitGroupId)( - FakeRequest(GET, s"/data/groups/$hobbitGroupId/members")) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.getMemberList(hobbitGroupId)( + FakeRequest(GET, s"/data/groups/$hobbitGroupId/members")) - status(result) mustEqual 401 - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 401 + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } "return forbidden (403) if the user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId/members") => - defaultActionBuilder { - Results.Ok(hobbitGroupMembers) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.getMemberList(hobbitGroupId)( - FakeRequest(GET, s"/data/groups/$hobbitGroupId/members") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.getMemberList(hobbitGroupId)( + FakeRequest(GET, s"/data/groups/$hobbitGroupId/members") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) must beEqualTo(FORBIDDEN) - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) must beEqualTo(FORBIDDEN) + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } "return bad request (400) when the request is rejected by the back end" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId/members") => - defaultActionBuilder { - Results.BadRequest("Invalid maxItems") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.getMemberList(hobbitGroupId)( - FakeRequest(GET, s"/groups/$hobbitGroupId/members?maxItems=0") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(BAD_REQUEST) - hasCacheHeaders(result) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups/$hobbitGroupId/members" => + defaultActionBuilder { Results.BadRequest("Invalid maxItems") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.getMemberList(hobbitGroupId)( + FakeRequest(GET, s"/groups/$hobbitGroupId/members?maxItems=0") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(BAD_REQUEST) + hasCacheHeaders(result) } "return unauthorized (401) when request fails authentication" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId/members") => - defaultActionBuilder { - Results.Unauthorized("The supplied authentication is invalid") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.getMemberList(hobbitGroupId)( - FakeRequest(GET, s"/groups/$hobbitGroupId/members") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(UNAUTHORIZED) - hasCacheHeaders(result) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups/$hobbitGroupId/members" => + defaultActionBuilder { Results.Unauthorized("The supplied authentication is invalid") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.getMemberList(hobbitGroupId)( + FakeRequest(GET, s"/groups/$hobbitGroupId/members") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(UNAUTHORIZED) + hasCacheHeaders(result) } "return not found (404) when the group is not found in the backend" in new WithApplication( app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups/$hobbitGroupId/members") => - defaultActionBuilder { - Results.NotFound - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.getMemberList(hobbitGroupId)( - FakeRequest(GET, s"/groups/$hobbitGroupId/members") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(NOT_FOUND) - hasCacheHeaders(result) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups/$hobbitGroupId/members" => + defaultActionBuilder { Results.NotFound } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.getMemberList(hobbitGroupId)( + FakeRequest(GET, s"/groups/$hobbitGroupId/members") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(NOT_FOUND) + hasCacheHeaders(result) } } ".myGroups" should { "return the list of groups when requested - Ok(200)" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups") => - defaultActionBuilder { - Results.Ok(frodoGroupList) - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = underTest.getGroups()(FakeRequest(GET, s"/api/groups") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(OK) - hasCacheHeaders(result) - contentAsJson(result) must beEqualTo(frodoGroupList) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups" => + defaultActionBuilder { Results.Ok(frodoGroupList) } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = underTest.getGroups()( + FakeRequest(GET, s"/api/groups") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(OK) + hasCacheHeaders(result) + contentAsJson(result) must beEqualTo(frodoGroupList) } "return unauthorized (401) when user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups") => - defaultActionBuilder { - Results.Ok(frodoGroupList) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = underTest.getGroups()(FakeRequest(GET, s"/api/groups")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = underTest.getGroups()(FakeRequest(GET, s"/api/groups")) - status(result) mustEqual 401 - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 401 + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } "return forbidden (403) when user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups") => - defaultActionBuilder { - Results.Ok(frodoGroupList) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockLockedUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.getGroups()( - FakeRequest(GET, s"/api/groups") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockLockedUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.getGroups()( + FakeRequest(GET, s"/api/groups") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } "return unauthorized (401) when request fails authentication" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/groups") => - defaultActionBuilder { - Results.Unauthorized("The supplied authentication is invalid") - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockUserAccessor, - client, - components, - crypto) - val result = underTest.getGroups()(FakeRequest(GET, s"/api/groups") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(UNAUTHORIZED) - hasCacheHeaders(result) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/groups" => + defaultActionBuilder { Results.Unauthorized("The supplied authentication is invalid") } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockUserAccessor, + client, + components, + crypto) + val result = underTest.getGroups()( + FakeRequest(GET, s"/api/groups") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(UNAUTHORIZED) + hasCacheHeaders(result) } } @@ -1564,1047 +1363,674 @@ class VinylDNSSpec extends Specification with Mockito with TestApplicationData w ".getZones" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = underTest.getZones()(FakeRequest(GET, s"/api/zones")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = underTest.getZones()(FakeRequest(GET, s"/api/zones")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.getZones()( - FakeRequest(GET, s"/api/zones").withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.getZones()( + FakeRequest(GET, s"/api/zones").withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".getZone" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.getZone(hobbitZoneId)(FakeRequest(GET, s"/api/zones/$hobbitZoneId")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.getZone(hobbitZoneId)(FakeRequest(GET, s"/api/zones/$hobbitZoneId")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.getZone(hobbitZoneId)( - FakeRequest(GET, s"/api/zones/$hobbitZoneId").withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.getZone(hobbitZoneId)( + FakeRequest(GET, s"/api/zones/$hobbitZoneId").withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".getZoneByName" should { "return ok (200) if the zone is found" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/name/$hobbitZoneName") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.getZoneByName(hobbitZoneName)( - FakeRequest(GET, s"/zones/name/$hobbitZoneName") - .withSession( - "username" -> frodoUser.userName, - "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(OK) - hasCacheHeaders(result) - contentAsJson(result) must beEqualTo(hobbitZone) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/zones/name/$hobbitZoneName" => + defaultActionBuilder { Results.Ok(hobbitZone) } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.getZoneByName(hobbitZoneName)( + FakeRequest(GET, s"/zones/name/$hobbitZoneName") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(OK) + hasCacheHeaders(result) + contentAsJson(result) must beEqualTo(hobbitZone) } "return a not found (404) if the zone does not exist" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/name/not-hobbits") => - defaultActionBuilder { - Results.NotFound - } - } { implicit port => - WsTestClient.withClient { client => - val mockUserAccessor = mock[UserAccountAccessor] - mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) - mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) - val underTest = withClient(client) - val result = - underTest.getZoneByName("not-hobbits")(FakeRequest(GET, "/zones/name/not-hobbits") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - - status(result) must beEqualTo(NOT_FOUND) - hasCacheHeaders(result) - } + val client = MockWS { + case (GET, u) if u == s"http://localhost:9001/zones/name/not-hobbits" => + defaultActionBuilder { Results.NotFound } } + val mockUserAccessor = mock[UserAccountAccessor] + mockUserAccessor.get(anyString).returns(IO.pure(Some(frodoUser))) + mockUserAccessor.getUserByKey(anyString).returns(IO.pure(Some(frodoUser))) + val underTest = withClient(client) + val result = + underTest.getZoneByName("not-hobbits")( + FakeRequest(GET, "/zones/name/not-hobbits") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) must beEqualTo(NOT_FOUND) + hasCacheHeaders(result) } "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/name/$hobbitZoneName") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.getZoneByName(hobbitZoneName)( - FakeRequest(GET, s"/api/zones/name/$hobbitZoneName")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.getZoneByName(hobbitZoneName)( + FakeRequest(GET, s"/api/zones/name/$hobbitZoneName")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/name/$hobbitZoneName") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.getZoneByName(hobbitZoneName)( - FakeRequest(GET, s"/api/zones/name/$hobbitZoneName").withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.getZoneByName(hobbitZoneName)( + FakeRequest(GET, s"/api/zones/name/$hobbitZoneName").withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".syncZone" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/zones/$hobbitZoneId/sync") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.syncZone(hobbitZoneId)(FakeRequest(POST, s"/api/zones/$hobbitZoneId/sync")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.syncZone(hobbitZoneId)(FakeRequest(POST, s"/api/zones/$hobbitZoneId/sync")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId/sync") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.syncZone(hobbitZoneId)( - FakeRequest(POST, s"/api/zones/$hobbitZoneId/sync").withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.syncZone(hobbitZoneId)( + FakeRequest(POST, s"/api/zones/$hobbitZoneId/sync").withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".getRecordSets" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId/recordsets") => - defaultActionBuilder { - Results.Ok(hobbitRecordSet) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.getRecordSets(hobbitZoneId)( - FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets")) + val client = mock[WSClient] - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + val underTest = withClient(client) + val result = + underTest.getRecordSets(hobbitZoneId)( + FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets")) + + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId/recordsets") => - defaultActionBuilder { - Results.Ok(hobbitRecordSet) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.getRecordSets(hobbitZoneId)( - FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets").withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.getRecordSets(hobbitZoneId)( + FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets").withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".listRecordSetChanges" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId/recordsetchanges") => - defaultActionBuilder { - Results.Ok(hobbitRecordSet) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.listRecordSetChanges(hobbitZoneId)( - FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.listRecordSetChanges(hobbitZoneId)( + FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/$hobbitZoneId/recordsetchanges") => - defaultActionBuilder { - Results.Ok(hobbitRecordSet) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.listRecordSetChanges(hobbitZoneId)( - FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets").withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.listRecordSetChanges(hobbitZoneId)( + FakeRequest(GET, s"/api/zones/$hobbitZoneId/recordsets").withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".addZone" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/zones") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.addZone()(FakeRequest(POST, s"/api/zones").withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.addZone()(FakeRequest(POST, s"/api/zones").withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/zones") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.addZone()( - FakeRequest(POST, s"/api/zones") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.addZone()( + FakeRequest(POST, s"/api/zones") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".updateZone" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.updateZone(hobbitZoneId)( - FakeRequest(PUT, s"/api/zones/$hobbitZoneId").withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.updateZone(hobbitZoneId)( + FakeRequest(PUT, s"/api/zones/$hobbitZoneId").withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.updateZone(hobbitZoneId)( - FakeRequest(PUT, s"/api/zones/$hobbitZoneId") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.updateZone(hobbitZoneId)( + FakeRequest(PUT, s"/api/zones/$hobbitZoneId") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".addRecordSet" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/zones/$hobbitZoneId/recordsets") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.addRecordSet(hobbitRecordSetId)( - FakeRequest(POST, s"/api/zones/$hobbitZoneId/recordsets") - .withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.addRecordSet(hobbitRecordSetId)( + FakeRequest(POST, s"/api/zones/$hobbitZoneId/recordsets") + .withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.addRecordSet(hobbitRecordSetId)( - FakeRequest(POST, s"/api/zones/$hobbitZoneId/recordsets") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.addRecordSet(hobbitRecordSetId)( + FakeRequest(POST, s"/api/zones/$hobbitZoneId/recordsets") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".deleteZone" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.deleteZone(hobbitZoneId)( - FakeRequest(DELETE, s"/api/zones/$hobbitZoneId").withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.deleteZone(hobbitZoneId)( + FakeRequest(DELETE, s"/api/zones/$hobbitZoneId").withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/zones/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.deleteZone(hobbitZoneId)( - FakeRequest(DELETE, s"/api/zones/$hobbitZoneId") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.deleteZone(hobbitZoneId)( + FakeRequest(DELETE, s"/api/zones/$hobbitZoneId") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".updateRecordSet" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.updateRecordSet(hobbitZoneId, hobbitRecordSetId)( - FakeRequest(PUT, s"/api/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") - .withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.updateRecordSet(hobbitZoneId, hobbitRecordSetId)( + FakeRequest(PUT, s"/api/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") + .withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.updateRecordSet(hobbitZoneId, hobbitRecordSetId)( - FakeRequest(PUT, s"/api/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.updateRecordSet(hobbitZoneId, hobbitRecordSetId)( + FakeRequest(PUT, s"/api/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".deleteRecordSet" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.deleteRecordSet(hobbitZoneId, hobbitRecordSetId)( - FakeRequest(DELETE, s"/api/zones/$hobbitZoneId").withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.deleteRecordSet(hobbitZoneId, hobbitRecordSetId)( + FakeRequest(DELETE, s"/api/zones/$hobbitZoneId").withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendDELETE(p"/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.deleteRecordSet(hobbitZoneId, hobbitRecordSetId)( - FakeRequest(DELETE, s"/api/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.deleteRecordSet(hobbitZoneId, hobbitRecordSetId)( + FakeRequest(DELETE, s"/api/zones/$hobbitZoneId/recordsets/$hobbitRecordSetId") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".getBatchChange" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/batchrecordchanges/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.getBatchChange(hobbitZoneId)( - FakeRequest(GET, s"/api/dnschanges/$hobbitZoneId") - .withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.getBatchChange(hobbitZoneId)( + FakeRequest(GET, s"/api/dnschanges/$hobbitZoneId") + .withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/batchrecordchanges/$hobbitZoneId") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.getBatchChange(hobbitZoneId)( - FakeRequest(GET, s"/api/dnschanges/$hobbitZoneId") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.getBatchChange(hobbitZoneId)( + FakeRequest(GET, s"/api/dnschanges/$hobbitZoneId") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".newBatchChange" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.newBatchChange()( - FakeRequest(POST, s"/api/dnschanges").withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.newBatchChange()( + FakeRequest(POST, s"/api/dnschanges").withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.newBatchChange()( - FakeRequest(POST, s"/api/dnschanges") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.newBatchChange()( + FakeRequest(POST, s"/api/dnschanges") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".cancelBatchChange" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges/123/cancel") => - defaultActionBuilder { - Results.Ok(hobbitBatchChange) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.cancelBatchChange("123")(FakeRequest(POST, s"/api/dnschanges/123/cancel")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.cancelBatchChange("123")(FakeRequest(POST, s"/api/dnschanges/123/cancel")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges/123/cancel") => - defaultActionBuilder { - Results.Ok(hobbitBatchChange) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.cancelBatchChange("123")( - FakeRequest(POST, s"/api/dnschanges/123/cancel") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.cancelBatchChange("123")( + FakeRequest(POST, s"/api/dnschanges/123/cancel") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".approveBatchChange" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges/123/approve") => - defaultActionBuilder { - Results.Ok(hobbitBatchChange) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.approveBatchChange("123")(FakeRequest(POST, s"/api/dnschanges/123/approve")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.approveBatchChange("123")(FakeRequest(POST, s"/api/dnschanges/123/approve")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges/123/approve") => - defaultActionBuilder { - Results.Ok(hobbitBatchChange) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.approveBatchChange("123")( - FakeRequest(POST, s"/api/dnschanges/123/approve") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.approveBatchChange("123")( + FakeRequest(POST, s"/api/dnschanges/123/approve") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".rejectBatchChange" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges/123/reject") => - defaultActionBuilder { - Results.Ok(hobbitBatchChange) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.rejectBatchChange("123")(FakeRequest(POST, s"/api/dnschanges/123/reject")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.rejectBatchChange("123")(FakeRequest(POST, s"/api/dnschanges/123/reject")) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPOST(p"/batchrecordchanges/123/reject") => - defaultActionBuilder { - Results.Ok(hobbitBatchChange) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.rejectBatchChange("123")( - FakeRequest(POST, s"/api/dnschanges/123/reject") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.rejectBatchChange("123")( + FakeRequest(POST, s"/api/dnschanges/123/reject") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".listBatchChanges" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/batchrecordchanges") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.listBatchChanges()( - FakeRequest(GET, s"/api/dnschanges").withJsonBody(hobbitZoneRequest)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.listBatchChanges()( + FakeRequest(GET, s"/api/dnschanges").withJsonBody(hobbitZoneRequest)) - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/batchrecordchanges") => - defaultActionBuilder { - Results.Ok(hobbitZone) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.listBatchChanges()( - FakeRequest(GET, s"/api/dnschanges") - .withJsonBody(hobbitZoneRequest) - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.listBatchChanges()( + FakeRequest(GET, s"/api/dnschanges") + .withJsonBody(hobbitZoneRequest) + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } ".lockUser" should { "return successful if requesting user is a super user" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/users/${frodoUser.id}/lock") => - defaultActionBuilder { - Results.Ok(userJson) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockMultiUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.lockUser(frodoUser.id)( - FakeRequest(PUT, s"/users/${frodoUser.id}/lock") - .withSession( - "username" -> superFrodoUser.userName, - "accessKey" -> superFrodoUser.accessKey)) - - status(result) must beEqualTo(OK) - contentAsJson(result) must beEqualTo(userJson) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/users/${frodoUser.id}/lock" => + defaultActionBuilder { Results.Ok(userJson) } } + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockMultiUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.lockUser(frodoUser.id)( + FakeRequest(PUT, s"/users/${frodoUser.id}/lock") + .withSession( + "username" -> superFrodoUser.userName, + "accessKey" -> superFrodoUser.accessKey)) + + status(result) must beEqualTo(OK) + contentAsJson(result) must beEqualTo(userJson) + hasCacheHeaders(result) } "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/users/${frodoUser.id}/lock") => - defaultActionBuilder { - Results.Ok(userJson) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockMultiUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = - underTest.lockUser(frodoUser.id)(FakeRequest(PUT, s"/users/${frodoUser.id}/lock")) + val client = mock[WSClient] + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockMultiUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = + underTest.lockUser(frodoUser.id)(FakeRequest(PUT, s"/users/${frodoUser.id}/lock")) - status(result) mustEqual 401 - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 401 + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } "return Forbidden if requesting user is not a super user" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/users/${frodoUser.id}/lock") => - defaultActionBuilder { - Results.Ok(userJson) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.lockUser(frodoUser.id)(FakeRequest(PUT, s"/users/${frodoUser.id}/lock") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.lockUser(frodoUser.id)( + FakeRequest(PUT, s"/users/${frodoUser.id}/lock") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) - status(result) must beEqualTo(403) - contentAsString(result) must beEqualTo("Request restricted to super users only.") - hasCacheHeaders(result) - } - } + status(result) must beEqualTo(403) + contentAsString(result) must beEqualTo("Request restricted to super users only.") + hasCacheHeaders(result) } } ".unlockUser" should { "return successful if requesting user is a super user" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/users/${lockedFrodoUser.id}/unlock") => - defaultActionBuilder { - Results.Ok(userJson) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = - TestVinylDNS( - testConfigLdap, - mockLdapAuthenticator, - mockMultiUserAccessor, - client, - components, - crypto, - mockOidcAuth) - val result = underTest.unlockUser(lockedFrodoUser.id)( - FakeRequest(PUT, s"/users/${lockedFrodoUser.id}/unlock") - .withSession( - "username" -> superFrodoUser.userName, - "accessKey" -> superFrodoUser.accessKey)) - - status(result) must beEqualTo(OK) - contentAsJson(result) must beEqualTo(userJson) - hasCacheHeaders(result) - } + val client = MockWS { + case (PUT, u) if u == s"http://localhost:9001/users/${lockedFrodoUser.id}/unlock" => + defaultActionBuilder { Results.Ok(userJson) } } + val underTest = + TestVinylDNS( + testConfigLdap, + mockLdapAuthenticator, + mockMultiUserAccessor, + client, + components, + crypto, + mockOidcAuth) + val result = underTest.unlockUser(lockedFrodoUser.id)( + FakeRequest(PUT, s"/users/${lockedFrodoUser.id}/unlock") + .withSession( + "username" -> superFrodoUser.userName, + "accessKey" -> superFrodoUser.accessKey)) + + status(result) must beEqualTo(OK) + contentAsJson(result) must beEqualTo(userJson) + hasCacheHeaders(result) } "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/users/${frodoUser.id}/unlock") => - defaultActionBuilder { - Results.Ok(userJson) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.lockUser(frodoUser.id)(FakeRequest(PUT, s"/users/${frodoUser.id}/unlock")) + val client = mock[WSClient] + val underTest = withClient(client) + val result = + underTest.lockUser(frodoUser.id)(FakeRequest(PUT, s"/users/${frodoUser.id}/unlock")) - status(result) mustEqual 401 - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - hasCacheHeaders(result) - } - } + status(result) mustEqual 401 + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") + hasCacheHeaders(result) } "return forbidden (403) if requesting user is not a super user" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendPUT(p"/users/${frodoUser.id}/unlock") => - defaultActionBuilder { - Results.Ok(userJson) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.unlockUser(frodoUser.id)(FakeRequest(PUT, s"/users/${frodoUser.id}/unlock") - .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + val client = mock[WSClient] - status(result) mustEqual 403 - contentAsString(result) must beEqualTo("Request restricted to super users only.") - hasCacheHeaders(result) - } - } + val underTest = withClient(client) + val result = + underTest.unlockUser(frodoUser.id)( + FakeRequest(PUT, s"/users/${frodoUser.id}/unlock") + .withSession("username" -> frodoUser.userName, "accessKey" -> frodoUser.accessKey)) + + status(result) mustEqual 403 + contentAsString(result) must beEqualTo("Request restricted to super users only.") + hasCacheHeaders(result) } } "getBackendIds" should { "return unauthorized (401) if requesting user is not logged in" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/backendids") => - defaultActionBuilder { - Results.Ok(Json.parse("['backend-1', 'backend-2']")) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withClient(client) - val result = - underTest.getBackendIds()(FakeRequest(GET, "/zones/backendids")) + val client = mock[WSClient] - status(result) mustEqual 401 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - "You are not logged in. Please login to continue.") - } - } + val underTest = withClient(client) + val result = + underTest.getBackendIds()(FakeRequest(GET, "/zones/backendids")) + + status(result) mustEqual 401 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo("You are not logged in. Please login to continue.") } "return forbidden (403) if user account is locked" in new WithApplication(app) { - Server.withRouter(ServerConfig(port = Some(simulatedBackendPort), mode = Mode.Test)) { - case backendGET(p"/zones/backendids") => - defaultActionBuilder { - Results.Ok(Json.parse("['backend-1', 'backend-2']")) - } - } { implicit port => - WsTestClient.withClient { client => - val underTest = withLockedClient(client) - val result = underTest.getBackendIds()( - FakeRequest(GET, "/zones/backendids") - .withSession( - "username" -> lockedFrodoUser.userName, - "accessKey" -> lockedFrodoUser.accessKey)) + val client = mock[WSClient] + val underTest = withLockedClient(client) + val result = underTest.getBackendIds()( + FakeRequest(GET, "/zones/backendids") + .withSession( + "username" -> lockedFrodoUser.userName, + "accessKey" -> lockedFrodoUser.accessKey)) - status(result) mustEqual 403 - hasCacheHeaders(result) - contentAsString(result) must beEqualTo( - s"User account for `${lockedFrodoUser.userName}` is locked.") - } - } + status(result) mustEqual 403 + hasCacheHeaders(result) + contentAsString(result) must beEqualTo( + s"User account for `${lockedFrodoUser.userName}` is locked.") } } } diff --git a/modules/sqs/docker/custom.conf b/modules/sqs/docker/custom.conf deleted file mode 100644 index 59c6f3376..000000000 --- a/modules/sqs/docker/custom.conf +++ /dev/null @@ -1,21 +0,0 @@ -node-address { - protocol = http - host = "localhost" - port = 19005 - context-path = "" -} - -rest-sqs { - enabled = true - bind-port = 9324 - bind-hostname = "0.0.0.0" - // Possible values: relaxed, strict - sqs-limits = relaxed -} - -queues { - sqs { - defaultVisibilityTimeout = 10 seconds - receiveMessageWait = 0 seconds - } -} diff --git a/modules/sqs/docker/docker-compose.yml b/modules/sqs/docker/docker-compose.yml deleted file mode 100644 index 164528907..000000000 --- a/modules/sqs/docker/docker-compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -version: "3.0" -services: - localstack: - image: localstack/localstack:0.10.4 - ports: - - "19005:19005" - environment: - - SERVICES=sqs:19005 - - START_WEB=0 diff --git a/modules/sqs/src/it/resources/application.conf b/modules/sqs/src/it/resources/application.conf index 0595bb5bf..7396c5b20 100644 --- a/modules/sqs/src/it/resources/application.conf +++ b/modules/sqs/src/it/resources/application.conf @@ -6,7 +6,7 @@ sqs { access-key = "x" secret-key = "x" signing-region = "x" - service-endpoint = "http://localhost:19005/" + service-endpoint = "http://localhost:19007/" queue-name = "sqs-override-name" } } diff --git a/modules/sqs/src/it/scala/vinyldns/sqs/queue/SqsMessageQueueProviderIntegrationSpec.scala b/modules/sqs/src/it/scala/vinyldns/sqs/queue/SqsMessageQueueProviderIntegrationSpec.scala index c14068501..20f4b360e 100644 --- a/modules/sqs/src/it/scala/vinyldns/sqs/queue/SqsMessageQueueProviderIntegrationSpec.scala +++ b/modules/sqs/src/it/scala/vinyldns/sqs/queue/SqsMessageQueueProviderIntegrationSpec.scala @@ -34,7 +34,7 @@ class SqsMessageQueueProviderIntegrationSpec extends WordSpec with Matchers { | settings { | access-key = "x" | signing-region = "x" - | service-endpoint = "http://localhost:19005/" + | service-endpoint = "http://localhost:19007/" | queue-name = "queue-name" | } | """.stripMargin) @@ -56,7 +56,7 @@ class SqsMessageQueueProviderIntegrationSpec extends WordSpec with Matchers { | access-key = "x" | secret-key = "x" | signing-region = "x" - | service-endpoint = "http://localhost:19005/" + | service-endpoint = "http://localhost:19007/" | queue-name = "new-queue" | } | """.stripMargin) @@ -81,7 +81,7 @@ class SqsMessageQueueProviderIntegrationSpec extends WordSpec with Matchers { | access-key = "x" | secret-key = "x" | signing-region = "x" - | service-endpoint = "http://localhost:19005/" + | service-endpoint = "http://localhost:19007/" | queue-name = "bad*queue*name" | } | """.stripMargin) @@ -101,7 +101,7 @@ class SqsMessageQueueProviderIntegrationSpec extends WordSpec with Matchers { | access-key = "x" | secret-key = "x" | signing-region = "x" - | service-endpoint = "http://localhost:19005/" + | service-endpoint = "http://localhost:19007/" | queue-name = "queue.fifo" | } | """.stripMargin) @@ -123,7 +123,7 @@ class SqsMessageQueueProviderIntegrationSpec extends WordSpec with Matchers { | access-key = "x" | secret-key = "x" | signing-region = "x" - | service-endpoint = "http://localhost:19005/" + | service-endpoint = "http://localhost:19007/" | queue-name = "new-queue" | } | """.stripMargin) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 2c9f65459..d1a84580f 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -117,6 +117,7 @@ object Dependencies { "io.netty" % "netty-transport-native-unix-common" % "4.1.37.Final" % "test", "com.nimbusds" % "oauth2-oidc-sdk" % "6.5", "com.nimbusds" % "nimbus-jose-jwt" % "7.0", - "co.fs2" %% "fs2-core" % fs2V + "co.fs2" %% "fs2-core" % fs2V, + "de.leanovate.play-mockws" %% "play-mockws" % "2.7.1" % "test" ) }