2022-07-13 15:50:25 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\E2E\Services\GraphQL;
|
|
|
|
|
|
|
|
use CURLFile;
|
|
|
|
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\Permission;
|
|
|
|
use Utopia\Database\Helpers\Role;
|
2022-07-13 15:50:25 +12:00
|
|
|
|
2022-09-22 20:29:42 +12:00
|
|
|
class ContentTypeTest extends Scope
|
2022-07-13 15:50:25 +12:00
|
|
|
{
|
|
|
|
use ProjectCustom;
|
|
|
|
use SideServer;
|
2022-09-22 20:29:42 +12:00
|
|
|
use Base;
|
2022-07-13 15:50:25 +12:00
|
|
|
|
|
|
|
public function testGraphQLContentType()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-09-21 19:03:28 +12:00
|
|
|
$query = 'query { localeListCountries { total countries { code } } }';
|
2022-07-13 15:50:25 +12:00
|
|
|
$graphQLPayload = [$query]; // Needs to be an array because the test client expects it
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/graphql',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($response['body']['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $response['body']);
|
2022-09-21 19:03:28 +12:00
|
|
|
$response = $response['body']['data']['localeListCountries'];
|
2022-07-13 15:50:25 +12:00
|
|
|
$this->assertEquals(194, $response['total']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSingleQueryJSONContentType()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-09-21 19:03:28 +12:00
|
|
|
$query = 'query { localeListCountries { total countries { code } } }';
|
2022-07-13 15:50:25 +12:00
|
|
|
$graphQLPayload = ['query' => $query];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($response['body']['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $response['body']);
|
2022-09-21 19:03:28 +12:00
|
|
|
$response = $response['body']['data']['localeListCountries'];
|
2022-07-13 15:50:25 +12:00
|
|
|
$this->assertEquals(194, $response['total']);
|
|
|
|
}
|
|
|
|
|
2022-07-14 20:11:39 +12:00
|
|
|
public function testArrayBatchedJSONContentType()
|
2022-07-13 15:50:25 +12:00
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-09-21 19:03:28 +12:00
|
|
|
$query1 = 'query { localeListCountries { total countries { code } } }';
|
|
|
|
$query2 = 'query { localeListContinents { total continents { code } } }';
|
2022-07-13 15:50:25 +12:00
|
|
|
$graphQLPayload = [
|
|
|
|
['query' => $query1],
|
|
|
|
['query' => $query2],
|
|
|
|
];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $graphQLPayload);
|
|
|
|
|
2022-10-14 14:33:42 +13:00
|
|
|
$this->assertIsArray($response['body'][0]['data']);
|
|
|
|
$this->assertIsArray($response['body'][1]['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $response['body'][0]);
|
|
|
|
$this->assertArrayNotHasKey('errors', $response['body'][1]);
|
|
|
|
$this->assertArrayHasKey('localeListCountries', $response['body'][0]['data']);
|
|
|
|
$this->assertArrayHasKey('localeListContinents', $response['body'][1]['data']);
|
|
|
|
$this->assertEquals(194, $response['body'][0]['data']['localeListCountries']['total']);
|
|
|
|
$this->assertEquals(7, $response['body'][1]['data']['localeListContinents']['total']);
|
2022-07-14 20:11:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testQueryBatchedJSONContentType()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = '
|
|
|
|
query {
|
2022-09-21 19:03:28 +12:00
|
|
|
localeListCountries { total countries { code } }
|
|
|
|
localeListContinents { total continents { code } }
|
2022-07-14 20:11:39 +12:00
|
|
|
}
|
|
|
|
';
|
|
|
|
$graphQLPayload = [
|
|
|
|
['query' => $query],
|
|
|
|
];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $graphQLPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($response['body']['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $response['body']);
|
2022-09-21 19:03:28 +12:00
|
|
|
$this->assertArrayHasKey('localeListCountries', $response['body']['data']);
|
|
|
|
$this->assertArrayHasKey('localeListContinents', $response['body']['data']);
|
|
|
|
$this->assertEquals(194, $response['body']['data']['localeListCountries']['total']);
|
|
|
|
$this->assertEquals(7, $response['body']['data']['localeListContinents']['total']);
|
2022-07-13 15:50:25 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipartFormDataContentType()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
|
|
|
|
$query = $this->getQuery(self::$CREATE_BUCKET);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-09-22 13:53:41 +12:00
|
|
|
'bucketId' => ID::unique(),
|
2022-07-13 15:50:25 +12:00
|
|
|
'name' => 'Test Bucket',
|
2022-09-21 20:17:17 +12:00
|
|
|
'fileSecurity' => false,
|
|
|
|
'permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
],
|
2023-08-24 08:23:52 +12:00
|
|
|
]
|
2022-07-13 15:50:25 +12:00
|
|
|
];
|
|
|
|
$bucket = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
2022-07-13 17:21:41 +12:00
|
|
|
|
2022-07-13 15:50:25 +12:00
|
|
|
$bucket = $bucket['body']['data']['storageCreateBucket'];
|
2022-07-13 17:21:41 +12:00
|
|
|
|
2022-07-13 15:50:25 +12:00
|
|
|
$query = $this->getQuery(self::$CREATE_FILE);
|
|
|
|
$gqlPayload = [
|
|
|
|
'operations' => \json_encode([
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'bucketId' => $bucket['_id'],
|
2022-09-22 13:53:41 +12:00
|
|
|
'fileId' => ID::unique(),
|
2022-07-13 15:50:25 +12:00
|
|
|
'file' => null,
|
2022-09-21 20:17:17 +12:00
|
|
|
'fileSecurity' => true,
|
|
|
|
'permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
],
|
2023-08-24 08:23:52 +12:00
|
|
|
]
|
2022-07-13 15:50:25 +12:00
|
|
|
]),
|
|
|
|
'map' => \json_encode([
|
2023-08-24 08:23:52 +12:00
|
|
|
'file' => ["variables.file"]
|
2022-07-13 15:50:25 +12:00
|
|
|
]),
|
2023-08-24 08:23:52 +12:00
|
|
|
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
|
2022-07-13 15:50:25 +12:00
|
|
|
];
|
|
|
|
|
|
|
|
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'multipart/form-data',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($file['body']['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $file['body']);
|
|
|
|
$this->assertIsArray($file['body']['data']['storageCreateFile']);
|
|
|
|
}
|
2022-07-14 15:58:17 +12:00
|
|
|
|
2022-07-19 14:11:58 +12:00
|
|
|
public function testPostNoBody()
|
2022-07-14 15:58:17 +12:00
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
2022-10-14 13:18:08 +13:00
|
|
|
$this->assertEquals('Param "query" is not optional.', $response['body']['message']);
|
2022-07-14 15:58:17 +12:00
|
|
|
}
|
|
|
|
|
2022-07-19 14:11:58 +12:00
|
|
|
public function testPostEmptyBody()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), []);
|
|
|
|
|
2022-10-14 13:18:08 +13:00
|
|
|
$this->assertEquals('Param "query" is not optional.', $response['body']['message']);
|
2022-07-19 14:11:58 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPostRandomBody()
|
2022-07-14 15:58:17 +12:00
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), ['foo' => 'bar']);
|
|
|
|
|
2022-10-14 13:18:08 +13:00
|
|
|
$this->assertEquals('Param "query" is not optional.', $response['body']['message']);
|
2022-07-14 15:58:17 +12:00
|
|
|
}
|
2022-07-19 14:11:58 +12:00
|
|
|
|
|
|
|
public function testGetNoQuery()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/graphql', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()));
|
2022-07-20 01:29:15 +12:00
|
|
|
|
2022-10-14 13:18:08 +13:00
|
|
|
$this->assertEquals('Param "query" is not optional.', $response['body']['message']);
|
2022-07-19 14:11:58 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetEmptyQuery()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/graphql?query=', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
2022-10-14 13:18:08 +13:00
|
|
|
$this->assertEquals('Param "query" is not optional.', $response['body']['message']);
|
2022-07-19 14:11:58 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRandomParameters()
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2022-10-14 13:18:08 +13:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, '/graphql?random=random', \array_merge([
|
2022-07-19 14:11:58 +12:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
2022-10-14 13:18:08 +13:00
|
|
|
$this->assertEquals('Param "query" is not optional.', $response['body']['message']);
|
2022-07-19 14:11:58 +12:00
|
|
|
}
|
2022-07-13 17:21:41 +12:00
|
|
|
}
|