appwrite/tests/e2e/Services/Storage/StorageConsoleClientTest.php

200 lines
8.6 KiB
PHP
Raw Normal View History

2020-01-12 21:28:26 +00:00
<?php
namespace Tests\E2E\Services\Storage;
use CURLFile;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
2024-03-06 17:34:21 +00:00
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideConsole;
2023-02-05 20:07:46 +00:00
use Utopia\Database\Helpers\ID;
2020-01-12 21:28:26 +00:00
class StorageConsoleClientTest extends Scope
{
use SideConsole;
2020-01-12 21:28:26 +00:00
use StorageBase;
use ProjectCustom;
public function testGetStorageUsage()
{
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_GET, '/storage/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'range' => '32h'
]);
$this->assertEquals($response['headers']['status-code'], 400);
2021-10-26 13:19:28 +00:00
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_GET, '/storage/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'range' => '24h'
]);
2023-11-26 20:57:57 +00:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(7, count($response['body']));
$this->assertEquals('24h', $response['body']['range']);
$this->assertIsNumeric($response['body']['bucketsTotal']);
$this->assertIsNumeric($response['body']['filesTotal']);
$this->assertIsNumeric($response['body']['filesStorageTotal']);
$this->assertIsArray($response['body']['buckets']);
$this->assertIsArray($response['body']['files']);
$this->assertIsArray($response['body']['storage']);
}
public function testGetStorageBucketUsage()
{
2021-09-13 08:18:40 +00:00
//create bucket
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2022-08-14 10:33:36 +00:00
'bucketId' => ID::unique(),
2021-09-13 08:18:40 +00:00
'name' => 'Test Bucket',
2021-10-17 07:12:59 +00:00
'permission' => 'file'
2021-09-13 08:18:40 +00:00
]);
$this->assertEquals(201, $bucket['headers']['status-code']);
$bucketId = $bucket['body']['$id'];
/**
* Test for FAILURE
*/
2021-09-13 08:18:40 +00:00
$response = $this->client->call(Client::METHOD_GET, '/storage/' . $bucketId . '/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'range' => '32h'
]);
2023-11-26 20:57:57 +00:00
$this->assertEquals(400, $response['headers']['status-code']);
// TODO: Uncomment once we implement check for missing bucketId in the usage endpoint.
2021-09-10 08:50:15 +00:00
$response = $this->client->call(Client::METHOD_GET, '/storage/randomBucketId/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'range' => '24h'
]);
2023-11-26 20:57:57 +00:00
$this->assertEquals(404, $response['headers']['status-code']);
/**
* Test for SUCCESS
*/
2021-09-13 08:18:40 +00:00
$response = $this->client->call(Client::METHOD_GET, '/storage/' . $bucketId . '/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()), [
'range' => '24h'
]);
2023-11-26 20:57:57 +00:00
$this->assertEquals(200, $response['headers']['status-code']);
2025-02-21 13:28:49 +00:00
$this->assertEquals(7, count($response['body']));
2023-11-26 20:57:57 +00:00
$this->assertEquals('24h', $response['body']['range']);
$this->assertIsNumeric($response['body']['filesTotal']);
$this->assertIsNumeric($response['body']['filesStorageTotal']);
$this->assertIsArray($response['body']['files']);
$this->assertIsArray($response['body']['storage']);
2025-02-25 04:43:44 +00:00
$this->assertIsArray($response['body']['imageTransformations']);
$this->assertIsNumeric($response['body']['imageTransformationsTotal']);
}
public function testCreateBucketTransformationsDisabledConsole(): void
{
// Create a bucket with default settings
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'bucketId' => ID::unique(),
'name' => 'Test Console Bucket Transformations Disabled',
]);
$this->assertEquals(201, $bucket['headers']['status-code']);
// Create a file in the bucket
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucket['body']['$id'] . '/files', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'fileId' => ID::unique(),
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'transformations.png'),
]);
$this->assertEquals(201, $file['headers']['status-code']);
// Try to get the file preview
$preview = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucket['body']['$id'] . '/files/' . $file['body']['$id'] . '/preview', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $preview['headers']['status-code']);
// Update the bucket to disable transformations
$bucket = $this->client->call(Client::METHOD_PUT, '/storage/buckets/' . $bucket['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Test Bucket Transformations Disabled',
'transformations' => false,
]);
// Try to get the file preview again
$preview = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucket['body']['$id'] . '/files/' . $file['body']['$id'] . '/preview', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $preview['headers']['status-code']); // Returns 200 since image transformations are not counted for console requests
// Delete the bucket
2025-10-28 09:36:43 +00:00
$response = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucket['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
2025-10-28 09:36:43 +00:00
$this->assertEquals(204, $response['headers']['status-code']);
}
2025-12-28 02:02:34 +00:00
public function testFilePermissionNotAutoSetInConsole(): void
{
// Create a bucket
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'bucketId' => ID::unique(),
'name' => 'Test Bucket Permissions',
'fileSecurity' => true,
]);
$this->assertEquals(201, $bucket['headers']['status-code']);
$bucketId = $bucket['body']['$id'];
// Create a file without providing permissions (console client should not auto-set permissions)
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'fileId' => ID::unique(),
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'test.png'),
]);
$this->assertEquals(201, $file['headers']['status-code']);
// Verify file permissions are empty (not auto-set for privileged console user)
$this->assertIsArray($file['body']['$permissions']);
$this->assertEmpty($file['body']['$permissions']);
// Clean up: delete the bucket
$response = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(204, $response['headers']['status-code']);
}
2021-10-26 13:19:28 +00:00
}