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

489 lines
20 KiB
PHP
Raw Normal View History

2020-05-05 20:37:59 +00:00
<?php
namespace Tests\E2E\Services\Functions;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
2023-02-05 20:07:46 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
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;
2024-09-19 13:17:38 +00:00
public function testCreateFunction()
2020-05-05 20:37:59 +00:00
{
/**
2024-09-19 11:35:52 +00:00
* Test for FAILURE
2020-05-05 20:37:59 +00:00
*/
2024-09-19 11:35:52 +00:00
$function = $this->createFunction([
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,
]);
2024-09-19 11:35:52 +00:00
$this->assertEquals(401, $function['headers']['status-code']);
2020-05-05 20:37:59 +00:00
}
2020-12-29 23:00:44 +00:00
2024-09-19 11:35:52 +00:00
public function testCreateExecution()
2020-12-29 23:00:44 +00:00
{
/**
* Test for SUCCESS
*/
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
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-09-19 11:35:52 +00:00
'schedule' => '* * * * *', // Execute every minute
2020-12-29 23:00:44 +00:00
'timeout' => 10,
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
2021-09-01 09:48:56 +00:00
'entrypoint' => 'index.php',
2024-09-19 11:35:52 +00:00
'code' => $this->packageFunction('php'),
2023-03-01 12:00:36 +00:00
'activate' => true
2020-12-29 23:00:44 +00:00
]);
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
'async' => false,
2020-12-29 23:00:44 +00:00
]);
$this->assertEquals(401, $execution['headers']['status-code']);
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
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-09-19 11:35:52 +00:00
$this->assertEventually(function () use ($functionId) {
$executions = $this->listExecutions($functionId);
2024-09-18 13:29:44 +00:00
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(2, $executions['body']['executions']);
2024-05-20 12:14:48 +00:00
2024-09-19 11:35:52 +00:00
$asyncExecution = $executions['body']['executions'][1];
$this->assertEquals('schedule', $asyncExecution['trigger']);
$this->assertEquals('completed', $asyncExecution['status']);
$this->assertEquals(200, $asyncExecution['responseStatusCode']);
$this->assertEquals('', $asyncExecution['responseBody']);
$this->assertNotEmpty($asyncExecution['logs']);
$this->assertNotEmpty($asyncExecution['errors']);
$this->assertGreaterThan(0, $asyncExecution['duration']);
2024-09-19 13:17:38 +00:00
}, 10000, 250);
2024-09-19 11:35:52 +00:00
$this->cleanupFunction($functionId);
2020-12-29 23:00:44 +00:00
}
2021-03-10 20:50:20 +00:00
2024-06-11 12:57:03 +00:00
public function testCreateScheduledExecution(): void
{
/**
* Test for SUCCESS
*/
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
2024-06-11 12:57:03 +00:00
'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,
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
2024-06-11 12:57:03 +00:00
'entrypoint' => 'index.php',
2024-09-19 11:35:52 +00:00
'code' => $this->packageFunction('php'),
2024-06-11 12:57:03 +00:00
'activate' => true
]);
// Schedule execution for the future
2024-06-17 12:44:12 +00:00
\date_default_timezone_set('UTC');
2024-09-19 13:17:38 +00:00
$futureTime = (new \DateTime())->add(new \DateInterval('PT15S')); // 15 seconds from now
2024-08-16 09:11:13 +00:00
$futureTime->setTime($futureTime->format('H'), $futureTime->format('i'), 0, 0);
2024-06-26 11:30:23 +00:00
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
2024-06-11 12:57:03 +00:00
'async' => true,
2024-09-19 11:35:52 +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
]);
2024-09-19 11:35:52 +00:00
$executionId = $execution['body']['$id'];
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-09-19 13:17:38 +00:00
\sleep(15);
2024-09-19 11:35:52 +00:00
$this->assertEventually(function () use ($functionId, $executionId) {
$execution = $this->getExecution($functionId, $executionId);
2024-09-18 13:29:44 +00:00
$this->assertEquals('completed', $execution['body']['status']);
2024-09-19 11:35:52 +00:00
$this->assertEquals(200, $execution['headers']['status-code']);
$this->assertEquals(200, $execution['body']['responseStatusCode']);
$this->assertEquals('completed', $execution['body']['status']);
$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']);
$this->assertGreaterThan(0, $execution['body']['duration']);
2024-09-19 13:17:38 +00:00
}, 10000, 250);
2024-07-19 09:54:20 +00:00
2024-06-26 10:01:43 +00:00
/* Test for FAILURE */
// Schedule synchronous execution
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
2024-06-26 10:01:43 +00:00
'async' => false,
2024-09-19 11:35:52 +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
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
2024-08-16 09:11:13 +00:00
'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
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
2024-08-16 09:11:13 +00:00
'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
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
2024-08-16 09:11:13 +00:00
'async' => true,
2024-09-19 11:35:52 +00:00
'scheduledAt' => (new \DateTime())->add(new \DateInterval('PT1S'))->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-09-19 11:35:52 +00:00
$this->cleanupFunction($functionId, $executionId);
2024-06-11 12:57:03 +00:00
}
2022-05-23 14:54:50 +00:00
public function testCreateCustomExecution(): array
2021-03-10 20:50:20 +00:00
{
/**
* Test for SUCCESS
*/
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
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,
]);
2024-09-19 11:35:52 +00:00
$deploymentId = $this->setupDeployment($functionId, [
2021-09-01 09:48:56 +00:00
'entrypoint' => 'index.php',
2024-09-19 11:35:52 +00:00
'code' => $this->packageFunction('php-fn'),
2023-03-01 12:00:36 +00:00
'activate' => true
2021-03-10 20:50:20 +00:00
]);
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
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']);
2024-09-19 11:35:52 +00:00
$this->assertEquals($this->getProject()['$id'], $output['APPWRITE_FUNCTION_PROJECT_ID']);
2021-07-22 14:49:52 +00:00
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
2023-08-16 06:19:42 +00:00
'body' => 'foobar',
'async' => true
]);
2024-09-19 11:35:52 +00:00
$executionId = $execution['body']['$id'];
2023-08-16 06:19:42 +00:00
$this->assertEquals(202, $execution['headers']['status-code']);
2024-09-19 11:35:52 +00:00
$this->assertEventually(function () use ($functionId, $executionId) {
$execution = $this->getExecution($functionId, $executionId);
2024-09-18 13:29:44 +00:00
$this->assertEquals('completed', $execution['body']['status']);
$this->assertEquals(200, $execution['body']['responseStatusCode']);
2024-09-19 11:35:52 +00:00
}, 10000, 250);
2023-08-16 06:19:42 +00:00
return [
'functionId' => $functionId
];
}
public function testCreateCustomExecutionGuest()
{
/**
* Test for SUCCESS
*/
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
'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,
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
'entrypoint' => 'index.php',
2024-09-19 11:35:52 +00:00
'code' => $this->packageFunction('php-fn'),
2023-03-01 12:00:36 +00:00
'activate' => true
]);
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
2024-09-19 11:35:52 +00:00
'x-appwrite-project' => $this->getProject()['$id'],
], [
'data' => 'foobar',
'async' => true,
]);
$this->assertEquals(202, $execution['headers']['status-code']);
}
2024-09-19 11:35:52 +00:00
public function testCreateExecutionNoDeployment()
2022-01-18 07:04:36 +00:00
{
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
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,
]);
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
'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
}
2024-09-19 11:35:52 +00:00
public function testSynchronousExecution()
2021-09-13 11:06:50 +00:00
{
/**
* Test for SUCCESS
*/
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
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,
]);
2024-09-19 11:35:52 +00:00
$deploymentId = $this->setupDeployment($functionId, [
2021-09-13 11:06:50 +00:00
'entrypoint' => 'index.php',
2024-09-19 11:35:52 +00:00
'code' => $this->packageFunction('php-fn'),
2023-03-01 12:00:36 +00:00
'activate' => true
2021-09-13 11:06:50 +00:00
]);
2024-09-19 11:35:52 +00:00
$execution = $this->createExecution($functionId, [
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']);
2024-09-19 11:35:52 +00:00
$this->assertEquals($this->getProject()['$id'], $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
2024-09-19 11:35:52 +00:00
$this->cleanupFunction($functionId);
2021-09-13 11:06:50 +00:00
}
2024-09-19 11:35:52 +00:00
public function testNonOverrideOfHeaders()
{
2024-09-19 11:35:52 +00:00
$functionId = $this->setupFunction([
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::any()->toString()],
'runtime' => 'node-18.0',
'entrypoint' => 'index.js'
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
'entrypoint' => 'index.js',
2024-09-19 11:35:52 +00:00
'code' => $this->packageFunction('node'),
'activate' => true
]);
$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']);
2024-09-19 11:35:52 +00:00
$this->cleanupFunction($functionId);
}
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
*/
2024-09-19 11:35:52 +00:00
// List all templates
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-09-19 11:35:52 +00:00
foreach ($templates['body']['templates'] as $template) {
$this->assertArrayHasKey('name', $template);
$this->assertArrayHasKey('id', $template);
$this->assertArrayHasKey('icon', $template);
$this->assertArrayHasKey('tagline', $template);
$this->assertArrayHasKey('useCases', $template);
$this->assertArrayHasKey('vcsProvider', $template);
$this->assertArrayHasKey('runtimes', $template);
$this->assertArrayHasKey('variables', $template);
$this->assertArrayHasKey('scopes', $template);
2024-07-30 10:41:04 +00:00
}
2024-09-19 11:35:52 +00:00
// List templates with pagination
$templatesOffset = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
2024-07-30 10:41:04 +00:00
'content-type' => 'application/json',
], $this->getHeaders()), [
'limit' => 1,
'offset' => 2
]);
2024-09-19 11:35:52 +00:00
$this->assertEquals(200, $templatesOffset['headers']['status-code']);
$this->assertEquals(1, $templatesOffset['body']['total']);
$this->assertEquals($templates['body']['templates'][2]['id'], $templatesOffset['body']['templates'][0]['id']);
2024-07-30 10:41:04 +00:00
2024-09-19 11:35:52 +00:00
// List templates with filters
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
2024-09-19 11:35:52 +00:00
// List templates with pagination and filters
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
*/
2024-09-19 11:35:52 +00:00
// List templates with invalid limit
2024-07-30 10:41:04 +00:00
$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']);
2024-09-19 11:35:52 +00:00
// List templates with invalid offset
2024-07-30 10:41:04 +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()), [
'limit' => 5,
'offset' => 5001,
]);
$this->assertEquals(400, $templates['headers']['status-code']);
2024-07-29 13:39:48 +00:00
}
2024-07-30 11:46:45 +00:00
public function testGetTemplate()
{
/**
* Test for SUCCESS
*/
2024-09-19 11:35:52 +00:00
$template = $this->getTemplate('query-neo4j-auradb');
2024-07-30 11:46:45 +00:00
$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']);
2024-09-19 11:35:52 +00:00
$this->assertIsArray($template['body']['runtimes']);
$this->assertIsArray($template['body']['scopes']);
2024-07-30 11:46:45 +00:00
/**
* Test for FAILURE
*/
2024-09-19 11:35:52 +00:00
$template = $this->getTemplate('invalid-template-id');
2024-07-30 11:46:45 +00:00
$this->assertEquals(404, $template['headers']['status-code']);
}
2021-03-10 20:50:20 +00:00
}