adds test cases for topic controller

This commit is contained in:
prateek banga
2023-08-25 04:36:41 +05:30
parent b8f8fb1708
commit d0c50403c8
6 changed files with 144 additions and 30 deletions
+3 -1
View File
@@ -25,7 +25,9 @@ $member = [
'providers.write',
'providers.read',
'messages.write',
'messages.read'
'messages.read',
'topics.write',
'topics.read'
];
$admins = [
+21 -27
View File
@@ -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();
});
+2
View File
@@ -87,6 +87,8 @@ trait ProjectCustom
'providers.write',
'messages.read',
'messages.write',
'topics.write',
'topics.read'
],
]);
+88 -2
View File
@@ -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']);
}
}
}
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']);
}
}
@@ -0,0 +1,15 @@
<?php
namespace Tests\E2E\Services\Messaging;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideConsole;
class MessagingConsoleClientTest extends Scope
{
use MessagingBase;
use ProjectCustom;
use SideConsole;
}
@@ -0,0 +1,15 @@
<?php
namespace Tests\E2E\Services\Messaging;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
class MessagingCustomClientTest extends Scope
{
use MessagingBase;
use ProjectCustom;
use SideClient;
}