From 299cb126dc664794f8fab314aaddf91ca56e383a Mon Sep 17 00:00:00 2001 From: Luciano Righetti Date: Mon, 17 Jan 2022 16:02:15 +0100 Subject: [PATCH] add: wiremock tests and boilerplate, update test readme, extend openapi spec --- .gitignore | 1 + composer.json | 11 ++- phpunit.xml.dist | 17 +++-- tests/Fixture/BroodsFixture.php | 17 +++++ tests/Helper/ApiTestTrait.php | 8 ++- tests/Helper/WireMockTestTrait.php | 49 +++++++++++++ tests/Helper/wiremock/start.sh | 38 ++++++++++ tests/Helper/wiremock/stop.sh | 23 ++++++ tests/README.md | 70 +++++++++++++++--- tests/TestCase/Api/Broods/AddBroodApiTest.php | 6 -- .../Api/Broods/DeleteBroodsApiTest.php | 6 -- .../TestCase/Api/Broods/EditBroodApiTest.php | 6 -- .../Api/Broods/IndexBroodsApiTest.php | 6 -- .../Api/Broods/TestBroodConnectionApiTest.php | 72 +++++++++++++++++++ .../TestCase/Api/Broods/ViewBroodApiTest.php | 6 -- .../Api/Inbox/CreateInboxEntryApiTest.php | 6 -- .../TestCase/Api/Inbox/IndexInboxApiTest.php | 6 -- .../Api/Individuals/AddIndividualApiTest.php | 6 -- .../Individuals/DeleteIndividualApiTest.php | 6 -- .../Api/Individuals/EditIndividualApiTest.php | 6 -- .../Individuals/IndexIndividualsApiTest.php | 6 -- .../Api/Individuals/ViewIndividualApiTest.php | 6 -- .../Organisations/AddOrganisationApiTest.php | 6 -- .../DeleteOrganisationApiTest.php | 6 -- .../Organisations/EditOrganisationApiTest.php | 6 -- .../IndexOrganisationsApiTest.php | 6 -- .../Organisations/TagOrganisationApiTest.php | 6 -- .../UntagOrganisationApiTest.php | 6 -- .../Organisations/ViewOrganisationApiTest.php | 6 -- .../SharingGroups/AddSharingGroupApiTest.php | 6 -- .../DeleteSharingGroupApiTest.php | 6 -- .../SharingGroups/EditSharingGroupApiTest.php | 6 -- .../IndexSharingGroupsApiTest.php | 6 -- .../SharingGroups/ViewSharingGroupApiTest.php | 6 -- tests/TestCase/Api/Tags/IndexTagsApiTest.php | 6 -- tests/TestCase/Api/Users/AddUserApiTest.php | 6 -- .../TestCase/Api/Users/DeleteUserApiTest.php | 6 -- tests/TestCase/Api/Users/EditUserApiTest.php | 6 -- .../TestCase/Api/Users/IndexUsersApiTest.php | 6 -- tests/TestCase/Api/Users/ViewUserApiTest.php | 6 -- tests/bootstrap.php | 4 +- webroot/docs/openapi.yaml | 54 ++++++++++++++ 42 files changed, 340 insertions(+), 204 deletions(-) create mode 100644 tests/Helper/WireMockTestTrait.php create mode 100644 tests/Helper/wiremock/start.sh create mode 100644 tests/Helper/wiremock/stop.sh create mode 100644 tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php diff --git a/.gitignore b/.gitignore index ba05ac6..ce8fde9 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ webroot/theme/node_modules docker/run/ .phpunit.result.cache config.json +phpunit.xml diff --git a/composer.json b/composer.json index 4e1af85..f51bc6f 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "josegonzalez/dotenv": "^3.2", "league/openapi-psr7-validator": "^0.16.4", "phpunit/phpunit": "^8.5", - "psy/psysh": "@stable" + "psy/psysh": "@stable", + "wiremock-php/wiremock-php": "^2.32" }, "suggest": { "markstory/asset_compress": "An asset compression plugin which provides file concatenation and a flexible filter system for preprocessing and minification.", @@ -53,11 +54,15 @@ "cs-check": "phpcs --colors -p --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "cs-fix": "phpcbf --colors --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/", "stan": "phpstan analyse src/", - "test": "phpunit --colors=always" + "test": [ + "sh ./tests/Helper/wiremock/start.sh", + "phpunit", + "sh ./tests/Helper/wiremock/stop.sh" + ] }, "prefer-stable": true, "config": { "sort-packages": true }, "minimum-stability": "dev" -} +} \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7be6529..403e1e5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,12 @@ - + - - + + + + + + @@ -37,4 +36,4 @@ - + \ No newline at end of file diff --git a/tests/Fixture/BroodsFixture.php b/tests/Fixture/BroodsFixture.php index adc035c..c329e6a 100644 --- a/tests/Fixture/BroodsFixture.php +++ b/tests/Fixture/BroodsFixture.php @@ -16,6 +16,9 @@ class BroodsFixture extends TestFixture public const BROOD_B_ID = 2; public const BROOD_B_API_KEY = 'ae4f281df5a5d0ff3cad6371f76d5c29b6d953ec'; + public const BROOD_WIREMOCK_ID = 3; + public const BROOD_WIREMOCK_API_KEY = 'bfc63c07f74fa18b52d3cced97072cad00e51346'; + public function init(): void { $faker = \Faker\Factory::create(); @@ -48,6 +51,20 @@ class BroodsFixture extends TestFixture 'authkey' => self::BROOD_B_API_KEY, 'created' => $faker->dateTime()->getTimestamp(), 'modified' => $faker->dateTime()->getTimestamp() + ], + [ + 'id' => self::BROOD_WIREMOCK_ID, + 'uuid' => $faker->uuid(), + 'name' => 'wiremock', + 'url' => 'http://localhost:8080', + 'description' => $faker->text, + 'organisation_id' => OrganisationsFixture::ORGANISATION_B_ID, + 'trusted' => true, + 'pull' => true, + 'skip_proxy' => true, + 'authkey' => self::BROOD_WIREMOCK_API_KEY, + 'created' => $faker->dateTime()->getTimestamp(), + 'modified' => $faker->dateTime()->getTimestamp() ] ]; parent::init(); diff --git a/tests/Helper/ApiTestTrait.php b/tests/Helper/ApiTestTrait.php index 21b0c8b..8e20a36 100644 --- a/tests/Helper/ApiTestTrait.php +++ b/tests/Helper/ApiTestTrait.php @@ -24,6 +24,12 @@ trait ApiTestTrait /** @var ResponseValidator */ private $responseValidator; + public function setUp(): void + { + parent::setUp(); + $this->initializeOpenApiValidator($_ENV['OPENAPI_SPEC'] ?? APP . '../webroot/docs/openapi.yaml'); + } + public function setAuthToken(string $authToken): void { $this->_authToken = $authToken; @@ -51,7 +57,7 @@ trait ApiTestTrait * @param string $specFile * @return void */ - public function initializeValidator(string $specFile): void + public function initializeOpenApiValidator(string $specFile): void { $this->validator = (new ValidatorBuilder)->fromYamlFile($specFile); $this->requestValidator = $this->validator->getRequestValidator(); diff --git a/tests/Helper/WireMockTestTrait.php b/tests/Helper/WireMockTestTrait.php new file mode 100644 index 0000000..9b42f7e --- /dev/null +++ b/tests/Helper/WireMockTestTrait.php @@ -0,0 +1,49 @@ + */ + private $config = [ + 'hostname' => 'localhost', + 'port' => 8080 + ]; + + public function initializeWireMock(): void + { + $this->wiremock = WireMock::create( + $_ENV['WIREMOCK_HOST'] ?? $this->config['hostname'], + $_ENV['WIREMOCK_PORT'] ?? $this->config['port'] + ); + + if (!$this->wiremock->isAlive()) { + throw new Exception('Failed to connect to WireMock server.'); + } + + $this->clearWireMockStubs(); + } + + public function clearWireMockStubs(): void + { + $this->wiremock->resetToDefault(); + } + + public function getWireMock(): WireMock + { + return $this->wiremock; + } + + public function getWireMockBaseUrl(): string + { + return sprintf('http://%s:%s', $this->config['hostname'], $this->config['port']); + } +} diff --git a/tests/Helper/wiremock/start.sh b/tests/Helper/wiremock/start.sh new file mode 100644 index 0000000..60b8197 --- /dev/null +++ b/tests/Helper/wiremock/start.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +# Adapted from @rowanhill wiremock start.sh script +# https://github.com/rowanhill/wiremock-php/blob/master/wiremock/start.sh + +cd ./tmp/ + +instance=1 +port=8080 +if [ $# -gt 0 ]; then + instance=$1 + port=$2 +fi +pidFile=wiremock.$instance.pid +logFile=wiremock.$instance.log + +# Ensure WireMock isn't already running +if [ -e $pidFile ]; then + echo WireMock is already started: see process `cat $pidFile` 1>&2 + exit 0 +fi + +# Download the wiremock jar if we need it +if ! [ -e wiremock-standalone.jar ]; then + echo WireMock standalone JAR missing. Downloading. + curl https://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-jre8-standalone/2.32.0/wiremock-jre8-standalone-2.32.0.jar -o wiremock-standalone.jar + status=$? + if [ ${status} -ne 0 ]; then + echo curl could not download WireMock JAR 1>&2 + exit ${status} + fi +fi + +# Start WireMock in standalone mode (in a background process) and save its output to a log +java -jar wiremock-standalone.jar --port $port --root-dir $instance --disable-banner &> $logFile 2>&1 & +pgrep -f wiremock-standalone.jar > $pidFile + +echo WireMock $instance started on port $port \ No newline at end of file diff --git a/tests/Helper/wiremock/stop.sh b/tests/Helper/wiremock/stop.sh new file mode 100644 index 0000000..f9e3f9e --- /dev/null +++ b/tests/Helper/wiremock/stop.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# Adapted from @rowanhill wiremock stop.sh script +# https://github.com/rowanhill/wiremock-php/blob/master/wiremock/stop.sh + +cd ./tmp/ + +instance=1 +if [ $# -gt 0 ]; then + instance=$1 +fi +pidFile=wiremock.$instance.pid + + +if [ -e $pidFile ]; then + kill -9 `cat $pidFile` + rm $pidFile +else + echo WireMock is not started 2>&1 + exit 1 +fi + +echo WireMock $instance stopped \ No newline at end of file diff --git a/tests/README.md b/tests/README.md index 0486b3b..e080442 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,6 +1,5 @@ # Testing -## Configuration -1. Add a `cerebrate_test` database to the db: +1. Add a `cerebrate_test` database to the database: ```mysql CREATE DATABASE cerebrate_test; GRANT ALL PRIVILEGES ON cerebrate_test.* to cerebrate@localhost; @@ -28,12 +27,24 @@ QUIT; ``` ## Runing the tests - ``` $ composer install -$ vendor/bin/phpunit +$ composer test +> sh ./tests/Helper/wiremock/start.sh +WireMock 1 started on port 8080 +> phpunit +[ * ] Running DB migrations, it may take some time ... + +The WireMock server is started ..... +port: 8080 +enable-browser-proxying: false +disable-banner: true +no-request-journal: false +verbose: false + PHPUnit 8.5.22 by Sebastian Bergmann and contributors. + ..... 5 / 5 (100%) Time: 11.61 seconds, Memory: 26.00 MB @@ -51,12 +62,35 @@ Available suites: * `controller`: runs only controller tests * _to be continued ..._ -By default the database is re-generated before running the test suite, to skip this step and speed up the test run use the `-d skip-migrations` option: -``` -$ vendor/bin/phpunit -d skip-migrations +By default the database is re-generated before running the test suite, to skip this step and speed up the test run set the following env variable in `phpunit.xml`: +```xml + + ... + + ``` +## Extras +### WireMock +Some integration tests perform calls to external APIs, we use WireMock to mock the response of these API calls. -## Coverage +To download and run WireMock run the following script in a separate terminal: + ``` + sh ./tests/Helper/wiremock/start.sh + ``` + +You can also run WireMock with docker, check the official docs: http://wiremock.org/docs/docker/ + +> NOTE: When running the tests with `composer test` WireMock is automatically started and stoped after the tests finish. + +The default `hostname` and `port` for WireMock are set in `phpunit.xml` as environment variables: +```xml + + ... + + + +``` +### Coverage HTML: ``` $ vendor/bin/phpunit --coverage-html tmp/coverage @@ -66,3 +100,23 @@ XML: ``` $ vendor/bin/phpunit --verbose --coverage-clover=coverage.xml ``` + +### OpenAPI validation +API tests can assert the API response matches the OpenAPI specification, after the request add this line: + +```php +$this->assertResponseMatchesOpenApiSpec(self::ENDPOINT); +``` + +The default OpenAPI spec path is set in `phpunit.xml` as a environment variablea: +```xml + + ... + + +``` + +## TODO +- [ ] Validate API requests against the OpenAPI spec +- [ ] Validate response content matches / implement _seeResponseContainsJson()_ or equivalent +- [ ] Parse OpenAPI spec only once per test run, currently re-loading in every _TestCase::setUp()_ \ No newline at end of file diff --git a/tests/TestCase/Api/Broods/AddBroodApiTest.php b/tests/TestCase/Api/Broods/AddBroodApiTest.php index 099f3bd..14fb2c5 100644 --- a/tests/TestCase/Api/Broods/AddBroodApiTest.php +++ b/tests/TestCase/Api/Broods/AddBroodApiTest.php @@ -26,12 +26,6 @@ class AddBroodApiTest extends TestCase 'app.Broods' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testAddBrood(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Broods/DeleteBroodsApiTest.php b/tests/TestCase/Api/Broods/DeleteBroodsApiTest.php index 1b8d2ce..94aa0a8 100644 --- a/tests/TestCase/Api/Broods/DeleteBroodsApiTest.php +++ b/tests/TestCase/Api/Broods/DeleteBroodsApiTest.php @@ -26,12 +26,6 @@ class DeleteBroodsApiTest extends TestCase 'app.Broods' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testDeleteBrood(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Broods/EditBroodApiTest.php b/tests/TestCase/Api/Broods/EditBroodApiTest.php index bd15a39..d8fa01f 100644 --- a/tests/TestCase/Api/Broods/EditBroodApiTest.php +++ b/tests/TestCase/Api/Broods/EditBroodApiTest.php @@ -27,12 +27,6 @@ class EditBroodApiTest extends TestCase 'app.Broods' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testEditBrood(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Broods/IndexBroodsApiTest.php b/tests/TestCase/Api/Broods/IndexBroodsApiTest.php index 97d0df8..856b151 100644 --- a/tests/TestCase/Api/Broods/IndexBroodsApiTest.php +++ b/tests/TestCase/Api/Broods/IndexBroodsApiTest.php @@ -26,12 +26,6 @@ class IndexBroodsApiTest extends TestCase 'app.Broods' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexBroods(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php b/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php new file mode 100644 index 0000000..1084131 --- /dev/null +++ b/tests/TestCase/Api/Broods/TestBroodConnectionApiTest.php @@ -0,0 +1,72 @@ +setAuthToken(AuthKeysFixture::ADMIN_API_KEY); + $this->initializeWireMock(); + $this->mockCerebrateStatusResponse(); + + $url = sprintf('%s/%d', self::ENDPOINT, BroodsFixture::BROOD_WIREMOCK_ID); + $this->get($url); + + $this->getWireMock()->verify( + WireMock::getRequestedFor(WireMock::urlEqualTo('/instance/status.json')) + ->withHeader('Content-Type', WireMock::equalTo('application/json')) + ->withHeader('Authorization', WireMock::equalTo(BroodsFixture::BROOD_WIREMOCK_API_KEY)) + ); + + $this->assertResponseOk(); + $this->assertResponseContains('"user": "wiremock"'); + // TODO: $this->assertRequestMatchesOpenApiSpec(); + $this->assertResponseMatchesOpenApiSpec($url); + } + + private function mockCerebrateStatusResponse(): \WireMock\Stubbing\StubMapping + { + return $this->getWireMock()->stubFor( + WireMock::get(WireMock::urlEqualTo('/instance/status.json')) + ->willReturn(WireMock::aResponse() + ->withHeader('Content-Type', 'application/json') + ->withBody((string)json_encode([ + "version" => "0.1", + "application" => "Cerebrate", + "user" => [ + "id" => 1, + "username" => "wiremock", + "role" => [ + "id" => 1 + ] + ] + ]))) + ); + } +} diff --git a/tests/TestCase/Api/Broods/ViewBroodApiTest.php b/tests/TestCase/Api/Broods/ViewBroodApiTest.php index b3f520d..35bb957 100644 --- a/tests/TestCase/Api/Broods/ViewBroodApiTest.php +++ b/tests/TestCase/Api/Broods/ViewBroodApiTest.php @@ -26,12 +26,6 @@ class ViewBroodApiTest extends TestCase 'app.Broods' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testViewBroodGroupById(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Inbox/CreateInboxEntryApiTest.php b/tests/TestCase/Api/Inbox/CreateInboxEntryApiTest.php index 30a44c2..39879f3 100644 --- a/tests/TestCase/Api/Inbox/CreateInboxEntryApiTest.php +++ b/tests/TestCase/Api/Inbox/CreateInboxEntryApiTest.php @@ -25,12 +25,6 @@ class CreateInboxEntryApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testAddUserRegistrationInbox(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Inbox/IndexInboxApiTest.php b/tests/TestCase/Api/Inbox/IndexInboxApiTest.php index 00c2cfd..46f1b92 100644 --- a/tests/TestCase/Api/Inbox/IndexInboxApiTest.php +++ b/tests/TestCase/Api/Inbox/IndexInboxApiTest.php @@ -26,12 +26,6 @@ class IndexInboxApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexInbox(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Individuals/AddIndividualApiTest.php b/tests/TestCase/Api/Individuals/AddIndividualApiTest.php index 64855e8..0320c30 100644 --- a/tests/TestCase/Api/Individuals/AddIndividualApiTest.php +++ b/tests/TestCase/Api/Individuals/AddIndividualApiTest.php @@ -24,12 +24,6 @@ class AddIndividualApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testAddIndividual(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Individuals/DeleteIndividualApiTest.php b/tests/TestCase/Api/Individuals/DeleteIndividualApiTest.php index c493c4b..e50cf63 100644 --- a/tests/TestCase/Api/Individuals/DeleteIndividualApiTest.php +++ b/tests/TestCase/Api/Individuals/DeleteIndividualApiTest.php @@ -25,12 +25,6 @@ class DeleteIndividualApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testDeleteIndividual(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Individuals/EditIndividualApiTest.php b/tests/TestCase/Api/Individuals/EditIndividualApiTest.php index fcef7fd..642482c 100644 --- a/tests/TestCase/Api/Individuals/EditIndividualApiTest.php +++ b/tests/TestCase/Api/Individuals/EditIndividualApiTest.php @@ -25,12 +25,6 @@ class EditIndividualApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testEditIndividualAsAdmin(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Individuals/IndexIndividualsApiTest.php b/tests/TestCase/Api/Individuals/IndexIndividualsApiTest.php index 55f6be1..d8254d1 100644 --- a/tests/TestCase/Api/Individuals/IndexIndividualsApiTest.php +++ b/tests/TestCase/Api/Individuals/IndexIndividualsApiTest.php @@ -25,12 +25,6 @@ class IndexIndividualsApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexIndividuals(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Individuals/ViewIndividualApiTest.php b/tests/TestCase/Api/Individuals/ViewIndividualApiTest.php index 4a0d4a4..5d47610 100644 --- a/tests/TestCase/Api/Individuals/ViewIndividualApiTest.php +++ b/tests/TestCase/Api/Individuals/ViewIndividualApiTest.php @@ -25,12 +25,6 @@ class ViewIndividualApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testViewIndividualById(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/AddOrganisationApiTest.php b/tests/TestCase/Api/Organisations/AddOrganisationApiTest.php index a3f2585..d561292 100644 --- a/tests/TestCase/Api/Organisations/AddOrganisationApiTest.php +++ b/tests/TestCase/Api/Organisations/AddOrganisationApiTest.php @@ -24,12 +24,6 @@ class AddOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testAddOrganisation(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/DeleteOrganisationApiTest.php b/tests/TestCase/Api/Organisations/DeleteOrganisationApiTest.php index e16f57b..6a323fb 100644 --- a/tests/TestCase/Api/Organisations/DeleteOrganisationApiTest.php +++ b/tests/TestCase/Api/Organisations/DeleteOrganisationApiTest.php @@ -25,12 +25,6 @@ class DeleteOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testDeleteOrganisation(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/EditOrganisationApiTest.php b/tests/TestCase/Api/Organisations/EditOrganisationApiTest.php index d9cc7c6..75c032c 100644 --- a/tests/TestCase/Api/Organisations/EditOrganisationApiTest.php +++ b/tests/TestCase/Api/Organisations/EditOrganisationApiTest.php @@ -25,12 +25,6 @@ class EditOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testEditOrganisation(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/IndexOrganisationsApiTest.php b/tests/TestCase/Api/Organisations/IndexOrganisationsApiTest.php index ba7c255..709883a 100644 --- a/tests/TestCase/Api/Organisations/IndexOrganisationsApiTest.php +++ b/tests/TestCase/Api/Organisations/IndexOrganisationsApiTest.php @@ -25,12 +25,6 @@ class IndexOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexOrganisations(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/TagOrganisationApiTest.php b/tests/TestCase/Api/Organisations/TagOrganisationApiTest.php index ed74c74..c79109a 100644 --- a/tests/TestCase/Api/Organisations/TagOrganisationApiTest.php +++ b/tests/TestCase/Api/Organisations/TagOrganisationApiTest.php @@ -28,12 +28,6 @@ class TagOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testTagOrganisation(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/UntagOrganisationApiTest.php b/tests/TestCase/Api/Organisations/UntagOrganisationApiTest.php index c8878d6..1aff58a 100644 --- a/tests/TestCase/Api/Organisations/UntagOrganisationApiTest.php +++ b/tests/TestCase/Api/Organisations/UntagOrganisationApiTest.php @@ -28,12 +28,6 @@ class UntagOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testUntagOrganisation(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Organisations/ViewOrganisationApiTest.php b/tests/TestCase/Api/Organisations/ViewOrganisationApiTest.php index 630fd92..7170e98 100644 --- a/tests/TestCase/Api/Organisations/ViewOrganisationApiTest.php +++ b/tests/TestCase/Api/Organisations/ViewOrganisationApiTest.php @@ -28,12 +28,6 @@ class ViewOrganisationApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testViewOrganisationById(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/SharingGroups/AddSharingGroupApiTest.php b/tests/TestCase/Api/SharingGroups/AddSharingGroupApiTest.php index 45a79aa..0b0c8ab 100644 --- a/tests/TestCase/Api/SharingGroups/AddSharingGroupApiTest.php +++ b/tests/TestCase/Api/SharingGroups/AddSharingGroupApiTest.php @@ -27,12 +27,6 @@ class AddSharingGroupApiTest extends TestCase 'app.SharingGroups' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testAddSharingGroup(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/SharingGroups/DeleteSharingGroupApiTest.php b/tests/TestCase/Api/SharingGroups/DeleteSharingGroupApiTest.php index 8b49f3b..c93bb05 100644 --- a/tests/TestCase/Api/SharingGroups/DeleteSharingGroupApiTest.php +++ b/tests/TestCase/Api/SharingGroups/DeleteSharingGroupApiTest.php @@ -26,12 +26,6 @@ class DeleteSharingGroupApiTest extends TestCase 'app.SharingGroups' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testDeleteSharingGroup(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/SharingGroups/EditSharingGroupApiTest.php b/tests/TestCase/Api/SharingGroups/EditSharingGroupApiTest.php index 0cf6f05..7de711c 100644 --- a/tests/TestCase/Api/SharingGroups/EditSharingGroupApiTest.php +++ b/tests/TestCase/Api/SharingGroups/EditSharingGroupApiTest.php @@ -27,12 +27,6 @@ class EditSharingGroupApiTest extends TestCase 'app.SharingGroups' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testEditSharingGroup(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/SharingGroups/IndexSharingGroupsApiTest.php b/tests/TestCase/Api/SharingGroups/IndexSharingGroupsApiTest.php index 85779aa..82f7255 100644 --- a/tests/TestCase/Api/SharingGroups/IndexSharingGroupsApiTest.php +++ b/tests/TestCase/Api/SharingGroups/IndexSharingGroupsApiTest.php @@ -26,12 +26,6 @@ class IndexSharingGroupsApiTest extends TestCase 'app.SharingGroups' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexSharingGroups(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/SharingGroups/ViewSharingGroupApiTest.php b/tests/TestCase/Api/SharingGroups/ViewSharingGroupApiTest.php index 0e86fc7..3944122 100644 --- a/tests/TestCase/Api/SharingGroups/ViewSharingGroupApiTest.php +++ b/tests/TestCase/Api/SharingGroups/ViewSharingGroupApiTest.php @@ -26,12 +26,6 @@ class ViewSharingGroupApiTest extends TestCase 'app.SharingGroups' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testViewSharingGroupById(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Tags/IndexTagsApiTest.php b/tests/TestCase/Api/Tags/IndexTagsApiTest.php index 137151f..330e93f 100644 --- a/tests/TestCase/Api/Tags/IndexTagsApiTest.php +++ b/tests/TestCase/Api/Tags/IndexTagsApiTest.php @@ -25,12 +25,6 @@ class IndexTagsApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexTags(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Users/AddUserApiTest.php b/tests/TestCase/Api/Users/AddUserApiTest.php index e60cf3a..396307e 100644 --- a/tests/TestCase/Api/Users/AddUserApiTest.php +++ b/tests/TestCase/Api/Users/AddUserApiTest.php @@ -27,12 +27,6 @@ class AddUserApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testAddUser(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Users/DeleteUserApiTest.php b/tests/TestCase/Api/Users/DeleteUserApiTest.php index 69bd87c..50df2e5 100644 --- a/tests/TestCase/Api/Users/DeleteUserApiTest.php +++ b/tests/TestCase/Api/Users/DeleteUserApiTest.php @@ -27,12 +27,6 @@ class DeleteUserApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testDeleteUser(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Users/EditUserApiTest.php b/tests/TestCase/Api/Users/EditUserApiTest.php index a8afa11..8d2810d 100644 --- a/tests/TestCase/Api/Users/EditUserApiTest.php +++ b/tests/TestCase/Api/Users/EditUserApiTest.php @@ -26,12 +26,6 @@ class EditUserApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testEditUser(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Users/IndexUsersApiTest.php b/tests/TestCase/Api/Users/IndexUsersApiTest.php index 403a046..7d96936 100644 --- a/tests/TestCase/Api/Users/IndexUsersApiTest.php +++ b/tests/TestCase/Api/Users/IndexUsersApiTest.php @@ -25,12 +25,6 @@ class IndexUsersApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testIndexUsers(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/TestCase/Api/Users/ViewUserApiTest.php b/tests/TestCase/Api/Users/ViewUserApiTest.php index 99bdafe..d1d2c54 100644 --- a/tests/TestCase/Api/Users/ViewUserApiTest.php +++ b/tests/TestCase/Api/Users/ViewUserApiTest.php @@ -25,12 +25,6 @@ class ViewUserApiTest extends TestCase 'app.AuthKeys' ]; - public function setUp(): void - { - parent::setUp(); - $this->initializeValidator(APP . '../webroot/docs/openapi.yaml'); - } - public function testViewMyUser(): void { $this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c9a8b8c..34939c3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -18,7 +18,6 @@ declare(strict_types=1); use Cake\Core\Configure; use Cake\Datasource\ConnectionManager; -use Cake\TestSuite\Fixture\SchemaLoader; use Migrations\TestSuite\Migrator; /** @@ -54,8 +53,7 @@ ConnectionManager::alias('test_debug_kit', 'debug_kit'); // has been written to. session_id('cli'); -// hacky way to skip migrations -if (!in_array('skip-migrations', $_SERVER['argv'])) { +if (!$_ENV['SKIP_DB_MIGRATIONS']) { echo "[ * ] Running DB migrations, it may take some time ...\n"; $migrator = new Migrator(); $migrator->runMany([ diff --git a/webroot/docs/openapi.yaml b/webroot/docs/openapi.yaml index 81ac9aa..f9418b9 100644 --- a/webroot/docs/openapi.yaml +++ b/webroot/docs/openapi.yaml @@ -613,6 +613,24 @@ paths: default: $ref: "#/components/responses/ApiErrorResponse" + /api/v1/broods/testConnection/{broodId}: + get: + summary: "Test brood connection by ID" + operationId: testBroodConnectionById + tags: + - Broods + parameters: + - $ref: "#/components/parameters/broodId" + responses: + "200": + $ref: "#/components/responses/TestBroodConnectionResponse" + "403": + $ref: "#/components/responses/UnauthorizedApiErrorResponse" + "405": + $ref: "#/components/responses/MethodNotAllowedApiErrorResponse" + default: + $ref: "#/components/responses/ApiErrorResponse" + components: schemas: # General @@ -714,6 +732,15 @@ components: $ref: "#/components/schemas/DateTime" organisation_id: $ref: "#/components/schemas/ID" + organisation: + $ref: "#/components/schemas/Organisation" + individual: + $ref: "#/components/schemas/Individual" + role: + $ref: "#/components/schemas/Role" + # user_settings: TODO + # user_settings_by_name: TODO + # user_settings_by_name_with_fallback: TODO UserList: type: array @@ -1605,6 +1632,33 @@ components: schema: $ref: "#/components/schemas/BroodList" + TestBroodConnectionResponse: + description: "Brood list response" + content: + application/json: + schema: + type: object + properties: + code: + type: integer + description: "HTTP status code" + example: 200 + response: + type: object + properties: + version: + type: string + example: "0.1" + application: + type: string + example: "Cerebrate" + user: + type: string + example: "sync" + ping: + type: number + format: float + # Errors ApiErrorResponse: description: "Unexpected API error"