From e55faff0a1ae80530d7f784b98bf054d6f8d7883 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 5 Sep 2022 19:51:33 +1200 Subject: [PATCH] Fix tests --- app/controllers/api/databases.php | 2 +- app/controllers/api/storage.php | 2 +- composer.lock | 8 +-- .../Databases/DatabasesCustomClientTest.php | 30 +++++------ .../DatabasesPermissionsGuestTest.php | 50 +++++++++++++++++++ .../Storage/StorageCustomClientTest.php | 4 +- 6 files changed, 74 insertions(+), 22 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index ff2ecc0d08..64421b52ce 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2207,7 +2207,7 @@ App::patch('/v1/databases/:databaseId/collections/:collectionId/documents/:docum ->param('collectionId', null, new UID(), 'Collection ID.') ->param('documentId', null, new UID(), 'Document ID.') ->param('data', [], new JSON(), 'Document data as JSON object. Include only attribute and value pairs to be updated.', true) - ->param('permissions', null, new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE, [Database::PERMISSION_READ, Database::PERMISSION_CREATE, Database::PERMISSION_UPDATE, 'write']), 'An array of permissions strings. By default the current permissions are inherited. [Learn more about permissions](/docs/permissions).', true) + ->param('permissions', null, new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE, [Database::PERMISSION_READ, Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE, 'write']), 'An array of permissions strings. By default the current permissions are inherited. [Learn more about permissions](/docs/permissions).', true) ->inject('response') ->inject('dbForProject') ->inject('events') diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index e9baa2971f..5799f04d20 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1251,7 +1251,7 @@ App::put('/v1/storage/buckets/:bucketId/files/:fileId') ->label('sdk.response.model', Response::MODEL_FILE) ->param('bucketId', null, new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](/docs/server/storage#createBucket).') ->param('fileId', '', new UID(), 'File unique ID.') - ->param('permissions', null, new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE, [Database::PERMISSION_READ, Database::PERMISSION_CREATE, Database::PERMISSION_UPDATE, 'write']), 'An array of permission string. By default the current permissions are inherited. [Learn more about permissions](/docs/permissions).', true) + ->param('permissions', null, new Permissions(APP_LIMIT_ARRAY_PARAMS_SIZE, [Database::PERMISSION_READ, Database::PERMISSION_UPDATE, Database::PERMISSION_DELETE, 'write']), 'An array of permission string. By default the current permissions are inherited. [Learn more about permissions](/docs/permissions).', true) ->inject('response') ->inject('dbForProject') ->inject('user') diff --git a/composer.lock b/composer.lock index 0a611f17e0..ac4e07cb0e 100644 --- a/composer.lock +++ b/composer.lock @@ -2064,12 +2064,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "d6a18e52df0118b33eda2228a8911c88533a1dbe" + "reference": "0a4119703ba11112f0e1d1fffa383fa1a882a588" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/d6a18e52df0118b33eda2228a8911c88533a1dbe", - "reference": "d6a18e52df0118b33eda2228a8911c88533a1dbe", + "url": "https://api.github.com/repos/utopia-php/database/zipball/0a4119703ba11112f0e1d1fffa383fa1a882a588", + "reference": "0a4119703ba11112f0e1d1fffa383fa1a882a588", "shasum": "" }, "require": { @@ -2120,7 +2120,7 @@ "issues": "https://github.com/utopia-php/database/issues", "source": "https://github.com/utopia-php/database/tree/feat-write-helper" }, - "time": "2022-09-05T01:47:47+00:00" + "time": "2022-09-05T03:51:48+00:00" }, { "name": "utopia-php/domains", diff --git a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php index 595d0d632c..9f86bb66c0 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomClientTest.php @@ -47,24 +47,32 @@ class DatabasesCustomClientTest extends Scope ], ]); + $moviesId = $movies['body']['$id']; + $this->assertContains(Permission::create(Role::user($this->getUser()['$id'])), $movies['body']['$permissions']); $this->assertContains(Permission::update(Role::user($this->getUser()['$id'])), $movies['body']['$permissions']); $this->assertContains(Permission::delete(Role::user($this->getUser()['$id'])), $movies['body']['$permissions']); + $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $moviesId . '/attributes/string', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'key' => 'title', + 'size' => 256, + 'required' => true, + ]); + + sleep(1); + // Document aliases write to update, delete - $document1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents', array_merge([ + $document1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $moviesId . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'documentId' => ID::unique(), 'data' => [ 'title' => 'Captain America', - 'releaseYear' => 1944, - 'birthDay' => '1975-06-12 14:12:55+02:00', - 'actors' => [ - 'Chris Evans', - 'Samuel Jackson', - ] ], 'permissions' => [ Permission::write(Role::user($this->getUser()['$id'])), @@ -80,19 +88,13 @@ class DatabasesCustomClientTest extends Scope */ // Document does not allow create permission - $document2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/documents', array_merge([ + $document2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $moviesId . '/documents', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'documentId' => ID::unique(), 'data' => [ 'title' => 'Captain America', - 'releaseYear' => 1944, - 'birthDay' => '1975-06-12 14:12:55+02:00', - 'actors' => [ - 'Chris Evans', - 'Samuel Jackson', - ] ], 'permissions' => [ Permission::create(Role::user($this->getUser()['$id'])), diff --git a/tests/e2e/Services/Databases/DatabasesPermissionsGuestTest.php b/tests/e2e/Services/Databases/DatabasesPermissionsGuestTest.php index 067e477695..c2293e30f4 100644 --- a/tests/e2e/Services/Databases/DatabasesPermissionsGuestTest.php +++ b/tests/e2e/Services/Databases/DatabasesPermissionsGuestTest.php @@ -225,4 +225,54 @@ class DatabasesPermissionsGuestTest extends Scope Authorization::setRole($role); } } + + public function testWriteDocumentWithPermissions() + { + $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'] + ]), [ + 'databaseId' => ID::unique(), + 'name' => 'GuestPermissionsWrite', + ]); + $this->assertEquals(201, $database['headers']['status-code']); + $this->assertEquals('InvalidDocumentDatabase', $database['body']['name']); + + $databaseId = $database['body']['$id']; + $movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', $this->getServerHeader(), [ + 'collectionId' => ID::unique(), + 'name' => 'Movies', + 'permissions' => [ + Permission::create(Role::any()), + ], + 'documentSecurity' => true + ]); + + $moviesId = $movies['body']['$id']; + + $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $moviesId . '/attributes/string', $this->getServerHeader(), [ + 'key' => 'title', + 'size' => 256, + 'required' => true, + ]); + + sleep(1); + + $document = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $moviesId . '/documents', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], [ + 'documentId' => ID::unique(), + 'data' => [ + 'title' => 'Thor: Ragnarok', + ], + 'permissions' => [ + Permission::read(Role::any()), + ] + ]); + + $this->assertEquals(201, $document['headers']['status-code']); + $this->assertEquals('Thor: Ragnarok', $document['body']['title']); + } } diff --git a/tests/e2e/Services/Storage/StorageCustomClientTest.php b/tests/e2e/Services/Storage/StorageCustomClientTest.php index f24e8c0f21..0e4daa9957 100644 --- a/tests/e2e/Services/Storage/StorageCustomClientTest.php +++ b/tests/e2e/Services/Storage/StorageCustomClientTest.php @@ -1093,10 +1093,10 @@ class StorageCustomClientTest extends Scope $this->assertContains(Permission::delete(Role::user($this->getUser()['$id'])), $bucket['body']['$permissions']); // File aliases write to update, delete - $file1 = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', [ + $file1 = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ 'content-type' => 'multipart/form-data', 'x-appwrite-project' => $this->getProject()['$id'], - ], [ + ], $this->getHeaders()), [ 'fileId' => ID::unique(), 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'permissions.png'), 'permissions' => [