1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

update bucket endpoint and test

This commit is contained in:
Damodar Lohani 2021-06-16 15:44:47 +05:45
parent 67823a6e5d
commit dca5c3491b
2 changed files with 104 additions and 4 deletions

View file

@ -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'])

View file

@ -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];
}
}