From 59be4ad2d0719fa3bb0a943fb7bc9a88383a7e37 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Thu, 1 Apr 2021 19:32:10 +0300 Subject: [PATCH] Enhance API tester (#4976) --- CHANGELOG.md | 1 + .../codeception/functional/MailInviteCest.php | 4 +- .../codeception/_support/AcceptanceTester.php | 11 +--- .../tests/codeception/_support/ApiTester.php | 60 +++++++++++++++---- .../tests/codeception/_support/BaseTester.php | 53 ++++++++++++++++ .../codeception/_support/FunctionalTester.php | 24 ++------ 6 files changed, 111 insertions(+), 42 deletions(-) create mode 100644 protected/humhub/tests/codeception/_support/BaseTester.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f3235374ca..257ec91fd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ HumHub Changelog - Fix #4959: Horizontal scrollbar for images - Fix #4898: No streamExcludes option when loading single stream entry - Enh #4960: Added “codeception/module-rest” for testing of REST API modules +- Enh #4976: Added REST API Codeception tester - Enh #4967: Module update broken with expired licence key - Enh #4972: Fix enabling to send notification on remove user from group - Fix #4985: Fix Activity Mail QueryParams on console mode diff --git a/protected/humhub/modules/space/tests/codeception/functional/MailInviteCest.php b/protected/humhub/modules/space/tests/codeception/functional/MailInviteCest.php index 4b5f033a59..e23fa30f89 100644 --- a/protected/humhub/modules/space/tests/codeception/functional/MailInviteCest.php +++ b/protected/humhub/modules/space/tests/codeception/functional/MailInviteCest.php @@ -23,9 +23,9 @@ class MailInviteCest $I->amUser2(); $I->amOnSpace1(); $I->see('Invite'); - $I->amGoingTo('invte a user by mail'); + $I->amGoingTo('invite a user by mail'); - $I->sendAjaxPostRequest(Url::to(['/space/membership/invite', 'cguid' => $I->spaces[0]]), ['InviteForm[inviteExternal]' => 'a@test.de,b@test.de']); + $I->sendAjaxPostRequest(Url::to(['/space/membership/invite', 'cguid' => $I->getFixtureSpace(0)->guid]), ['InviteForm[inviteExternal]' => 'a@test.de,b@test.de']); $I->seeEmailIsSent(2); diff --git a/protected/humhub/tests/codeception/_support/AcceptanceTester.php b/protected/humhub/tests/codeception/_support/AcceptanceTester.php index d0ab153b18..e2eff75ceb 100644 --- a/protected/humhub/tests/codeception/_support/AcceptanceTester.php +++ b/protected/humhub/tests/codeception/_support/AcceptanceTester.php @@ -19,7 +19,7 @@ use \Facebook\WebDriver\WebDriverKeys; * * @SuppressWarnings(PHPMD) */ -class AcceptanceTester extends \Codeception\Actor +class AcceptanceTester extends BaseTester { use _generated\AcceptanceTesterActions; @@ -61,13 +61,6 @@ class AcceptanceTester extends \Codeception\Actor $this->amUser('User3', '123qwe', $logout); } - public $spaces = [ - '5396d499-20d6-4233-800b-c6c86e5fa34a', - '5396d499-20d6-4233-800b-c6c86e5fa34b', - '5396d499-20d6-4233-800b-c6c86e5fa34c', - '5396d499-20d6-4233-800b-c6c86e5fa34d', - ]; - public function amOnSpace1($path = '/space/space', $params = []) { $this->amOnSpace(1, $path, $params); @@ -95,7 +88,7 @@ class AcceptanceTester extends \Codeception\Actor } if(is_int($guid)) { - $guid = $this->spaces[--$guid]; + $guid = $this->getFixtureSpaceGuid(--$guid); } $params['sguid'] = $guid; diff --git a/protected/humhub/tests/codeception/_support/ApiTester.php b/protected/humhub/tests/codeception/_support/ApiTester.php index 35afb20159..f2d0202bc1 100644 --- a/protected/humhub/tests/codeception/_support/ApiTester.php +++ b/protected/humhub/tests/codeception/_support/ApiTester.php @@ -21,7 +21,7 @@ use yii\web\Link; * * @SuppressWarnings(PHPMD) */ -class ApiTester extends \Codeception\Actor +class ApiTester extends BaseTester { use _generated\ApiTesterActions; @@ -50,25 +50,61 @@ class ApiTester extends \Codeception\Actor $this->amHttpAuthenticated($user, $password); } - public function seeSuccessResponseContainsJson($json = []) + public function seeCodeResponseContainsJson($code, $json = []) { - $this->seeResponseCodeIs(HttpCode::OK); + $this->seeResponseCodeIs($code); $this->seeResponseIsJson(); $this->seeResponseContainsJson($json); } + public function seeSuccessResponseContainsJson($json = []) + { + $this->seeCodeResponseContainsJson(HttpCode::OK, $json); + } + public function seeForbiddenResponseContainsJson($json = []) { - $this->seeResponseCodeIs(HttpCode::FORBIDDEN); - $this->seeResponseIsJson(); - $this->seeResponseContainsJson($json); + $this->seeCodeResponseContainsJson(HttpCode::FORBIDDEN, $json); } public function seeBadResponseContainsJson($json = []) { - $this->seeResponseCodeIs(HttpCode::BAD_REQUEST); - $this->seeResponseIsJson(); - $this->seeResponseContainsJson($json); + $this->seeCodeResponseContainsJson(HttpCode::BAD_REQUEST, $json); + } + + public function seeNotFoundResponseContainsJson($json = []) + { + $this->seeCodeResponseContainsJson(HttpCode::NOT_FOUND, $json); + } + + public function seeServerErrorResponseContainsJson($json = []) + { + $this->seeCodeResponseContainsJson(HttpCode::INTERNAL_SERVER_ERROR, $json); + } + + public function seeSuccessMessage($message) + { + $this->seeCodeResponseContainsJson(HttpCode::OK, ['message' => $message]); + } + + public function seeForbiddenMessage($message) + { + $this->seeCodeResponseContainsJson(HttpCode::FORBIDDEN, ['message' => $message]); + } + + public function seeBadMessage($message) + { + $this->seeCodeResponseContainsJson(HttpCode::BAD_REQUEST, ['message' => $message]); + } + + public function seeNotFoundMessage($message) + { + $this->seeCodeResponseContainsJson(HttpCode::NOT_FOUND, ['message' => $message]); + } + + public function seeServerErrorMessage($message) + { + $this->seeCodeResponseContainsJson(HttpCode::INTERNAL_SERVER_ERROR, ['message' => $message]); } /** @@ -134,10 +170,12 @@ class ApiTester extends \Codeception\Actor */ public function seePaginationResponseContainsJson($url, $jsonResults = [], $paginationParams = []) { + $jsonResultsCount = count($jsonResults); + $json = array_merge([ - 'total' => count($jsonResults), + 'total' => $jsonResultsCount, 'page' => 1, - 'pages' => 1, + 'pages' => $jsonResultsCount ? 1 : 0, ], $paginationParams); $json['links'] = $this->getPaginationUrls($url, $json); diff --git a/protected/humhub/tests/codeception/_support/BaseTester.php b/protected/humhub/tests/codeception/_support/BaseTester.php new file mode 100644 index 0000000000..e239e230f0 --- /dev/null +++ b/protected/humhub/tests/codeception/_support/BaseTester.php @@ -0,0 +1,53 @@ +haveFixtures(['space' => SpaceFixture::class]); + return $this->grabFixture('space', $index); + } else { + // Acceptance tests have no the methods above, try to get spaces from DB instead: + if (!isset($this->spaces)) { + $this->spaces = Space::find()->orderBy('id')->all(); + } + return isset($this->spaces[$index]) ? $this->spaces[$index] : null; + } + } + + public function getFixtureSpaceGuid(int $index) : string + { + $space = $this->getFixtureSpace($index); + return ($space instanceof Space ? $space->guid : ''); + } + + public function enableModule($indexOrGuid, $moduleId) + { + if (is_int($indexOrGuid)) { + $space = $this->getFixtureSpace(--$indexOrGuid); + } else { + $space = Space::findOne(['guid' => $indexOrGuid]); + } + + if ($space) { + $space->enableModule($moduleId); + Yii::$app->moduleManager->flushCache(); + } + } +} diff --git a/protected/humhub/tests/codeception/_support/FunctionalTester.php b/protected/humhub/tests/codeception/_support/FunctionalTester.php index 075e53afa1..5f39fd96fd 100644 --- a/protected/humhub/tests/codeception/_support/FunctionalTester.php +++ b/protected/humhub/tests/codeception/_support/FunctionalTester.php @@ -22,7 +22,7 @@ use yii\helpers\Url; * * @SuppressWarnings(PHPMD) */ -class FunctionalTester extends \Codeception\Actor +class FunctionalTester extends BaseTester { use _generated\FunctionalTesterActions; @@ -235,13 +235,6 @@ class FunctionalTester extends \Codeception\Actor $this->amOnSpace(4, $path, $params, $post); } - public $spaces = [ - '5396d499-20d6-4233-800b-c6c86e5fa34a', - '5396d499-20d6-4233-800b-c6c86e5fa34b', - '5396d499-20d6-4233-800b-c6c86e5fa34c', - '5396d499-20d6-4233-800b-c6c86e5fa34d', - ]; - public function amOnSpace($guid, $path = '/space/space', $params = [], $post = false) { if(is_bool($params)) { @@ -254,9 +247,11 @@ class FunctionalTester extends \Codeception\Actor } if(is_int($guid)) { - $guid = $this->spaces[--$guid]; + $guid = $this->getFixtureSpaceGuid(--$guid); } else if($guid instanceof Space) { $guid = $guid->guid; + } else { + $guid = ''; } $params['cguid'] = $guid; @@ -288,15 +283,4 @@ class FunctionalTester extends \Codeception\Actor tests\codeception\_pages\DashboardPage::openBy($this); } - public function enableModule($guid, $moduleId) - { - if(is_int($guid)) { - $guid = $this->spaces[--$guid]; - } - - $space = Space::findOne(['guid' => $guid]); - $space->enableModule($moduleId); - Yii::$app->moduleManager->flushCache(); - } - }