From 6aae8cbf64cbc4a5292ab677a755cb18354e8ac6 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 12 Feb 2024 15:27:19 +0200 Subject: [PATCH] Add cursor test --- app/controllers/api/messaging.php | 6 +- .../e2e/Services/Messaging/MessagingBase.php | 55 +++++++++++++++---- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 4b89befa27..aa0f44ac68 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -2987,9 +2987,7 @@ App::get('/v1/messaging/messages/:messageId/targets') ->param('queries', [], new Targets(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Targets::ALLOWED_ATTRIBUTES), true) ->inject('response') ->inject('dbForProject') - ->inject('locale') - ->inject('geodb') - ->action(function (string $messageId, array $queries, Response $response, Database $dbForProject, Locale $locale, Reader $geodb) { + ->action(function (string $messageId, array $queries, Response $response, Database $dbForProject) { $message = $dbForProject->getDocument('messages', $messageId); if ($message->isEmpty()) { @@ -3028,7 +3026,7 @@ App::get('/v1/messaging/messages/:messageId/targets') $cursor->setValue($cursorDocument); } - +var_dump($queries); $response->dynamic(new Document([ 'targets' => $dbForProject->find('targets', $queries), 'total' => $dbForProject->count('targets', $queries, APP_LIMIT_COUNT), diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index 59a5d5dec2..111f0294fc 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -5,6 +5,7 @@ namespace Tests\E2E\Services\Messaging; use Appwrite\Messaging\Status as MessageStatus; use Tests\E2E\Client; use Utopia\App; +use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; use Utopia\DSN\DSN; @@ -682,12 +683,27 @@ trait MessagingBase 'x-appwrite-key' => $this->getProject()['apiKey'], ]); - $this->assertEquals(200, $response['headers']['status-code']); - $targetList = $response['body']; - $this->assertEquals(1, $targetList['total']); - $this->assertEquals(1, count($targetList['targets'])); + $this->assertEquals(2, $targetList['total']); + $this->assertEquals(2, count($targetList['targets'])); $this->assertEquals($message['targets'][0], $targetList['targets'][0]['$id']); + $this->assertEquals($message['targets'][1], $targetList['targets'][1]['$id']); + + /** + * Cursor Test + */ + $response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $message['$id'] . '/targets', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'queries' => [ + Query::cursorAfter(new Document(['$id' => $targetList['targets'][0]['$id']]))->toString(), + ] + ]); + $this->assertEquals(2, $response['body']['total']); + $this->assertEquals(1, count($response['body']['targets'])); + $this->assertEquals($targetList['targets'][1]['$id'], $response['body']['targets'][0]['$id']); // Test for empty targets $response = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [ @@ -719,7 +735,7 @@ trait MessagingBase public function testCreateDraftEmail() { - // Create User + // Create User 1 $response = $this->client->call(Client::METHOD_POST, '/users', [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -728,15 +744,33 @@ trait MessagingBase 'userId' => ID::unique(), 'email' => uniqid() . "@example.com", 'password' => 'password', - 'name' => 'Messaging User', + 'name' => 'Messaging User 1', ]); $this->assertEquals(201, $response['headers']['status-code'], "Error creating user: " . var_export($response['body'], true)); - $user = $response['body']; + $user1 = $response['body']; - $this->assertEquals(1, \count($user['targets'])); - $targetId = $user['targets'][0]['$id']; + $this->assertEquals(1, \count($user1['targets'])); + $targetId1 = $user1['targets'][0]['$id']; + + // Create User 2 + $response = $this->client->call(Client::METHOD_POST, '/users', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'userId' => ID::unique(), + 'email' => uniqid() . "@example.com", + 'password' => 'password', + 'name' => 'Messaging User 2', + ]); + + $this->assertEquals(201, $response['headers']['status-code'], "Error creating user: " . var_export($response['body'], true)); + $user2 = $response['body']; + + $this->assertEquals(1, \count($user2['targets'])); + $targetId2 = $user2['targets'][0]['$id']; // Create Email $response = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [ @@ -745,13 +779,12 @@ trait MessagingBase 'x-appwrite-key' => $this->getProject()['apiKey'], ], [ 'messageId' => ID::unique(), - 'targets' => [$targetId], + 'targets' => [$targetId1, $targetId2], 'subject' => 'New blog post', 'content' => 'Check out the new blog post at http://localhost', ]); $this->assertEquals(201, $response['headers']['status-code']); - $message = $response['body']; $this->assertEquals(MessageStatus::DRAFT, $message['status']);