From 2f843943e18cc3bfd19da750101b5c622794a678 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Sun, 12 Jan 2020 17:20:51 +0200 Subject: [PATCH] Improved health check tests --- app/app.php | 2 +- app/config/roles.php | 2 +- .../Avatars/AvatarsCustomServerTest.php | 1 - tests/e2e/Services/Health/HealthBase.php | 154 ++++++++++++++++++ .../Health/HealthConsoleClientTest.php | 14 ++ .../Health/HealthCustomClientTest.php | 14 ++ .../Health/HealthCustomServerTest.php | 15 ++ .../Locale/LocaleCustomServerTest.php | 2 - tests/old/ConsoleHealthTest.php | 42 ++--- 9 files changed, 220 insertions(+), 26 deletions(-) create mode 100644 tests/e2e/Services/Health/HealthBase.php create mode 100644 tests/e2e/Services/Health/HealthConsoleClientTest.php create mode 100644 tests/e2e/Services/Health/HealthCustomClientTest.php create mode 100644 tests/e2e/Services/Health/HealthCustomServerTest.php diff --git a/app/app.php b/app/app.php index 773af7289..af3e07f0e 100644 --- a/app/app.php +++ b/app/app.php @@ -138,7 +138,7 @@ $utopia->init(function () use ($utopia, $request, $response, &$user, $project, $ ]); $role = Auth::USER_ROLE_APP; - $scopes = $key->getAttribute('scopes', []); + $scopes = array_merge($roles[$role]['scopes'], $key->getAttribute('scopes', [])); Authorization::disable(); // Cancel security segmentation for API keys. } diff --git a/app/config/roles.php b/app/config/roles.php index 911079541..e69078317 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -82,6 +82,6 @@ return [ ], ROLE_APP => [ 'label' => 'Application', - 'scopes' => ['public'], + 'scopes' => ['health.read'], ], ]; diff --git a/tests/e2e/Services/Avatars/AvatarsCustomServerTest.php b/tests/e2e/Services/Avatars/AvatarsCustomServerTest.php index 4b22b29cb..be8b4d1a7 100644 --- a/tests/e2e/Services/Avatars/AvatarsCustomServerTest.php +++ b/tests/e2e/Services/Avatars/AvatarsCustomServerTest.php @@ -2,7 +2,6 @@ namespace Tests\E2E\Services\Avatars; -use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; diff --git a/tests/e2e/Services/Health/HealthBase.php b/tests/e2e/Services/Health/HealthBase.php new file mode 100644 index 000000000..e579f4680 --- /dev/null +++ b/tests/e2e/Services/Health/HealthBase.php @@ -0,0 +1,154 @@ +client->call(Client::METHOD_GET, '/health', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('OK', $response['body']['status']); + + /** + * Test for FAILURE + */ + + return []; + } + + public function testDBSuccess():array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/db', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('OK', $response['body']['status']); + + /** + * Test for FAILURE + */ + + return []; + } + + public function testCacheSuccess():array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/db', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('OK', $response['body']['status']); + + /** + * Test for FAILURE + */ + + return []; + } + + public function testTimeSuccess():array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/time', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertIsInt($response['body']['remote']); + $this->assertIsInt($response['body']['local']); + $this->assertNotEmpty($response['body']['remote']); + $this->assertNotEmpty($response['body']['local']); + $this->assertLessThan(10, $response['body']['diff']); + + /** + * Test for FAILURE + */ + + return []; + } + + public function testWebhooksSuccess():array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/webhooks', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertIsInt($response['body']['size']); + $this->assertLessThan(10, $response['body']['size']); + + /** + * Test for FAILURE + */ + + return []; + } + + public function xtestStorageLocalSuccess():array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/storage/local', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('OK', $response['body']['status']); + + /** + * Test for FAILURE + */ + + return []; + } + + public function testStorageAntiVirusSuccess():array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/storage/anti-virus', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('online', $response['body']['status']); + $this->assertStringStartsWith('ClamAV ', $response['body']['version']); + + /** + * Test for FAILURE + */ + + return []; + } +} \ No newline at end of file diff --git a/tests/e2e/Services/Health/HealthConsoleClientTest.php b/tests/e2e/Services/Health/HealthConsoleClientTest.php new file mode 100644 index 000000000..f52d51c7b --- /dev/null +++ b/tests/e2e/Services/Health/HealthConsoleClientTest.php @@ -0,0 +1,14 @@ +getHeaders() } \ No newline at end of file diff --git a/tests/old/ConsoleHealthTest.php b/tests/old/ConsoleHealthTest.php index a664e4d01..26c49fa95 100644 --- a/tests/old/ConsoleHealthTest.php +++ b/tests/old/ConsoleHealthTest.php @@ -8,10 +8,10 @@ class ConsoleHealthTest extends BaseConsole { public function testHTTPSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('OK', $response['body']['status']); @@ -19,10 +19,10 @@ class ConsoleHealthTest extends BaseConsole public function testDBSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health/db', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health/db', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('OK', $response['body']['status']); @@ -30,10 +30,10 @@ class ConsoleHealthTest extends BaseConsole public function testCacheSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health/db', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health/db', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('OK', $response['body']['status']); @@ -41,10 +41,10 @@ class ConsoleHealthTest extends BaseConsole public function testTimeSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health/time', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health/time', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertIsInt($response['body']['remote']); @@ -56,10 +56,10 @@ class ConsoleHealthTest extends BaseConsole public function testWebhooksSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health/webhooks', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health/webhooks', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertIsInt($response['body']['size']); @@ -68,10 +68,10 @@ class ConsoleHealthTest extends BaseConsole public function xtestStorageLocalSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health/storage/local', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health/storage/local', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('OK', $response['body']['status']); @@ -79,10 +79,10 @@ class ConsoleHealthTest extends BaseConsole public function testStorageAntiVirusSuccess(): void { - $response = $this->client->call(Client::METHOD_GET, '/health/storage/anti-virus', [ - 'origin' => 'http://localhost', + $response = $this->client->call(Client::METHOD_GET, '/health/storage/anti-virus', array_merge([ 'content-type' => 'application/json', - ], []); + 'x-appwrite-project' => $this->getProject()['$uid'], + ], $this->getHeaders()), []); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('online', $response['body']['status']);