From d0c50403c8a02efce65df46fa3da08f7b4586785 Mon Sep 17 00:00:00 2001 From: prateek banga Date: Fri, 25 Aug 2023 04:36:41 +0530 Subject: [PATCH] adds test cases for topic controller --- app/config/roles.php | 4 +- app/controllers/api/messaging.php | 48 +++++----- tests/e2e/Scopes/ProjectCustom.php | 2 + .../e2e/Services/Messaging/MessagingBase.php | 90 ++++++++++++++++++- .../Messaging/MessagingConsoleClientTest.php | 15 ++++ .../Messaging/MessagingCustomClientTest.php | 15 ++++ 6 files changed, 144 insertions(+), 30 deletions(-) create mode 100644 tests/e2e/Services/Messaging/MessagingConsoleClientTest.php create mode 100644 tests/e2e/Services/Messaging/MessagingCustomClientTest.php diff --git a/app/config/roles.php b/app/config/roles.php index 4bc85f7a28..4666268d0c 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -25,7 +25,9 @@ $member = [ 'providers.write', 'providers.read', 'messages.write', - 'messages.read' + 'messages.read', + 'topics.write', + 'topics.read' ]; $admins = [ diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index a0bf2d2ead..4f14903f0c 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -936,7 +936,7 @@ App::get('/v1/messaging/topics') ->desc('List topics.') ->groups(['api', 'messaging']) ->label('scope', 'topics.read') - ->label('sdk.auth', [APP_AUTH_TYPE_JWT, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) ->label('sdk.namespace', 'messaging') ->label('sdk.method', 'listTopics') ->label('sdk.description', '/docs/references/messaging/list-topics.md') @@ -970,7 +970,7 @@ App::get('/v1/messaging/topics') $filterQueries = Query::groupByType($queries)['filters']; $response->dynamic(new Document([ 'total' => $dbForProject->count('topics', $filterQueries, APP_LIMIT_COUNT), - 'indexes' => $dbForProject->find('topics', $queries), + 'topics' => $dbForProject->find('topics', $queries), ]), Response::MODEL_TOPIC_LIST); }); @@ -978,7 +978,7 @@ App::get('/v1/messaging/topics/:id') ->desc('Get a topic.') ->groups(['api', 'messaging']) ->label('scope', 'topics.read') - ->label('sdk.auth', [APP_AUTH_TYPE_JWT, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) ->label('sdk.namespace', 'messaging') ->label('sdk.method', 'getTopic') ->label('sdk.description', '/docs/references/messaging/get-topic.md') @@ -1007,21 +1007,21 @@ App::post('/v1/messaging/topics') ->label('audits.event', 'topics.create') ->label('audits.resource', 'topics/{response.$id}') ->label('scope', 'topics.write') - ->label('sdk.auth', [APP_AUTH_TYPE_JWT, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) ->label('sdk.namespace', 'messaging') ->label('sdk.method', 'createTopic') ->label('sdk.description', '/docs/references/messaging/create-topic.md') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_TOPIC) - ->param('id', '', new CustomId(), 'Topic ID. Choose a custom Topic ID or a new Topic ID.') + ->param('topicId', '', new CustomId(), 'Topic ID. Choose a custom Topic ID or a new Topic ID.') ->param('providerId', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Topic Name.') ->param('description', '', new Text(2048), 'Topic Description.', true) - ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $providerId, string $name, string $description, Document $user, Database $dbForProject, Response $response) { + ->action(function (string $topicId, string $providerId, string $name, string $description, Database $dbForProject, Response $response) { + $topicId = $topicId == 'unique()' ? ID::unique() : $topicId; $provider = $dbForProject->getDocument('providers', $providerId); if ($provider->isEmpty()) { @@ -1029,16 +1029,10 @@ App::post('/v1/messaging/topics') } $topic = new Document([ - '$id' => $id, - '$permissions' => [ - Permission::read(Role::any()), - Permission::update(Role::user($user->getId())), - Permission::delete(Role::user($user->getId())), - ], + '$id' => $topicId, 'providerId' => $providerId, 'providerInternalId' => $provider->getInternalId(), 'name' => $name, - ]); if ($description) { @@ -1052,26 +1046,26 @@ App::post('/v1/messaging/topics') ->dynamic($topic, Response::MODEL_TOPIC); }); -App::patch('/v1/messaging/topics/:id') +App::patch('/v1/messaging/topics/:topicId') ->desc('Update a topic.') ->groups(['api', 'messaging']) ->label('audits.event', 'topics.update') ->label('audits.resource', 'topics/{response.$id}') ->label('scope', 'topics.write') - ->label('sdk.auth', [APP_AUTH_TYPE_JWT, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) ->label('sdk.namespace', 'messaging') ->label('sdk.method', 'updateTopic') ->label('sdk.description', '/docs/references/messaging/update-topic.md') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_TOPIC) - ->param('id', '', new UID(), 'Topic ID.') + ->param('topicId', '', new UID(), 'Topic ID.') ->param('name', '', new Text(128), 'Topic Name.', true) ->param('description', null, new Text(128), 'Topic Description.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, string $description, Database $dbForProject, Response $response) { - $topic = $dbForProject->getDocument('topics', $id); + ->action(function (string $topicId, string $name, string $description, Database $dbForProject, Response $response) { + $topic = $dbForProject->getDocument('topics', $topicId); if ($topic->isEmpty()) { throw new Exception(Exception::TOPIC_NOT_FOUND); @@ -1085,36 +1079,36 @@ App::patch('/v1/messaging/topics/:id') $topic->setAttribute('description', $description); } - $topic = $dbForProject->updateDocument('topics', $id, $topic); + $topic = $dbForProject->updateDocument('topics', $topicId, $topic); $response ->dynamic($topic, Response::MODEL_TOPIC); }); -App::delete('/v1/messaging/topics/:id') +App::delete('/v1/messaging/topics/:topicId') ->desc('Delete a topic.') ->groups(['api', 'messaging']) ->label('audits.event', 'topics.delete') - ->label('audits.resource', 'topics/{request.id}') + ->label('audits.resource', 'topics/{request.topicId}') ->label('scope', 'topics.write') - ->label('sdk.auth', [APP_AUTH_TYPE_JWT, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) ->label('sdk.namespace', 'messaging') ->label('sdk.method', 'deleteTopic') ->label('sdk.description', '/docs/references/messaging/delete-topic.md') ->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_NONE) - ->param('id', '', new UID(), 'Topic ID.') + ->param('topicId', '', new UID(), 'Topic ID.') ->inject('dbForProject') ->inject('response') - ->action(function (string $id, Database $dbForProject, Response $response) { - $topic = $dbForProject->getDocument('topics', $id); + ->action(function (string $topicId, Database $dbForProject, Response $response) { + $topic = $dbForProject->getDocument('topics', $topicId); if ($topic->isEmpty()) { throw new Exception(Exception::TOPIC_NOT_FOUND); } - $topic = $dbForProject->deleteDocument('topics', $id); + $topic = $dbForProject->deleteDocument('topics', $topicId); $response->noContent(); }); diff --git a/tests/e2e/Scopes/ProjectCustom.php b/tests/e2e/Scopes/ProjectCustom.php index 2a1b0a8039..140c3b8137 100644 --- a/tests/e2e/Scopes/ProjectCustom.php +++ b/tests/e2e/Scopes/ProjectCustom.php @@ -87,6 +87,8 @@ trait ProjectCustom 'providers.write', 'messages.read', 'messages.write', + 'topics.write', + 'topics.read' ], ]); diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index 8a0937c0eb..5303fbb5c9 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -6,7 +6,7 @@ use Tests\E2E\Client; trait MessagingBase { - public function testCreateProviders(): array + public function testCreateProviders(): array { $providersParams = [ 'sendgrid' => [ @@ -190,4 +190,90 @@ trait MessagingBase $this->assertEquals(204, $response['headers']['status-code']); } } -} \ No newline at end of file + + public function testCreateTopic(): string + { + $provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/sendgrid', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]), [ + 'id' => 'unique()', + 'name' => 'Sengrid1', + 'apiKey' => 'my-apikey', + ]); + $this->assertEquals(201, $provider['headers']['status-code']); + $response = $this->client->call(Client::METHOD_POST, '/messaging/topics', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => $provider['body']['$id'], + 'topicId' => 'unique()', + 'name' => 'my-app', + 'description' => 'web app' + ]); + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertEquals('my-app', $response['body']['name']); + + return $response['body']['$id']; + } + + /** + * @depends testCreateTopic + */ + public function testUpdateTopic(string $topicId): string + { + $response = $this->client->call(Client::METHOD_PATCH, '/messaging/topics/' . $topicId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'name' => 'android-app', + 'description' => 'updated-description' + ]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('android-app', $response['body']['name']); + $this->assertEquals('updated-description', $response['body']['description']); + return $topicId; + } + + public function testListTopic() + { + $response = $this->client->call(Client::METHOD_GET, '/messaging/topics', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(1, \count($response['body']['topics'])); + } + + /** + * @depends testUpdateTopic + */ + public function testGetTopic(string $topicId) + { + $response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' .$topicId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('android-app', $response['body']['name']); + $this->assertEquals('updated-description', $response['body']['description']); + } + + /** + * @depends testUpdateTopic + */ + public function testDeleteTopic(string $topicId) + { + $response = $this->client->call(Client::METHOD_DELETE, '/messaging/topics/' .$topicId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(204, $response['headers']['status-code']); + } +} diff --git a/tests/e2e/Services/Messaging/MessagingConsoleClientTest.php b/tests/e2e/Services/Messaging/MessagingConsoleClientTest.php new file mode 100644 index 0000000000..8250c0229a --- /dev/null +++ b/tests/e2e/Services/Messaging/MessagingConsoleClientTest.php @@ -0,0 +1,15 @@ +