From ea30d976a858bb99e7d332eab4c49aa7032cb752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 4 Mar 2025 16:04:37 +0100 Subject: [PATCH] Fix wrong path, implement tests --- .../Deployment}/Update.php | 10 ++-- .../Modules/Functions/Services/Http.php | 4 +- .../Status => Sites/Deployment}/Update.php | 10 ++-- .../Platform/Modules/Sites/Services/Http.php | 4 +- .../e2e/Services/Functions/FunctionsBase.php | 12 ++++ .../Functions/FunctionsCustomServerTest.php | 54 +++++++++++++++++- tests/e2e/Services/Sites/SitesBase.php | 12 ++++ .../Services/Sites/SitesCustomServerTest.php | 55 ++++++++++++++++++- 8 files changed, 145 insertions(+), 16 deletions(-) rename src/Appwrite/Platform/Modules/Functions/Http/{Deployments/Status => Functions/Deployment}/Update.php (92%) rename src/Appwrite/Platform/Modules/Sites/Http/{Deployments/Status => Sites/Deployment}/Update.php (91%) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php similarity index 92% rename from src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php rename to src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php index a26065d09a..bcd62c8320 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) - ->setHttpPath('/v1/functions/:functionId/deployments/:deploymentId/status') + ->setHttpPath('/v1/functions/:functionId/deployment') ->httpAlias('/v1/functions/:functionId/deployments/:deploymentId') - ->desc('Update deployment status') + ->desc('Update function\'s deployment') ->groups(['api', 'functions']) ->label('scope', 'functions.write') ->label('event', 'functions.[functionId].deployments.[deploymentId].update') @@ -39,7 +39,7 @@ class Update extends Action ->label('audits.resource', 'function/{request.functionId}') ->label('sdk', new Method( namespace: 'functions', - name: 'updateDeploymentStatus', + name: 'updateFunctionDeployment', description: <<addAction(CreateDeployment::getName(), new CreateDeployment()); $this->addAction(GetDeployment::getName(), new GetDeployment()); - $this->addAction(UpdateDeploymentStatus::getName(), new UpdateDeploymentStatus()); + $this->addAction(UpdateFunctionDeployment::getName(), new UpdateFunctionDeployment()); $this->addAction(ListDeployments::getName(), new ListDeployments()); $this->addAction(DeleteDeployment::getName(), new DeleteDeployment()); $this->addAction(CreateTemplateDeployment::getName(), new CreateTemplateDeployment()); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php similarity index 91% rename from src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php rename to src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php index 5119568645..4dfbee6cde 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) - ->setHttpPath('/v1/sites/:siteId/deployments/:deploymentId/status') + ->setHttpPath('/v1/sites/:siteId/deployment') ->httpAlias('/v1/sites/:siteId/deployments/:deploymentId') - ->desc('Update deployment status') + ->desc('Update site\'s deployment') ->groups(['api', 'sites']) ->label('scope', 'sites.write') ->label('event', 'sites.[siteId].deployments.[deploymentId].update') @@ -37,7 +37,7 @@ class Update extends Action ->label('audits.resource', 'site/{request.siteId}') ->label('sdk', new Method( namespace: 'sites', - name: 'updateDeploymentStatus', + name: 'updateSiteDeployment', description: <<addAction(CreateVcsDeployment::getName(), new CreateVcsDeployment()); $this->addAction(GetDeployment::getName(), new GetDeployment()); $this->addAction(ListDeployments::getName(), new ListDeployments()); - $this->addAction(UpdateDeploymentStatus::getName(), new UpdateDeploymentStatus()); + $this->addAction(UpdateSiteDeployment::getName(), new UpdateSiteDeployment()); $this->addAction(DeleteDeployment::getName(), new DeleteDeployment()); $this->addAction(DownloadDeployment::getName(), new DownloadDeployment()); $this->addAction(CreateBuild::getName(), new CreateBuild()); diff --git a/tests/e2e/Services/Functions/FunctionsBase.php b/tests/e2e/Services/Functions/FunctionsBase.php index f97245b0af..b55eea42ac 100644 --- a/tests/e2e/Services/Functions/FunctionsBase.php +++ b/tests/e2e/Services/Functions/FunctionsBase.php @@ -324,4 +324,16 @@ trait FunctionsBase return $response; } + + protected function updateFunctionDeployment(string $functionId, string $deploymentId): mixed + { + $function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployment', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'deploymentId' => $deploymentId + ]); + + return $function; + } } diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 5ad63676fe..481f4b8263 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1992,6 +1992,58 @@ class FunctionsCustomServerTest extends Scope public function testUpdateDeploymentStatus(): void { - // TODO: Create function, create deployment A, create execution A. create deployment B, ensure function B. Activate deployment A, ensure execution A. Cleanup + + $functionId = $this->setupFunction([ + 'functionId' => ID::unique(), + 'runtime' => 'php-8.0', + 'name' => 'Re-activate Test', + 'entrypoint' => 'index.php', + ]); + $this->assertNotEmpty($functionId); + + $deploymentId1 = $this->setupDeployment($functionId, [ + 'code' => $this->packageFunction('php-cookie'), + 'activate' => true + ]); + $this->assertNotEmpty($deploymentId1); + + $execution = $this->createExecution($functionId, [ + 'headers' => [ 'cookie' => 'cookieName=cookieValue' ] + ]); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertNotEmpty($execution['body']['$id']); + $this->assertStringContainsString('cookieValue', $execution['body']['responseBody']); + + $deploymentId2 = $this->setupDeployment($functionId, [ + 'code' => $this->packageFunction('php'), + 'activate' => true + ]); + $this->assertNotEmpty($deploymentId2); + + $execution = $this->createExecution($functionId); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertNotEmpty($execution['body']['$id']); + $this->assertStringContainsString('UNICODE_TEST', $execution['body']['responseBody']); + + $function = $this->getFunction($functionId); + $this->assertEquals(200, $function['headers']['status-code']); + $this->assertEquals($deploymentId2, $function['body']['deployment']); + + $function = $this->updateFunctionDeployment($functionId, $deploymentId1); + $this->assertEquals(200, $function['headers']['status-code']); + $this->assertEquals($deploymentId1, $function['body']['deployment']); + + $function = $this->getFunction($functionId); + $this->assertEquals(200, $function['headers']['status-code']); + $this->assertEquals($deploymentId1, $function['body']['deployment']); + + $execution = $this->createExecution($functionId, [ + 'headers' => [ 'cookie' => 'cookieName=cookieValue' ] + ]); + $this->assertEquals(201, $execution['headers']['status-code']); + $this->assertNotEmpty($execution['body']['$id']); + $this->assertStringContainsString('cookieValue', $execution['body']['responseBody']); + + $this->cleanupFunction($functionId); } } diff --git a/tests/e2e/Services/Sites/SitesBase.php b/tests/e2e/Services/Sites/SitesBase.php index 6e597b31ec..e0d3696c59 100644 --- a/tests/e2e/Services/Sites/SitesBase.php +++ b/tests/e2e/Services/Sites/SitesBase.php @@ -359,4 +359,16 @@ trait SitesBase return $response; } + + protected function updateFunctionDeployment(string $siteId, string $deploymentId): mixed + { + $site = $this->client->call(Client::METHOD_PATCH, '/sites/' . $siteId . '/deployment', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'deploymentId' => $deploymentId + ]); + + return $site; + } } diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 73dd0132f7..de0e3ce0b2 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -1750,6 +1750,59 @@ class SitesCustomServerTest extends Scope public function testUpdateDeploymentStatus(): void { - // TODO: Create site, create deployment A, ensure site A. create dpeloyment B, ensure site B. Activate deploymnt A, ensure site A. Cleanup + $siteId = $this->setupSite([ + 'buildRuntime' => 'ssr-22', + 'framework' => 'other', + 'name' => 'Activate test Site', + 'siteId' => ID::unique() + ]); + $this->assertNotEmpty($siteId); + + $domain = $this->setupSiteDomain($siteId); + $this->assertNotEmpty($domain); + + $deploymentId1 = $this->setupDeployment($siteId, [ + 'code' => $this->packageSite('static'), + 'activate' => true + ]); + $this->assertNotEmpty($deploymentId1); + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://' . $domain); + $response = $proxyClient->call(Client::METHOD_GET, '/'); + $this->assertEquals(200, $response['headers']['status-code']); + // TODO: Ensure response from static + + $deploymentId2 = $this->setupDeployment($siteId, [ + 'code' => $this->packageSite('static-spa'), + 'activate' => true + ]); + $this->assertNotEmpty($deploymentId2); + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://' . $domain); + $response = $proxyClient->call(Client::METHOD_GET, '/'); + $this->assertEquals(200, $response['headers']['status-code']); + // TODO: Ensure response from static-spa + + $function = $this->getSite($siteId); + $this->assertEquals(200, $function['headers']['status-code']); + $this->assertEquals($deploymentId2, $function['body']['deploymentId']); + + $function = $this->updateFunctionDeployment($siteId, $deploymentId1); + $this->assertEquals(200, $function['headers']['status-code']); + $this->assertEquals($deploymentId1, $function['body']['deploymentId']); + + $function = $this->getSite($siteId); + $this->assertEquals(200, $function['headers']['status-code']); + $this->assertEquals($deploymentId1, $function['body']['deploymentId']); + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://' . $domain); + $response = $proxyClient->call(Client::METHOD_GET, '/'); + $this->assertEquals(200, $response['headers']['status-code']); + // TODO: Ensure response from static + + $this->cleanupSite($siteId); } }