From ab2bb8176c291f9427642a0b89d9fccbc699f24d Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Tue, 25 Oct 2022 12:16:05 -0700 Subject: [PATCH] Fix create deployment realtime not triggering Because the deployment event was missing the project ID, the realtime event never fired. --- CHANGES.md | 1 + src/Appwrite/Messaging/Adapter/Realtime.php | 2 +- .../Realtime/RealtimeConsoleClientTest.php | 77 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 340aec16d4..2c649ab5f0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ## Bugs - Fix license detection for Flutter and Dart SDKs [#4435](https://github.com/appwrite/appwrite/pull/4435) +- Fix missing realtime event for create function deployment [#4574](https://github.com/appwrite/appwrite/pull/4574) # Version 1.0.3 ## Bugs diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 9151a5c0b5..dddbc59e7f 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -321,7 +321,7 @@ class Realtime extends Adapter } } elseif ($parts[2] === 'deployments') { $channels[] = 'console'; - + $projectId = 'console'; $roles = [Role::team($project->getAttribute('teamId'))->toString()]; } diff --git a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php index efcfe95f53..500b3d48be 100644 --- a/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeConsoleClientTest.php @@ -2,16 +2,19 @@ namespace Tests\E2E\Services\Realtime; +use CURLFile; use Tests\E2E\Client; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\SideConsole; +use Tests\E2E\Services\Functions\FunctionsBase; use Utopia\Database\ID; use Utopia\Database\Permission; use Utopia\Database\Role; class RealtimeConsoleClientTest extends Scope { + use FunctionsBase; use RealtimeBase; use ProjectCustom; use SideConsole; @@ -425,4 +428,78 @@ class RealtimeConsoleClientTest extends Scope $client->close(); } + + public function testCreateDeployment() + { + $response1 = $this->client->call(Client::METHOD_POST, '/functions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'functionId' => ID::unique(), + 'name' => 'Test', + 'runtime' => 'php-8.0', + 'events' => [ + 'users.*.create', + 'users.*.delete', + ], + 'schedule' => '0 0 1 1 *', + 'timeout' => 10, + ]); + + $functionId = $response1['body']['$id'] ?? ''; + + $this->assertEquals(201, $response1['headers']['status-code']); + + + $projectId = 'console'; + + $client = $this->getWebsocket(['console'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ], $projectId); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('console', $response['data']['channels']); + $this->assertNotEmpty($response['data']['user']); + + /** + * Test Create Deployment + */ + + $folder = 'php'; + $code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz"; + $this->packageCode($folder); + + $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'entrypoint' => 'index.php', + 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), + ]); + + $deploymentId = $deployment['body']['$id'] ?? ''; + + $this->assertEquals(202, $deployment['headers']['status-code']); + + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $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("functions.{$functionId}.deployments.{$deploymentId}.create", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + + $client->close(); + } }