1
0
Fork 0
mirror of synced 2024-09-09 14:21:24 +12:00
appwrite/tests/e2e/Services/GraphQL/FunctionsServerTest.php

492 lines
16 KiB
PHP
Raw Normal View History

2022-07-04 16:14:37 +12:00
<?php
namespace Tests\E2E\Services\GraphQL;
2022-07-06 20:56:58 +12:00
use CURLFile;
2022-07-04 16:14:37 +12:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
2023-01-16 22:25:40 +13:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Role;
2022-07-04 16:14:37 +12:00
2022-09-22 20:29:42 +12:00
class FunctionsServerTest extends Scope
2022-07-04 16:14:37 +12:00
{
use ProjectCustom;
use SideServer;
2022-09-22 20:29:42 +12:00
use Base;
2022-07-04 16:14:37 +12:00
public function testCreateFunction(): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_FUNCTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-09-22 13:53:41 +12:00
'functionId' => ID::unique(),
2022-07-04 16:14:37 +12:00
'name' => 'Test Function',
2023-08-21 05:20:37 +12:00
'entrypoint' => 'index.php',
2022-09-22 14:16:13 +12:00
'runtime' => 'php-8.0',
'execute' => [Role::any()->toString()],
2022-07-04 16:14:37 +12:00
]
];
$function = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($function['body']['data']);
$this->assertArrayNotHasKey('errors', $function['body']);
2022-09-22 14:16:13 +12:00
2022-07-04 16:14:37 +12:00
$function = $function['body']['data']['functionsCreate'];
2022-12-08 16:08:57 +13:00
$functionId = $function['_id'];
2022-09-22 14:16:13 +12:00
$query = '
mutation createVariables($functionId: String!) {
var1: functionsCreateVariable(functionId: $functionId, key: "name", value: "John Doe") {
2022-12-08 16:08:57 +13:00
_id
2022-09-22 14:16:13 +12:00
}
var2: functionsCreateVariable(functionId: $functionId, key: "age", value: "42") {
2022-12-08 16:08:57 +13:00
_id
2022-09-22 14:16:13 +12:00
}
}
';
$gqlPayload = [
'query' => $query,
'variables' => [
'functionId' => $functionId,
]
];
$variables = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
], $gqlPayload);
$this->assertIsArray($variables['body']['data']);
$this->assertArrayNotHasKey('errors', $variables['body']);
2022-07-04 16:14:37 +12:00
return $function;
}
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testCreateDeployment($function): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_DEPLOYMENT);
2023-10-27 03:50:35 +13:00
$folder = 'php';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);
2022-07-04 16:14:37 +12:00
$gqlPayload = [
2022-07-07 17:50:49 +12:00
'operations' => \json_encode([
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-07 17:50:49 +12:00
'activate' => true,
'code' => null,
]
]),
'map' => \json_encode([
'code' => ["variables.code"]
]),
'code' => new CURLFile($code, 'application/gzip', 'code.tar.gz'),
2022-07-04 16:14:37 +12:00
];
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-07 17:50:49 +12:00
'content-type' => 'multipart/form-data',
2022-07-04 16:14:37 +12:00
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
2023-08-29 13:11:57 +12:00
// Poll get deployment until an error, or status is either 'ready' or 'failed'
$deployment = $deployment['body']['data']['functionsCreateDeployment'];
$deploymentId = $deployment['_id'];
2022-07-07 17:50:49 +12:00
2023-08-29 13:11:57 +12:00
$query = $this->getQuery(self::$GET_DEPLOYMENT);
$gqlPayload = [
'query' => $query,
'variables' => [
'functionId' => $function['_id'],
'deploymentId' => $deploymentId,
]
];
while (true) {
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
$deployment = $deployment['body']['data']['functionsGetDeployment'];
if (
$deployment['status'] === 'ready'
|| $deployment['status'] === 'failed'
) {
break;
}
\sleep(1);
}
$this->assertEquals('ready', $deployment['status']);
return $deployment;
2022-07-04 16:14:37 +12:00
}
/**
* @depends testCreateDeployment
2023-08-29 13:11:57 +12:00
* @param $deployment
2022-07-04 16:14:37 +12:00
* @return array
* @throws \Exception
*/
2023-08-29 13:11:57 +12:00
public function testCreateExecution($deployment): array
2022-07-04 16:14:37 +12:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_EXECUTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 13:11:57 +12:00
'functionId' => $deployment['resourceId'],
2022-07-04 16:14:37 +12:00
]
];
$execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($execution['body']['data']);
$this->assertArrayNotHasKey('errors', $execution['body']);
2022-07-07 17:50:49 +12:00
return $execution['body']['data']['functionsCreateExecution'];
2022-07-04 16:14:37 +12:00
}
/**
2022-07-07 17:50:49 +12:00
* @depends testGetDeployment
2022-07-04 16:14:37 +12:00
* @param $deployment
2023-08-29 13:11:57 +12:00
* @return void
2022-07-04 16:14:37 +12:00
* @throws \Exception
*/
2023-08-29 13:11:57 +12:00
public function testCreateRetryBuild($deployment): void
2022-07-04 16:14:37 +12:00
{
$projectId = $this->getProject()['$id'];
2022-07-04 18:16:06 +12:00
$query = $this->getQuery(self::$RETRY_BUILD);
2022-07-04 16:14:37 +12:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 13:11:57 +12:00
'functionId' => $deployment['resourceId'],
2022-12-08 16:08:57 +13:00
'deploymentId' => $deployment['_id'],
2022-07-07 17:50:49 +12:00
'buildId' => $deployment['buildId'],
2022-07-04 16:14:37 +12:00
]
];
2023-08-29 13:11:57 +12:00
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-04 16:14:37 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2023-08-29 13:11:57 +12:00
$this->assertIsNotArray($response['body']);
$this->assertEquals(204, $response['headers']['status-code']);
2022-07-04 16:14:37 +12:00
}
public function testGetFunctions(): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FUNCTIONS);
$gqlPayload = [
'query' => $query,
];
$functions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($functions['body']['data']);
$this->assertArrayNotHasKey('errors', $functions['body']);
$functions = $functions['body']['data']['functionsList'];
$this->assertIsArray($functions);
return $functions;
}
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testGetFunction($function): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FUNCTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-04 16:14:37 +12:00
]
];
$function = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($function['body']['data']);
$this->assertArrayNotHasKey('errors', $function['body']);
$function = $function['body']['data']['functionsGet'];
$this->assertIsArray($function);
return $function;
}
public function testGetRuntimes(): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_RUNTIMES);
$gqlPayload = [
'query' => $query,
];
$runtimes = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($runtimes['body']['data']);
$this->assertArrayNotHasKey('errors', $runtimes['body']);
$runtimes = $runtimes['body']['data']['functionsListRuntimes'];
$this->assertIsArray($runtimes);
return $runtimes;
}
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testGetDeployments($function)
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_DEPLOYMENTS);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-04 16:14:37 +12:00
]
];
$deployments = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployments['body']['data']);
$this->assertArrayNotHasKey('errors', $deployments['body']);
$deployments = $deployments['body']['data']['functionsListDeployments'];
$this->assertIsArray($deployments);
return $deployments;
}
2022-07-07 17:50:49 +12:00
/**
* @depends testCreateDeployment
2023-08-29 13:11:57 +12:00
* @param $deployment
2022-07-07 17:50:49 +12:00
* @return array
* @throws \Exception
*/
2023-08-29 13:11:57 +12:00
public function testGetDeployment($deployment)
2022-07-07 17:50:49 +12:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_DEPLOYMENT);
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 13:11:57 +12:00
'functionId' => $deployment['resourceId'],
2022-12-08 16:08:57 +13:00
'deploymentId' => $deployment['_id'],
2022-07-07 17:50:49 +12:00
]
];
$deployment = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($deployment['body']['data']);
$this->assertArrayNotHasKey('errors', $deployment['body']);
$deployment = $deployment['body']['data']['functionsGetDeployment'];
$this->assertIsArray($deployment);
return $deployment;
}
2022-07-04 16:14:37 +12:00
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testGetExecutions($function): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_EXECUTIONS);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-04 16:14:37 +12:00
]
];
$executions = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($executions['body']['data']);
$this->assertArrayNotHasKey('errors', $executions['body']);
$executions = $executions['body']['data']['functionsListExecutions'];
$this->assertIsArray($executions);
return $executions;
}
/**
* @depends testCreateExecution
* @param $execution
* @return array
* @throws \Exception
*/
2023-08-29 13:11:57 +12:00
public function testGetExecution($execution): array
2022-07-04 16:14:37 +12:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_EXECUTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 13:11:57 +12:00
'functionId' => $execution['functionId'],
2022-12-08 16:08:57 +13:00
'executionId' => $execution['_id'],
2022-07-04 16:14:37 +12:00
]
];
$execution = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($execution['body']['data']);
$this->assertArrayNotHasKey('errors', $execution['body']);
$execution = $execution['body']['data']['functionsGetExecution'];
$this->assertIsArray($execution);
return $execution;
}
/**
* @depends testCreateFunction
* @param $function
* @return array
* @throws \Exception
*/
public function testUpdateFunction($function): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$UPDATE_FUNCTION);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'functionId' => $function['_id'],
2022-07-04 16:14:37 +12:00
'name' => 'Test Function Updated',
2022-09-22 14:16:13 +12:00
'execute' => [Role::any()->toString()],
2023-08-21 05:40:44 +12:00
'entrypoint' => 'index.php',
'runtime' => 'php-8.0',
2022-07-04 16:14:37 +12:00
'vars' => [
'name' => 'John Doe',
'age' => 42,
],
]
];
$function = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($function['body']['data']);
$this->assertArrayNotHasKey('errors', $function['body']);
$function = $function['body']['data']['functionsUpdate'];
$this->assertIsArray($function);
return $function;
}
/**
2022-07-07 17:50:49 +12:00
* @depends testCreateDeployment
* @param $deployment
2022-07-04 16:14:37 +12:00
* @throws \Exception
*/
2023-08-29 13:11:57 +12:00
public function testDeleteDeployment($deployment): array
2022-07-04 16:14:37 +12:00
{
$projectId = $this->getProject()['$id'];
2022-07-07 17:50:49 +12:00
$query = $this->getQuery(self::$DELETE_DEPLOYMENT);
2022-07-04 16:14:37 +12:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 13:11:57 +12:00
'functionId' => $deployment['resourceId'],
2022-12-08 16:08:57 +13:00
'deploymentId' => $deployment['_id'],
2022-07-04 16:14:37 +12:00
]
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-18 21:38:13 +12:00
$this->assertIsNotArray($response['body']);
2022-07-13 23:21:02 +12:00
$this->assertEquals(204, $response['headers']['status-code']);
2023-08-29 13:11:57 +12:00
return $deployment;
2022-07-04 16:14:37 +12:00
}
/**
2022-07-07 17:50:49 +12:00
* @depends testDeleteDeployment
2023-08-29 13:11:57 +12:00
* @param $deployment
2022-07-04 16:14:37 +12:00
* @throws \Exception
*/
2023-08-29 13:11:57 +12:00
public function testDeleteFunction($deployment): void
2022-07-04 16:14:37 +12:00
{
$projectId = $this->getProject()['$id'];
2022-07-07 17:50:49 +12:00
$query = $this->getQuery(self::$DELETE_FUNCTION);
2022-07-04 16:14:37 +12:00
$gqlPayload = [
'query' => $query,
'variables' => [
2023-08-29 13:11:57 +12:00
'functionId' => $deployment['resourceId'],
2022-07-04 16:14:37 +12:00
]
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-18 21:38:13 +12:00
$this->assertIsNotArray($response['body']);
2022-07-13 23:21:02 +12:00
$this->assertEquals(204, $response['headers']['status-code']);
2022-07-04 16:14:37 +12:00
}
2022-07-06 15:51:37 +12:00
}