From dca5c3491b8563aa177e75717794eb75d6e47645 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 16 Jun 2021 15:44:47 +0545 Subject: [PATCH] update bucket endpoint and test --- app/controllers/api/storage.php | 65 +++++++++++++++++-- .../Storage/StorageCustomServerTest.php | 43 ++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 2191342102..810c84eb5b 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -48,13 +48,10 @@ App::post('/v1/storage/buckets') ->param('antiVirus', true, new Boolean(), 'Is virus scanning enabled?', true) ->inject('response') ->inject('dbForInternal') - ->inject('user') ->inject('audits') - ->action(function ($name, $read, $write, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $response, $dbForInternal, $user, $audits) { - /** @var Utopia\Swoole\Request $request */ + ->action(function ($name, $read, $write, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $response, $dbForInternal, $audits) { /** @var Appwrite\Utopia\Response $response */ /** @var Utopia\Database\Database $dbForInternal */ - /** @var Appwrite\Database\Document $user */ /** @var Appwrite\Event\Event $audits */ $data = $dbForInternal->createDocument('buckets', new Document([ @@ -138,6 +135,66 @@ App::get('/v1/storage/buckets/:bucketId') $response->dynamic2($bucket, Response::MODEL_BUCKET); }); +App::put('/v1/storage/buckets/:bucketId') + ->desc('Get Bucket') + ->groups(['api', 'storage']) + ->label('scope', 'buckets.read') + ->label('sdk.auth', [APP_AUTH_TYPE_KEY]) + ->label('sdk.namespace', 'storage') + ->label('sdk.method', 'getBucket') + ->label('sdk.description', '/docs/references/storage/get-bucket.md') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_BUCKET) + ->param('bucketId', '', new UID(), 'Bucket unique ID.') + ->param('name', null, new Text(128), 'Bucket name', false) + ->param('read', null, new ArrayList(new Text(64)), 'An array of strings with read permissions. By default inherits the existing read permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true) + ->param('write', null, new ArrayList(new Text(64)), 'An array of strings with write permissions. By default inherits the existing write permissions. [learn more about permissions](/docs/permissions) and get a full list of available permissions.', true) + ->param('maximumFileSize', 0, new Integer(), 'Maximum file size allowed.', true) + ->param('allowedFileExtensions', ['*'], new ArrayList(new Text(64)), 'Allowed file extensions', true) + ->param('enabled', true, new Boolean(), 'Is bucket enabled?', true) + ->param('adapter', 'local', new WhiteList(['local']), 'Storage adapter.', true) + ->param('encryption', true, new Boolean(), 'Is encryption enabled?', true) + ->param('antiVirus', true, new Boolean(), 'Is virus scanning enabled?', true) + ->inject('response') + ->inject('dbForInternal') + ->inject('user') + ->inject('audits') + ->action(function ($bucketId, $name, $read, $write, $maximumFileSize, $allowedFileExtensions, $enabled, $adapter, $encryption, $antiVirus, $response, $dbForInternal, $user, $audits) { + /** @var Appwrite\Utopia\Response $response */ + /** @var Utopia\Database\Database $dbForInternal */ + /** @var Appwrite\Event\Event $audits */ + + $bucket = $dbForInternal->getDocument('buckets', $bucketId); + + if (empty($bucket->getId())) { + throw new Exception('Bucket not found', 404); + } + + $read = (is_null($read)) ? $bucket->getAttribute('$read', []) : $read; // By default inherit read permissions + $write = (is_null($write)) ? $bucket->getAttribute('$write',[]) : $write; // By default inherit write permissions + + $bucket = $dbForInternal->updateDocument('buckets', $bucket->getId(), $bucket + ->setAttribute('name',$name) + ->setAttribute('$read',$read) + ->setAttribute('$write',$write) + ->setAttribute('maximumFileSize',$maximumFileSize) + ->setAttribute('allowedFileExtensions',$allowedFileExtensions) + ->setAttribute('enabled',$enabled) + ->setAttribute('adapter',$adapter) + ->setAttribute('encryption',$encryption) + ->setAttribute('antiVirus',$antiVirus) + ); + + $audits + ->setParam('event', 'storage.buckets.update') + ->setParam('resource', 'storage/buckets/' . $bucket->getId()) + ->setParam('data', $bucket->getArrayCopy()) + ; + + $response->dynamic2($bucket, Response::MODEL_BUCKET); + }); + App::post('/v1/storage/files') ->desc('Create File') ->groups(['api', 'storage']) diff --git a/tests/e2e/Services/Storage/StorageCustomServerTest.php b/tests/e2e/Services/Storage/StorageCustomServerTest.php index 87fa92836e..c6b1428601 100644 --- a/tests/e2e/Services/Storage/StorageCustomServerTest.php +++ b/tests/e2e/Services/Storage/StorageCustomServerTest.php @@ -111,4 +111,47 @@ class StorageCustomServerTest extends Scope return $data; } + + /** + * @depends testCreateBucket + */ + public function testUpdateBucket($data):array + { + $id = $data['bucketId'] ?? ''; + /** + * Test for SUCCESS + */ + $bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $id, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Test Bucket Updated', + 'enabled' => false, + ]); + $this->assertEquals(201, $bucket['headers']['status-code']); + $this->assertNotEmpty($bucket['body']['$id']); + $this->assertIsInt($bucket['body']['dateCreated']); + $this->assertIsArray($bucket['body']['$read']); + $this->assertIsArray($bucket['body']['$write']); + $this->assertIsArray($bucket['body']['allowedFileExtensions']); + $this->assertEquals('Test Bucket Updated', $bucket['body']['name']); + $this->assertEquals(false, $bucket['body']['enabled']); + $this->assertEquals(true, $bucket['body']['encryption']); + $this->assertEquals(true, $bucket['body']['antiVirus']); + $this->assertEquals('local', $bucket['body']['adapter']); + $bucketId = $bucket['body']['$id']; + /** + * Test for FAILURE + */ + $bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $id, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => '', + 'enabled' => 'false', + ]); + $this->assertEquals(400, $bucket['headers']['status-code']); + + return ['bucketId' => $bucketId]; + } } \ No newline at end of file