appwrite/tests/e2e/Services/GraphQL/StorageClientTest.php

325 lines
9.6 KiB
PHP
Raw Normal View History

2022-07-04 04:14:37 +00:00
<?php
namespace Tests\E2E\Services\GraphQL;
2022-07-07 05:51:20 +00:00
use CURLFile;
2022-07-04 04:14:37 +00:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
2023-01-16 09:25:40 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2022-07-04 04:14:37 +00:00
2022-09-22 08:29:42 +00:00
class StorageClientTest extends Scope
2022-07-04 04:14:37 +00:00
{
use ProjectCustom;
use SideClient;
2022-09-22 08:29:42 +00:00
use Base;
2022-07-04 04:14:37 +00:00
private static array $cachedBucket = [];
private static array $cachedFile = [];
protected function setupBucket(): array
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedBucket[$key])) {
return self::$cachedBucket[$key];
}
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_BUCKET);
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => ID::unique(),
'name' => 'Actors',
2022-09-21 08:17:17 +00: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']);
2026-03-31 16:34:37 +00:00
self::$cachedBucket[$key] = $bucket;
return $bucket;
}
protected function setupFile(): array
2022-07-04 04:14:37 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedFile[$key])) {
return self::$cachedFile[$key];
}
$bucket = $this->setupBucket();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_FILE);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
2022-07-07 05:51:20 +00:00
'operations' => \json_encode([
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
2022-09-22 01:53:41 +00:00
'fileId' => ID::unique(),
2022-07-07 05:51:20 +00:00
'file' => null,
2022-09-21 08:17:17 +00:00
'fileSecurity' => true,
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-07-07 05:51:20 +00:00
]
]),
'map' => \json_encode([
2022-12-13 02:43:29 +00:00
'0' => ["variables.file"]
2022-07-07 05:51:20 +00:00
]),
2022-12-13 02:43:29 +00:00
'0' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
2022-07-04 04:14:37 +00:00
];
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-07 05:51:20 +00:00
'content-type' => 'multipart/form-data',
2022-07-04 04:14:37 +00:00
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($file['body']['data']);
$this->assertArrayNotHasKey('errors', $file['body']);
2026-03-31 16:34:37 +00:00
self::$cachedFile[$key] = $file['body']['data']['storageCreateFile'];
return self::$cachedFile[$key];
}
public function testCreateBucket(): void
{
$bucket = $this->setupBucket();
$this->assertEquals('Actors', $bucket['name']);
}
public function testCreateFile(): void
{
$file = $this->setupFile();
$this->assertIsArray($file);
2022-07-04 04:14:37 +00:00
}
/**
* @return array
* @throws \Exception
*/
public function testGetFiles(): array
2022-07-04 04:14:37 +00:00
{
$bucket = $this->setupBucket();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILES);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
2022-07-04 04:14:37 +00: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;
}
/**
* @return array
* @throws \Exception
*/
public function testGetFile()
2022-07-04 04:14:37 +00:00
{
$bucket = $this->setupBucket();
$file = $this->setupFile();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILE);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
'fileId' => $file['_id'],
2022-07-04 04:14:37 +00: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 05:51:20 +00:00
return $file['body']['data']['storageGetFile'];
2022-07-04 04:14:37 +00:00
}
/**
* @return array
* @throws \Exception
*/
public function testGetFilePreview()
2022-07-04 04:14:37 +00:00
{
$file = $this->setupFile();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILE_PREVIEW);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 03:08:57 +00:00
'fileId' => $file['_id'],
2022-07-04 04:14:37 +00: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 05:51:20 +00:00
$this->assertEquals(46719, \strlen($file['body']));
2022-07-04 04:14:37 +00:00
return $file;
}
/**
2024-10-08 07:54:40 +00:00
* @return array
2022-07-04 04:14:37 +00:00
* @throws \Exception
*/
public function testGetFileDownload()
2022-07-04 04:14:37 +00:00
{
$file = $this->setupFile();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILE_DOWNLOAD);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 03:08:57 +00:00
'fileId' => $file['_id'],
2022-07-04 04:14:37 +00: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 05:51:20 +00:00
$this->assertEquals(47218, \strlen($file['body']));
2022-07-04 04:14:37 +00:00
}
/**
* @throws \Exception
*/
public function testGetFileView(): void
2022-07-04 04:14:37 +00:00
{
$file = $this->setupFile();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILE_VIEW);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 03:08:57 +00:00
'fileId' => $file['_id'],
2022-07-04 04:14:37 +00: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 05:51:20 +00:00
$this->assertEquals(47218, \strlen($file['body']));
2022-07-04 04:14:37 +00:00
}
/**
* @return array
* @throws \Exception
*/
public function testUpdateFile(): array
2022-07-04 04:14:37 +00:00
{
$file = $this->setupFile();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_FILE);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 03:08:57 +00:00
'fileId' => $file['_id'],
2022-09-21 08:17:17 +00:00
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-07-04 04:14:37 +00: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;
}
/**
* @throws \Exception
*/
public function testDeleteFile(): void
2022-07-04 04:14:37 +00:00
{
$file = $this->setupFile();
2022-07-04 04:14:37 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_FILE);
2022-07-04 04:14:37 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => $file['bucketId'],
2022-12-08 03:08:57 +00:00
'fileId' => $file['_id'],
2022-07-04 04:14:37 +00: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 09:38:13 +00:00
$this->assertIsNotArray($file['body']);
$this->assertEquals(204, $file['headers']['status-code']);
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedFile[$key] = [];
2022-07-04 04:14:37 +00:00
}
}