From 40f70faa897ea505701db80c25f4166cdb237c14 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Thu, 18 May 2023 18:55:22 +0530 Subject: [PATCH 01/17] Feat: Add enable param in Database and Collection --- app/config/collections.php | 11 +++++++++++ app/controllers/api/databases.php | 15 ++++++++++----- docker-compose.yml | 1 + src/Appwrite/Utopia/Response/Model/Database.php | 6 ++++++ .../Databases/DatabasesCustomServerTest.php | 3 ++- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index 0b4dda6851..85a279faa5 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -32,6 +32,17 @@ $collections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('enabled'), + 'type' => Database::VAR_BOOLEAN, + 'signed' => true, + 'size' => 0, + 'format' => '', + 'filters' => [], + 'required' => false, + 'default' => true, + 'array' => false, + ], [ '$id' => ID::custom('search'), 'type' => Database::VAR_STRING, diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 8a18e28dba..71a444778c 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -384,11 +384,12 @@ App::post('/v1/databases') ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_DATABASE) // Model for database needs to be created ->param('databaseId', '', new CustomId(), 'Unique Id. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') - ->param('name', '', new Text(128), 'Collection name. Max length: 128 chars.') + ->param('name', '', new Text(128), 'Database name. Max length: 128 chars.') + ->param('enabled', true, new Boolean(), 'Is Database enabled?', true) ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $databaseId, string $name, Response $response, Database $dbForProject, Event $events) { + ->action(function (string $databaseId, string $name, bool $enabled, Response $response, Database $dbForProject, Event $events) { $databaseId = $databaseId == 'unique()' ? ID::unique() : $databaseId; @@ -396,6 +397,7 @@ App::post('/v1/databases') $dbForProject->createDocument('databases', new Document([ '$id' => $databaseId, 'name' => $name, + 'enabled' => $enabled, 'search' => implode(' ', [$databaseId, $name]), ])); $database = $dbForProject->getDocument('databases', $databaseId); @@ -619,10 +621,11 @@ App::put('/v1/databases/:databaseId') ->label('sdk.response.model', Response::MODEL_DATABASE) ->param('databaseId', '', new UID(), 'Database ID.') ->param('name', null, new Text(128), 'Database name. Max length: 128 chars.') + ->param('enabled', true, new Boolean(), 'Is Database enabled?', true) ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $databaseId, string $name, Response $response, Database $dbForProject, Event $events) { + ->action(function (string $databaseId, string $name, bool $enabled, Response $response, Database $dbForProject, Event $events) { $database = $dbForProject->getDocument('databases', $databaseId); @@ -633,6 +636,7 @@ App::put('/v1/databases/:databaseId') try { $database = $dbForProject->updateDocument('databases', $databaseId, $database ->setAttribute('name', $name) + ->setAttribute('enabled', $enabled) ->setAttribute('search', implode(' ', [$databaseId, $name]))); } catch (AuthorizationException $exception) { throw new Exception(Exception::USER_UNAUTHORIZED); @@ -714,10 +718,11 @@ App::post('/v1/databases/:databaseId/collections') ->param('name', '', new Text(128), 'Collection name. Max length: 128 chars.') ->param('permissions', null, new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE), 'An array of permissions strings. By default, no user is granted with any permissions. [Learn more about permissions](/docs/permissions).', true) ->param('documentSecurity', false, new Boolean(true), 'Enables configuring permissions for individual documents. A user needs one of document or collection level permissions to access a document. [Learn more about permissions](/docs/permissions).', true) + ->param('enabled', true, new Boolean(), 'Is collection enabled?', true) ->inject('response') ->inject('dbForProject') ->inject('events') - ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, Response $response, Database $dbForProject, Event $events) { + ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, Event $events) { $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); @@ -737,7 +742,7 @@ App::post('/v1/databases/:databaseId/collections') 'databaseId' => $databaseId, '$permissions' => $permissions ?? [], 'documentSecurity' => $documentSecurity, - 'enabled' => true, + 'enabled' => $enabled, 'name' => $name, 'search' => implode(' ', [$collectionId, $name]), ])); diff --git a/docker-compose.yml b/docker-compose.yml index ecb10b9eea..612d59f72a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -177,6 +177,7 @@ services: - _APP_GRAPHQL_MAX_BATCH_SIZE - _APP_GRAPHQL_MAX_COMPLEXITY - _APP_GRAPHQL_MAX_DEPTH + - PHP_IDE_CONFIG=serverName=swoole-http appwrite-realtime: entrypoint: realtime diff --git a/src/Appwrite/Utopia/Response/Model/Database.php b/src/Appwrite/Utopia/Response/Model/Database.php index 9d9a4b2762..bd9ae4625c 100644 --- a/src/Appwrite/Utopia/Response/Model/Database.php +++ b/src/Appwrite/Utopia/Response/Model/Database.php @@ -34,6 +34,12 @@ class Database extends Model 'default' => '', 'example' => self::TYPE_DATETIME_EXAMPLE, ]) + ->addRule('enabled', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Database enabled.', + 'default' => true, + 'example' => false, + ]) ; } diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index ce6602c176..bfa2f3dea8 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -27,6 +27,7 @@ class DatabasesCustomServerTest extends Scope 'databaseId' => ID::custom('first'), 'name' => 'Test 1', ]); + $this->assertEquals(201, $test1['headers']['status-code']); $this->assertEquals('Test 1', $test1['body']['name']); @@ -244,7 +245,7 @@ class DatabasesCustomServerTest extends Scope $this->assertEquals(200, $database['headers']['status-code']); $this->assertEquals($databaseId, $database['body']['$id']); $this->assertEquals('Test 1', $database['body']['name']); - + $this->assertEquals(true, $database['body']['enabled']); return ['databaseId' => $database['body']['$id']]; } From e384a3d3a1dfdb4eed086aa8596fb48daab97b70 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Thu, 18 May 2023 18:58:29 +0530 Subject: [PATCH 02/17] remove debug config --- composer.lock | 58 ++++++++++++++++++++-------------------------- docker-compose.yml | 1 - 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/composer.lock b/composer.lock index 978d14f490..4f1c64b69c 100644 --- a/composer.lock +++ b/composer.lock @@ -481,16 +481,16 @@ }, { "name": "guzzlehttp/guzzle", - "version": "7.5.1", + "version": "7.6.1", "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "b964ca597e86b752cd994f27293e9fa6b6a95ed9" + "reference": "8444a2bacf1960bc6a2b62ed86b8e72e11eebe51" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/b964ca597e86b752cd994f27293e9fa6b6a95ed9", - "reference": "b964ca597e86b752cd994f27293e9fa6b6a95ed9", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/8444a2bacf1960bc6a2b62ed86b8e72e11eebe51", + "reference": "8444a2bacf1960bc6a2b62ed86b8e72e11eebe51", "shasum": "" }, "require": { @@ -521,9 +521,6 @@ "bamarni-bin": { "bin-links": true, "forward-command": false - }, - "branch-alias": { - "dev-master": "7.5-dev" } }, "autoload": { @@ -589,7 +586,7 @@ ], "support": { "issues": "https://github.com/guzzle/guzzle/issues", - "source": "https://github.com/guzzle/guzzle/tree/7.5.1" + "source": "https://github.com/guzzle/guzzle/tree/7.6.1" }, "funding": [ { @@ -605,7 +602,7 @@ "type": "tidelift" } ], - "time": "2023-04-17T16:30:08+00:00" + "time": "2023-05-15T20:43:01+00:00" }, { "name": "guzzlehttp/promises", @@ -3784,16 +3781,16 @@ }, { "name": "phpstan/phpdoc-parser", - "version": "1.20.3", + "version": "1.20.4", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "6c04009f6cae6eda2f040745b6b846080ef069c2" + "reference": "7d568c87a9df9c5f7e8b5f075fc469aa8cb0a4cd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/6c04009f6cae6eda2f040745b6b846080ef069c2", - "reference": "6c04009f6cae6eda2f040745b6b846080ef069c2", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/7d568c87a9df9c5f7e8b5f075fc469aa8cb0a4cd", + "reference": "7d568c87a9df9c5f7e8b5f075fc469aa8cb0a4cd", "shasum": "" }, "require": { @@ -3823,9 +3820,9 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.20.3" + "source": "https://github.com/phpstan/phpdoc-parser/tree/1.20.4" }, - "time": "2023-04-25T09:01:03+00:00" + "time": "2023-05-02T09:19:37+00:00" }, { "name": "phpunit/php-code-coverage", @@ -4548,16 +4545,16 @@ }, { "name": "sebastian/diff", - "version": "4.0.4", + "version": "4.0.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/diff.git", - "reference": "3461e3fccc7cfdfc2720be910d3bd73c69be590d" + "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/3461e3fccc7cfdfc2720be910d3bd73c69be590d", - "reference": "3461e3fccc7cfdfc2720be910d3bd73c69be590d", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/74be17022044ebaaecfdf0c5cd504fc9cd5a7131", + "reference": "74be17022044ebaaecfdf0c5cd504fc9cd5a7131", "shasum": "" }, "require": { @@ -4602,7 +4599,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/diff/issues", - "source": "https://github.com/sebastianbergmann/diff/tree/4.0.4" + "source": "https://github.com/sebastianbergmann/diff/tree/4.0.5" }, "funding": [ { @@ -4610,7 +4607,7 @@ "type": "github" } ], - "time": "2020-10-26T13:10:38+00:00" + "time": "2023-05-07T05:35:17+00:00" }, { "name": "sebastian/environment", @@ -5577,16 +5574,16 @@ }, { "name": "twig/twig", - "version": "v3.5.1", + "version": "v3.6.0", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "a6e0510cc793912b451fd40ab983a1d28f611c15" + "reference": "106c170d08e8415d78be2d16c3d057d0d108262b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/a6e0510cc793912b451fd40ab983a1d28f611c15", - "reference": "a6e0510cc793912b451fd40ab983a1d28f611c15", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/106c170d08e8415d78be2d16c3d057d0d108262b", + "reference": "106c170d08e8415d78be2d16c3d057d0d108262b", "shasum": "" }, "require": { @@ -5595,15 +5592,10 @@ "symfony/polyfill-mbstring": "^1.3" }, "require-dev": { - "psr/container": "^1.0", + "psr/container": "^1.0|^2.0", "symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0" }, "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.5-dev" - } - }, "autoload": { "psr-4": { "Twig\\": "src/" @@ -5637,7 +5629,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.5.1" + "source": "https://github.com/twigphp/Twig/tree/v3.6.0" }, "funding": [ { @@ -5649,7 +5641,7 @@ "type": "tidelift" } ], - "time": "2023-02-08T07:49:20+00:00" + "time": "2023-05-03T19:06:57+00:00" } ], "aliases": [], diff --git a/docker-compose.yml b/docker-compose.yml index 612d59f72a..ecb10b9eea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -177,7 +177,6 @@ services: - _APP_GRAPHQL_MAX_BATCH_SIZE - _APP_GRAPHQL_MAX_COMPLEXITY - _APP_GRAPHQL_MAX_DEPTH - - PHP_IDE_CONFIG=serverName=swoole-http appwrite-realtime: entrypoint: realtime From c2a13b079e01821c90e108a6cf4b4a11d70cb81f Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Fri, 19 May 2023 12:54:07 +0530 Subject: [PATCH 03/17] chore: lowercase --- app/controllers/api/databases.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 71a444778c..db99a78f90 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -385,7 +385,7 @@ App::post('/v1/databases') ->label('sdk.response.model', Response::MODEL_DATABASE) // Model for database needs to be created ->param('databaseId', '', new CustomId(), 'Unique Id. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Database name. Max length: 128 chars.') - ->param('enabled', true, new Boolean(), 'Is Database enabled?', true) + ->param('enabled', true, new Boolean(), 'Is database enabled?', true) ->inject('response') ->inject('dbForProject') ->inject('events') @@ -621,7 +621,7 @@ App::put('/v1/databases/:databaseId') ->label('sdk.response.model', Response::MODEL_DATABASE) ->param('databaseId', '', new UID(), 'Database ID.') ->param('name', null, new Text(128), 'Database name. Max length: 128 chars.') - ->param('enabled', true, new Boolean(), 'Is Database enabled?', true) + ->param('enabled', true, new Boolean(), 'Is database enabled?', true) ->inject('response') ->inject('dbForProject') ->inject('events') From 7fe1f5612246ef5a5777b5235db9e5f31cc20963 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Fri, 19 May 2023 18:18:26 +0530 Subject: [PATCH 04/17] chore: add more test coverage --- tests/e2e/Services/Databases/DatabasesCustomServerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index bfa2f3dea8..72c566277e 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -286,6 +286,7 @@ class DatabasesCustomServerTest extends Scope ]); $this->assertEquals(201, $database['headers']['status-code']); $this->assertEquals('invalidDocumentDatabase', $database['body']['name']); + $this->assertTrue($database['body']['enabled']); $databaseId = $database['body']['$id']; /** @@ -330,7 +331,9 @@ class DatabasesCustomServerTest extends Scope $this->assertEquals(2, $collections['body']['total']); $this->assertEquals($test1['body']['$id'], $collections['body']['collections'][0]['$id']); + $this->assertEquals($test1['body']['enabled'], $collections['body']['collections'][0]['enabled']); $this->assertEquals($test2['body']['$id'], $collections['body']['collections'][1]['$id']); + $this->assertEquals($test1['body']['enabled'], $collections['body']['collections'][0]['enabled']); $base = array_reverse($collections['body']['collections']); From 45f5d932f69aae0f46788a4edbdfd05f80abee77 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Mon, 22 May 2023 20:10:09 +0530 Subject: [PATCH 05/17] Add more Tests --- .../Databases/DatabasesCustomServerTest.php | 142 ++++++++++++++---- 1 file changed, 110 insertions(+), 32 deletions(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 72c566277e..9df9dc5604 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -57,7 +57,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'limit(1)' ], + 'queries' => ['limit(1)'], ]); $this->assertEquals(200, $databases['headers']['status-code']); $this->assertCount(1, $databases['body']['databases']); @@ -66,7 +66,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'offset(1)' ], + 'queries' => ['offset(1)'], ]); $this->assertEquals(200, $databases['headers']['status-code']); $this->assertCount(1, $databases['body']['databases']); @@ -75,7 +75,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'equal("name", ["Test 1", "Test 2"])' ], + 'queries' => ['equal("name", ["Test 1", "Test 2"])'], ]); $this->assertEquals(200, $databases['headers']['status-code']); $this->assertCount(2, $databases['body']['databases']); @@ -84,7 +84,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'equal("name", "Test 2")' ], + 'queries' => ['equal("name", "Test 2")'], ]); $this->assertEquals(200, $databases['headers']['status-code']); $this->assertCount(1, $databases['body']['databases']); @@ -93,7 +93,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'equal("$id", "first")' ], + 'queries' => ['equal("$id", "first")'], ]); $this->assertEquals(200, $databases['headers']['status-code']); $this->assertCount(1, $databases['body']['databases']); @@ -105,7 +105,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'orderDesc("")' ], + 'queries' => ['orderDesc("")'], ]); $this->assertEquals(2, $databases['body']['total']); @@ -124,7 +124,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorAfter("' . $base['body']['databases'][0]['$id'] . '")' ], + 'queries' => ['cursorAfter("' . $base['body']['databases'][0]['$id'] . '")'], ]); $this->assertCount(1, $databases['body']['databases']); @@ -134,7 +134,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorAfter("' . $base['body']['databases'][1]['$id'] . '")' ], + 'queries' => ['cursorAfter("' . $base['body']['databases'][1]['$id'] . '")'], ]); $this->assertCount(0, $databases['body']['databases']); @@ -152,7 +152,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorBefore("' . $base['body']['databases'][1]['$id'] . '")' ], + 'queries' => ['cursorBefore("' . $base['body']['databases'][1]['$id'] . '")'], ]); $this->assertCount(1, $databases['body']['databases']); @@ -162,7 +162,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorBefore("' . $base['body']['databases'][0]['$id'] . '")' ], + 'queries' => ['cursorBefore("' . $base['body']['databases'][0]['$id'] . '")'], ]); $this->assertCount(0, $databases['body']['databases']); @@ -208,7 +208,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorAfter("unknown")' ], + 'queries' => ['cursorAfter("unknown")'], ]); $this->assertEquals(400, $response['headers']['status-code']); @@ -249,6 +249,37 @@ class DatabasesCustomServerTest extends Scope return ['databaseId' => $database['body']['$id']]; } + /** + * @depends testListDatabases + */ + public function testUpdateDatabase(array $data) + { + $databaseId = $data['databaseId']; + + $database = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'name' => 'Test 1 Updated', + 'enabled' => false]); + + $this->assertEquals(200, $database['headers']['status-code']); + $this->assertEquals('Test 1 Updated', $database['body']['name']); + $this->assertFalse($database['body']['enabled']); + + // Now update the database without the passing the enabled parameter + $database = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], ['name' => 'Test 1']); + + $this->assertEquals(200, $database['headers']['status-code']); + $this->assertEquals('Test 1', $database['body']['name']); + $this->assertTrue($database['body']['enabled']); + } + /** * @depends testListDatabases */ @@ -274,7 +305,7 @@ class DatabasesCustomServerTest extends Scope $this->assertEquals(404, $response['headers']['status-code']); } - public function testListCollections() + public function testListCollections(): array { $database = $this->client->call(Client::METHOD_POST, '/databases', array_merge([ 'content-type' => 'application/json', @@ -341,7 +372,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'limit(1)' ] + 'queries' => ['limit(1)'] ]); $this->assertEquals(200, $collections['headers']['status-code']); @@ -351,7 +382,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'offset(1)' ] + 'queries' => ['offset(1)'] ]); $this->assertEquals(200, $collections['headers']['status-code']); @@ -361,7 +392,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'equal("enabled", true)' ] + 'queries' => ['equal("enabled", true)'] ]); $this->assertEquals(200, $collections['headers']['status-code']); @@ -371,7 +402,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'equal("enabled", false)' ] + 'queries' => ['equal("enabled", false)'] ]); $this->assertEquals(200, $collections['headers']['status-code']); @@ -384,7 +415,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'orderDesc("")' ], + 'queries' => ['orderDesc("")'], ]); $this->assertEquals(2, $collections['body']['total']); @@ -403,7 +434,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorAfter("' . $base['body']['collections'][0]['$id'] . '")' ], + 'queries' => ['cursorAfter("' . $base['body']['collections'][0]['$id'] . '")'], ]); $this->assertCount(1, $collections['body']['collections']); @@ -413,7 +444,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorAfter("' . $base['body']['collections'][1]['$id'] . '")' ], + 'queries' => ['cursorAfter("' . $base['body']['collections'][1]['$id'] . '")'], ]); $this->assertCount(0, $collections['body']['collections']); @@ -431,7 +462,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorBefore("' . $base['body']['collections'][1]['$id'] . '")' ], + 'queries' => ['cursorBefore("' . $base['body']['collections'][1]['$id'] . '")'], ]); $this->assertCount(1, $collections['body']['collections']); @@ -441,7 +472,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorBefore("' . $base['body']['collections'][0]['$id'] . '")' ], + 'queries' => ['cursorBefore("' . $base['body']['collections'][0]['$id'] . '")'], ]); $this->assertCount(0, $collections['body']['collections']); @@ -487,7 +518,7 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'queries' => [ 'cursorAfter("unknown")' ], + 'queries' => ['cursorAfter("unknown")'], ]); $this->assertEquals(400, $response['headers']['status-code']); @@ -510,6 +541,53 @@ class DatabasesCustomServerTest extends Scope ]); $this->assertEquals(409, $response['headers']['status-code']); + return [ + 'databaseId' => $databaseId, + 'collectionId' => $test1['body']['$id'], + ]; + } + + /** + * @depends testListCollections + */ + public function testGetCollection(array $data): void + { + $databaseId = $data['databaseId']; + $collectionId = $data['collectionId']; + + $collection = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], $this->getHeaders())); + + $this->assertEquals(200, $collection['headers']['status-code']); + $this->assertEquals('Test 1', $collection['body']['name']); + $this->assertEquals('first', $collection['body']['$id']); + $this->assertTrue($collection['body']['enabled']); + } + + /** + * @depends testListCollections + */ + public function testUpdateCollection(array $data) + { + $databaseId = $data['databaseId']; + $collectionId = $data['collectionId']; + + $collection = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'name' => 'Test 1 Updated', + 'enabled' => false + ]); + + $this->assertEquals(200, $collection['headers']['status-code']); + $this->assertEquals('Test 1 Updated', $collection['body']['name']); + $this->assertEquals('first', $collection['body']['$id']); + $this->assertFalse($collection['body']['enabled']); } public function testDeleteAttribute(): array @@ -593,7 +671,7 @@ class DatabasesCustomServerTest extends Scope 'data' => [ 'firstName' => 'lorem', 'lastName' => 'ipsum', - 'unneeded' => 'dolor' + 'unneeded' => 'dolor' ], 'permissions' => [ Permission::read(Role::any()), @@ -1436,7 +1514,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals('lorem', $attribute['default']); @@ -1578,7 +1656,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals('torsten@appwrite.io', $attribute['default']); @@ -1721,7 +1799,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals('127.0.0.1', $attribute['default']); @@ -1863,7 +1941,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals('http://appwrite.io', $attribute['default']); @@ -2009,7 +2087,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals(123, $attribute['default']); @@ -2272,7 +2350,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals(123.456, $attribute['default']); @@ -2531,7 +2609,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals(true, $attribute['default']); @@ -2673,7 +2751,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals('1975-06-12 14:12:55+02:00', $attribute['default']); @@ -2820,7 +2898,7 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ])); - $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $attribute = array_values(array_filter($new['body']['attributes'], fn(array $a) => $a['key'] === $key))[0] ?? null; $this->assertNotNull($attribute); $this->assertFalse($attribute['required']); $this->assertEquals('lorem', $attribute['default']); From 6e5ee27f7ca1c10548f04c01ad1ef13c07097271 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi <62933155+2002Bishwajeet@users.noreply.github.com> Date: Tue, 23 May 2023 13:30:39 +0530 Subject: [PATCH 06/17] Update tests/e2e/Services/Databases/DatabasesCustomServerTest.php Co-authored-by: Jake Barnby --- tests/e2e/Services/Databases/DatabasesCustomServerTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 9df9dc5604..78e6c637c7 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -262,7 +262,8 @@ class DatabasesCustomServerTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ], [ 'name' => 'Test 1 Updated', - 'enabled' => false]); + 'enabled' => false, + ]); $this->assertEquals(200, $database['headers']['status-code']); $this->assertEquals('Test 1 Updated', $database['body']['name']); From 4964b5c5927943e76ff42237c6faffc81748c121 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi <62933155+2002Bishwajeet@users.noreply.github.com> Date: Tue, 23 May 2023 13:30:48 +0530 Subject: [PATCH 07/17] Update tests/e2e/Services/Databases/DatabasesCustomServerTest.php Co-authored-by: Jake Barnby --- tests/e2e/Services/Databases/DatabasesCustomServerTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 78e6c637c7..5e734fbcb2 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -274,7 +274,9 @@ class DatabasesCustomServerTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] - ], ['name' => 'Test 1']); + ], [ + 'name' => 'Test 1' + ]); $this->assertEquals(200, $database['headers']['status-code']); $this->assertEquals('Test 1', $database['body']['name']); From 1a8729177f9b9732a158f2752053774f5c1eddc7 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 24 May 2023 10:36:54 +0530 Subject: [PATCH 08/17] chore: formattin suggestion --- .../e2e/Services/Databases/DatabasesCustomServerTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 5e734fbcb2..348905a570 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -544,10 +544,11 @@ class DatabasesCustomServerTest extends Scope ]); $this->assertEquals(409, $response['headers']['status-code']); - return [ - 'databaseId' => $databaseId, - 'collectionId' => $test1['body']['$id'], - ]; + return + [ + 'databaseId' => $databaseId, + 'collectionId' => $test1['body']['$id'], + ]; } /** From 4706e6580de0640daa10dfa524d935123c8e7fbe Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 24 May 2023 15:39:08 +0530 Subject: [PATCH 09/17] chore: add new checks --- app/controllers/api/databases.php | 163 +++++++++++++----------------- 1 file changed, 73 insertions(+), 90 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index db99a78f90..6d6e01fb6c 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -73,7 +73,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att $default = $attribute->getAttribute('default'); $options = $attribute->getAttribute('options', []); - $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -193,16 +193,14 @@ function createAttribute(string $databaseId, string $collectionId, Document $att ->setType(DATABASE_TYPE_CREATE_ATTRIBUTE) ->setDatabase($db) ->setCollection($collection) - ->setDocument($attribute) - ; + ->setDocument($attribute); $events ->setContext('collection', $collection) ->setContext('database', $db) ->setParam('databaseId', $databaseId) ->setParam('collectionId', $collection->getId()) - ->setParam('attributeId', $attribute->getId()) - ; + ->setParam('attributeId', $attribute->getId()); $response->setStatusCode(Response::STATUS_CODE_CREATED); @@ -224,7 +222,7 @@ function updateAttribute( array $elements = null, array $options = [] ): Document { - $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -262,7 +260,7 @@ function updateAttribute( throw new Exception(Exception::ATTRIBUTE_DEFAULT_UNSUPPORTED, 'Cannot set default value for array attributes'); } - $collectionId = 'database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId(); + $collectionId = 'database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId(); $attribute ->setAttribute('default', $default) @@ -509,7 +507,7 @@ App::get('/v1/databases/:databaseId') ->inject('dbForProject') ->action(function (string $databaseId, Response $response, Database $dbForProject) { - $database = $dbForProject->getDocument('databases', $databaseId); + $database = $dbForProject->getDocument('databases', $databaseId); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -627,7 +625,7 @@ App::put('/v1/databases/:databaseId') ->inject('events') ->action(function (string $databaseId, string $name, bool $enabled, Response $response, Database $dbForProject, Event $events) { - $database = $dbForProject->getDocument('databases', $databaseId); + $database = $dbForProject->getDocument('databases', $databaseId); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -685,13 +683,11 @@ App::delete('/v1/databases/:databaseId') $deletes ->setType(DELETE_TYPE_DOCUMENT) - ->setDocument($database) - ; + ->setDocument($database); $events ->setParam('databaseId', $database->getId()) - ->setPayload($response->output($database, Response::MODEL_DATABASE)) - ; + ->setPayload($response->output($database, Response::MODEL_DATABASE)); $response->noContent(); }); @@ -724,9 +720,9 @@ App::post('/v1/databases/:databaseId/collections') ->inject('events') ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, Event $events) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled'))) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -786,9 +782,9 @@ App::get('/v1/databases/:databaseId/collections') ->inject('dbForProject') ->action(function (string $databaseId, array $queries, string $search, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled'))) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -841,9 +837,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || !$database->getAttribute('enabled')) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -879,7 +875,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/logs') ->inject('geodb') ->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -980,9 +976,9 @@ App::put('/v1/databases/:databaseId/collections/:collectionId') ->inject('events') ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, Event $events) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || !$database->getAttribute('enabled')) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -1045,9 +1041,9 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId') ->inject('deletes') ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject, Event $events, Delete $deletes) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || !$database->getAttribute('enabled')) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -1065,15 +1061,13 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId') $deletes ->setType(DELETE_TYPE_DOCUMENT) - ->setDocument($collection) - ; + ->setDocument($collection); $events ->setContext('database', $database) ->setParam('databaseId', $databaseId) ->setParam('collectionId', $collection->getId()) - ->setPayload($response->output($collection, Response::MODEL_COLLECTION)) - ; + ->setPayload($response->output($collection, Response::MODEL_COLLECTION)); $response->noContent(); }); @@ -1598,7 +1592,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati Database $dbForProject, EventDatabase $database, Event $events -) { + ) { $key ??= $relatedCollectionId; $twoWayKey ??= $collectionId; @@ -1658,7 +1652,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -1709,7 +1703,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/attributes/:key') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, string $key, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -2182,7 +2176,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/attributes/:key/ $events, type: Database::VAR_RELATIONSHIP, required: false, - options : [ + options: [ 'onDelete' => $onDelete ] ); @@ -2223,7 +2217,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/attributes/:key ->inject('events') ->action(function (string $databaseId, string $collectionId, string $key, Response $response, Database $dbForProject, EventDatabase $database, Event $events) { - $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -2276,8 +2270,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/attributes/:key ->setType(DATABASE_TYPE_DELETE_ATTRIBUTE) ->setCollection($collection) ->setDatabase($db) - ->setDocument($attribute) - ; + ->setDocument($attribute); // Select response model based on type and format $type = $attribute->getAttribute('type'); @@ -2305,8 +2298,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/attributes/:key ->setParam('attributeId', $attribute->getId()) ->setContext('collection', $collection) ->setContext('database', $db) - ->setPayload($response->output($attribute, $model)) - ; + ->setPayload($response->output($attribute, $model)); $response->noContent(); }); @@ -2340,7 +2332,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') ->inject('events') ->action(function (string $databaseId, string $collectionId, string $key, string $type, array $attributes, array $orders, Response $response, Database $dbForProject, EventDatabase $database, Event $events) { - $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -2363,7 +2355,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') } // Convert Document[] to array of attribute metadata - $oldAttributes = \array_map(fn ($a) => $a->getArrayCopy(), $collection->getAttribute('attributes')); + $oldAttributes = \array_map(fn($a) => $a->getArrayCopy(), $collection->getAttribute('attributes')); $oldAttributes[] = [ 'key' => '$id', @@ -2449,16 +2441,14 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') ->setType(DATABASE_TYPE_CREATE_INDEX) ->setDatabase($db) ->setCollection($collection) - ->setDocument($index) - ; + ->setDocument($index); $events ->setParam('databaseId', $databaseId) ->setParam('collectionId', $collection->getId()) ->setParam('indexId', $index->getId()) ->setContext('collection', $collection) - ->setContext('database', $db) - ; + ->setContext('database', $db); $response ->setStatusCode(Response::STATUS_CODE_ACCEPTED) @@ -2485,7 +2475,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -2525,7 +2515,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->inject('dbForProject') ->action(function (string $databaseId, string $collectionId, string $key, Response $response, Database $dbForProject) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -2576,7 +2566,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->inject('events') ->action(function (string $databaseId, string $collectionId, string $key, Response $response, Database $dbForProject, EventDatabase $database, Event $events) { - $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -2604,8 +2594,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->setType(DATABASE_TYPE_DELETE_INDEX) ->setDatabase($db) ->setCollection($collection) - ->setDocument($index) - ; + ->setDocument($index); $events ->setParam('databaseId', $databaseId) @@ -2613,8 +2602,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/indexes/:key') ->setParam('indexId', $index->getId()) ->setContext('collection', $collection) ->setContext('database', $db) - ->setPayload($response->output($index, Response::MODEL_INDEX)) - ; + ->setPayload($response->output($index, Response::MODEL_INDEX)); $response->noContent(); }); @@ -2665,7 +2653,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -2740,7 +2728,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -2819,7 +2807,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -2852,8 +2840,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/documents') ->setParam('collectionId', $collection->getId()) ->setParam('documentId', $document->getId()) ->setContext('collection', $collection) - ->setContext('database', $database) - ; + ->setContext('database', $database); $response ->setStatusCode(Response::STATUS_CODE_CREATED) @@ -2883,9 +2870,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents') ->inject('mode') ->action(function (string $databaseId, string $collectionId, array $queries, Response $response, Database $dbForProject, string $mode) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -2945,7 +2932,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents') $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -2961,8 +2948,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents') } $relatedCollectionId = $relationship->getAttribute('relatedCollection'); - $relatedCollection = Authorization::skip(fn() => - $dbForProject->getDocument('database_' . $database->getInternalId(), $relatedCollectionId)); + $relatedCollection = Authorization::skip(fn() => $dbForProject->getDocument('database_' . $database->getInternalId(), $relatedCollectionId)); foreach ($relations as $index => $doc) { if ($doc instanceof Document) { @@ -2982,7 +2968,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents') return true; }; - // The linter is forcing this indentation + // The linter is forcing this indentation foreach ($documents as $document) { $processDocument($collection, $document); } @@ -3018,9 +3004,9 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen ->inject('mode') ->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Database $dbForProject, string $mode) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -3058,7 +3044,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -3113,7 +3099,7 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/documents/:documen ->inject('geodb') ->action(function (string $databaseId, string $collectionId, string $documentId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); if ($database->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -3230,9 +3216,9 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum throw new Exception(Exception::DOCUMENT_MISSING_PAYLOAD); } - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -3309,7 +3295,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -3377,7 +3363,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum try { $document = $dbForProject->withRequestTimestamp( $requestTimestamp, - fn () => $dbForProject->updateDocument( + fn() => $dbForProject->updateDocument( 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $document->getId(), $newDocument @@ -3398,7 +3384,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -3431,8 +3417,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum ->setParam('collectionId', $collection->getId()) ->setParam('documentId', $document->getId()) ->setContext('collection', $collection) - ->setContext('database', $database) - ; + ->setContext('database', $database); $response->dynamic($document, Response::MODEL_DOCUMENT); }); @@ -3469,9 +3454,9 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu ->inject('mode') ->action(function (string $databaseId, string $collectionId, string $documentId, ?\DateTime $requestTimestamp, Response $response, Database $dbForProject, Event $events, Delete $deletes, string $mode) { - $database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); + $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty()) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -3506,7 +3491,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -3537,7 +3522,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu $checkPermissions($collection, $document); - Authorization::skip(fn () => $dbForProject->withRequestTimestamp($requestTimestamp, function () use ($dbForProject, $database, $collection, $documentId) { + Authorization::skip(fn() => $dbForProject->withRequestTimestamp($requestTimestamp, function () use ($dbForProject, $database, $collection, $documentId) { try { $dbForProject->deleteDocument( 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), @@ -3560,7 +3545,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu $relationships = \array_filter( $collection->getAttribute('attributes', []), - fn ($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP + fn($attribute) => $attribute->getAttribute('type') === Database::VAR_RELATIONSHIP ); foreach ($relationships as $relationship) { @@ -3590,8 +3575,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu $deletes ->setType(DELETE_TYPE_AUDIT) - ->setDocument($document) - ; + ->setDocument($document); $events ->setParam('databaseId', $databaseId) @@ -3599,8 +3583,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu ->setParam('documentId', $document->getId()) ->setContext('collection', $collection) ->setContext('database', $database) - ->setPayload($response->output($document, Response::MODEL_DOCUMENT)) - ; + ->setPayload($response->output($document, Response::MODEL_DOCUMENT)); $response->noContent(); }); @@ -3705,16 +3688,16 @@ App::get('/v1/databases/usage') 'databasesCount' => $stats['databases.$all.count.total'] ?? [], 'documentsCount' => $stats['documents.$all.count.total'] ?? [], 'collectionsCount' => $stats['collections.$all.count.total'] ?? [], - 'documentsCreate' => $stats['documents.$all.requests.create'] ?? [], - 'documentsRead' => $stats['documents.$all.requests.read'] ?? [], + 'documentsCreate' => $stats['documents.$all.requests.create'] ?? [], + 'documentsRead' => $stats['documents.$all.requests.read'] ?? [], 'documentsUpdate' => $stats['documents.$all.requests.update'] ?? [], 'documentsDelete' => $stats['documents.$all.requests.delete'] ?? [], 'collectionsCreate' => $stats['collections.$all.requests.create'] ?? [], - 'collectionsRead' => $stats['collections.$all.requests.read'] ?? [], + 'collectionsRead' => $stats['collections.$all.requests.read'] ?? [], 'collectionsUpdate' => $stats['collections.$all.requests.update'] ?? [], 'collectionsDelete' => $stats['collections.$all.requests.delete'] ?? [], 'databasesCreate' => $stats['databases.$all.requests.create'] ?? [], - 'databasesRead' => $stats['databases.$all.requests.read'] ?? [], + 'databasesRead' => $stats['databases.$all.requests.read'] ?? [], 'databasesUpdate' => $stats['databases.$all.requests.update'] ?? [], 'databasesDelete' => $stats['databases.$all.requests.delete'] ?? [], ]); @@ -3818,12 +3801,12 @@ App::get('/v1/databases/:databaseId/usage') 'range' => $range, 'collectionsCount' => $stats["collections.{$databaseId}.count.total"] ?? [], 'collectionsCreate' => $stats["collections.{$databaseId}.requests.create"] ?? [], - 'collectionsRead' => $stats["collections.{$databaseId}.requests.read"] ?? [], + 'collectionsRead' => $stats["collections.{$databaseId}.requests.read"] ?? [], 'collectionsUpdate' => $stats["collections.{$databaseId}.requests.update"] ?? [], 'collectionsDelete' => $stats["collections.{$databaseId}.requests.delete"] ?? [], 'documentsCount' => $stats["documents.{$databaseId}.count.total"] ?? [], - 'documentsCreate' => $stats["documents.{$databaseId}.requests.create"] ?? [], - 'documentsRead' => $stats["documents.{$databaseId}.requests.read"] ?? [], + 'documentsCreate' => $stats["documents.{$databaseId}.requests.create"] ?? [], + 'documentsRead' => $stats["documents.{$databaseId}.requests.read"] ?? [], 'documentsUpdate' => $stats["documents.{$databaseId}.requests.update"] ?? [], 'documentsDelete' => $stats["documents.{$databaseId}.requests.delete"] ?? [], ]); @@ -3933,8 +3916,8 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage') 'documentsCount' => $stats["documents.{$databaseId}/{$collectionId}.count.total"] ?? [], 'documentsCreate' => $stats["documents.{$databaseId}/{$collectionId}.requests.create"] ?? [], 'documentsRead' => $stats["documents.{$databaseId}/{$collectionId}.requests.read"] ?? [], - 'documentsUpdate' => $stats["documents.{$databaseId}/{$collectionId}.requests.update"] ?? [], - 'documentsDelete' => $stats["documents.{$databaseId}/{$collectionId}.requests.delete" ?? []] + 'documentsUpdate' => $stats["documents.{$databaseId}/{$collectionId}.requests.update"] ?? [], + 'documentsDelete' => $stats["documents.{$databaseId}/{$collectionId}.requests.delete" ?? []] ]); } From 6fc176d50899251d0dddc3c881c0b56e68c3247a Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 24 May 2023 17:58:40 +0530 Subject: [PATCH 10/17] chore: add admin mode assertion for create collection --- app/controllers/api/databases.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 6d6e01fb6c..28b9837a47 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -718,11 +718,12 @@ App::post('/v1/databases/:databaseId/collections') ->inject('response') ->inject('dbForProject') ->inject('events') + ->inject('mode') ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, Event $events) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty() || (!$database->getAttribute('enabled'))) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } From e4964195722c76f4fe1a4d38a4efbb8a90896265 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 24 May 2023 22:24:57 +0530 Subject: [PATCH 11/17] remove formatting updates --- app/controllers/api/databases.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 28b9837a47..e27ca94351 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -73,7 +73,7 @@ function createAttribute(string $databaseId, string $collectionId, Document $att $default = $attribute->getAttribute('default'); $options = $attribute->getAttribute('options', []); - $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -193,14 +193,16 @@ function createAttribute(string $databaseId, string $collectionId, Document $att ->setType(DATABASE_TYPE_CREATE_ATTRIBUTE) ->setDatabase($db) ->setCollection($collection) - ->setDocument($attribute); + ->setDocument($attribute) + ; $events ->setContext('collection', $collection) ->setContext('database', $db) ->setParam('databaseId', $databaseId) ->setParam('collectionId', $collection->getId()) - ->setParam('attributeId', $attribute->getId()); + ->setParam('attributeId', $attribute->getId()) + ; $response->setStatusCode(Response::STATUS_CODE_CREATED); @@ -222,7 +224,7 @@ function updateAttribute( array $elements = null, array $options = [] ): Document { - $db = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); + $db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId)); if ($db->isEmpty()) { throw new Exception(Exception::DATABASE_NOT_FOUND); @@ -260,7 +262,7 @@ function updateAttribute( throw new Exception(Exception::ATTRIBUTE_DEFAULT_UNSUPPORTED, 'Cannot set default value for array attributes'); } - $collectionId = 'database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId(); + $collectionId = 'database_' . $db->getInternalId() . '_collection_' . $collection->getInternalId(); $attribute ->setAttribute('default', $default) @@ -683,11 +685,13 @@ App::delete('/v1/databases/:databaseId') $deletes ->setType(DELETE_TYPE_DOCUMENT) - ->setDocument($database); + ->setDocument($database) + ; $events ->setParam('databaseId', $database->getId()) - ->setPayload($response->output($database, Response::MODEL_DATABASE)); + ->setPayload($response->output($database, Response::MODEL_DATABASE)) + ; $response->noContent(); }); @@ -719,7 +723,7 @@ App::post('/v1/databases/:databaseId/collections') ->inject('dbForProject') ->inject('events') ->inject('mode') - ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, Event $events) { + ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, string $mode, Event $events) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); From 2873918cc5ec50a6c03f5163eaaa92c7e6497f06 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 24 May 2023 22:56:58 +0530 Subject: [PATCH 12/17] Update databases.php fix: tests --- app/controllers/api/databases.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index e27ca94351..1522104cec 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -721,8 +721,8 @@ App::post('/v1/databases/:databaseId/collections') ->param('enabled', true, new Boolean(), 'Is collection enabled?', true) ->inject('response') ->inject('dbForProject') - ->inject('events') ->inject('mode') + ->inject('events') ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, string $mode, Event $events) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); From b0656acf9cde017e6e7320838a1cc710dafa66c2 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Wed, 24 May 2023 23:10:12 +0530 Subject: [PATCH 13/17] chore: Add unitTests DatabaseConsoleClientTest --- .../Databases/DatabasesConsoleClientTest.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php index ad97a2bb3e..9846c2548c 100644 --- a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php @@ -27,6 +27,7 @@ class DatabasesConsoleClientTest extends Scope ]); $this->assertEquals(201, $database['headers']['status-code']); $this->assertEquals('invalidDocumentDatabase', $database['body']['name']); + $this->assertTrue($database['body']['enabled']); $databaseId = $database['body']['$id']; /** @@ -50,6 +51,38 @@ class DatabasesConsoleClientTest extends Scope $this->assertEquals(201, $movies['headers']['status-code']); $this->assertEquals($movies['body']['name'], 'Movies'); + /** + * Test When database is disabled but can still create collections + */ + $database = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], [ + 'name' => 'invalidDocumentDatabase Updated', + 'enabled' => false, + ]); + + $this->assertFalse($database['body']['enabled']); + + $movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'collectionId' => ID::unique(), + 'name' => 'TvShows', + 'permissions' => [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + 'documentSecurity' => true, + ]); + + $this->assertEquals(201, $movies['headers']['status-code']); + $this->assertEquals($movies['body']['name'], 'TvShows'); + return ['moviesId' => $movies['body']['$id'], 'databaseId' => $databaseId]; } From 68c56daba137f00558e9398792021a83fae3e4ca Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Fri, 26 May 2023 17:08:09 +0530 Subject: [PATCH 14/17] add extra check for admin modes --- app/controllers/api/databases.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 1522104cec..9eedbb986c 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -785,11 +785,12 @@ App::get('/v1/databases/:databaseId/collections') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->inject('response') ->inject('dbForProject') - ->action(function (string $databaseId, array $queries, string $search, Response $response, Database $dbForProject) { + ->inject('mode') + ->action(function (string $databaseId, array $queries, string $search, Response $response, Database $dbForProject, string $mode) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty() || (!$database->getAttribute('enabled'))) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -840,11 +841,12 @@ App::get('/v1/databases/:databaseId/collections/:collectionId') ->param('collectionId', '', new UID(), 'Collection ID.') ->inject('response') ->inject('dbForProject') - ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject) { + ->inject('mode') + ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject, string $mode) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty() || !$database->getAttribute('enabled')) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -978,12 +980,13 @@ App::put('/v1/databases/:databaseId/collections/:collectionId') ->param('enabled', true, new Boolean(), 'Is collection enabled?', true) ->inject('response') ->inject('dbForProject') + ->inject('mode') ->inject('events') - ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, Event $events) { + ->action(function (string $databaseId, string $collectionId, string $name, ?array $permissions, bool $documentSecurity, bool $enabled, Response $response, Database $dbForProject, string $mode, Event $events) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty() || !$database->getAttribute('enabled')) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } @@ -1042,13 +1045,14 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId') ->param('collectionId', '', new UID(), 'Collection ID.') ->inject('response') ->inject('dbForProject') + ->inject('mode') ->inject('events') ->inject('deletes') - ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject, Event $events, Delete $deletes) { + ->action(function (string $databaseId, string $collectionId, Response $response, Database $dbForProject, string $mode, Event $events, Delete $deletes) { $database = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId)); - if ($database->isEmpty() || !$database->getAttribute('enabled')) { + if ($database->isEmpty() || (!$database->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) { throw new Exception(Exception::DATABASE_NOT_FOUND); } From 9d1d985d0d4b990573df723c5ede169198cc2f81 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Fri, 26 May 2023 17:54:35 +0530 Subject: [PATCH 15/17] chore: Add new unit tests --- .../Databases/DatabasesConsoleClientTest.php | 109 ++++++++++++++++-- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php index 9846c2548c..532023ba0e 100644 --- a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php @@ -20,8 +20,7 @@ class DatabasesConsoleClientTest extends Scope $database = $this->client->call(Client::METHOD_POST, '/databases', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ + ], $this->getHeaders()), [ 'databaseId' => ID::unique(), 'name' => 'invalidDocumentDatabase', ]); @@ -54,18 +53,17 @@ class DatabasesConsoleClientTest extends Scope /** * Test When database is disabled but can still create collections */ - $database = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, [ + $database = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ], [ + ], $this->getHeaders()), [ 'name' => 'invalidDocumentDatabase Updated', 'enabled' => false, ]); $this->assertFalse($database['body']['enabled']); - $movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ + $tvShows = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ @@ -80,10 +78,103 @@ class DatabasesConsoleClientTest extends Scope 'documentSecurity' => true, ]); - $this->assertEquals(201, $movies['headers']['status-code']); - $this->assertEquals($movies['body']['name'], 'TvShows'); + $this->assertEquals(201, $tvShows['headers']['status-code']); + $this->assertEquals($tvShows['body']['name'], 'TvShows'); - return ['moviesId' => $movies['body']['$id'], 'databaseId' => $databaseId]; + return ['moviesId' => $movies['body']['$id'], 'databaseId' => $databaseId, 'tvShowsId' => $tvShows['body']['$id']]; + } + + /** + * @depends testCreateCollection + * @param array $data + */ + public function testListCollection(array $data) + { + /** + * Test When database is disabled but can still call list collections + */ + $databaseId = $data['databaseId']; + + $collections = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ], $this->getHeaders())); + + $this->assertEquals(200, $collections['headers']['status-code']); + $this->assertEquals(2, $collections['body']['total']); + } + + /** + * @depends testCreateCollection + * @param array $data + */ + public function testGetCollection(array $data) + { + $databaseId = $data['databaseId']; + $moviesCollectionId = $data['moviesId']; + /** + * Test When database is disabled but can still call get collection + */ + + $collection = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $moviesCollectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $collection['headers']['status-code']); + $this->assertEquals('Movies', $collection['body']['name']); + $this->assertEquals($moviesCollectionId, $collection['body']['$id']); + $this->assertTrue($collection['body']['enabled']); + } + + + /** + * @depends testCreateCollection + * @param array $data + */ + public function testUpdateCollection(array $data) + { + + $databaseId = $data['databaseId']; + $moviesCollectionId = $data['moviesId']; + /** + * Test When database is disabled but can still call update collection + */ + + $collection = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $moviesCollectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Movies Updated', + 'enabled' => false + ]); + + $this->assertEquals(200, $collection['headers']['status-code']); + $this->assertEquals('Movies Updated', $collection['body']['name']); + $this->assertEquals($moviesCollectionId, $collection['body']['$id']); + $this->assertFalse($collection['body']['enabled']); + } + + + /** + * @depends testCreateCollection + * @param array $data + */ + public function testDeleteCollection(array $data) + { + $databaseId = $data['databaseId']; + $tvShowsId = $data['tvShowsId']; + /** + * Test When database is disabled but can still call Delete collection + */ + $response = $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $tvShowsId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(204, $response['headers']['status-code']); + $this->assertEquals($response['body'], ""); } /** From 729eed25e5f3b8367bab5a4dd13d130590b128c4 Mon Sep 17 00:00:00 2001 From: Bishwajeet Parhi Date: Mon, 29 May 2023 13:12:55 +0530 Subject: [PATCH 16/17] chore: fix formatting --- .../e2e/Services/Databases/DatabasesConsoleClientTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php index 532023ba0e..13d28df5f3 100644 --- a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php @@ -113,10 +113,10 @@ class DatabasesConsoleClientTest extends Scope { $databaseId = $data['databaseId']; $moviesCollectionId = $data['moviesId']; + /** * Test When database is disabled but can still call get collection */ - $collection = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $moviesCollectionId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -128,20 +128,18 @@ class DatabasesConsoleClientTest extends Scope $this->assertTrue($collection['body']['enabled']); } - /** * @depends testCreateCollection * @param array $data */ public function testUpdateCollection(array $data) { - $databaseId = $data['databaseId']; $moviesCollectionId = $data['moviesId']; + /** * Test When database is disabled but can still call update collection */ - $collection = $this->client->call(Client::METHOD_PUT, '/databases/' . $databaseId . '/collections/' . $moviesCollectionId, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -156,7 +154,6 @@ class DatabasesConsoleClientTest extends Scope $this->assertFalse($collection['body']['enabled']); } - /** * @depends testCreateCollection * @param array $data @@ -165,6 +162,7 @@ class DatabasesConsoleClientTest extends Scope { $databaseId = $data['databaseId']; $tvShowsId = $data['tvShowsId']; + /** * Test When database is disabled but can still call Delete collection */ From 0372b0c590178a2bfeaf6b813a9e11142476c0ec Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 29 May 2023 20:34:41 +1200 Subject: [PATCH 17/17] Update tests/e2e/Services/Databases/DatabasesCustomServerTest.php --- .../e2e/Services/Databases/DatabasesCustomServerTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 348905a570..5e734fbcb2 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -544,11 +544,10 @@ class DatabasesCustomServerTest extends Scope ]); $this->assertEquals(409, $response['headers']['status-code']); - return - [ - 'databaseId' => $databaseId, - 'collectionId' => $test1['body']['$id'], - ]; + return [ + 'databaseId' => $databaseId, + 'collectionId' => $test1['body']['$id'], + ]; } /**