From 6dab42e59a793b6b570b97dabb7fe5f9cc1712fc Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:00:19 +0100 Subject: [PATCH] chore: use project injectable --- app/controllers/general.php | 14 +------ tests/e2e/General/PingTest.php | 19 ++++----- .../Realtime/RealtimeConsoleClientTest.php | 40 +++++++++++++++++++ 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 7395a479a4..227745b028 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -1061,18 +1061,7 @@ App::get('/v1/ping') ->inject('dbForConsole') ->inject('queueForEvents') ->action(function (string $projectId, Response $response, Document $project, Database $dbForConsole, Event $queueForEvents) { - if (empty($projectId) || $projectId === 'console') { - throw new AppwriteException(AppwriteException::PROJECT_NOT_FOUND); - } - - Console::log('Ping' . json_encode(['projectId' => $projectId], JSON_PRETTY_PRINT)); - - $project = Authorization::skip(function () use ($dbForConsole, $projectId) { - return $dbForConsole->getDocument('projects', $projectId); - }); - if ($project->isEmpty()) { - Console::log('Ping' . json_encode($project, JSON_PRETTY_PRINT)); throw new AppwriteException(AppwriteException::PROJECT_NOT_FOUND); } @@ -1081,13 +1070,14 @@ App::get('/v1/ping') $project ->setAttribute('pingCount', $pingCount) - ->setAttribute('pingedAt', $pingedAt); + ->setAttribute('pingedAt', $pingedAt); Authorization::skip(function () use ($dbForConsole, $project) { $dbForConsole->updateDocument('projects', $project->getId(), $project); }); $queueForEvents + ->setProject($project) ->setParam('projectId', $projectId) ->setPayload($response->output($project, Response::MODEL_PROJECT)); diff --git a/tests/e2e/General/PingTest.php b/tests/e2e/General/PingTest.php index c1199841fb..96db658cc3 100644 --- a/tests/e2e/General/PingTest.php +++ b/tests/e2e/General/PingTest.php @@ -18,25 +18,24 @@ class PingTest extends Scope * Test for SUCCESS */ // Without user session - $response = $this->client->call(Client::METHOD_GET, '/ping', [], [ - 'projectId' => $this->getProject()['$id'], + $response = $this->client->call(Client::METHOD_GET, '/ping', [ + 'x-appwrite-project' => $this->getProject()['$id'], ]); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('Pong!', $response['body']); // With user session - $response = $this->client->call(Client::METHOD_GET, '/ping', $this->getHeaders(), [ - 'projectId' => $this->getProject()['$id'], - ]); + $response = $this->client->call(Client::METHOD_GET, '/ping', array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('Pong!', $response['body']); // With API key $response = $this->client->call(Client::METHOD_GET, '/ping', [ + 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'], - ], [ - 'projectId' => $this->getProject()['$id'], ]); /** @@ -44,10 +43,8 @@ class PingTest extends Scope */ // Fake project ID $response = $this->client->call(Client::METHOD_GET, '/ping', \array_merge([ - 'origin' => 'http://localhost', - ]), [ - 'projectId' => 'fake-project-id', - ]); + 'x-appwrite-project' => 'fake-project-id', + ], $this->getHeaders())); $this->assertEquals(404, $response['headers']['status-code']); $this->assertNotContains('Pong!', $response['body']); diff --git a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php index 60c96c6e19..4bb9d8711e 100644 --- a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php @@ -478,6 +478,46 @@ class RealtimeConsoleClientTest extends Scope $client->close(); } + public function testPing() + { + $client = $this->getWebsocket(['console'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ], 'console'); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertEquals('connected', $response['type']); + + fwrite(STDOUT, 'Project ID: ' . $this->getProject()['$id'] . "\n"); + + $pong = $this->client->call(Client::METHOD_GET, '/ping', [ + 'origin' => 'http://localhost', + 'x-appwrite-project' => $this->getProject()['$id'], + ]); + + $this->assertEquals(200, $pong['headers']['status-code']); + $this->assertEquals('Pong!', $pong['body']); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertContains("projects.{$this->getProject()['$id']}", $response['data']['channels']); + $this->assertContains("projects.{$this->getProject()['$id']}.ping", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertArrayHasKey('pingCount', $response['data']['payload']); + $this->assertArrayHasKey('pingedAt', $response['data']['payload']); + $this->assertEquals(1, $response['data']['payload']['pingCount']); + + $client->close(); + } + public function testCreateDeployment() { $response1 = $this->client->call(Client::METHOD_POST, '/functions', array_merge([