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

452 lines
14 KiB
PHP
Raw Normal View History

2022-07-01 06:58:15 +00:00
<?php
namespace Tests\E2E\Services\GraphQL;
2022-07-07 05:51:20 +00:00
use CURLFile;
2022-07-01 06:58:15 +00:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
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-01 06:58:15 +00:00
2022-09-22 08:29:42 +00:00
class StorageServerTest extends Scope
2022-07-01 06:58:15 +00:00
{
use ProjectCustom;
use SideServer;
2022-09-22 08:29:42 +00:00
use Base;
2022-07-01 06:58:15 +00:00
private static array $cachedBucket = [];
private static array $cachedFile = [];
protected function setupBucket(): array
2022-07-01 06:58:15 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedBucket[$key])) {
return self::$cachedBucket[$key];
}
2022-07-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_BUCKET);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
'bucketId' => ID::unique(),
2022-07-01 06:58:15 +00:00
'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()),
],
2022-07-01 06:58:15 +00:00
]
];
$bucket = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($bucket['body']['data']);
$this->assertArrayNotHasKey('errors', $bucket['body']);
2022-07-04 04:14:37 +00:00
$bucket = $bucket['body']['data']['storageCreateBucket'];
2022-07-01 06:58:15 +00:00
$this->assertEquals('Actors', $bucket['name']);
2026-03-31 16:34:37 +00:00
self::$cachedBucket[$key] = $bucket;
2022-07-01 06:58:15 +00:00
return $bucket;
}
protected function setupFile(): array
2022-07-01 06:58:15 +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-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_FILE);
2022-07-01 06:58:15 +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' => [
2022-09-21 09:03:14 +00:00
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
2022-09-21 08:17:17 +00:00
],
2022-07-07 05:51:20 +00:00
]
]),
'map' => \json_encode([
'file' => ["variables.file"]
]),
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
2022-07-01 06:58:15 +00:00
];
2023-05-30 07:41:06 +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-01 06:58:15 +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();
chore: bump PHPStan to level 4 and fix all new errors Raises `phpstan.neon` level from 3 to 4 and fixes the 549 new errors that level 4 surfaces across 157 files. Fixes are root-cause — no `@phpstan-ignore`, no `@var` casts, no baseline entries, no widened types. A handful of latent bugs were fixed along the way: - `app/controllers/general.php`: path-traversal guard was negating `\substr(...)` before the strict comparison (`!\substr(...) === $base` was always `false === $base`). Rewritten as `\substr(...) !== $base`. - `src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php` and `.../TablesDB/Logs/XList.php`: were importing the raw Matomo `DeviceDetector` (whose `getDevice()` returns `?int`) but treating the result as an array with `deviceName/deviceBrand/deviceModel` keys. Swapped to `Appwrite\Detector\Detector`, matching the wrapper already used a few lines below for `$os`/`$client`. - `src/Appwrite/Platform/Modules/Functions/Workers/Builds.php`: a match key was checking `$resourceKey === 'functions'` when `$resourceKey` is `'functionId'|'siteId'` — always false. Switched to the intended `$resource->getCollection() === 'functions'` check. - `src/Appwrite/OpenSSL/OpenSSL.php`: `encrypt()` return type tightened to `string|false` to match `openssl_encrypt`; this lets callers' `=== false` error handling remain meaningful. - `app/controllers/api/messaging.php`: removed a dead `array_key_exists('from', [])` branch in the Msg91 provider (empty array literal; branch was unreachable). Large cleanup categories across the 549 fixes: - Removed redundant `?? default` on array offsets and expressions that PHPStan now knows are non-nullable. - Removed unreachable statements (mostly `return;` after `throw` or `markTestSkipped()`). - Removed redundant `is_array`/`is_string`/`is_bool`/`instanceof` checks on already-narrowed types. - Added `default =>` arms (or throwing arms) to non-exhaustive matches on `string`/`mixed` input. - Removed dead `$document === false` branches where method return types were tightened to non-nullable `Document`. - Removed unused properties (`$version` on Etsy/Zoom OAuth2, `$paths` on Installer State, `$source` on MigrationsWorker, `$account2` on two GraphQL auth tests), unused traits (`ApiVectorsDB`, `DatabaseFixture`), and an unused `cleanupStaleExecutions` task method. - Replaced `assertTrue(true)` and redundant `assertIsArray`/`assertIsString`/ `assertNotNull` assertions with `addToAssertionCount(1)` or `assertNotEmpty` where the runtime type was already known.
2026-04-19 12:01:20 +00:00
$this->assertNotEmpty($file);
2022-07-01 06:58:15 +00:00
}
public function testGetBuckets(): array
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_BUCKETS);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
];
$buckets = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($buckets['body']['data']);
$this->assertArrayNotHasKey('errors', $buckets['body']);
2022-07-04 04:14:37 +00:00
$buckets = $buckets['body']['data']['storageListBuckets'];
2022-07-01 06:58:15 +00:00
$this->assertIsArray($buckets);
2026-01-07 12:35:33 +00:00
if (!empty($buckets['buckets'])) {
foreach ($buckets['buckets'] as $bucket) {
$this->assertArrayHasKey('totalSize', $bucket);
$this->assertIsInt($bucket['totalSize']);
/* always 0 because the stats worker runs hourly! */
$this->assertGreaterThanOrEqual(0, $bucket['totalSize']);
}
}
2022-07-01 06:58:15 +00:00
return $buckets;
}
/**
* @return array
* @throws \Exception
*/
public function testGetBucket(): array
2022-07-01 06:58:15 +00:00
{
$bucket = $this->setupBucket();
$this->setupFile(); // Ensure file exists
2022-07-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_BUCKET);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
2022-07-01 06:58:15 +00:00
]
];
$bucket = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($bucket['body']['data']);
$this->assertArrayNotHasKey('errors', $bucket['body']);
$bucket = $bucket['body']['data']['storageGetBucket'];
$this->assertEquals('Actors', $bucket['name']);
2026-01-03 10:12:55 +00:00
$this->assertArrayHasKey('totalSize', $bucket);
2022-07-01 06:58:15 +00:00
return $bucket;
}
/**
* @return array
* @throws \Exception
*/
public function testGetFiles(): array
2022-07-01 06:58:15 +00:00
{
$bucket = $this->setupBucket();
2022-07-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILES);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
2022-07-01 06:58:15 +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']);
2022-07-04 04:14:37 +00:00
$files = $files['body']['data']['storageListFiles'];
2022-07-01 06:58:15 +00:00
$this->assertIsArray($files);
return $files;
}
/**
* @return array
* @throws \Exception
*/
public function testGetFile()
2022-07-01 06:58:15 +00:00
{
$bucket = $this->setupBucket();
$file = $this->setupFile();
2022-07-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_FILE);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
'fileId' => $file['_id'],
2022-07-01 06:58:15 +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-01 06:58:15 +00:00
}
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']));
return $file;
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
}
2022-07-01 06:58:15 +00:00
/**
* @return array
* @throws \Exception
*/
public function testUpdateBucket(): array
2022-07-01 06:58:15 +00:00
{
$bucket = $this->setupBucket();
2022-07-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_BUCKET);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'bucketId' => $bucket['_id'],
2022-07-01 06:58:15 +00:00
'name' => 'Actors Updated',
2022-09-21 08:17:17 +00:00
'fileSecurity' => false,
2022-07-01 06:58:15 +00:00
]
];
$bucket = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsArray($bucket['body']['data']);
$this->assertArrayNotHasKey('errors', $bucket['body']);
$bucket = $bucket['body']['data']['storageUpdateBucket'];
$this->assertEquals('Actors Updated', $bucket['name']);
return $bucket;
}
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;
}
2022-07-01 06:58:15 +00:00
/**
* @throws \Exception
*/
public function testDeleteFile(): void
2022-07-01 06:58:15 +00:00
{
$file = $this->setupFile();
2022-07-01 06:58:15 +00:00
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_FILE);
2022-07-01 06:58:15 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-07-07 05:51:20 +00:00
'bucketId' => $file['bucketId'],
2022-12-08 03:08:57 +00:00
'fileId' => $file['_id'],
2022-07-01 06:58:15 +00:00
]
];
2022-07-07 05:51:20 +00:00
$file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-01 06:58:15 +00:00
'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
}
2022-07-01 06:58:15 +00:00
2022-07-04 04:14:37 +00:00
/**
* @return void
2022-07-04 04:14:37 +00:00
* @throws \Exception
*/
public function testDeleteBucket(): void
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::DELETE_BUCKET);
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
]
];
2022-07-07 05:51:20 +00:00
$bucket = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
2022-07-04 04:14:37 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
2022-07-18 09:38:13 +00:00
$this->assertIsNotArray($bucket['body']);
2022-07-07 05:51:20 +00:00
$this->assertEquals(204, $bucket['headers']['status-code']);
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedBucket[$key] = [];
self::$cachedFile[$key] = [];
2022-07-01 06:58:15 +00:00
}
2022-07-06 03:51:37 +00:00
}