From 55fc9d7fbdecfabe29c05e0c7b31571bd1516e7f Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 29 Jul 2024 10:12:57 +0000 Subject: [PATCH] tests and fixes --- app/console | 1 + .../Modules/DevelopmentKeys/Http/Create.php | 2 +- .../Modules/DevelopmentKeys/Http/Delete.php | 3 +- .../Modules/DevelopmentKeys/Http/Get.php | 1 + .../Modules/DevelopmentKeys/Http/Update.php | 3 +- .../Modules/DevelopmentKeys/Http/XList.php | 1 + .../Modules/DevelopmentKeys/Services/Http.php | 6 +- .../Projects/ProjectsConsoleClientTest.php | 2 + .../Projects/ProjectsDevelopmentKeys.php | 247 ++++++++++++++++++ 9 files changed, 260 insertions(+), 6 deletions(-) create mode 160000 app/console create mode 100644 tests/e2e/Services/Projects/ProjectsDevelopmentKeys.php diff --git a/app/console b/app/console new file mode 160000 index 0000000000..f978cecfaf --- /dev/null +++ b/app/console @@ -0,0 +1 @@ +Subproject commit f978cecfafe55e85fcd20fe90fc020e78a7c7952 diff --git a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Create.php b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Create.php index cb588d3696..c1f164987f 100644 --- a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Create.php +++ b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Create.php @@ -66,7 +66,7 @@ class Create extends Action 'expire' => $expire, 'sdks' => [], 'accessedAt' => null, - 'secret' => API_KEY_STANDARD . '_' . \bin2hex(\random_bytes(128)), + 'secret' => API_KEY_TEST . '_' . \bin2hex(\random_bytes(128)), ]); $key = $dbForConsole->createDocument('developmentKeys', $key); diff --git a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Delete.php b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Delete.php index a1e0dad5b0..c088e1059e 100644 --- a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Delete.php +++ b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Delete.php @@ -1,4 +1,5 @@ deleteDocument('keys', $key->getId()); + $dbForConsole->deleteDocument('developmentKeys', $key->getId()); $dbForConsole->purgeCachedDocument('projects', $project->getId()); diff --git a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Get.php b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Get.php index 6f9f2d73b2..43435a6a27 100644 --- a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Get.php +++ b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/Get.php @@ -1,4 +1,5 @@ setAttribute('name', $name) ->setAttribute('expire', $expire); - $dbForConsole->updateDocument('keys', $key->getId(), $key); + $dbForConsole->updateDocument('developmentKeys', $key->getId(), $key); $dbForConsole->purgeCachedDocument('projects', $project->getId()); diff --git a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/XList.php b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/XList.php index 49605b0fdd..65198037b2 100644 --- a/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/XList.php +++ b/src/Appwrite/Platform/Modules/DevelopmentKeys/Http/XList.php @@ -1,4 +1,5 @@ client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Key Test' + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertEquals('Key Test', $response['body']['name']); + $this->assertNotEmpty($response['body']['secret']); + $this->assertArrayHasKey('sdks', $response['body']); + $this->assertEmpty($response['body']['sdks']); + $this->assertArrayHasKey('accessedAt', $response['body']); + $this->assertEmpty($response['body']['accessedAt']); + + $data = array_merge($data, [ + 'keyId' => $response['body']['$id'], + 'secret' => $response['body']['secret'] + ]); + + return $data; + } + + + /** + * @depends testCreateProjectDevelopmentKey + * @group developmentKeys + */ + public function testListProjectDevelopmentKey($data): array + { + $id = $data['projectId'] ?? ''; + + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(1, $response['body']['total']); + + return $data; + } + + + /** + * @depends testCreateProjectDevelopmentKey + * @group developmentKeys + */ + public function testGetProjectDevelopmentKey($data): array + { + $id = $data['projectId'] ?? ''; + $keyId = $data['keyId'] ?? ''; + + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertEquals($keyId, $response['body']['$id']); + $this->assertEquals('Key Test', $response['body']['name']); + $this->assertNotEmpty($response['body']['secret']); + $this->assertArrayHasKey('sdks', $response['body']); + $this->assertEmpty($response['body']['sdks']); + $this->assertArrayHasKey('accessedAt', $response['body']); + $this->assertEmpty($response['body']['accessedAt']); + + /** + * Test for FAILURE + */ + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/error', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(404, $response['headers']['status-code']); + + return $data; + } + + /** + * @depends testCreateProject + * @group developmentKeys + */ + public function testValidateProjectDevelopmentKey($data): void + { + $id = $data['projectId'] ?? ''; + + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Key Test', + 'expire' => DateTime::addSeconds(new \DateTime(), 3600), + ]); + var_dump($response['body']['secret']); + $response = $this->client->call(Client::METHOD_GET, '/health', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $id, + 'x-appwrite-key' => $response['body']['secret'] + ], []); + + $this->assertEquals(200, $response['headers']['status-code']); + + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Key Test', + 'expire' => null, + ]); + + $response = $this->client->call(Client::METHOD_GET, '/health', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $id, + 'x-appwrite-key' => $response['body']['secret'] + ], []); + + $this->assertEquals(200, $response['headers']['status-code']); + + /** + * Test for FAILURE + */ + $response = $this->client->call(Client::METHOD_POST, '/projects/' . $id . '/development-keys', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Key Test', + 'expire' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $response = $this->client->call(Client::METHOD_GET, '/health', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $id, + 'x-appwrite-key' => $response['body']['secret'] + ], []); + + $this->assertEquals(401, $response['headers']['status-code']); + } + + + /** + * @depends testCreateProjectDevelopmentKey + * @group developmentKeys + */ + public function testUpdateProjectDevelopmentKey($data): array + { + $id = $data['projectId'] ?? ''; + $keyId = $data['keyId'] ?? ''; + + $response = $this->client->call(Client::METHOD_PUT, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'name' => 'Key Test Update', + 'expire' => DateTime::addSeconds(new \DateTime(), 360), + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertEquals($keyId, $response['body']['$id']); + $this->assertEquals('Key Test Update', $response['body']['name']); + $this->assertArrayHasKey('sdks', $response['body']); + $this->assertEmpty($response['body']['sdks']); + $this->assertArrayHasKey('accessedAt', $response['body']); + $this->assertEmpty($response['body']['accessedAt']); + + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['$id']); + $this->assertEquals($keyId, $response['body']['$id']); + $this->assertEquals('Key Test Update', $response['body']['name']); + $this->assertArrayHasKey('sdks', $response['body']); + $this->assertEmpty($response['body']['sdks']); + $this->assertArrayHasKey('accessedAt', $response['body']); + $this->assertEmpty($response['body']['accessedAt']); + + return $data; + } + + /** + * @depends testCreateProjectDevelopmentKey + * @group developmentKeys + */ + public function testDeleteProjectDevelopmentKey($data): array + { + $id = $data['projectId'] ?? ''; + $keyId = $data['keyId'] ?? ''; + + $response = $this->client->call(Client::METHOD_DELETE, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(204, $response['headers']['status-code']); + $this->assertEmpty($response['body']); + + $response = $this->client->call(Client::METHOD_GET, '/projects/' . $id . '/development-keys/' . $keyId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(404, $response['headers']['status-code']); + + /** + * Test for FAILURE + */ + $response = $this->client->call(Client::METHOD_DELETE, '/projects/' . $id . '/development-keys/error', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(404, $response['headers']['status-code']); + + return $data; + } +}