1
0
Fork 0
mirror of synced 2024-07-03 13:41:01 +12:00
appwrite/tests/e2e/Services/Functions/FunctionsCustomClientTest.php

413 lines
16 KiB
PHP
Raw Normal View History

2020-05-06 08:37:59 +12:00
<?php
namespace Tests\E2E\Services\Functions;
2020-12-30 12:00:44 +13:00
use CURLFile;
2020-05-06 08:37:59 +12:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
2022-02-20 02:57:48 +13:00
use Utopia\CLI\Console;
use Utopia\Database\Database;
2020-05-06 08:37:59 +12:00
2020-12-27 01:10:14 +13:00
class FunctionsCustomClientTest extends Scope
2020-05-06 08:37:59 +12:00
{
use FunctionsBase;
use ProjectCustom;
use SideClient;
2022-05-24 02:54:50 +12:00
public function testCreate(): array
2020-05-06 08:37:59 +12: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()), [
2021-07-19 20:24:32 +12:00
'functionId' => 'unique()',
2020-05-06 08:37:59 +12:00
'name' => 'Test',
'vars' => [
2020-10-31 08:53:27 +13:00
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
2020-05-06 08:37:59 +12:00
],
'events' => [
'users.*.create',
'users.*.delete',
2020-05-06 08:37:59 +12:00
],
2021-04-24 03:05:17 +12:00
'schedule' => '0 0 1 1 *',
2020-05-06 08:37:59 +12:00
'timeout' => 10,
]);
$this->assertEquals(401, $response1['headers']['status-code']);
return [];
}
2020-12-30 12:00:44 +13:00
2022-05-24 02:54:50 +12:00
public function testCreateExecution(): array
2020-12-30 12:00:44 +13: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'],
], [
2021-07-30 18:02:59 +12:00
'functionId' => 'unique()',
2020-12-30 12:00:44 +13:00
'name' => 'Test',
2022-05-24 02:54:50 +12:00
'execute' => ['user:' . $this->getUser()['$id']],
'runtime' => 'php-8.0',
2020-12-30 12:00:44 +13:00
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
2020-12-30 12:00:44 +13:00
],
2021-04-24 03:05:17 +12:00
'schedule' => '0 0 1 1 *',
2020-12-30 12:00:44 +13:00
'timeout' => 10,
]);
$this->assertEquals(201, $function['headers']['status-code']);
2022-02-20 02:57:48 +13:00
$folder = 'php';
2022-05-24 02:54:50 +12:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
2022-02-20 02:57:48 +13:00
$this->packageCode($folder);
2022-05-24 02:54:50 +12:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/deployments', [
2020-12-30 12:00:44 +13:00
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2021-09-01 21:48:56 +12:00
'entrypoint' => 'index.php',
2022-02-20 02:57:48 +13:00
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)),
2020-12-30 12:00:44 +13:00
]);
2022-01-25 12:37:44 +13:00
$deploymentId = $deployment['body']['$id'] ?? '';
2021-03-13 02:49:57 +13:00
2022-01-25 12:37:44 +13:00
$this->assertEquals(201, $deployment['headers']['status-code']);
2021-03-13 02:49:57 +13:00
2022-01-25 12:37:44 +13:00
// Wait for deployment to be built.
2022-02-01 12:44:55 +13:00
sleep(10);
2022-05-24 02:54:50 +12:00
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $function['body']['$id'] . '/deployments/' . $deploymentId, [
2020-12-30 12:00:44 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
2022-02-01 12:44:55 +13:00
2020-12-30 12:00:44 +13:00
$this->assertEquals(200, $function['headers']['status-code']);
2022-05-24 02:54:50 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', [
2020-12-30 12:00:44 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], [
2021-12-07 04:04:00 +13:00
'async' => true,
2020-12-30 12:00:44 +13:00
]);
$this->assertEquals(401, $execution['headers']['status-code']);
2022-05-24 02:54:50 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([
2020-12-30 12:00:44 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-12-07 04:04:00 +13:00
'async' => true,
2020-12-30 12:00:44 +13:00
]);
$this->assertEquals(201, $execution['headers']['status-code']);
2022-01-29 13:00:25 +13:00
// Cleanup : Delete function
2022-05-24 02:54:50 +12:00
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $function['body']['$id'], [
2021-03-14 12:56:29 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2022-01-29 13:00:25 +13:00
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);
2021-03-13 02:49:57 +13:00
2022-01-29 13:00:25 +13:00
$this->assertEquals(204, $response['headers']['status-code']);
2020-12-30 12:00:44 +13:00
return [];
}
2021-03-11 09:50:20 +13:00
2022-05-24 02:54:50 +12:00
public function testCreateCustomExecution(): array
2021-03-11 09:50:20 +13:00
{
/**
* Test for SUCCESS
*/
2021-03-12 05:42:19 +13:00
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
2021-03-11 09:50:20 +13:00
$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
2021-03-12 05:42:19 +13:00
'x-appwrite-project' => $projectId,
2021-03-13 02:49:57 +13:00
'x-appwrite-key' => $apikey,
2021-03-11 09:50:20 +13:00
], [
2021-07-19 20:24:32 +12:00
'functionId' => 'unique()',
2021-03-11 09:50:20 +13:00
'name' => 'Test',
2021-06-12 06:23:16 +12:00
'execute' => ['role:all'],
'runtime' => 'php-8.0',
2021-03-11 09:50:20 +13:00
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'timeout' => 10,
]);
2021-03-12 03:19:40 +13:00
$functionId = $function['body']['$id'] ?? '';
2021-03-11 09:50:20 +13:00
$this->assertEquals(201, $function['headers']['status-code']);
2022-02-20 02:57:48 +13:00
$folder = 'php-fn';
2022-05-24 02:54:50 +12:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
2022-02-20 02:57:48 +13:00
$this->packageCode($folder);
2022-05-24 02:54:50 +12:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
2021-03-11 09:50:20 +13:00
'content-type' => 'multipart/form-data',
2021-03-13 02:49:57 +13:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
2021-03-11 09:50:20 +13:00
], [
2021-09-01 21:48:56 +12:00
'entrypoint' => 'index.php',
2022-02-20 02:57:48 +13:00
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
2021-03-11 09:50:20 +13:00
]);
$deploymentId = $deployment['body']['$id'] ?? '';
2021-03-13 02:49:57 +13:00
// Wait for deployment to be built.
sleep(5);
$this->assertEquals(201, $deployment['headers']['status-code']);
2021-03-13 02:49:57 +13:00
2022-05-24 02:54:50 +12:00
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, [
2021-03-11 09:50:20 +13:00
'content-type' => 'application/json',
2021-03-13 02:49:57 +13:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], []);
2021-03-13 02:49:57 +13:00
2021-03-11 09:50:20 +13:00
$this->assertEquals(200, $function['headers']['status-code']);
2022-05-24 02:54:50 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
2021-03-11 09:50:20 +13:00
'content-type' => 'application/json',
2021-03-13 02:49:57 +13:00
'x-appwrite-project' => $projectId,
2021-03-11 09:50:20 +13:00
], $this->getHeaders()), [
'data' => 'foobar',
]);
$this->assertEquals(201, $execution['headers']['status-code']);
2021-03-12 03:19:40 +13:00
2021-07-23 02:49:52 +12:00
$executionId = $execution['body']['$id'] ?? '';
2021-03-20 04:47:06 +13:00
sleep(10);
2021-07-23 02:49:52 +12:00
2022-05-24 02:54:50 +12:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions/' . $executionId, [
2021-07-23 02:49:52 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
]);
$output = json_decode($executions['body']['response'], true);
2021-07-23 02:49:52 +12:00
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertEquals('completed', $executions['body']['status']);
$this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
$this->assertEquals('Test', $output['APPWRITE_FUNCTION_NAME']);
2022-01-25 13:36:33 +13:00
$this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']);
2021-07-23 02:49:52 +12: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']);
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']);
$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 23:41:42 +12:00
$this->assertEquals($projectId, $output['APPWRITE_FUNCTION_PROJECT_ID']);
2021-07-23 02:49:52 +12:00
return [
'functionId' => $functionId
];
}
2022-05-24 02:54:50 +12:00
public function testCreateExecutionUnauthorized(): array
2022-01-18 20:04:36 +13: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'],
], [
'functionId' => 'unique()',
'name' => 'Test',
'execute' => [],
'runtime' => 'php-8.0',
'timeout' => 10,
]);
2022-05-24 02:54:50 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', [
2022-01-18 20:04:36 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], [
2022-01-27 12:19:02 +13:00
'async' => true,
2022-01-18 20:04:36 +13:00
]);
$this->assertEquals(401, $execution['headers']['status-code']);
return [];
}
/**
* @depends testCreateCustomExecution
*/
public function testListExecutions(array $data)
{
$functionId = $data['functionId'];
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];
2022-05-24 02:54:50 +12: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',
]);
$this->assertEquals(201, $execution['headers']['status-code']);
sleep(10);
2022-05-24 02:54:50 +12: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']);
$this->assertCount(2, $base['body']['executions']);
$this->assertEquals('completed', $base['body']['executions'][0]['status']);
$this->assertEquals('completed', $base['body']['executions'][1]['status']);
2022-05-24 02:54:50 +12:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
2021-03-11 09:50:20 +13:00
'content-type' => 'application/json',
2021-03-13 02:49:57 +13:00
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'cursor' => $base['body']['executions'][0]['$id']
2021-03-12 05:42:19 +13:00
]);
2021-03-11 09:50:20 +13:00
2021-03-13 04:01:03 +13:00
$this->assertCount(1, $executions['body']['executions']);
$this->assertEquals($base['body']['executions'][1]['$id'], $executions['body']['executions'][0]['$id']);
2022-05-24 02:54:50 +12:00
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'cursor' => $base['body']['executions'][1]['$id'],
'cursorDirection' => Database::CURSOR_BEFORE
]);
2022-01-29 13:00:25 +13:00
// Cleanup : Delete function
2022-05-24 02:54:50 +12:00
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
2022-01-29 13:00:25 +13: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 15:18:53 +13:00
}
2022-05-24 02:54:50 +12:00
public function testSynchronousExecution(): array
2021-09-13 23:06:50 +12: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,
], [
2021-11-15 15:18:53 +13:00
'functionId' => 'unique()',
2021-09-13 23:06:50 +12:00
'name' => 'Test',
2021-11-15 15:18:53 +13:00
'execute' => ['role:all'],
2021-09-13 23:06:50 +12:00
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'timeout' => 10,
]);
$functionId = $function['body']['$id'] ?? '';
$this->assertEquals(201, $function['headers']['status-code']);
2022-02-20 02:57:48 +13:00
$folder = 'php-fn';
2022-05-24 02:54:50 +12:00
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
2022-02-20 02:57:48 +13:00
$this->packageCode($folder);
2022-05-24 02:54:50 +12:00
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
2021-09-13 23:06:50 +12:00
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'entrypoint' => 'index.php',
2022-02-20 02:57:48 +13:00
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
2021-09-13 23:06:50 +12:00
]);
2022-01-25 12:40:03 +13:00
$deploymentId = $deployment['body']['$id'] ?? '';
2021-09-13 23:06:50 +12:00
2022-01-25 12:40:03 +13:00
$this->assertEquals(201, $deployment['headers']['status-code']);
2021-09-13 23:06:50 +12:00
2022-01-25 12:40:03 +13:00
// Wait for deployment to be built.
2022-02-01 12:44:55 +13:00
sleep(10);
2022-05-24 02:54:50 +12:00
$function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, [
2021-09-13 23:06:50 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], []);
2021-09-13 23:06:50 +12:00
$this->assertEquals(200, $function['headers']['status-code']);
2022-05-24 02:54:50 +12:00
$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
2021-09-13 23:06:50 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
'data' => 'foobar',
2021-12-07 04:04:00 +13:00
'async' => false
2021-09-13 23:06:50 +12:00
]);
$output = json_decode($execution['body']['response'], true);
2021-09-13 23:06:50 +12:00
$this->assertEquals(201, $execution['headers']['status-code']);
$this->assertEquals('completed', $execution['body']['status']);
$this->assertEquals($functionId, $output['APPWRITE_FUNCTION_ID']);
$this->assertEquals('Test', $output['APPWRITE_FUNCTION_NAME']);
2022-01-25 13:36:33 +13:00
$this->assertEquals($deploymentId, $output['APPWRITE_FUNCTION_DEPLOYMENT']);
2021-09-13 23:06:50 +12: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']);
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT']);
$this->assertEquals('', $output['APPWRITE_FUNCTION_EVENT_DATA']);
$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']);
2022-05-24 02:54:50 +12:00
// Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
2022-01-29 13:00:25 +13: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 23:06:50 +12:00
return [];
}
2021-03-11 09:50:20 +13:00
}