1
0
Fork 0
mirror of synced 2024-08-26 23:51:39 +12:00
appwrite/tests/e2e/Services/GraphQL/StorageClientTest.php

297 lines
9 KiB
PHP
Raw Normal View History

2022-07-04 16:14:37 +12:00
<?php
namespace Tests\E2E\Services\GraphQL;
2022-07-07 17:51:20 +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\SideClient;
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-04 16:14:37 +12:00
2022-09-22 20:29:42 +12:00
class StorageClientTest extends Scope
2022-07-04 16:14:37 +12:00
{
use ProjectCustom;
use SideClient;
2022-09-22 20:29:42 +12:00
use Base;
2022-07-04 16:14:37 +12:00
public function testCreateBucket(): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_BUCKET);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => 'actors',
'name' => 'Actors',
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()),
],
]
];
$bucket = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
], $gqlPayload);
$this->assertIsArray($bucket['body']['data']);
$this->assertArrayNotHasKey('errors', $bucket['body']);
$bucket = $bucket['body']['data']['storageCreateBucket'];
$this->assertEquals('Actors', $bucket['name']);
return $bucket;
}
/**
* @depends testCreateBucket
*/
2022-07-04 16:14:37 +12:00
public function testCreateFile($bucket): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_FILE);
$gqlPayload = [
2022-07-07 17:51:20 +12:00
'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-07 17:51:20 +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()),
],
2022-07-07 17:51:20 +12:00
]
]),
'map' => \json_encode([
2022-12-13 15:43:29 +13:00
'0' => ["variables.file"]
2022-07-07 17:51:20 +12:00
]),
2022-12-13 15:43:29 +13:00
'0' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
2022-07-04 16:14:37 +12:00
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-07 17:51:20 +12:00
'content-type' => 'multipart/form-data',
2022-07-04 16:14:37 +12:00
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($file['body']['data']);
$this->assertArrayNotHasKey('errors', $file['body']);
2022-07-07 17:51:20 +12:00
return $file['body']['data']['storageCreateFile'];
2022-07-04 16:14:37 +12:00
}
/**
* @depends testCreateBucket
* @param $bucket
* @return array
* @throws \Exception
*/
public function testGetFiles($bucket): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FILES);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'bucketId' => $bucket['_id'],
2022-07-04 16:14:37 +12:00
]
];
$files = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($files['body']['data']);
$this->assertArrayNotHasKey('errors', $files['body']);
$files = $files['body']['data']['storageListFiles'];
$this->assertIsArray($files);
return $files;
}
/**
2022-07-07 17:51:20 +12:00
* @depends testCreateBucket
2022-07-04 16:14:37 +12:00
* @depends testCreateFile
2022-07-07 17:51:20 +12:00
* @param $bucket
2022-07-04 16:14:37 +12:00
* @param $file
* @return array
* @throws \Exception
*/
public function testGetFile($bucket, $file)
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FILE);
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 16:08:57 +13:00
'bucketId' => $bucket['_id'],
'fileId' => $file['_id'],
2022-07-04 16:14:37 +12:00
]
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($file['body']['data']);
$this->assertArrayNotHasKey('errors', $file['body']);
2022-07-07 17:51:20 +12:00
return $file['body']['data']['storageGetFile'];
2022-07-04 16:14:37 +12:00
}
/**
* @depends testCreateFile
* @param $file
* @return array
* @throws \Exception
*/
public function testGetFilePreview($file)
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FILE_PREVIEW);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 16:08:57 +13:00
'fileId' => $file['_id'],
2022-07-04 16:14:37 +12:00
'width' => 100,
'height' => 100,
]
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-07 17:51:20 +12:00
$this->assertEquals(46719, \strlen($file['body']));
2022-07-04 16:14:37 +12:00
return $file;
}
/**
* @depends testCreateFile
* @param $file
* @return array
* @throws \Exception
*/
public function testGetFileDownload($file)
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FILE_DOWNLOAD);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 16:08:57 +13:00
'fileId' => $file['_id'],
2022-07-04 16:14:37 +12:00
]
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-07 17:51:20 +12:00
$this->assertEquals(47218, \strlen($file['body']));
2022-07-04 16:14:37 +12:00
}
/**
* @depends testCreateFile
* @param $file
* @return array
* @throws \Exception
*/
2022-07-07 17:51:20 +12:00
public function testGetFileView($file): void
2022-07-04 16:14:37 +12:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_FILE_VIEW);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 16:08:57 +13:00
'fileId' => $file['_id'],
2022-07-04 16:14:37 +12:00
]
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-07 17:51:20 +12:00
$this->assertEquals(47218, \strlen($file['body']));
2022-07-04 16:14:37 +12:00
}
/**
* @depends testCreateFile
* @param $file
* @return array
* @throws \Exception
*/
public function testUpdateFile($file): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$UPDATE_FILE);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 16:08:57 +13:00
'fileId' => $file['_id'],
2022-09-21 20:17:17 +12:00
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-07-04 16:14:37 +12:00
]
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($file['body']['data']);
$this->assertArrayNotHasKey('errors', $file['body']);
$file = $file['body']['data']['storageUpdateFile'];
$this->assertIsArray($file);
return $file;
}
/**
* @depends testCreateFile
* @param $file
* @throws \Exception
*/
public function testDeleteFile($file): void
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$DELETE_FILE);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 16:08:57 +13:00
'fileId' => $file['_id'],
2022-07-04 16:14:37 +12:00
]
];
$file = $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($file['body']);
$this->assertEquals(204, $file['headers']['status-code']);
2022-07-04 16:14:37 +12:00
}
}