From f534b9e460a1ce3727fd73168ace33dfffd039a3 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 21 May 2025 11:00:01 +0530 Subject: [PATCH 1/6] added checking for encrypt and plan allowing encryption of string attribute --- app/controllers/api/databases.php | 6 +++++- tests/e2e/Services/Databases/DatabasesBase.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 8883e245b6..90b482e0b8 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1347,7 +1347,11 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string ->inject('dbForProject') ->inject('queueForDatabase') ->inject('queueForEvents') - ->action(function (string $databaseId, string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, bool $encrypt, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents) { + ->inject('plan') + ->action(function (string $databaseId, string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, bool $encrypt, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents, array $plan) { + if ($encrypt && !isset($plan['databasesAllowEncrypt'])) { + throw new Exception(Exception::ATTRIBUTE_FORMAT_UNSUPPORTED, 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); + } // Ensure attribute default is within required size $validator = new Text($size, 0); if (!is_null($default) && !$validator->isValid($default)) { diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index 7c0060ecaa..c16701db42 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -195,6 +195,20 @@ trait DatabasesBase public function testCreateAttributes(array $data): array { $databaseId = $data['databaseId']; + // must fail as we are using encrypt and plan has no 'databasesAllowEncrypt' + $encryptStringAttribute = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['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, + 'encrypt' => true + ]); + + $this->assertEquals(400, $encryptStringAttribute['headers']['status-code']); + $this->assertEquals($encryptStringAttribute['body']['message'], 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); $title = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/attributes/string', array_merge([ 'content-type' => 'application/json', From 43aae24132f6a60f169848c365c58ad3b1459595 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 21 May 2025 11:38:34 +0530 Subject: [PATCH 2/6] updated tests for the custom server test --- .../Databases/DatabasesCustomServerTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 829960b54f..36b50bed62 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -687,6 +687,7 @@ class DatabasesCustomServerTest extends Scope 'required' => true, ]); + // must fail due to encrpt and no plan $lastName = $this->client->call(Client::METHOD_POST, $attributesPath . '/string', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -698,6 +699,18 @@ class DatabasesCustomServerTest extends Scope 'encrypt' => true, ]); + $this->assertEquals(400, $lastName['headers']['status-code']); + + $lastName = $this->client->call(Client::METHOD_POST, $attributesPath . '/string', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'key' => 'lastName', + 'size' => 256, + 'required' => true, + ]); + /** * Check status of every attribute From 9fa6deae6104fd3d1308de9a01b774e3b54c0b38 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 21 May 2025 13:14:22 +0530 Subject: [PATCH 3/6] reverted the tests and updated the plan checking condition --- app/controllers/api/databases.php | 2 +- tests/e2e/Services/Databases/DatabasesBase.php | 14 -------------- .../Databases/DatabasesCustomServerTest.php | 14 -------------- 3 files changed, 1 insertion(+), 29 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 90b482e0b8..c2c1a9d6b5 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1349,7 +1349,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string ->inject('queueForEvents') ->inject('plan') ->action(function (string $databaseId, string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, bool $encrypt, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents, array $plan) { - if ($encrypt && !isset($plan['databasesAllowEncrypt'])) { + if ($encrypt && !empty($plan) && !isset($plan['databasesAllowEncrypt'])) { throw new Exception(Exception::ATTRIBUTE_FORMAT_UNSUPPORTED, 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); } // Ensure attribute default is within required size diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index c16701db42..7c0060ecaa 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -195,20 +195,6 @@ trait DatabasesBase public function testCreateAttributes(array $data): array { $databaseId = $data['databaseId']; - // must fail as we are using encrypt and plan has no 'databasesAllowEncrypt' - $encryptStringAttribute = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['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, - 'encrypt' => true - ]); - - $this->assertEquals(400, $encryptStringAttribute['headers']['status-code']); - $this->assertEquals($encryptStringAttribute['body']['message'], 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); $title = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $data['moviesId'] . '/attributes/string', array_merge([ 'content-type' => 'application/json', diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index 36b50bed62..b0517a9fbd 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -687,20 +687,6 @@ class DatabasesCustomServerTest extends Scope 'required' => true, ]); - // must fail due to encrpt and no plan - $lastName = $this->client->call(Client::METHOD_POST, $attributesPath . '/string', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'key' => 'lastName', - 'size' => 256, - 'required' => true, - 'encrypt' => true, - ]); - - $this->assertEquals(400, $lastName['headers']['status-code']); - $lastName = $this->client->call(Client::METHOD_POST, $attributesPath . '/string', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], From 8796670c667b94ff841c410fbadb167388922a17 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 21 May 2025 13:19:07 +0530 Subject: [PATCH 4/6] reverted test --- tests/e2e/Services/Databases/DatabasesCustomServerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index b0517a9fbd..67f7a07a9b 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -695,6 +695,7 @@ class DatabasesCustomServerTest extends Scope 'key' => 'lastName', 'size' => 256, 'required' => true, + 'encrypt' => true ]); From 82fd8553bf4745c89aaa21c55016209fb9e56073 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 21 May 2025 13:29:24 +0530 Subject: [PATCH 5/6] updated the condition --- 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 c2c1a9d6b5..5d6533e2b2 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1349,7 +1349,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string ->inject('queueForEvents') ->inject('plan') ->action(function (string $databaseId, string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, bool $encrypt, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents, array $plan) { - if ($encrypt && !empty($plan) && !isset($plan['databasesAllowEncrypt'])) { + if ($encrypt && !empty($plan) && !($plan['databasesAllowEncrypt'] ?? false)) { throw new Exception(Exception::ATTRIBUTE_FORMAT_UNSUPPORTED, 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); } // Ensure attribute default is within required size From c4df1c8c4b7e9f505155f3cbc1f73f3032b6a6f1 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Wed, 21 May 2025 13:34:08 +0530 Subject: [PATCH 6/6] updated exception type --- 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 5d6533e2b2..d495a1d924 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1350,7 +1350,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/string ->inject('plan') ->action(function (string $databaseId, string $collectionId, string $key, ?int $size, ?bool $required, ?string $default, bool $array, bool $encrypt, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents, array $plan) { if ($encrypt && !empty($plan) && !($plan['databasesAllowEncrypt'] ?? false)) { - throw new Exception(Exception::ATTRIBUTE_FORMAT_UNSUPPORTED, 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); + throw new Exception(Exception::GENERAL_BAD_REQUEST, 'Encrypted string attributes are not available on your plan. Please upgrade to create encrypted string attributes.'); } // Ensure attribute default is within required size $validator = new Text($size, 0);