appwrite/tests/e2e/Services/Functions/FunctionsCustomClientTest.php

1016 lines
43 KiB
PHP
Raw Normal View History

2020-05-05 20:37:59 +00:00
<?php
namespace Tests\E2E\Services\Functions;
use Appwrite\Tests\Retry;
2020-12-29 23:00:44 +00:00
use CURLFile;
2020-05-05 20:37:59 +00:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
2024-07-30 10:41:04 +00:00
use Utopia\Config\Config;
2023-12-20 10:55:09 +00:00
use Utopia\Database\Document;
2023-02-05 20:07:46 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
2023-12-20 10:55:09 +00:00
use Utopia\Database\Query;
2020-05-05 20:37:59 +00:00
2020-12-26 12:10:14 +00:00
class FunctionsCustomClientTest extends Scope
2020-05-05 20:37:59 +00:00
{
use FunctionsBase;
use ProjectCustom;
use SideClient;
2022-05-23 14:54:50 +00:00
public function testCreate(): array
2020-05-05 20:37:59 +00:00
{
/**
* Test for SUCCESS
*/
$response1 = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2022-08-14 10:33:36 +00:00
'functionId' => ID::unique(),
2020-05-05 20:37:59 +00:00
'name' => 'Test',
'events' => [
'users.*.create',
'users.*.delete',
2020-05-05 20:37:59 +00:00
],
2021-04-23 15:05:17 +00:00
'schedule' => '0 0 1 1 *',
2020-05-05 20:37:59 +00:00
'timeout' => 10,
]);
$this->assertEquals(401, $response1['headers']['status-code']);
return [];
}
2020-12-29 23:00:44 +00:00
#[Retry(count: 2)]
2022-05-23 14:54:50 +00:00
public function testCreateExecution(): array
2020-12-29 23:00:44 +00:00
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2022-08-14 10:33:36 +00:00
'functionId' => ID::unique(),
2020-12-29 23:00:44 +00:00
'name' => 'Test',
2022-08-27 03:16:37 +00:00
'execute' => [Role::user($this->getUser()['$id'])->toString()],
'runtime' => 'php-8.0',
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php',
2020-12-29 23:00:44 +00:00
'events' => [
'users.*.create',
'users.*.delete',
2020-12-29 23:00:44 +00:00
],
2024-05-20 12:14:48 +00:00
'schedule' => '* * * * *', // execute every minute
2020-12-29 23:00:44 +00:00
'timeout' => 10,
]);
$this->assertEquals(201, $function['headers']['status-code']);
2022-08-03 13:32:50 +00:00
/** Create Variables */
2022-08-09 15:29:24 +00:00
$variable = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
2022-08-09 15:29:24 +00:00
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
2022-08-09 15:29:24 +00:00
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
2022-08-03 13:35:11 +00:00
2022-08-03 13:32:50 +00:00
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
2022-02-19 13:57:48 +00:00
$folder = 'php';
2022-05-23 14:54:50 +00:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
2022-02-19 13:57:48 +00:00
$this->packageCode($folder);
2022-05-23 14:54:50 +00:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/deployments', [
2020-12-29 23:00:44 +00:00
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2021-09-01 09:48:56 +00:00
'entrypoint' => 'index.php',
2022-02-19 13:57:48 +00:00
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)),
2023-03-01 12:00:36 +00:00
'activate' => true
2020-12-29 23:00:44 +00:00
]);
2022-01-24 23:37:44 +00:00
$deploymentId = $deployment['body']['$id'] ?? '';
2021-03-12 13:49:57 +00:00
2022-07-20 12:01:18 +00:00
$this->assertEquals(202, $deployment['headers']['status-code']);
2021-03-12 13:49:57 +00:00
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId);
2022-05-23 14:54:50 +00:00
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $function['body']['$id'] . '/deployments/' . $deploymentId, [
2020-12-29 23:00:44 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
2022-01-31 23:44:55 +00:00
2020-12-29 23:00:44 +00:00
$this->assertEquals(200, $function['headers']['status-code']);
2022-05-23 14:54:50 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', [
2020-12-29 23:00:44 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], [
2021-12-06 15:04:00 +00:00
'async' => true,
2020-12-29 23:00:44 +00:00
]);
$this->assertEquals(401, $execution['headers']['status-code']);
2022-05-23 14:54:50 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
2020-12-29 23:00:44 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-12-06 15:04:00 +00:00
'async' => true,
2020-12-29 23:00:44 +00:00
]);
2022-07-20 12:01:18 +00:00
$this->assertEquals(202, $execution['headers']['status-code']);
2024-05-20 12:14:48 +00:00
// Wait for the first scheduled execution to be created
2024-06-25 09:30:59 +00:00
sleep(90);
2024-05-20 12:14:48 +00:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(2, $executions['body']['executions']);
$this->assertIsArray($executions['body']['executions']);
$this->assertEquals($executions['body']['executions'][1]['trigger'], 'schedule');
$this->assertEquals($executions['body']['executions'][1]['status'], 'completed');
$this->assertEquals($executions['body']['executions'][1]['responseStatusCode'], 200);
$this->assertEquals($executions['body']['executions'][1]['responseBody'], '');
$this->assertEquals($executions['body']['executions'][1]['logs'], '');
2024-07-03 12:35:08 +00:00
$this->assertGreaterThan(0, $executions['body']['executions'][1]['duration']);
2024-05-20 12:14:48 +00:00
2022-01-29 00:00:25 +00:00
// Cleanup : Delete function
2022-05-23 14:54:50 +00:00
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $function['body']['$id'], [
2021-03-13 23:56:29 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2022-01-29 00:00:25 +00:00
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
2021-03-12 13:49:57 +00:00
2022-01-29 00:00:25 +00:00
$this->assertEquals(204, $response['headers']['status-code']);
2020-12-29 23:00:44 +00:00
return [];
}
2021-03-10 20:50:20 +00:00
2024-06-11 12:57:03 +00:00
public function testCreateScheduledExecution(): void
{
/**
* Test for SUCCESS
*/
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'functionId' => ID::unique(),
'name' => 'Test',
2024-08-09 12:10:07 +00:00
'execute' => [Role::user($this->getUser()['$id'])->toString()],
2024-06-11 12:57:03 +00:00
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'timeout' => 10,
]);
$this->assertEquals(201, $function['headers']['status-code']);
2024-08-09 09:43:08 +00:00
$folder = 'php';
2024-06-11 12:57:03 +00:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/deployments', [
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'entrypoint' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)),
'activate' => true
]);
$deploymentId = $deployment['body']['$id'] ?? '';
$this->assertEquals(202, $deployment['headers']['status-code']);
2024-08-09 09:43:08 +00:00
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId, true);
2024-06-11 12:57:03 +00:00
// Schedule execution for the future
2024-06-17 12:44:12 +00:00
\date_default_timezone_set('UTC');
2024-08-16 09:11:13 +00:00
$futureTime = (new \DateTime())->add(new \DateInterval('PT2M'));
$futureTime->setTime($futureTime->format('H'), $futureTime->format('i'), 0, 0);
2024-06-26 11:30:23 +00:00
2024-06-11 12:57:03 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'async' => true,
2024-08-09 12:10:07 +00:00
'scheduledAt' => $futureTime->format(\DateTime::ATOM),
2024-09-07 10:20:23 +00:00
'path' => '/custom-path',
'method' => 'PATCH',
'body' => 'custom-body',
'headers' => [
'x-custom-header' => 'custom-value'
]
2024-06-11 12:57:03 +00:00
]);
$this->assertEquals(202, $execution['headers']['status-code']);
2024-06-26 10:01:43 +00:00
$this->assertEquals('scheduled', $execution['body']['status']);
2024-09-07 10:20:23 +00:00
$this->assertEquals('PATCH', $execution['body']['requestMethod']);
$this->assertEquals('/custom-path', $execution['body']['requestPath']);
$this->assertCount(0, $execution['body']['requestHeaders']);
2024-06-11 12:57:03 +00:00
2024-06-13 08:36:01 +00:00
$executionId = $execution['body']['$id'];
2024-08-09 09:43:08 +00:00
$start = \microtime(true);
while (true) {
$execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
if ($execution['body']['status'] === 'completed') {
break;
}
2024-09-07 10:20:23 +00:00
$timeout = 60 + 60 + 15; // up to 1 minute round up, 1 minute schedule postpone, 15s cold start safety
if (\microtime(true) - $start > $timeout) {
2024-08-16 09:11:13 +00:00
$this->fail('Scheduled execution did not complete with status ' . $execution['body']['status'] . ': ' . \json_encode($execution));
2024-08-09 09:43:08 +00:00
}
2024-08-09 12:10:07 +00:00
usleep(500000); // 0.5 seconds
2024-08-09 09:43:08 +00:00
}
2024-06-13 08:36:01 +00:00
2024-06-14 13:01:04 +00:00
$this->assertEquals(200, $execution['headers']['status-code']);
2024-06-26 11:42:35 +00:00
$this->assertEquals(200, $execution['body']['responseStatusCode']);
2024-06-26 10:01:43 +00:00
$this->assertEquals('completed', $execution['body']['status']);
2024-09-07 10:20:23 +00:00
$this->assertEquals('/custom-path', $execution['body']['requestPath']);
$this->assertEquals('PATCH', $execution['body']['requestMethod']);
$this->assertStringContainsString('body-is-custom-body', $execution['body']['logs']);
$this->assertStringContainsString('custom-header-is-custom-value', $execution['body']['logs']);
$this->assertStringContainsString('method-is-patch', $execution['body']['logs']);
$this->assertStringContainsString('path-is-/custom-path', $execution['body']['logs']);
$this->assertStringContainsString('user-is-' . $this->getUser()['$id'], $execution['body']['logs']);
$this->assertStringContainsString('jwt-is-valid', $execution['body']['logs']);
2024-07-03 12:35:08 +00:00
$this->assertGreaterThan(0, $execution['body']['duration']);
2024-07-19 09:54:20 +00:00
2024-06-26 10:01:43 +00:00
/* Test for FAILURE */
// Schedule synchronous execution
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'async' => false,
2024-08-09 12:10:07 +00:00
'scheduledAt' => $futureTime->format(\DateTime::ATOM),
2024-06-26 10:01:43 +00:00
]);
$this->assertEquals(400, $execution['headers']['status-code']);
2024-08-16 09:11:13 +00:00
// Execution with seconds precision
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'async' => true,
'scheduledAt' => (new \DateTime("2100-12-08 16:12:02"))->format(\DateTime::ATOM)
]);
$this->assertEquals(400, $execution['headers']['status-code']);
// Execution with milliseconds precision
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'async' => true,
'scheduledAt' => (new \DateTime("2100-12-08 16:12:02.255"))->format(\DateTime::ATOM)
]);
$this->assertEquals(400, $execution['headers']['status-code']);
// Execution too soon
$futureTime = (new \DateTime())->add(new \DateInterval('PT1M'));
$futureTime->setTime($futureTime->format('H'), $futureTime->format('i'), 0, 0);
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'async' => true,
2024-08-16 10:17:35 +00:00
'scheduledAt' => $futureTime->format(\DateTime::ATOM),
2024-08-16 09:11:13 +00:00
]);
$this->assertEquals(400, $execution['headers']['status-code']);
2024-06-13 08:36:01 +00:00
2024-06-11 12:57:03 +00:00
// Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $function['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
$this->assertEquals(204, $response['headers']['status-code']);
}
2022-05-23 14:54:50 +00:00
public function testCreateCustomExecution(): array
2021-03-10 20:50:20 +00:00
{
/**
* Test for SUCCESS
*/
2021-03-11 16:42:19 +00:00
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
2021-03-10 20:50:20 +00:00
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
2021-03-11 16:42:19 +00:00
'x-appwrite-project' => $projectId,
2021-03-12 13:49:57 +00:00
'x-appwrite-key' => $apikey,
2021-03-10 20:50:20 +00:00
], [
2022-08-14 10:33:36 +00:00
'functionId' => ID::unique(),
2021-03-10 20:50:20 +00:00
'name' => 'Test',
2022-08-19 04:04:33 +00:00
'execute' => [Role::any()->toString()],
'runtime' => 'php-8.0',
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php',
2021-03-10 20:50:20 +00:00
'timeout' => 10,
]);
2021-03-11 14:19:40 +00:00
$functionId = $function['body']['$id'] ?? '';
2021-03-10 20:50:20 +00:00
$this->assertEquals(201, $function['headers']['status-code']);
2022-08-03 13:35:11 +00:00
2022-08-03 13:32:50 +00:00
/** Create Variables */
2022-08-09 15:29:24 +00:00
$variable = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
2022-08-03 13:35:11 +00:00
2022-08-09 15:29:24 +00:00
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
2022-08-03 13:35:11 +00:00
2022-08-09 15:29:24 +00:00
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
2022-08-03 13:35:11 +00:00
2022-08-03 13:32:50 +00:00
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
2021-03-10 20:50:20 +00:00
2022-02-19 13:57:48 +00:00
$folder = 'php-fn';
2022-05-23 14:54:50 +00:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
2022-02-19 13:57:48 +00:00
$this->packageCode($folder);
2022-05-23 14:54:50 +00:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
2021-03-10 20:50:20 +00:00
'content-type' => 'multipart/form-data',
2021-03-12 13:49:57 +00:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
2021-03-10 20:50:20 +00:00
], [
2021-09-01 09:48:56 +00:00
'entrypoint' => 'index.php',
2022-02-19 13:57:48 +00:00
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
2023-03-01 12:00:36 +00:00
'activate' => true
2021-03-10 20:50:20 +00:00
]);
$deploymentId = $deployment['body']['$id'] ?? '';
2021-03-12 13:49:57 +00:00
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId);
2021-03-12 13:49:57 +00:00
2022-05-23 14:54:50 +00:00
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, [
2021-03-10 20:50:20 +00:00
'content-type' => 'application/json',
2021-03-12 13:49:57 +00:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], []);
2021-03-12 13:49:57 +00:00
2021-03-10 20:50:20 +00:00
$this->assertEquals(200, $function['headers']['status-code']);
2022-05-23 14:54:50 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
2021-03-10 20:50:20 +00:00
'content-type' => 'application/json',
2021-03-12 13:49:57 +00:00
'x-appwrite-project' => $projectId,
2021-03-10 20:50:20 +00:00
], $this->getHeaders()), [
2023-08-16 06:19:42 +00:00
'body' => 'foobar',
'async' => false
2021-07-22 14:49:52 +00:00
]);
2023-08-16 06:19:42 +00:00
$output = json_decode($execution['body']['responseBody'], true);
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertEquals(200, $execution['body']['responseStatusCode']);
2024-07-03 12:35:08 +00:00
$this->assertGreaterThan(0, $execution['body']['duration']);
2023-08-16 06:19:42 +00:00
$this->assertEquals('completed', $execution['body']['status']);
2021-07-22 14:49:52 +00:00
$this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
$this->assertEquals('Test', $output['APPWRITE_FUNCTION_NAME']);
2022-01-25 00:36:33 +00:00
$this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']);
2021-07-22 14:49:52 +00:00
$this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']);
$this->assertEquals('PHP', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
$this->assertEquals('8.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
2024-06-27 12:43:15 +00:00
$this->assertEquals(APP_VERSION_STABLE, $output['APPWRITE_VERSION']);
2024-07-15 07:10:11 +00:00
$this->assertEquals('default', $output['APPWRITE_REGION']);
2021-07-22 14:49:52 +00:00
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
$this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']);
$this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']);
$this->assertNotEmpty($output['APPWRITE_FUNCTION_JWT']);
2021-09-03 11:41:42 +00:00
$this->assertEquals($projectId, $output['APPWRITE_FUNCTION_PROJECT_ID']);
2021-07-22 14:49:52 +00:00
2023-08-16 06:19:42 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
'body' => 'foobar',
'async' => true
]);
$this->assertEquals(202, $execution['headers']['status-code']);
$executionId = $execution['body']['$id'] ?? '';
sleep(5);
$execution = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions/' . $executionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
]);
$this->assertEmpty($execution['body']['responseBody']);
$this->assertEquals(200, $execution['headers']['status-code']);
$this->assertEquals(200, $execution['body']['responseStatusCode']);
return [
'functionId' => $functionId
];
}
public function testCreateCustomExecutionGuest()
{
/**
* Test for SUCCESS
*/
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::any()->toString()],
'runtime' => 'php-8.0',
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'timeout' => 10,
]);
$functionId = $function['body']['$id'] ?? '';
$this->assertEquals(201, $function['headers']['status-code']);
$folder = 'php-fn';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'entrypoint' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
2023-03-01 12:00:36 +00:00
'activate' => true
]);
$deploymentId = $deployment['body']['$id'] ?? '';
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId);
// Why do we have to do this?
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], []);
$this->assertEquals(200, $function['headers']['status-code']);
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], [
'data' => 'foobar',
'async' => true,
]);
$this->assertEquals(202, $execution['headers']['status-code']);
}
public function testCreateExecutionNoDeployment(): array
2022-01-18 07:04:36 +00:00
{
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2022-08-14 10:33:36 +00:00
'functionId' => ID::unique(),
2022-01-18 07:04:36 +00:00
'name' => 'Test',
'execute' => [],
'runtime' => 'php-8.0',
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php',
2022-01-18 07:04:36 +00:00
'timeout' => 10,
]);
2022-05-23 14:54:50 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', [
2022-01-18 07:04:36 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], [
2022-01-26 23:19:02 +00:00
'async' => true,
2022-01-18 07:04:36 +00:00
]);
$this->assertEquals(404, $execution['headers']['status-code']);
2022-01-18 07:04:36 +00:00
return [];
}
/**
* @depends testCreateCustomExecution
*/
public function testListExecutions(array $data)
{
$functionId = $data['functionId'];
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
2022-05-23 14:54:50 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
'data' => 'foobar'
]);
2023-08-23 03:22:06 +00:00
$this->assertEquals(201, $execution['headers']['status-code']);
2022-05-23 14:54:50 +00:00
$base = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
]);
$this->assertEquals(200, $base['headers']['status-code']);
2023-08-16 06:19:42 +00:00
$this->assertCount(3, $base['body']['executions']);
$this->assertEquals('completed', $base['body']['executions'][0]['status']);
$this->assertEquals('completed', $base['body']['executions'][1]['status']);
2022-05-23 14:54:50 +00:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
2021-03-10 20:50:20 +00:00
'content-type' => 'application/json',
2021-03-12 13:49:57 +00:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2023-12-20 10:55:09 +00:00
'queries' => [
Query::limit(1)->toString(),
],
2022-08-24 11:55:43 +00:00
]);
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(1, $executions['body']['executions']);
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2023-12-20 10:55:09 +00:00
'queries' => [
Query::offset(1)->toString(),
],
2022-08-24 11:55:43 +00:00
]);
$this->assertEquals(200, $executions['headers']['status-code']);
2023-08-16 06:19:42 +00:00
$this->assertCount(2, $executions['body']['executions']);
2022-08-24 11:55:43 +00:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2023-12-20 10:55:09 +00:00
'queries' => [
Query::equal('status', ['completed'])->toString(),
],
2022-08-24 11:55:43 +00:00
]);
$this->assertEquals(200, $executions['headers']['status-code']);
2023-08-16 06:19:42 +00:00
$this->assertCount(3, $executions['body']['executions']);
2022-08-24 11:55:43 +00:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2023-12-20 10:55:09 +00:00
'queries' => [
Query::equal('status', ['failed'])->toString(),
],
2022-08-24 11:55:43 +00:00
]);
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(0, $executions['body']['executions']);
2022-05-23 14:54:50 +00:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
2021-03-10 20:50:20 +00:00
'content-type' => 'application/json',
2021-03-12 13:49:57 +00:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2023-12-20 10:55:09 +00:00
'queries' => [
Query::cursorAfter(new Document(['$id' => $base['body']['executions'][0]['$id']]))->toString(),
],
2021-03-11 16:42:19 +00:00
]);
2021-03-10 20:50:20 +00:00
2023-08-16 06:19:42 +00:00
$this->assertCount(2, $executions['body']['executions']);
$this->assertEquals($base['body']['executions'][1]['$id'], $executions['body']['executions'][0]['$id']);
2022-05-23 14:54:50 +00:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2023-12-20 10:55:09 +00:00
'queries' => [
Query::cursorBefore(new Document(['$id' => $base['body']['executions'][1]['$id']]))->toString(),
],
]);
2022-01-29 00:00:25 +00:00
// Cleanup : Delete function
2022-05-23 14:54:50 +00:00
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
2022-01-29 00:00:25 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
$this->assertEquals(204, $response['headers']['status-code']);
2021-11-15 02:18:53 +00:00
}
2022-05-23 14:54:50 +00:00
public function testSynchronousExecution(): array
2021-09-13 11:06:50 +00:00
{
/**
* Test for SUCCESS
*/
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
2022-08-14 10:33:36 +00:00
'functionId' => ID::unique(),
2021-09-13 11:06:50 +00:00
'name' => 'Test',
2022-08-19 04:04:33 +00:00
'execute' => [Role::any()->toString()],
2021-09-13 11:06:50 +00:00
'runtime' => 'php-8.0',
2023-08-11 13:34:57 +00:00
'entrypoint' => 'index.php',
2021-09-13 11:06:50 +00:00
'timeout' => 10,
]);
$functionId = $function['body']['$id'] ?? '';
$this->assertEquals(201, $function['headers']['status-code']);
2022-08-03 13:32:50 +00:00
/** Create Variables */
2022-08-09 15:29:24 +00:00
$variable = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
2022-08-03 13:35:11 +00:00
2022-08-09 15:29:24 +00:00
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
2022-08-03 13:35:11 +00:00
2022-08-09 15:29:24 +00:00
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', [
2022-08-03 13:32:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
2022-08-03 13:35:11 +00:00
2022-08-03 13:32:50 +00:00
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
2021-09-13 11:06:50 +00:00
2022-02-19 13:57:48 +00:00
$folder = 'php-fn';
2022-05-23 14:54:50 +00:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
2022-02-19 13:57:48 +00:00
$this->packageCode($folder);
2022-05-23 14:54:50 +00:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
2021-09-13 11:06:50 +00:00
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'entrypoint' => 'index.php',
2022-02-19 13:57:48 +00:00
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
2023-03-01 12:00:36 +00:00
'activate' => true
2021-09-13 11:06:50 +00:00
]);
2022-01-24 23:40:03 +00:00
$deploymentId = $deployment['body']['$id'] ?? '';
2021-09-13 11:06:50 +00:00
2022-07-20 12:01:18 +00:00
$this->assertEquals(202, $deployment['headers']['status-code']);
2021-09-13 11:06:50 +00:00
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId);
2022-05-23 14:54:50 +00:00
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, [
2021-09-13 11:06:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
]);
2021-09-13 11:06:50 +00:00
$this->assertEquals(200, $function['headers']['status-code']);
2022-05-23 14:54:50 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
2021-09-13 11:06:50 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
2023-08-16 06:19:42 +00:00
'body' => 'foobar',
// Testing default value, should be 'async' => false
2021-09-13 11:06:50 +00:00
]);
2023-08-16 06:19:42 +00:00
$output = json_decode($execution['body']['responseBody'], true);
2021-09-13 11:06:50 +00:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertEquals('completed', $execution['body']['status']);
2023-08-16 06:19:42 +00:00
$this->assertEquals(200, $execution['body']['responseStatusCode']);
2021-09-13 11:06:50 +00:00
$this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
$this->assertEquals('Test', $output['APPWRITE_FUNCTION_NAME']);
2022-01-25 00:36:33 +00:00
$this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']);
2021-09-13 11:06:50 +00:00
$this->assertEquals('http', $output['APPWRITE_FUNCTION_TRIGGER']);
$this->assertEquals('PHP', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
$this->assertEquals('8.0', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
2024-06-27 12:43:15 +00:00
$this->assertEquals(APP_VERSION_STABLE, $output['APPWRITE_VERSION']);
2024-07-15 07:10:11 +00:00
$this->assertEquals('default', $output['APPWRITE_REGION']);
2021-09-13 11:06:50 +00:00
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
$this->assertEquals('foobar', $output['APPWRITE_FUNCTION_DATA']);
$this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']);
$this->assertNotEmpty($output['APPWRITE_FUNCTION_JWT']);
$this->assertEquals($projectId, $output['APPWRITE_FUNCTION_PROJECT_ID']);
// Client should never see logs and errors
2023-02-14 11:01:38 +00:00
$this->assertEmpty($execution['body']['logs']);
$this->assertEmpty($execution['body']['errors']);
2021-09-13 11:06:50 +00:00
2022-05-23 14:54:50 +00:00
// Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
2022-01-29 00:00:25 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
$this->assertEquals(204, $response['headers']['status-code']);
2021-09-13 11:06:50 +00:00
return [];
}
2024-06-03 08:43:08 +00:00
public function testNonOverrideOfHeaders(): array
{
/**
* Test for SUCCESS
*/
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::any()->toString()],
'runtime' => 'node-18.0',
'entrypoint' => 'index.js'
]);
$functionId = $function['body']['$id'] ?? '';
$this->assertEquals(201, $function['headers']['status-code']);
$folder = 'node';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'entrypoint' => 'index.js',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
'activate' => true
]);
$deploymentId = $deployment['body']['$id'] ?? '';
$this->assertEquals(202, $deployment['headers']['status-code']);
$this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId);
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2024-06-03 08:40:20 +00:00
'x-appwrite-event' => "OVERRIDDEN",
'x-appwrite-trigger' => "OVERRIDDEN",
'x-appwrite-user-id' => "OVERRIDDEN",
'x-appwrite-user-jwt' => "OVERRIDDEN",
]);
$output = json_decode($execution['body']['responseBody'], true);
2024-06-03 08:40:20 +00:00
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_JWT']);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_EVENT']);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_TRIGGER']);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_USER_ID']);
// Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
$this->assertEquals(204, $response['headers']['status-code']);
return [];
}
2024-07-29 13:39:48 +00:00
2024-07-30 11:46:45 +00:00
public function testListTemplates()
2024-07-29 13:39:48 +00:00
{
2024-07-30 10:41:04 +00:00
/**
* Test for SUCCESS
*/
$expectedTemplates = array_slice(Config::getParam('function-templates', []), 0, 25);
2024-07-29 13:39:48 +00:00
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
], $this->getHeaders()));
$this->assertEquals(200, $templates['headers']['status-code']);
$this->assertGreaterThan(0, $templates['body']['total']);
$this->assertIsArray($templates['body']['templates']);
2024-07-29 13:39:48 +00:00
$this->assertArrayHasKey('runtimes', $templates['body']['templates'][0]);
2024-07-30 10:41:04 +00:00
$this->assertArrayHasKey('useCases', $templates['body']['templates'][0]);
for ($i = 0; $i < 25; $i++) {
$this->assertEquals($expectedTemplates[$i]['name'], $templates['body']['templates'][$i]['name']);
$this->assertEquals($expectedTemplates[$i]['id'], $templates['body']['templates'][$i]['id']);
$this->assertEquals($expectedTemplates[$i]['icon'], $templates['body']['templates'][$i]['icon']);
$this->assertEquals($expectedTemplates[$i]['tagline'], $templates['body']['templates'][$i]['tagline']);
$this->assertEquals($expectedTemplates[$i]['useCases'], $templates['body']['templates'][$i]['useCases']);
$this->assertEquals($expectedTemplates[$i]['vcsProvider'], $templates['body']['templates'][$i]['vcsProvider']);
$this->assertEquals($expectedTemplates[$i]['runtimes'], $templates['body']['templates'][$i]['runtimes']);
$this->assertEquals($expectedTemplates[$i]['variables'], $templates['body']['templates'][$i]['variables']);
2024-08-08 14:08:59 +00:00
if (array_key_exists('scopes', $expectedTemplates[$i])) {
$this->assertEquals($expectedTemplates[$i]['scopes'], $templates['body']['templates'][$i]['scopes']);
}
2024-07-30 10:41:04 +00:00
}
$templates_offset = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
], $this->getHeaders()), [
'limit' => 1,
'offset' => 2
]);
$this->assertEquals(200, $templates_offset['headers']['status-code']);
$this->assertEquals(1, $templates_offset['body']['total']);
// assert that offset works as expected
$this->assertEquals($templates['body']['templates'][2]['id'], $templates_offset['body']['templates'][0]['id']);
2024-07-29 13:39:48 +00:00
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
], $this->getHeaders()), [
2024-07-30 10:41:04 +00:00
'useCases' => ['starter', 'ai'],
2024-07-29 13:39:48 +00:00
'runtimes' => ['bun-1.0', 'dart-2.16']
]);
$this->assertEquals(200, $templates['headers']['status-code']);
2024-07-30 10:41:04 +00:00
$this->assertGreaterThanOrEqual(3, $templates['body']['total']);
2024-07-29 13:39:48 +00:00
$this->assertIsArray($templates['body']['templates']);
2024-07-30 10:41:04 +00:00
foreach ($templates['body']['templates'] as $template) {
$this->assertContains($template['useCases'][0], ['starter', 'ai']);
}
2024-07-29 13:39:48 +00:00
$this->assertArrayHasKey('runtimes', $templates['body']['templates'][0]);
2024-07-29 20:55:18 +00:00
$this->assertContains('bun-1.0', array_column($templates['body']['templates'][0]['runtimes'], 'name'));
2024-07-29 13:39:48 +00:00
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2024-07-29 20:55:18 +00:00
'limit' => 5,
2024-07-29 13:39:48 +00:00
'offset' => 2,
2024-07-30 10:41:04 +00:00
'useCases' => ['databases'],
2024-07-29 20:55:18 +00:00
'runtimes' => ['node-16.0']
2024-07-29 13:39:48 +00:00
]);
$this->assertEquals(200, $templates['headers']['status-code']);
2024-07-29 20:55:18 +00:00
$this->assertEquals(5, $templates['body']['total']);
2024-07-29 13:39:48 +00:00
$this->assertIsArray($templates['body']['templates']);
$this->assertArrayHasKey('runtimes', $templates['body']['templates'][0]);
2024-07-30 10:41:04 +00:00
foreach ($templates['body']['templates'] as $template) {
$this->assertContains($template['useCases'][0], ['databases']);
}
2024-07-29 20:55:18 +00:00
$this->assertContains('node-16.0', array_column($templates['body']['templates'][0]['runtimes'], 'name'));
2024-07-30 10:41:04 +00:00
/**
* Test for FAILURE
*/
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
], $this->getHeaders()), [
'limit' => 5001,
'offset' => 10,
]);
$this->assertEquals(400, $templates['headers']['status-code']);
$this->assertEquals('Invalid `limit` param: Value must be a valid range between 1 and 5,000', $templates['body']['message']);
$templates = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'limit' => 5,
'offset' => 5001,
]);
$this->assertEquals(400, $templates['headers']['status-code']);
$this->assertEquals('Invalid `offset` param: Value must be a valid range between 0 and 5,000', $templates['body']['message']);
2024-07-29 13:39:48 +00:00
}
2024-07-30 11:46:45 +00:00
public function testGetTemplate()
{
/**
* Test for SUCCESS
*/
$template = $this->client->call(Client::METHOD_GET, '/functions/templates/query-neo4j-auradb', array_merge([
'content-type' => 'application/json',
], $this->getHeaders()), []);
$this->assertEquals(200, $template['headers']['status-code']);
$this->assertIsArray($template['body']);
$this->assertEquals('query-neo4j-auradb', $template['body']['id']);
$this->assertEquals('Query Neo4j AuraDB', $template['body']['name']);
$this->assertEquals('icon-neo4j', $template['body']['icon']);
$this->assertEquals('Graph database with focus on relations between data.', $template['body']['tagline']);
$this->assertEquals(['databases'], $template['body']['useCases']);
$this->assertEquals('github', $template['body']['vcsProvider']);
/**
* Test for FAILURE
*/
$template = $this->client->call(Client::METHOD_GET, '/functions/templates/invalid-template-id', array_merge([
'content-type' => 'application/json',
], $this->getHeaders()), []);
$this->assertEquals(404, $template['headers']['status-code']);
$this->assertEquals('Function Template with the requested ID could not be found.', $template['body']['message']);
}
2021-03-10 20:50:20 +00:00
}