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

612 lines
24 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;
2026-01-30 09:44:19 +00:00
use Utopia\Database\Helpers\Permission;
2023-02-05 20:07:46 +00:00
use Utopia\Database\Helpers\Role;
2025-03-19 08:29:18 +00:00
use Utopia\System\System;
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']);
2025-10-10 10:00:56 +00:00
/**
* Test for DUPLICATE functionId
*/
$functionId = $this->setupFunction([
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::user($this->getUser()['$id'])->toString()],
'runtime' => 'node-22',
'entrypoint' => 'index.js',
'events' => [
'users.*.create',
'users.*.delete',
],
'timeout' => 10,
]);
$response = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'functionId' => $functionId,
'name' => 'Test',
'execute' => [Role::user($this->getUser()['$id'])->toString()],
'runtime' => 'node-22',
'entrypoint' => 'index.js',
'events' => [
'users.*.create',
'users.*.delete',
],
'timeout' => 10,
]);
$this->assertEquals(409, $response['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()],
2025-07-09 14:58:55 +00:00
'runtime' => 'node-22',
'entrypoint' => 'index.js',
2020-12-29 23:00:44 +00:00
'events' => [
'users.*.create',
'users.*.delete',
2020-12-29 23:00:44 +00:00
],
'timeout' => 10,
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
2025-07-09 14:58:55 +00:00
'code' => $this->packageFunction('basic'),
2023-03-01 12:00:36 +00:00
'activate' => true
2020-12-29 23:00:44 +00:00
]);
2024-09-19 19:21:00 +00:00
// Deny create async execution as guest
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], [
'async' => true,
2020-12-29 23:00:44 +00:00
]);
$this->assertEquals(401, $execution['headers']['status-code']);
2024-09-19 19:21:00 +00:00
// Allow create async execution as user
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->cleanupFunction($functionId);
2020-12-29 23:00:44 +00:00
}
2021-03-10 20:50:20 +00:00
public function testCreateHeadExecution()
{
/**
* Test for SUCCESS
*/
$functionId = $this->setupFunction([
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::user($this->getUser()['$id'])->toString()],
'runtime' => 'node-22',
'entrypoint' => 'index.js',
'events' => [
'users.*.create',
'users.*.delete',
],
'timeout' => 10,
]);
$this->setupDeployment($functionId, [
'code' => $this->packageFunction('basic'),
'activate' => true
]);
// Deny create async execution as guest
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], [
'async' => true,
]);
$this->assertEquals(401, $execution['headers']['status-code']);
// Allow create async execution as user
$execution = $this->client->call(Client::METHOD_HEAD, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'async' => true,
]);
$this->assertEquals(200, $execution['headers']['status-code']);
$this->assertEmpty($execution['body']);
$this->cleanupFunction($functionId);
}
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(),
2025-07-10 12:25:53 +00:00
'name' => 'Test',
2022-08-19 04:04:33 +00:00
'execute' => [Role::any()->toString()],
2025-07-09 14:58:55 +00:00
'runtime' => 'node-22',
'entrypoint' => 'index.js',
2021-03-10 20:50:20 +00:00
'timeout' => 10,
]);
2024-09-19 11:35:52 +00:00
$deploymentId = $this->setupDeployment($functionId, [
2025-07-09 14:58:55 +00:00
'code' => $this->packageFunction('basic'),
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',
2024-10-02 13:16:25 +00:00
'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->assertNotEmpty($execution['body']['responseHeaders']);
$executionIdHeader = null;
foreach ($execution['body']['responseHeaders'] as $header) {
if ($header['name'] === 'x-appwrite-execution-id') {
$executionIdHeader = $header['value'];
break;
}
}
$this->assertNotEmpty($executionIdHeader);
$this->assertEquals($execution['body']['$id'], $executionIdHeader);
2023-08-16 06:19:42 +00:00
$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']);
2025-07-10 12:25:53 +00:00
$this->assertEquals('Node.js', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
$this->assertEquals('22', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
2024-06-27 12:43:15 +00:00
$this->assertEquals(APP_VERSION_STABLE, $output['APPWRITE_VERSION']);
2025-03-19 08:29:18 +00:00
$this->assertEquals(System::getEnv('_APP_REGION', '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
$executionId = $execution['body']['$id'] ?? '';
$this->assertNotEmpty($output['APPWRITE_FUNCTION_EXECUTION_ID']);
$this->assertEquals($executionId, $output['APPWRITE_FUNCTION_EXECUTION_ID']);
$this->assertNotEmpty($output['APPWRITE_FUNCTION_CLIENT_IP']);
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 20:54:25 +00:00
}, 10000, 500);
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(),
2025-07-09 14:58:55 +00:00
'name' => 'Test guest execution',
'execute' => [Role::any()->toString()],
2025-07-09 14:58:55 +00:00
'runtime' => 'node-22',
'entrypoint' => 'index.js',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'timeout' => 10,
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
2025-07-09 14:58:55 +00:00
'code' => $this->packageFunction('basic'),
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' => [],
2025-07-10 12:25:53 +00:00
'runtime' => 'node-22',
'entrypoint' => 'index.js',
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(),
2025-07-09 14:58:55 +00:00
'name' => 'Test synchronous execution',
2022-08-19 04:04:33 +00:00
'execute' => [Role::any()->toString()],
2025-07-09 14:58:55 +00:00
'runtime' => 'node-22',
'entrypoint' => 'index.js',
2021-09-13 11:06:50 +00:00
'timeout' => 10,
]);
2024-09-19 11:35:52 +00:00
$deploymentId = $this->setupDeployment($functionId, [
2025-07-09 14:58:55 +00:00
'code' => $this->packageFunction('basic'),
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',
2024-10-02 13:16:25 +00:00
// 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']);
2025-07-10 12:25:53 +00:00
$this->assertEquals('Test synchronous execution', $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']);
2025-07-10 12:25:53 +00:00
$this->assertEquals('Node.js', $output['APPWRITE_FUNCTION_RUNTIME_NAME']);
$this->assertEquals('22', $output['APPWRITE_FUNCTION_RUNTIME_VERSION']);
2024-06-27 12:43:15 +00:00
$this->assertEquals(APP_VERSION_STABLE, $output['APPWRITE_VERSION']);
2025-03-19 08:29:18 +00:00
$this->assertEquals(System::getEnv('_APP_REGION', '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()],
2025-07-10 12:25:53 +00:00
'runtime' => 'node-22',
'entrypoint' => 'index.js'
]);
2024-09-19 11:35:52 +00:00
$this->setupDeployment($functionId, [
'entrypoint' => 'index.js',
2025-07-10 12:25:53 +00:00
'code' => $this->packageFunction('basic'),
'activate' => true
]);
2024-10-09 13:12:46 +00:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'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);
$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-10-09 13:02:55 +00:00
2024-06-03 08:40:20 +00:00
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([
2025-03-09 14:33:06 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-07-29 13:39:48 +00:00
], $this->getHeaders()));
2024-09-19 19:21:00 +00:00
2024-07-29 13:39:48 +00:00
$this->assertEquals(200, $templates['headers']['status-code']);
$this->assertGreaterThan(0, $templates['body']['total']);
$this->assertIsArray($templates['body']['templates']);
2024-09-19 19:21:00 +00:00
/**
* Test for SUCCESS with total=false
*/
$templatesWithIncludeTotalFalse = $this->client->call(Client::METHOD_GET, '/functions/templates', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'total' => false
]);
2025-10-20 15:38:14 +00:00
$this->assertEquals(200, $templatesWithIncludeTotalFalse['headers']['status-code']);
$this->assertIsArray($templatesWithIncludeTotalFalse['body']);
$this->assertIsArray($templatesWithIncludeTotalFalse['body']['templates']);
$this->assertIsInt($templatesWithIncludeTotalFalse['body']['total']);
$this->assertEquals(0, $templatesWithIncludeTotalFalse['body']['total']);
$this->assertGreaterThan(0, count($templatesWithIncludeTotalFalse['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);
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',
2025-03-09 14:33:06 +00:00
'x-appwrite-project' => $this->getProject()['$id'],
2024-07-30 10:41:04 +00:00
], $this->getHeaders()), [
'limit' => 1,
'offset' => 2
]);
2024-09-19 11:35:52 +00:00
$this->assertEquals(200, $templatesOffset['headers']['status-code']);
2025-04-16 13:39:08 +00:00
$this->addToAssertionCount(1, $templatesOffset['body']['templates']);
2024-09-19 11:35:52 +00:00
$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',
2025-03-09 14:33:06 +00:00
'x-appwrite-project' => $this->getProject()['$id'],
2024-07-29 13:39:48 +00:00
], $this->getHeaders()), [
2024-07-30 10:41:04 +00:00
'useCases' => ['starter', 'ai'],
2025-12-05 07:10:35 +00:00
'runtimes' => ['node-22']
2024-07-29 13:39:48 +00:00
]);
$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]);
2025-04-01 13:29:07 +00:00
foreach ($templates['body']['templates'] as $template) {
$this->assertThat(
\array_column($template['runtimes'], 'name'),
$this->logicalOr(
2025-12-05 07:10:35 +00:00
$this->containsEqual('node-22'),
2025-04-01 13:29:07 +00:00
),
);
}
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'],
2025-07-10 12:25:53 +00:00
'runtimes' => ['node-22']
2024-07-29 13:39:48 +00:00
]);
$this->assertEquals(200, $templates['headers']['status-code']);
2025-04-16 13:39:08 +00:00
$this->assertCount(5, $templates['body']['templates']);
2024-07-29 13:39:48 +00:00
$this->assertIsArray($templates['body']['templates']);
$this->assertArrayHasKey('runtimes', $templates['body']['templates'][0]);
2024-09-19 19:21:00 +00:00
2024-07-30 10:41:04 +00:00
foreach ($templates['body']['templates'] as $template) {
$this->assertContains($template['useCases'][0], ['databases']);
}
2024-09-19 19:21:00 +00:00
2025-07-10 12:25:53 +00:00
$this->assertContains('node-22', 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',
2025-03-09 14:33:06 +00:00
'x-appwrite-project' => $this->getProject()['$id'],
2024-07-30 10:41:04 +00:00
], $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']);
}
2026-01-30 08:41:01 +00:00
/**
* Test that event-triggered functions work when the triggering request
* comes from a client SDK (session auth) that doesn't have permission
* to read the functions collection.
*/
public function testEventTriggerWithClientAuth()
{
$functionId = $this->setupFunction([
'functionId' => ID::unique(),
'name' => 'Test Client Event Trigger',
'runtime' => 'node-22',
'entrypoint' => 'index.js',
'events' => [
'databases.*.collections.*.documents.*.create',
],
'timeout' => 15,
]);
$this->setupDeployment($functionId, [
'code' => $this->packageFunction('event-handler'),
'activate' => true
]);
$database = $this->client->call(Client::METHOD_POST, '/databases', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'databaseId' => ID::unique(),
'name' => 'Test Database',
]);
$this->assertEquals(201, $database['headers']['status-code']);
$databaseId = $database['body']['$id'];
$collection = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'collectionId' => ID::unique(),
'name' => 'Test Collection',
'permissions' => [
2026-01-30 09:44:19 +00:00
Permission::create(Role::users()),
2026-01-30 08:41:01 +00:00
],
'documentSecurity' => false,
]);
$this->assertEquals(201, $collection['headers']['status-code']);
$collectionId = $collection['body']['$id'];
$attribute = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'name',
'size' => 255,
'required' => false,
]);
$this->assertEquals(202, $attribute['headers']['status-code']);
sleep(2);
$document = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => ['name' => 'Test Document'],
]);
$this->assertEquals(201, $document['headers']['status-code']);
$documentId = $document['body']['$id'];
$this->assertEventually(function () use ($functionId, $documentId) {
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/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->assertGreaterThan(0, count($executions['body']['executions']), 'Function should have been triggered by document creation');
$lastExecution = $executions['body']['executions'][0];
$this->assertEquals('completed', $lastExecution['status']);
$this->assertEquals(204, $lastExecution['responseStatusCode']);
$this->assertStringContainsString($documentId, $lastExecution['logs']);
}, 20000, 500);
$this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->cleanupFunction($functionId);
}
2021-03-10 20:50:20 +00:00
}