1
0
Fork 0
mirror of synced 2024-06-14 00:34:51 +12:00
appwrite/tests/e2e/Services/Messaging/MessagingBase.php

1727 lines
68 KiB
PHP
Raw Normal View History

2023-08-23 22:01:29 +12:00
<?php
namespace Tests\E2E\Services\Messaging;
2024-02-02 18:48:34 +13:00
use Appwrite\Messaging\Status as MessageStatus;
2024-03-08 22:58:46 +13:00
use CURLFile;
2023-08-23 22:01:29 +12:00
use Tests\E2E\Client;
2023-09-28 02:38:35 +13:00
use Utopia\App;
use Utopia\Database\DateTime;
2024-02-13 02:27:19 +13:00
use Utopia\Database\Document;
2023-08-29 03:11:05 +12:00
use Utopia\Database\Helpers\ID;
2024-03-08 22:58:46 +13:00
use Utopia\Database\Helpers\Permission;
2024-02-13 17:41:07 +13:00
use Utopia\Database\Helpers\Role;
2024-01-26 05:53:51 +13:00
use Utopia\Database\Query;
2023-10-31 09:35:50 +13:00
use Utopia\DSN\DSN;
2023-08-23 22:01:29 +12:00
2023-08-25 09:39:12 +12:00
trait MessagingBase
2023-08-23 22:01:29 +12:00
{
2023-08-25 11:06:41 +12:00
public function testCreateProviders(): array
2023-08-23 22:01:29 +12:00
{
$providersParams = [
'sendgrid' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Sengrid1',
'apiKey' => 'my-apikey',
'from' => 'sender-email@my-domain.com',
2023-08-23 22:01:29 +12:00
],
'mailgun' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Mailgun1',
'apiKey' => 'my-apikey',
'domain' => 'my-domain',
2023-12-06 07:24:55 +13:00
'fromName' => 'sender name',
'fromEmail' => 'sender-email@my-domain.com',
'isEuRegion' => false,
2023-08-23 22:01:29 +12:00
],
2024-02-01 01:30:09 +13:00
'smtp' => [
'providerId' => ID::unique(),
'name' => 'SMTP1',
'host' => 'smtp.appwrite.io',
'port' => 587,
'security' => 'tls',
'username' => 'my-username',
'password' => 'my-password',
'fromName' => 'sender name',
'fromEmail' => 'tester@appwrite.io',
],
2023-08-23 22:01:29 +12:00
'twilio' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Twilio1',
'accountSid' => 'my-accountSid',
'authToken' => 'my-authToken',
2023-10-24 08:35:46 +13:00
'from' => '+123456789',
2023-08-23 22:01:29 +12:00
],
'telesign' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Telesign1',
2024-02-13 15:20:10 +13:00
'customerId' => 'my-username',
'apiKey' => 'my-password',
2023-10-24 08:35:46 +13:00
'from' => '+123456789',
2023-08-23 22:01:29 +12:00
],
'textmagic' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Textmagic1',
'username' => 'my-username',
'apiKey' => 'my-apikey',
2023-10-24 08:35:46 +13:00
'from' => '+123456789',
2023-08-23 22:01:29 +12:00
],
'msg91' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Ms91-1',
'senderId' => 'my-senderid',
'authKey' => 'my-authkey',
'from' => '+123456789'
2023-08-23 22:01:29 +12:00
],
'vonage' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'Vonage1',
'apiKey' => 'my-apikey',
'apiSecret' => 'my-apisecret',
2023-10-24 08:35:46 +13:00
'from' => '+123456789',
2023-08-23 22:01:29 +12:00
],
'fcm' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'FCM1',
2023-12-20 07:15:20 +13:00
'serviceAccountJSON' => [
'type' => 'service_account',
2024-01-09 13:41:55 +13:00
"project_id" => "test-project",
"private_key_id" => "test-private-key-id",
"private_key" => "test-private-key",
2023-12-20 07:15:20 +13:00
],
2023-08-23 22:01:29 +12:00
],
'apns' => [
2023-10-05 01:00:40 +13:00
'providerId' => ID::unique(),
2023-08-23 22:01:29 +12:00
'name' => 'APNS1',
'authKey' => 'my-authkey',
'authKeyId' => 'my-authkeyid',
'teamId' => 'my-teamid',
'bundleId' => 'my-bundleid',
],
];
$providers = [];
2024-02-24 13:46:45 +13:00
foreach ($providersParams as $key => $params) {
$response = $this->client->call(Client::METHOD_POST, '/messaging/providers/' . $key, \array_merge([
2023-08-23 22:01:29 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2023-08-25 09:39:12 +12:00
'x-appwrite-key' => $this->getProject()['apiKey'],
2024-02-24 13:46:45 +13:00
]), $params);
2023-12-17 12:41:20 +13:00
2023-08-23 22:01:29 +12:00
$this->assertEquals(201, $response['headers']['status-code']);
2024-02-24 13:46:45 +13:00
$this->assertEquals($params['name'], $response['body']['name']);
$providers[] = $response['body'];
switch ($key) {
case 'apns':
$this->assertEquals(false, $response['body']['options']['sandbox']);
break;
}
2023-08-23 22:01:29 +12:00
}
return $providers;
}
/**
* @depends testCreateProviders
*/
public function testUpdateProviders(array $providers): array
{
$providersParams = [
'sendgrid' => [
'name' => 'Sengrid2',
'apiKey' => 'my-apikey',
],
'mailgun' => [
'name' => 'Mailgun2',
'apiKey' => 'my-apikey',
'domain' => 'my-domain',
],
2024-02-01 01:30:09 +13:00
'smtp' => [
'name' => 'SMTP2',
'host' => 'smtp.appwrite.io',
'port' => 587,
'security' => 'tls',
'username' => 'my-username',
'password' => 'my-password',
],
2023-08-23 22:01:29 +12:00
'twilio' => [
'name' => 'Twilio2',
'accountSid' => 'my-accountSid',
'authToken' => 'my-authToken',
],
'telesign' => [
'name' => 'Telesign2',
2024-02-13 15:20:10 +13:00
'customerId' => 'my-username',
'apiKey' => 'my-password',
2023-08-23 22:01:29 +12:00
],
'textmagic' => [
'name' => 'Textmagic2',
'username' => 'my-username',
'apiKey' => 'my-apikey',
],
'msg91' => [
'name' => 'Ms91-2',
'senderId' => 'my-senderid',
'authKey' => 'my-authkey',
],
'vonage' => [
'name' => 'Vonage2',
'apiKey' => 'my-apikey',
'apiSecret' => 'my-apisecret',
],
'fcm' => [
'name' => 'FCM2',
2023-12-20 07:15:20 +13:00
'serviceAccountJSON' => [
'type' => 'service_account',
2024-01-09 13:41:55 +13:00
"project_id" => "test-project",
"private_key_id" => "test-private-key-id",
"private_key" => "test-private-key",
2023-12-20 07:15:20 +13:00
]
2023-08-23 22:01:29 +12:00
],
'apns' => [
'name' => 'APNS2',
'authKey' => 'my-authkey',
'authKeyId' => 'my-authkeyid',
'teamId' => 'my-teamid',
'bundleId' => 'my-bundleid',
],
];
foreach (\array_keys($providersParams) as $index => $name) {
$response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/' . $name . '/' . $providers[$index]['$id'], [
2023-08-23 22:01:29 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $providersParams[$name]);
2023-08-23 22:01:29 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($providersParams[$name]['name'], $response['body']['name']);
if ($name === 'smtp') {
$this->assertArrayHasKey('encryption', $response['body']['options']);
$this->assertArrayHasKey('autoTLS', $response['body']['options']);
$this->assertArrayHasKey('mailer', $response['body']['options']);
$this->assertArrayNotHasKey('encryption', $response['body']['credentials']);
$this->assertArrayNotHasKey('autoTLS', $response['body']['credentials']);
$this->assertArrayNotHasKey('mailer', $response['body']['credentials']);
}
$providers[$index] = $response['body'];
2023-08-23 22:01:29 +12:00
}
2023-10-04 23:45:59 +13:00
$response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/mailgun/' . $providers[1]['$id'], [
2023-09-20 22:25:22 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2024-01-04 13:06:50 +13:00
'name' => 'Mailgun2',
'apiKey' => 'my-apikey',
'domain' => 'my-domain',
'isEuRegion' => true,
'enabled' => false,
2023-09-20 22:25:22 +12:00
]);
2023-09-20 22:25:22 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Mailgun2', $response['body']['name']);
$this->assertEquals(false, $response['body']['enabled']);
$providers[1] = $response['body'];
2023-08-23 22:01:29 +12:00
return $providers;
}
public function testUpdateProviderMissingCredentialsThrows(): void
{
// Create new FCM provider with no serviceAccountJSON
2024-01-24 17:34:59 +13:00
$response = $this->client->call(Client::METHOD_POST, '/messaging/providers/fcm', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-01-24 17:34:59 +13:00
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'providerId' => ID::unique(),
'name' => 'FCM3',
]);
$this->assertEquals(201, $response['headers']['status-code']);
// Enable provider with no serviceAccountJSON
2024-01-24 17:34:59 +13:00
$response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/fcm/' . $response['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
2024-01-24 17:34:59 +13:00
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'enabled' => true,
]);
$this->assertEquals(400, $response['headers']['status-code']);
}
2023-08-23 22:01:29 +12:00
/**
* @depends testUpdateProviders
*/
2023-08-23 22:06:12 +12:00
public function testListProviders(array $providers)
{
2023-08-23 22:01:29 +12:00
$response = $this->client->call(Client::METHOD_GET, '/messaging/providers/', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
2023-08-23 22:01:29 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
2024-02-01 01:30:09 +13:00
$this->assertEquals(11, \count($response['body']['providers']));
return $providers;
2023-08-23 22:01:29 +12:00
}
/**
* @depends testUpdateProviders
*/
2023-08-23 22:06:12 +12:00
public function testGetProvider(array $providers)
{
$response = $this->client->call(Client::METHOD_GET, '/messaging/providers/' . $providers[0]['$id'], [
2023-08-23 22:01:29 +12:00
'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($providers[0]['name'], $response['body']['name']);
}
/**
* @depends testUpdateProviders
*/
2023-08-23 22:06:12 +12:00
public function testDeleteProvider(array $providers)
{
2023-08-23 22:01:29 +12:00
foreach ($providers as $provider) {
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/providers/' . $provider['$id'], [
2023-08-23 22:01:29 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(204, $response['headers']['status-code']);
}
}
2023-08-25 11:06:41 +12:00
2023-08-29 03:11:05 +12:00
public function testCreateTopic(): array
2023-08-25 11:06:41 +12:00
{
2024-02-13 17:41:07 +13:00
$response1 = $this->client->call(Client::METHOD_POST, '/messaging/topics', [
2023-08-25 11:06:41 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
2023-08-25 11:06:41 +12:00
], [
2023-10-31 08:42:31 +13:00
'topicId' => ID::unique(),
'name' => 'my-app',
2023-08-25 11:06:41 +12:00
]);
2024-02-13 17:41:07 +13:00
$this->assertEquals(201, $response1['headers']['status-code']);
$this->assertEquals('my-app', $response1['body']['name']);
$response2 = $this->client->call(Client::METHOD_POST, '/messaging/topics', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'topicId' => ID::unique(),
'name' => 'my-app2',
'subscribe' => [Role::user('invalid')->toString()],
]);
$this->assertEquals(201, $response2['headers']['status-code']);
$this->assertEquals('my-app2', $response2['body']['name']);
$this->assertEquals(1, \count($response2['body']['subscribe']));
2023-08-25 11:06:41 +12:00
2024-02-13 17:41:07 +13:00
return [
'public' => $response1['body'],
'private' => $response2['body'],
];
2023-08-25 11:06:41 +12:00
}
/**
2023-08-25 11:06:41 +12:00
* @depends testCreateTopic
*/
2024-02-13 17:41:07 +13:00
public function testUpdateTopic(array $topics): string
2023-08-25 11:06:41 +12:00
{
2024-02-13 17:41:07 +13:00
$response = $this->client->call(Client::METHOD_PATCH, '/messaging/topics/' . $topics['public']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'name' => 'android-app',
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('android-app', $response['body']['name']);
2024-02-13 17:41:07 +13:00
2024-02-24 00:01:46 +13:00
$response2 = $this->client->call(Client::METHOD_PATCH, '/messaging/topics/' . $topics['private']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'name' => 'ios-app',
'subscribe' => [Role::user('some-user')->toString()],
]);
$this->assertEquals(200, $response2['headers']['status-code']);
$this->assertEquals('ios-app', $response2['body']['name']);
2023-08-29 03:11:05 +12:00
return $response['body']['$id'];
2023-08-25 11:06:41 +12:00
}
/**
* @depends testUpdateTopic
*/
public function testListTopic(string $topicId)
2023-08-25 11:06:41 +12:00
{
2024-01-04 13:06:50 +13:00
$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'],
], [
'queries' => [
2024-02-15 17:30:09 +13:00
Query::equal('emailTotal', [0])->toString(),
Query::equal('smsTotal', [0])->toString(),
Query::equal('pushTotal', [0])->toString(),
2024-01-04 13:06:50 +13:00
],
]);
$this->assertEquals(200, $response['headers']['status-code']);
2024-02-13 17:41:07 +13:00
$this->assertEquals(2, \count($response['body']['topics']));
2024-01-04 13:06:50 +13:00
$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'],
], [
'queries' => [
2024-02-15 17:30:09 +13:00
Query::greaterThan('emailTotal', 0)->toString(),
Query::greaterThan('smsTotal', 0)->toString(),
Query::greaterThan('pushTotal', 0)->toString(),
2024-01-04 13:06:50 +13:00
],
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(0, \count($response['body']['topics']));
return $topicId;
2023-08-25 11:06:41 +12:00
}
/**
2023-08-25 11:06:41 +12:00
* @depends testUpdateTopic
*/
public function testGetTopic(string $topicId)
{
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $topicId, [
2023-08-25 11:06:41 +12:00
'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']);
2024-02-15 17:30:09 +13:00
$this->assertEquals(0, $response['body']['emailTotal']);
$this->assertEquals(0, $response['body']['smsTotal']);
$this->assertEquals(0, $response['body']['pushTotal']);
2023-08-25 11:06:41 +12:00
}
2023-08-29 03:11:05 +12:00
/**
* @depends testCreateTopic
*/
2024-02-13 17:41:07 +13:00
public function testCreateSubscriber(array $topics)
2023-08-29 03:13:18 +12:00
{
2023-08-29 03:11:05 +12:00
$userId = $this->getUser()['$id'];
2023-10-27 03:14:06 +13:00
$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'],
]), [
'providerId' => ID::unique(),
'name' => 'Sendgrid1',
'apiKey' => 'my-apikey',
'from' => 'sender-email@my-domain.com',
]);
$this->assertEquals(201, $provider['headers']['status-code']);
2023-08-29 03:11:05 +12:00
$target = $this->client->call(Client::METHOD_POST, '/users/' . $userId . '/targets', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'targetId' => ID::unique(),
2023-11-15 01:44:07 +13:00
'providerType' => 'email',
2023-10-27 03:14:06 +13:00
'providerId' => $provider['body']['$id'],
2023-11-21 22:41:09 +13:00
'identifier' => 'random-email@mail.org',
2023-08-29 03:11:05 +12:00
]);
2023-08-29 03:11:05 +12:00
$this->assertEquals(201, $target['headers']['status-code']);
2024-02-13 17:41:07 +13:00
$response = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topics['public']['$id'] . '/subscribers', \array_merge([
2023-08-29 03:11:05 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'subscriberId' => ID::unique(),
2023-08-29 03:11:05 +12:00
'targetId' => $target['body']['$id'],
]);
2023-08-29 03:11:05 +12:00
$this->assertEquals(201, $response['headers']['status-code']);
2023-11-23 22:05:16 +13:00
$this->assertEquals($target['body']['userId'], $response['body']['target']['userId']);
$this->assertEquals($target['body']['providerType'], $response['body']['target']['providerType']);
2024-02-21 13:16:25 +13:00
// Test duplicate subscribers not allowed
2024-02-21 13:31:48 +13:00
$failure = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topics['public']['$id'] . '/subscribers', \array_merge([
2024-02-21 13:16:25 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'subscriberId' => ID::unique(),
'targetId' => $target['body']['$id'],
]);
2024-02-21 13:31:48 +13:00
$this->assertEquals(409, $failure['headers']['status-code']);
2024-02-21 13:16:25 +13:00
2024-02-13 17:41:07 +13:00
$topic = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $topics['public']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $topic['headers']['status-code']);
$this->assertEquals('android-app', $topic['body']['name']);
2024-02-15 17:30:09 +13:00
$this->assertEquals(1, $topic['body']['emailTotal']);
$this->assertEquals(0, $topic['body']['smsTotal']);
$this->assertEquals(0, $topic['body']['pushTotal']);
2024-02-13 17:41:07 +13:00
$response2 = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topics['private']['$id'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'subscriberId' => ID::unique(),
'targetId' => $target['body']['$id'],
]);
if ($this->getSide() === 'client') {
$this->assertEquals(401, $response2['headers']['status-code']);
} else {
$this->assertEquals(201, $response2['headers']['status-code']);
}
2023-08-29 03:11:05 +12:00
return [
'topicId' => $topic['body']['$id'],
2023-08-29 03:13:18 +12:00
'targetId' => $target['body']['$id'],
'userId' => $target['body']['userId'],
'subscriberId' => $response['body']['$id'],
'identifier' => $target['body']['identifier'],
'providerType' => $target['body']['providerType'],
2023-08-29 03:11:05 +12:00
];
}
/**
* @depends testCreateSubscriber
*/
2023-08-29 03:13:18 +12:00
public function testGetSubscriber(array $data)
{
2023-11-14 21:37:52 +13:00
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers/' . $data['subscriberId'], \array_merge([
2023-08-29 03:11:05 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
2023-08-29 03:11:05 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($data['topicId'], $response['body']['topicId']);
$this->assertEquals($data['targetId'], $response['body']['targetId']);
2023-11-23 22:05:16 +13:00
$this->assertEquals($data['userId'], $response['body']['target']['userId']);
$this->assertEquals($data['providerType'], $response['body']['target']['providerType']);
$this->assertEquals($data['identifier'], $response['body']['target']['identifier']);
2023-08-29 03:11:05 +12:00
}
/**
* @depends testCreateSubscriber
*/
2023-08-29 03:13:18 +12:00
public function testListSubscribers(array $data)
{
$subscriberId = $data['subscriberId'];
$targetId = $data['targetId'];
$userId = $data['userId'];
$providerType = $data['providerType'];
$identifier = $data['identifier'];
2023-08-29 03:11:05 +12:00
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
2023-08-29 03:11:05 +12:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(1, $response['body']['total']);
$this->assertEquals($userId, $response['body']['subscribers'][0]['target']['userId']);
$this->assertEquals($providerType, $response['body']['subscribers'][0]['target']['providerType']);
$this->assertEquals($identifier, $response['body']['subscribers'][0]['target']['identifier']);
2023-08-29 03:11:05 +12:00
$this->assertEquals(\count($response['body']['subscribers']), $response['body']['total']);
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'search' => 'DOES_NOT_EXIST',
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(0, $response['body']['total']);
$searches = [
$subscriberId,
$targetId,
$userId,
$providerType
];
foreach ($searches as $search) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'search' => $search,
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(1, $response['body']['total']);
}
return $data;
2023-08-29 03:11:05 +12:00
}
2023-11-09 06:56:46 +13:00
/**
* @depends testListSubscribers
2023-11-09 06:56:46 +13:00
*/
public function testGetSubscriberLogs(array $data): void
{
/**
* Test for SUCCESS
*/
$logs = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals($logs['headers']['status-code'], 200);
$this->assertIsArray($logs['body']['logs']);
$this->assertIsNumeric($logs['body']['total']);
$logs = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::limit(1)->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($logs['headers']['status-code'], 200);
$this->assertIsArray($logs['body']['logs']);
$this->assertLessThanOrEqual(1, count($logs['body']['logs']));
$this->assertIsNumeric($logs['body']['total']);
$logs = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::offset(1)->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($logs['headers']['status-code'], 200);
$this->assertIsArray($logs['body']['logs']);
$this->assertIsNumeric($logs['body']['total']);
$logs = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::limit(1)->toString(),
Query::offset(1)->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($logs['headers']['status-code'], 200);
$this->assertIsArray($logs['body']['logs']);
$this->assertLessThanOrEqual(1, count($logs['body']['logs']));
$this->assertIsNumeric($logs['body']['total']);
/**
* Test for FAILURE
*/
$response = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::limit(-1)->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($response['headers']['status-code'], 400);
$response = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::offset(-1)->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($response['headers']['status-code'], 400);
$response = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::equal('$id', ['asdf'])->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($response['headers']['status-code'], 400);
$response = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
Query::orderAsc('$id')->toString(),
],
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($response['headers']['status-code'], 400);
$response = $this->client->call(Client::METHOD_GET, '/messaging/subscribers/' . $data['subscriberId'] . '/logs', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-12-20 23:55:09 +13:00
'queries' => [
2024-01-09 16:44:15 +13:00
'{ "method": "cursorAsc", "attribute": "$id" }'
2023-12-20 23:55:09 +13:00
]
2023-11-09 06:56:46 +13:00
]);
$this->assertEquals($response['headers']['status-code'], 400);
}
2023-08-29 03:11:05 +12:00
/**
* @depends testCreateSubscriber
*/
public function testDeleteSubscriber(array $data)
{
2023-11-14 21:37:52 +13:00
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/topics/' . $data['topicId'] . '/subscribers/' . $data['subscriberId'], \array_merge([
2023-08-29 03:11:05 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
2023-08-29 03:11:05 +12:00
$this->assertEquals(204, $response['headers']['status-code']);
$topic = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $data['topicId'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $topic['headers']['status-code']);
$this->assertEquals('android-app', $topic['body']['name']);
2024-02-15 17:30:09 +13:00
$this->assertEquals(0, $topic['body']['emailTotal']);
$this->assertEquals(0, $topic['body']['smsTotal']);
$this->assertEquals(0, $topic['body']['pushTotal']);
2023-08-29 03:11:05 +12:00
}
/**
2023-08-25 11:06:41 +12:00
* @depends testUpdateTopic
*/
public function testDeleteTopic(string $topicId)
{
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/topics/' . $topicId, [
2023-08-25 11:06:41 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(204, $response['headers']['status-code']);
}
2023-09-28 02:38:35 +13:00
2024-01-29 17:20:45 +13:00
/**
* @depends testCreateDraftEmail
*/
public function testListTargets(array $message)
{
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/does_not_exist/targets', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(404, $response['headers']['status-code']);
$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'],
]);
$targetList = $response['body'];
2024-02-13 02:27:19 +13:00
$this->assertEquals(2, $targetList['total']);
$this->assertEquals(2, count($targetList['targets']));
2024-01-29 17:20:45 +13:00
$this->assertEquals($message['targets'][0], $targetList['targets'][0]['$id']);
2024-02-13 02:27:19 +13:00
$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']);
2024-01-29 17:20:45 +13:00
// Test for empty targets
$response = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
2024-02-27 00:44:46 +13:00
'draft' => true
2024-01-29 17:20:45 +13:00
]);
$this->assertEquals(201, $response['headers']['status-code']);
$message = $response['body'];
$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'],
]);
$this->assertEquals(200, $response['headers']['status-code']);
$targetList = $response['body'];
$this->assertEquals(0, $targetList['total']);
$this->assertEquals(0, count($targetList['targets']));
}
public function testCreateDraftEmail()
{
2024-02-13 02:27:19 +13:00
// Create User 1
$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',
2024-02-13 02:27:19 +13:00
'name' => 'Messaging User 1',
]);
$this->assertEquals(201, $response['headers']['status-code'], "Error creating user: " . var_export($response['body'], true));
2024-02-13 02:27:19 +13:00
$user1 = $response['body'];
2024-01-26 06:01:48 +13:00
2024-02-13 02:27:19 +13:00
$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', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2024-02-13 02:27:19 +13:00
'targets' => [$targetId1, $targetId2],
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
2024-02-27 00:44:46 +13:00
'draft' => true
]);
$this->assertEquals(201, $response['headers']['status-code']);
$message = $response['body'];
$this->assertEquals(MessageStatus::DRAFT, $message['status']);
return $message;
}
2024-03-08 22:58:46 +13:00
public function testCreateDraftPushWithImage()
{
// Create User 1
$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 1',
]);
$this->assertEquals(201, $response['headers']['status-code'], "Error creating user: " . var_export($response['body'], true));
$user1 = $response['body'];
$this->assertEquals(1, \count($user1['targets']));
$targetId1 = $user1['targets'][0]['$id'];
// Create bucket
$bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'bucketId' => ID::unique(),
'name' => 'Test Bucket',
'fileSecurity' => true,
'maximumFileSize' => 2000000, // 2MB
'allowedFileExtensions' => ['jpg', 'png'],
'permissions' => [
Permission::read(Role::user('x')),
Permission::create(Role::user('x')),
Permission::update(Role::user('x')),
Permission::delete(Role::user('x')),
],
]);
$this->assertEquals(201, $response['headers']['status-code']);
$bucketId = $response['body']['$id'];
// Create file
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'fileId' => ID::unique(),
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'),
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]);
$fileId = $file['body']['$id'];
// Create Push
$response = $this->client->call(Client::METHOD_POST, '/messaging/messages/push', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
'targets' => [$targetId1],
'title' => 'New blog post',
'body' => 'Check out the new blog post at http://localhost',
'image' => "{$bucketId}:{$fileId}",
'draft' => true
]);
$this->assertEquals(201, $response['headers']['status-code']);
$message = $response['body'];
$this->assertEquals(MessageStatus::DRAFT, $message['status']);
$imageUrl = $message['data']['image']['url'];
$image = $this->client->call(Client::METHOD_GET, $imageUrl);
$this->assertEquals(200, $image['headers']['status-code']);
return $message;
}
public function testScheduledMessage(): void
{
// Create user
$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 1',
]);
$targetId = $response['body']['targets'][0]['$id'];
// Create scheduled message
$message = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
'targets' => [$targetId],
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
'scheduledAt' => DateTime::addSeconds(new \DateTime(), 3),
]);
$this->assertEquals(201, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::SCHEDULED, $message['body']['status']);
\sleep(8);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::FAILED, $message['body']['status']);
}
public function testScheduledToDraftMessage(): void
{
// Create user
$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 1',
]);
$targetId = $response['body']['targets'][0]['$id'];
// Create scheduled message
$message = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
'targets' => [$targetId],
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
'scheduledAt' => DateTime::addSeconds(new \DateTime(), 5),
]);
$this->assertEquals(201, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::SCHEDULED, $message['body']['status']);
$message = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2024-02-27 00:27:35 +13:00
'draft' => true,
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::DRAFT, $message['body']['status']);
\sleep(8);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::DRAFT, $message['body']['status']);
}
public function testDraftToScheduledMessage(): void
{
// Create user
$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 1',
]);
$targetId = $response['body']['targets'][0]['$id'];
// Create draft message
$message = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
'targets' => [$targetId],
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
2024-02-27 00:27:35 +13:00
'draft' => true,
]);
$this->assertEquals(201, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::DRAFT, $message['body']['status']);
$message = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2024-02-27 00:27:35 +13:00
'draft' => false,
'scheduledAt' => DateTime::addSeconds(new \DateTime(), 3),
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::SCHEDULED, $message['body']['status']);
\sleep(8);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::FAILED, $message['body']['status']);
}
public function testUpdateScheduledAt(): void
{
// Create user
$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 1',
]);
$targetId = $response['body']['targets'][0]['$id'];
// Create scheduled message
$message = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
'targets' => [$targetId],
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
'scheduledAt' => DateTime::addSeconds(new \DateTime(), 3),
]);
$this->assertEquals(201, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::SCHEDULED, $message['body']['status']);
$scheduledAt = DateTime::addSeconds(new \DateTime(), 10);
$message = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'scheduledAt' => $scheduledAt,
]);
$this->assertEquals(200, $message['headers']['status-code']);
\sleep(8);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::SCHEDULED, $message['body']['status']);
\sleep(8);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $message['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(MessageStatus::FAILED, $message['body']['status']);
}
2023-09-28 02:38:35 +13:00
public function testSendEmail()
{
if (empty(App::getEnv('_APP_MESSAGE_EMAIL_TEST_DSN'))) {
$this->markTestSkipped('Email DSN not provided');
}
2023-10-31 09:35:50 +13:00
$emailDSN = new DSN(App::getEnv('_APP_MESSAGE_EMAIL_TEST_DSN'));
$to = $emailDSN->getParam('to');
2023-12-06 07:24:55 +13:00
$fromName = $emailDSN->getParam('fromName');
$fromEmail = $emailDSN->getParam('fromEmail');
2023-10-31 09:35:50 +13:00
$apiKey = $emailDSN->getPassword();
2023-12-17 12:41:20 +13:00
if (empty($to) || empty($apiKey)) {
2023-09-28 02:38:35 +13:00
$this->markTestSkipped('Email provider not configured');
}
// Create provider
2023-12-17 12:41:20 +13:00
$provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/sendgrid', \array_merge([
2023-09-28 02:38:35 +13:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'providerId' => ID::unique(),
'name' => 'Sendgrid-provider',
2023-09-28 02:38:35 +13:00
'apiKey' => $apiKey,
2023-12-06 07:24:55 +13:00
'fromName' => $fromName,
2024-01-11 19:47:55 +13:00
'fromEmail' => $fromEmail,
'enabled' => true,
2023-09-28 02:38:35 +13:00
]);
2023-11-15 01:44:07 +13:00
2023-09-28 02:38:35 +13:00
$this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic
$topic = $this->client->call(Client::METHOD_POST, '/messaging/topics', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'topicId' => ID::unique(),
2023-09-28 02:38:35 +13:00
'name' => 'topic1',
]);
2023-11-15 01:44:07 +13:00
2023-09-28 02:38:35 +13:00
$this->assertEquals(201, $topic['headers']['status-code']);
// Create User
$user = $this->client->call(Client::METHOD_POST, '/users', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2023-10-19 20:59:48 +13:00
'userId' => ID::unique(),
'email' => $to,
2023-09-28 02:38:35 +13:00
'password' => 'password',
'name' => 'Messaging User',
2023-10-19 20:59:48 +13:00
]);
2023-09-28 02:38:35 +13:00
$this->assertEquals(201, $user['headers']['status-code']);
// Get target
$target = $user['body']['targets'][0];
2023-09-28 02:38:35 +13:00
// Create Subscriber
$subscriber = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topic['body']['$id'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'subscriberId' => ID::unique(),
'targetId' => $target['$id'],
2023-09-28 02:38:35 +13:00
]);
$this->assertEquals(201, $subscriber['headers']['status-code']);
// Create Email
$email = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2023-10-31 07:07:57 +13:00
'topics' => [$topic['body']['$id']],
2023-12-17 12:41:20 +13:00
'subject' => 'New blog post',
'content' => 'Check out the new blog post at http://localhost',
2023-09-28 02:38:35 +13:00
]);
$this->assertEquals(201, $email['headers']['status-code']);
2023-12-17 12:41:20 +13:00
\sleep(2);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $email['body']['$id'], [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-31 07:07:57 +13:00
$this->assertEquals(1, $message['body']['deliveredTotal']);
$this->assertEquals(0, \count($message['body']['deliveryErrors']));
2023-10-19 20:59:48 +13:00
2024-01-29 17:20:45 +13:00
return [
'message' => $email['body'],
'topic' => $topic['body'],
];
2023-10-19 20:59:48 +13:00
}
/**
* @depends testSendEmail
*/
2024-01-29 17:20:45 +13:00
public function testUpdateEmail(array $params): void
2023-10-19 20:59:48 +13:00
{
2024-01-29 17:20:45 +13:00
$email = $params['message'];
2023-10-19 20:59:48 +13:00
$message = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $email['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
// Test failure as the message has already been sent.
$this->assertEquals(400, $message['headers']['status-code']);
// Create Email
$email = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2024-02-27 00:27:35 +13:00
'draft' => true,
2023-11-15 08:54:55 +13:00
'topics' => [$email['body']['topics'][0]],
2023-10-19 20:59:48 +13:00
'subject' => 'Khali beats Undertaker',
'content' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
]);
$this->assertEquals(201, $email['headers']['status-code']);
$email = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/email/' . $email['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2024-02-27 00:27:35 +13:00
'draft' => false,
2023-10-19 20:59:48 +13:00
]);
$this->assertEquals(200, $email['headers']['status-code']);
\sleep(5);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $email['body']['$id'], [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-31 07:07:57 +13:00
$this->assertEquals(1, $message['body']['deliveredTotal']);
2023-10-19 20:59:48 +13:00
$this->assertEquals(0, \count($message['body']['deliveryErrors']));
}
public function testSendSMS()
{
if (empty(App::getEnv('_APP_MESSAGE_SMS_TEST_DSN'))) {
$this->markTestSkipped('SMS DSN not provided');
}
2023-10-31 09:35:50 +13:00
$smsDSN = new DSN(App::getEnv('_APP_MESSAGE_SMS_TEST_DSN'));
$to = $smsDSN->getParam('to');
$from = $smsDSN->getParam('from');
$senderId = $smsDSN->getUser();
2023-12-17 12:41:20 +13:00
$authKey = $smsDSN->getPassword();
2023-10-31 09:35:50 +13:00
2023-10-19 20:59:48 +13:00
if (empty($to) || empty($from) || empty($senderId) || empty($authKey)) {
$this->markTestSkipped('SMS provider not configured');
}
// Create provider
$provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/msg91', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'providerId' => ID::unique(),
2023-12-17 12:41:20 +13:00
'name' => 'Msg91Sender',
2023-10-19 20:59:48 +13:00
'senderId' => $senderId,
'authKey' => $authKey,
2024-01-11 19:47:55 +13:00
'from' => $from,
'enabled' => true,
2023-10-19 20:59:48 +13:00
]);
2023-11-15 01:44:07 +13:00
2023-10-19 20:59:48 +13:00
$this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic
$topic = $this->client->call(Client::METHOD_POST, '/messaging/topics', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'topicId' => ID::unique(),
'name' => 'topic1',
]);
2023-11-15 01:44:07 +13:00
2023-10-19 20:59:48 +13:00
$this->assertEquals(201, $topic['headers']['status-code']);
// Create User
$user = $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' => 'random1-email@mail.org',
'password' => 'password',
'name' => 'Messaging User',
]);
$this->assertEquals(201, $user['headers']['status-code']);
// Create Target
$target = $this->client->call(Client::METHOD_POST, '/users/' . $user['body']['$id'] . '/targets', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'targetId' => ID::unique(),
2023-11-15 01:44:07 +13:00
'providerType' => 'sms',
2023-10-19 20:59:48 +13:00
'providerId' => $provider['body']['$id'],
'identifier' => $to,
]);
$this->assertEquals(201, $target['headers']['status-code']);
// Create Subscriber
$subscriber = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topic['body']['$id'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'subscriberId' => ID::unique(),
'targetId' => $target['body']['$id'],
]);
$this->assertEquals(201, $subscriber['headers']['status-code']);
// Create SMS
$sms = $this->client->call(Client::METHOD_POST, '/messaging/messages/sms', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2023-10-31 07:07:57 +13:00
'topics' => [$topic['body']['$id']],
2023-10-19 20:59:48 +13:00
'content' => '064763',
]);
$this->assertEquals(201, $sms['headers']['status-code']);
\sleep(5);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $sms['body']['$id'], [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-31 07:07:57 +13:00
$this->assertEquals(1, $message['body']['deliveredTotal']);
2023-10-19 20:59:48 +13:00
$this->assertEquals(0, \count($message['body']['deliveryErrors']));
return $message;
}
/**
* @depends testSendSMS
*/
public function testUpdateSMS(array $sms)
{
$message = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/sms/' . $sms['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
// Test failure as the message has already been sent.
$this->assertEquals(400, $message['headers']['status-code']);
// Create SMS
$sms = $this->client->call(Client::METHOD_POST, '/messaging/messages/sms', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2024-02-27 00:27:35 +13:00
'draft' => true,
2023-11-15 08:54:55 +13:00
'topics' => [$sms['body']['topics'][0]],
2023-12-17 12:41:20 +13:00
'content' => 'Your OTP code is 123456',
2023-10-19 20:59:48 +13:00
]);
$this->assertEquals(201, $sms['headers']['status-code']);
$sms = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/sms/' . $sms['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2024-02-27 00:27:35 +13:00
'draft' => false,
2023-10-19 20:59:48 +13:00
]);
$this->assertEquals(200, $sms['headers']['status-code']);
2023-12-17 12:41:20 +13:00
\sleep(2);
2023-10-19 20:59:48 +13:00
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $sms['body']['$id'], [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-31 07:07:57 +13:00
$this->assertEquals(1, $message['body']['deliveredTotal']);
2023-10-19 20:59:48 +13:00
$this->assertEquals(0, \count($message['body']['deliveryErrors']));
}
public function testSendPushNotification()
{
if (empty(App::getEnv('_APP_MESSAGE_PUSH_TEST_DSN'))) {
$this->markTestSkipped('Push DSN empty');
}
2023-12-17 12:41:20 +13:00
$dsn = new DSN(App::getEnv('_APP_MESSAGE_PUSH_TEST_DSN'));
$to = $dsn->getParam('to');
2024-01-09 13:41:55 +13:00
$serviceAccountJSON = $dsn->getParam('serviceAccountJSON');
2023-10-31 09:35:50 +13:00
2023-12-20 07:15:20 +13:00
if (empty($to) || empty($serviceAccountJSON)) {
2023-10-19 20:59:48 +13:00
$this->markTestSkipped('Push provider not configured');
}
// Create provider
$provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/fcm', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), [
'providerId' => ID::unique(),
'name' => 'FCM-1',
2023-12-17 12:41:20 +13:00
'serviceAccountJSON' => $serviceAccountJSON,
2024-01-11 19:47:55 +13:00
'enabled' => true,
2023-10-19 20:59:48 +13:00
]);
2023-11-15 01:44:07 +13:00
2023-10-19 20:59:48 +13:00
$this->assertEquals(201, $provider['headers']['status-code']);
// Create Topic
$topic = $this->client->call(Client::METHOD_POST, '/messaging/topics', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'topicId' => ID::unique(),
'name' => 'topic1',
]);
2023-11-15 01:44:07 +13:00
2023-10-19 20:59:48 +13:00
$this->assertEquals(201, $topic['headers']['status-code']);
// Create User
$user = $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' => 'random3-email@mail.org',
'password' => 'password',
'name' => 'Messaging User',
]);
$this->assertEquals(201, $user['headers']['status-code']);
// Create Target
$target = $this->client->call(Client::METHOD_POST, '/users/' . $user['body']['$id'] . '/targets', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'targetId' => ID::unique(),
2023-11-15 01:44:07 +13:00
'providerType' => 'push',
2023-10-19 20:59:48 +13:00
'providerId' => $provider['body']['$id'],
'identifier' => $to,
]);
$this->assertEquals(201, $target['headers']['status-code']);
// Create Subscriber
$subscriber = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topic['body']['$id'] . '/subscribers', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'subscriberId' => ID::unique(),
'targetId' => $target['body']['$id'],
]);
$this->assertEquals(201, $subscriber['headers']['status-code']);
// Create push notification
$push = $this->client->call(Client::METHOD_POST, '/messaging/messages/push', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2023-10-31 07:07:57 +13:00
'topics' => [$topic['body']['$id']],
2023-10-19 20:59:48 +13:00
'title' => 'Test-Notification',
'body' => 'Test-Notification-Body',
]);
$this->assertEquals(201, $push['headers']['status-code']);
\sleep(5);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $push['body']['$id'], [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-31 07:07:57 +13:00
$this->assertEquals(1, $message['body']['deliveredTotal']);
2023-10-19 20:59:48 +13:00
$this->assertEquals(0, \count($message['body']['deliveryErrors']));
return $message;
}
/**
* @depends testSendPushNotification
*/
public function testUpdatePushNotification(array $push)
{
$message = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/push/' . $push['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
// Test failure as the message has already been sent.
$this->assertEquals(400, $message['headers']['status-code']);
// Create push notification
$push = $this->client->call(Client::METHOD_POST, '/messaging/messages/push', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2024-02-27 00:27:35 +13:00
'draft' => true,
2023-11-15 08:54:55 +13:00
'topics' => [$push['body']['topics'][0]],
2023-10-19 20:59:48 +13:00
'title' => 'Test-Notification',
'body' => 'Test-Notification-Body',
]);
$this->assertEquals(201, $push['headers']['status-code']);
$push = $this->client->call(Client::METHOD_PATCH, '/messaging/messages/push/' . $push['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
2024-02-27 00:27:35 +13:00
'draft' => false,
2023-10-19 20:59:48 +13:00
]);
$this->assertEquals(200, $push['headers']['status-code']);
\sleep(5);
$message = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $push['body']['$id'], [
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-31 07:07:57 +13:00
$this->assertEquals(1, $message['body']['deliveredTotal']);
2023-10-19 20:59:48 +13:00
$this->assertEquals(0, \count($message['body']['deliveryErrors']));
2023-09-28 02:38:35 +13:00
}
/**
2024-01-29 17:20:45 +13:00
* @depends testSendEmail
* @return void
* @throws \Exception
*/
2024-01-29 17:20:45 +13:00
public function testDeleteMessage(array $params): void
{
2024-01-29 17:20:45 +13:00
$message = $params['message'];
$topic = $params['topic'];
2024-01-29 17:20:45 +13:00
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/messages/' . $message['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
2024-01-29 17:20:45 +13:00
$this->assertEquals(204, $response['headers']['status-code']);
2024-01-29 17:20:45 +13:00
// Test for FAILURE
$response = $this->client->call(Client::METHOD_POST, '/messaging/messages/email', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'messageId' => ID::unique(),
2024-01-29 17:20:45 +13:00
'topics' => [$topic['$id']],
'subject' => 'Test subject',
'content' => 'Test content',
]);
2024-01-29 17:20:45 +13:00
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/messages/' . $response['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
2024-01-29 17:20:45 +13:00
$this->assertEquals(400, $response['headers']['status-code']);
2024-01-29 17:20:45 +13:00
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/messages/does_not_exist', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
2024-01-29 17:20:45 +13:00
$this->assertEquals(404, $response['headers']['status-code']);
}
2023-08-25 11:06:41 +12:00
}