From 5c011129b77699d8e25dff3d5c1d727ee7a4b1f2 Mon Sep 17 00:00:00 2001 From: IanM Date: Thu, 30 Nov 2023 08:07:00 +0000 Subject: [PATCH] chore: enable basic sanity test --- .github/workflows/backend.yml | 2 +- composer.json | 26 ++++++- src/Api/CurrentUserAttributes.php | 4 +- tests/.phpunit.result.cache | 1 + tests/fixtures/.gitkeep | 0 tests/integration/api/ForumSerializerTest.php | 73 +++++++++++++++++++ tests/integration/setup.php | 16 ++++ tests/phpunit.integration.xml | 25 +++++++ tests/phpunit.unit.xml | 27 +++++++ tests/unit/.gitkeep | 0 10 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 tests/.phpunit.result.cache create mode 100644 tests/fixtures/.gitkeep create mode 100644 tests/integration/api/ForumSerializerTest.php create mode 100644 tests/integration/setup.php create mode 100644 tests/phpunit.integration.xml create mode 100644 tests/phpunit.unit.xml create mode 100644 tests/unit/.gitkeep diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index ba14ed4..78c7b9b 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -6,7 +6,7 @@ jobs: run: uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@1.x with: - enable_backend_testing: false + enable_backend_testing: true enable_phpstan: true backend_directory: . diff --git a/composer.json b/composer.json index 94fa284..7404a6f 100644 --- a/composer.json +++ b/composer.json @@ -79,18 +79,36 @@ }, "flarum-cli": { "modules": { - "githubActions": true + "githubActions": true, + "backendTesting": true } } }, "require-dev": { - "flarum/phpstan": "*" + "flarum/phpstan": "*", + "flarum/testing": "^1.0.0" }, "scripts": { "analyse:phpstan": "phpstan analyse", - "clear-cache:phpstan": "phpstan clear-result-cache" + "clear-cache:phpstan": "phpstan clear-result-cache", + "test": [ + "@test:unit", + "@test:integration" + ], + "test:unit": "phpunit -c tests/phpunit.unit.xml", + "test:integration": "phpunit -c tests/phpunit.integration.xml", + "test:setup": "@php tests/integration/setup.php" }, "scripts-descriptions": { - "analyse:phpstan": "Run static analysis" + "analyse:phpstan": "Run static analysis", + "test": "Runs all tests.", + "test:unit": "Runs all unit tests.", + "test:integration": "Runs all integration tests.", + "test:setup": "Sets up a database for use with integration tests. Execute this only once." + }, + "autoload-dev": { + "psr-4": { + "FoF\\OAuth\\Tests\\": "tests/" + } } } diff --git a/src/Api/CurrentUserAttributes.php b/src/Api/CurrentUserAttributes.php index 64c1792..9640abb 100644 --- a/src/Api/CurrentUserAttributes.php +++ b/src/Api/CurrentUserAttributes.php @@ -32,7 +32,9 @@ public function __invoke(CurrentUserSerializer $serializer, User $user, array $a { $session = $serializer->getRequest()->getAttribute('session'); - $attributes['loginProvider'] = $this->cache->get(AbstractOAuthController::SESSION_OAUTH2PROVIDER.'_'.$session->getId()); + if ($session !== null) { + $attributes['loginProvider'] = $this->cache->get(AbstractOAuthController::SESSION_OAUTH2PROVIDER.'_'.$session->getId()); + } return $attributes; } diff --git a/tests/.phpunit.result.cache b/tests/.phpunit.result.cache new file mode 100644 index 0000000..5bc6cc3 --- /dev/null +++ b/tests/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"FoF\\OAuth\\Tests\\integration\\api\\ForumSerializerTest::it_includes_providers_in_forum_attributes_for_guests":3,"FoF\\OAuth\\Tests\\integration\\api\\ForumSerializerTest::it_does_not_include_providers_in_forum_attributes_for_logged_in_users":4,"FoF\\OAuth\\Tests\\integration\\api\\ForumSerializerTest::admin_panel_is_available":3},"times":{"FoF\\OAuth\\Tests\\integration\\api\\ForumSerializerTest::it_includes_providers_in_forum_attributes_for_guests":0.045,"FoF\\OAuth\\Tests\\integration\\api\\ForumSerializerTest::it_does_not_include_providers_in_forum_attributes_for_logged_in_users":0.059,"FoF\\OAuth\\Tests\\integration\\api\\ForumSerializerTest::admin_panel_is_available":0.351}} \ No newline at end of file diff --git a/tests/fixtures/.gitkeep b/tests/fixtures/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/api/ForumSerializerTest.php b/tests/integration/api/ForumSerializerTest.php new file mode 100644 index 0000000..1bede46 --- /dev/null +++ b/tests/integration/api/ForumSerializerTest.php @@ -0,0 +1,73 @@ +extension('fof-oauth'); + + $this->extend( + (new Extend\Csrf)->exemptRoute('login') + ); + } + + /** + * @test + */ + public function it_includes_providers_in_forum_attributes_for_guests() + { + $response = $this->send( + $this->request('GET', '/api') + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $body = json_decode($response->getBody(), true); + + $this->assertArrayHasKey('fof-oauth', $body['data']['attributes']); + } + + /** + * @test + */ + public function it_does_not_include_providers_in_forum_attributes_for_logged_in_users() + { + $response = $this->send( + $this->request('GET', '/api', ['authenticatedAs' => 1]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + + $body = json_decode($response->getBody()->getContents(), true); + + $this->assertArrayNotHasKey('fof-oauth', $body['data']['attributes']); + } + + /** + * @test + */ + public function admin_panel_is_available() + { + $login = $this->send( + $this->request('POST', '/login', [ + 'json' => [ + 'identification' => 'admin', + 'password' => 'password' + ] + ]) + ); + + $this->assertEquals(200, $login->getStatusCode()); + + $response = $this->send( + $this->request('GET', '/admin', ['cookiesFrom' => $login]) + ); + + $this->assertEquals(200, $response->getStatusCode()); + } +} diff --git a/tests/integration/setup.php b/tests/integration/setup.php new file mode 100644 index 0000000..67039c0 --- /dev/null +++ b/tests/integration/setup.php @@ -0,0 +1,16 @@ +run(); diff --git a/tests/phpunit.integration.xml b/tests/phpunit.integration.xml new file mode 100644 index 0000000..90fbbff --- /dev/null +++ b/tests/phpunit.integration.xml @@ -0,0 +1,25 @@ + + + + + ../src/ + + + + + ./integration + ./integration/tmp + + + diff --git a/tests/phpunit.unit.xml b/tests/phpunit.unit.xml new file mode 100644 index 0000000..d3a4a3e --- /dev/null +++ b/tests/phpunit.unit.xml @@ -0,0 +1,27 @@ + + + + + ../src/ + + + + + ./unit + + + + + + diff --git a/tests/unit/.gitkeep b/tests/unit/.gitkeep new file mode 100644 index 0000000..e69de29