2021-08-29 19:50:52 +12:00
|
|
|
<?php
|
|
|
|
|
2021-08-29 20:43:14 +12:00
|
|
|
namespace Tests\E2E\Services\Functions;
|
2021-08-29 19:50:52 +12:00
|
|
|
|
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
|
|
use Tests\E2E\Client;
|
2021-08-29 19:59:08 +12:00
|
|
|
use Tests\E2E\Scopes\SideConsole;
|
2022-08-14 22:33:36 +12:00
|
|
|
use Utopia\Database\ID;
|
2022-08-27 15:16:37 +12:00
|
|
|
use Utopia\Database\Role;
|
2021-08-29 19:50:52 +12:00
|
|
|
|
2021-08-29 20:11:29 +12:00
|
|
|
class FunctionsConsoleClientTest extends Scope
|
2021-08-29 19:50:52 +12:00
|
|
|
{
|
|
|
|
use ProjectCustom;
|
2021-08-29 19:59:08 +12:00
|
|
|
use SideConsole;
|
2021-08-29 19:50:52 +12:00
|
|
|
|
2021-10-27 02:19:28 +13:00
|
|
|
public function testCreateFunction(): array
|
2021-08-29 19:50:52 +12:00
|
|
|
{
|
2021-08-29 19:59:08 +12:00
|
|
|
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
|
2021-08-29 19:50:52 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'functionId' => ID::unique(),
|
2021-08-29 19:59:08 +12:00
|
|
|
'name' => 'Test',
|
2022-08-27 15:16:37 +12:00
|
|
|
'execute' => [Role::user($this->getUser()['$id'])->toString()],
|
2021-08-29 19:59:08 +12:00
|
|
|
'runtime' => 'php-8.0',
|
|
|
|
'events' => [
|
2022-04-14 00:39:31 +12:00
|
|
|
'users.*.create',
|
|
|
|
'users.*.delete',
|
2021-08-29 19:59:08 +12:00
|
|
|
],
|
|
|
|
'schedule' => '0 0 1 1 *',
|
|
|
|
'timeout' => 10,
|
2021-08-29 19:50:52 +12:00
|
|
|
]);
|
|
|
|
|
2021-08-29 19:59:08 +12:00
|
|
|
$this->assertEquals(201, $function['headers']['status-code']);
|
2021-08-29 19:50:52 +12:00
|
|
|
|
2022-07-11 05:25:27 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-14 22:33:36 +12:00
|
|
|
'functionId' => ID::unique(),
|
2022-07-11 05:25:27 +12:00
|
|
|
'name' => 'Test Failure',
|
|
|
|
'execute' => ['some-random-string'],
|
|
|
|
'runtime' => 'php-8.0'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
2021-08-29 19:59:08 +12:00
|
|
|
return [
|
|
|
|
'functionId' => $function['body']['$id']
|
|
|
|
];
|
2021-08-29 19:50:52 +12:00
|
|
|
}
|
2021-10-27 02:19:28 +13:00
|
|
|
|
2021-08-29 19:59:08 +12:00
|
|
|
/**
|
|
|
|
* @depends testCreateFunction
|
|
|
|
*/
|
|
|
|
public function testGetCollectionUsage(array $data)
|
2021-08-29 19:50:52 +12:00
|
|
|
{
|
|
|
|
/**
|
2021-08-29 19:59:08 +12:00
|
|
|
* Test for FAILURE
|
2021-08-29 19:50:52 +12:00
|
|
|
*/
|
|
|
|
|
2021-10-27 02:19:28 +13:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/usage', array_merge([
|
2021-08-29 19:50:52 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
], $this->getHeaders()), [
|
2021-08-29 19:59:08 +12:00
|
|
|
'range' => '232h'
|
2021-08-29 19:50:52 +12:00
|
|
|
]);
|
|
|
|
|
2021-08-29 19:59:08 +12:00
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/randomFunctionId/usage', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'range' => '24h'
|
|
|
|
]);
|
2021-08-29 19:50:52 +12:00
|
|
|
|
2021-08-29 19:59:08 +12:00
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
2021-08-29 19:50:52 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2021-10-27 02:19:28 +13:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/usage', array_merge([
|
2021-08-29 19:50:52 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id']
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'range' => '24h'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($response['headers']['status-code'], 200);
|
2022-08-25 23:33:34 +12:00
|
|
|
$this->assertEquals(count($response['body']), 9);
|
2021-08-29 19:50:52 +12:00
|
|
|
$this->assertEquals($response['body']['range'], '24h');
|
2022-08-25 23:33:34 +12:00
|
|
|
$this->assertIsArray($response['body']['executionsTotal']);
|
|
|
|
$this->assertIsArray($response['body']['executionsFailure']);
|
|
|
|
$this->assertIsArray($response['body']['executionsSuccess']);
|
|
|
|
$this->assertIsArray($response['body']['executionsTime']);
|
|
|
|
$this->assertIsArray($response['body']['buildsTotal']);
|
|
|
|
$this->assertIsArray($response['body']['buildsFailure']);
|
|
|
|
$this->assertIsArray($response['body']['buildsSuccess']);
|
|
|
|
$this->assertIsArray($response['body']['buildsTime']);
|
2021-08-29 19:50:52 +12:00
|
|
|
}
|
2022-08-03 04:11:15 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateFunction
|
|
|
|
*/
|
2022-08-03 21:13:34 +12:00
|
|
|
public function testCreateFunctionVariable(array $data)
|
|
|
|
{
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/variables', array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => 'APP_TEST',
|
|
|
|
'value' => 'TESTINGVALUE'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
2022-08-30 19:53:16 +12:00
|
|
|
$variableId = $response['body']['$id'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/variables', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => 'APP_TEST',
|
|
|
|
'value' => 'ANOTHER_TESTINGVALUE'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(409, $response['headers']['status-code']);
|
2022-08-03 04:11:15 +12:00
|
|
|
|
|
|
|
return array_merge(
|
|
|
|
$data,
|
|
|
|
[
|
2022-08-30 19:53:16 +12:00
|
|
|
'variableId' => $variableId
|
2022-08-03 04:11:15 +12:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
$longKey = str_repeat("A", 256);
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/variables', array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2022-08-30 19:53:16 +12:00
|
|
|
'key' => $longKey,
|
2022-08-03 04:11:15 +12:00
|
|
|
'value' => 'TESTINGVALUE'
|
|
|
|
]);
|
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
2022-08-03 04:11:15 +12:00
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
$longValue = str_repeat("#", 8193);
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/variables', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => 'LONGKEY',
|
|
|
|
'value' => $longValue
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
2022-08-03 04:11:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-30 19:53:16 +12:00
|
|
|
* @depends testCreateFunctionVariable
|
2022-08-03 04:11:15 +12:00
|
|
|
*/
|
2022-08-03 21:13:34 +12:00
|
|
|
public function testListVariables(array $data)
|
|
|
|
{
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/variables', array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals(1, sizeof($response['body']['variables']));
|
2022-09-12 21:02:14 +12:00
|
|
|
$this->assertEquals(1, $response['body']['total']);
|
2022-08-03 04:11:15 +12:00
|
|
|
$this->assertEquals("APP_TEST", $response['body']['variables'][0]['key']);
|
|
|
|
$this->assertEquals("TESTINGVALUE", $response['body']['variables'][0]['value']);
|
2022-08-03 21:13:34 +12:00
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
2022-08-03 04:11:15 +12:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testListVariables
|
|
|
|
*/
|
2022-08-03 21:13:34 +12:00
|
|
|
public function testGetVariable(array $data)
|
|
|
|
{
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals("APP_TEST", $response['body']['key']);
|
|
|
|
$this->assertEquals("TESTINGVALUE", $response['body']['value']);
|
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/variables/NON_EXISTING_VARIABLE', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
2022-08-03 04:11:15 +12:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGetVariable
|
|
|
|
*/
|
2022-08-03 21:13:34 +12:00
|
|
|
public function testUpdateVariable(array $data)
|
|
|
|
{
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => 'APP_TEST_UPDATE',
|
|
|
|
'value' => 'TESTINGVALUEUPDATED'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
2022-08-31 19:33:43 +12:00
|
|
|
$this->assertEquals("APP_TEST_UPDATE", $response['body']['key']);
|
|
|
|
$this->assertEquals("TESTINGVALUEUPDATED", $response['body']['value']);
|
2022-08-03 04:11:15 +12:00
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$variable = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $variable['headers']['status-code']);
|
|
|
|
$this->assertEquals("APP_TEST_UPDATE", $variable['body']['key']);
|
|
|
|
$this->assertEquals("TESTINGVALUEUPDATED", $variable['body']['value']);
|
|
|
|
|
2022-08-31 19:33:43 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => 'APP_TEST_UPDATE_2',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals("APP_TEST_UPDATE_2", $response['body']['key']);
|
|
|
|
$this->assertEquals("TESTINGVALUEUPDATED", $response['body']['value']);
|
|
|
|
|
|
|
|
$variable = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $variable['headers']['status-code']);
|
|
|
|
$this->assertEquals("APP_TEST_UPDATE_2", $variable['body']['key']);
|
|
|
|
$this->assertEquals("TESTINGVALUEUPDATED", $variable['body']['value']);
|
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
2022-08-31 19:33:43 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'value' => 'TESTINGVALUEUPDATED_2'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
$longKey = str_repeat("A", 256);
|
|
|
|
$response = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => $longKey,
|
|
|
|
'value' => 'TESTINGVALUEUPDATED'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
$longValue = str_repeat("#", 8193);
|
|
|
|
$response = $this->client->call(Client::METHOD_PUT, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
'key' => 'APP_TEST_UPDATE',
|
|
|
|
'value' => $longValue
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
2022-08-03 04:11:15 +12:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testUpdateVariable
|
|
|
|
*/
|
2022-08-03 21:13:34 +12:00
|
|
|
public function testDeleteVariable(array $data)
|
|
|
|
{
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for SUCCESS
|
|
|
|
*/
|
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/variables/' . $data['variableId'], array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(204, $response['headers']['status-code']);
|
|
|
|
|
2022-08-10 03:29:24 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/variables', array_merge([
|
2022-08-03 04:11:15 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals(0, sizeof($response['body']['variables']));
|
2022-09-12 21:02:14 +12:00
|
|
|
$this->assertEquals(0, $response['body']['total']);
|
2022-08-03 04:11:15 +12:00
|
|
|
|
2022-08-30 19:53:16 +12:00
|
|
|
/**
|
|
|
|
* Test for FAILURE
|
|
|
|
*/
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/variables/NON_EXISTING_VARIABLE', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
2022-08-03 04:11:15 +12:00
|
|
|
return $data;
|
|
|
|
}
|
2021-10-27 02:19:28 +13:00
|
|
|
}
|