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;
|
2023-08-23 22:01:29 +12:00
|
|
|
use Tests\E2E\Client;
|
2023-09-28 02:38:35 +13:00
|
|
|
use Utopia\App;
|
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-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',
|
2023-10-26 07:25:45 +13:00
|
|
|
'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',
|
2023-10-26 07:25:45 +13:00
|
|
|
'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',
|
|
|
|
'username' => 'my-username',
|
|
|
|
'password' => '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',
|
2023-09-28 02:28:00 +13:00
|
|
|
'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 = [];
|
|
|
|
|
|
|
|
foreach (\array_keys($providersParams) as $key) {
|
2023-08-25 09:02:51 +12:00
|
|
|
$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'],
|
|
|
|
]), $providersParams[$key]);
|
2023-12-17 12:41:20 +13:00
|
|
|
|
2023-08-23 22:01:29 +12:00
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals($providersParams[$key]['name'], $response['body']['name']);
|
2023-10-05 09:42:04 +13:00
|
|
|
\array_push($providers, $response['body']);
|
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',
|
|
|
|
'username' => 'my-username',
|
|
|
|
'password' => 'my-password',
|
|
|
|
],
|
|
|
|
'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 => $key) {
|
2023-10-04 23:45:59 +13:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/' . $key . '/' . $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[$key]);
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals($providersParams[$key]['name'], $response['body']['name']);
|
2023-10-05 09:42:04 +13:00
|
|
|
$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
|
|
|
]);
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals('Mailgun2', $response['body']['name']);
|
|
|
|
$this->assertEquals(false, $response['body']['enabled']);
|
2023-10-05 09:42:04 +13:00
|
|
|
$providers[1] = $response['body'];
|
2023-08-23 22:01:29 +12:00
|
|
|
return $providers;
|
|
|
|
}
|
|
|
|
|
2024-01-24 15:22:10 +13:00
|
|
|
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', [
|
2024-01-24 15:22:10 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2024-01-24 17:34:59 +13:00
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], [
|
2024-01-24 15:22:10 +13:00
|
|
|
'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'], [
|
2024-01-24 15:22:10 +13:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2024-01-24 17:34:59 +13:00
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], [
|
2024-01-24 15:22:10 +13:00
|
|
|
'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-11-10 20:01:21 +13:00
|
|
|
|
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']));
|
2023-11-10 20:01:21 +13:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-08-24 08:23:52 +12:00
|
|
|
$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) {
|
2023-08-24 08:23:52 +12:00
|
|
|
$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
|
|
|
{
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/messaging/topics', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
2023-08-25 11:19:02 +12:00
|
|
|
'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(),
|
2023-08-25 11:19:02 +12:00
|
|
|
'name' => 'my-app',
|
2023-08-25 11:06:41 +12:00
|
|
|
]);
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
2023-08-25 11:19:02 +12:00
|
|
|
$this->assertEquals('my-app', $response['body']['name']);
|
2023-08-25 11:06:41 +12:00
|
|
|
|
2023-08-29 03:11:05 +12:00
|
|
|
return $response['body'];
|
2023-08-25 11:06:41 +12:00
|
|
|
}
|
|
|
|
|
2023-08-25 11:19:02 +12:00
|
|
|
/**
|
2023-08-25 11:06:41 +12:00
|
|
|
* @depends testCreateTopic
|
|
|
|
*/
|
2023-08-29 03:11:05 +12:00
|
|
|
public function testUpdateTopic(array $topic): string
|
2023-08-25 11:06:41 +12:00
|
|
|
{
|
2023-08-29 03:11:05 +12:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, '/messaging/topics/' . $topic['$id'], [
|
2023-08-25 11:19:02 +12:00
|
|
|
'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']);
|
2023-08-29 03:11:05 +12:00
|
|
|
return $response['body']['$id'];
|
2023-08-25 11:06:41 +12:00
|
|
|
}
|
|
|
|
|
2023-11-10 20:01:21 +13: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-01-09 16:44:15 +13:00
|
|
|
Query::equal('total', [0])->toString(),
|
2024-01-04 13:06:50 +13:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals(1, \count($response['body']['topics']));
|
|
|
|
|
|
|
|
$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-01-09 16:44:15 +13:00
|
|
|
Query::greaterThan('total', 0)->toString(),
|
2024-01-04 13:06:50 +13:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals(0, \count($response['body']['topics']));
|
|
|
|
|
2023-11-10 20:01:21 +13:00
|
|
|
return $topicId;
|
2023-08-25 11:06:41 +12:00
|
|
|
}
|
|
|
|
|
2023-08-25 11:19:02 +12:00
|
|
|
/**
|
2023-08-25 11:06:41 +12:00
|
|
|
* @depends testUpdateTopic
|
|
|
|
*/
|
|
|
|
public function testGetTopic(string $topicId)
|
|
|
|
{
|
2023-08-25 11:19:02 +12:00
|
|
|
$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']);
|
2023-08-25 11:19:02 +12:00
|
|
|
$this->assertEquals('android-app', $response['body']['name']);
|
2023-10-26 20:07:39 +13:00
|
|
|
$this->assertEquals(0, $response['body']['total']);
|
2023-08-25 11:06:41 +12:00
|
|
|
}
|
|
|
|
|
2023-08-29 03:11:05 +12:00
|
|
|
/**
|
|
|
|
* @depends testCreateTopic
|
|
|
|
*/
|
2023-08-29 03:13:18 +12:00
|
|
|
public function testCreateSubscriber(array $topic)
|
|
|
|
{
|
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-11-20 20:04:50 +13:00
|
|
|
|
2023-08-29 03:11:05 +12:00
|
|
|
$this->assertEquals(201, $target['headers']['status-code']);
|
|
|
|
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/messaging/topics/' . $topic['$id'] . '/subscribers', \array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
], $this->getHeaders()), [
|
2023-10-26 20:07:39 +13:00
|
|
|
'subscriberId' => ID::unique(),
|
2023-08-29 03:11:05 +12:00
|
|
|
'targetId' => $target['body']['$id'],
|
|
|
|
]);
|
2023-11-20 20:04:50 +13:00
|
|
|
|
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']);
|
2023-10-26 20:07:39 +13:00
|
|
|
|
|
|
|
$topic = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $topic['$id'], [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
]);
|
2023-11-20 20:04:50 +13:00
|
|
|
|
2023-10-26 20:07:39 +13:00
|
|
|
$this->assertEquals(200, $topic['headers']['status-code']);
|
|
|
|
$this->assertEquals('android-app', $topic['body']['name']);
|
|
|
|
$this->assertEquals(1, $topic['body']['total']);
|
|
|
|
|
2023-08-29 03:11:05 +12:00
|
|
|
return [
|
2023-10-26 20:07:39 +13:00
|
|
|
'topicId' => $topic['body']['$id'],
|
2023-08-29 03:13:18 +12:00
|
|
|
'targetId' => $target['body']['$id'],
|
2023-11-20 20:04:50 +13:00
|
|
|
'userId' => $target['body']['userId'],
|
2023-11-23 08:09:55 +13:00
|
|
|
'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'],
|
2023-10-17 11:11:42 +13:00
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
]));
|
2023-11-20 20:04:50 +13:00
|
|
|
|
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)
|
|
|
|
{
|
2024-01-18 15:32:32 +13:00
|
|
|
$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'],
|
2023-10-17 11:11:42 +13:00
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
]));
|
2023-11-10 20:01:21 +13:00
|
|
|
|
2023-08-29 03:11:05 +12:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
$this->assertEquals(1, $response['body']['total']);
|
2024-01-18 15:32:32 +13:00
|
|
|
$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']);
|
2023-11-10 20:01:21 +13:00
|
|
|
|
2024-01-18 15:32:32 +13: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'],
|
|
|
|
]), [
|
|
|
|
'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']);
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:01:21 +13:00
|
|
|
return $data;
|
2023-08-29 03:11:05 +12:00
|
|
|
}
|
|
|
|
|
2023-11-09 06:56:46 +13:00
|
|
|
/**
|
2023-11-10 20:01:21 +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-10-26 20:07:39 +13:00
|
|
|
|
2023-08-29 03:11:05 +12:00
|
|
|
$this->assertEquals(204, $response['headers']['status-code']);
|
2023-10-26 20:07:39 +13:00
|
|
|
|
|
|
|
$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']);
|
|
|
|
$this->assertEquals(0, $topic['body']['total']);
|
2023-08-29 03:11:05 +12:00
|
|
|
}
|
|
|
|
|
2023-08-25 11:19:02 +12:00
|
|
|
/**
|
2023-08-25 11:06:41 +12:00
|
|
|
* @depends testUpdateTopic
|
|
|
|
*/
|
|
|
|
public function testDeleteTopic(string $topicId)
|
|
|
|
{
|
2023-08-25 11:19:02 +12:00
|
|
|
$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',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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']));
|
|
|
|
}
|
|
|
|
|
2024-01-18 14:33:32 +13:00
|
|
|
public function testCreateDraftEmail()
|
|
|
|
{
|
2024-02-13 02:27:19 +13:00
|
|
|
// Create User 1
|
2024-01-18 14:33:32 +13:00
|
|
|
$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',
|
2024-01-18 14:33:32 +13:00
|
|
|
]);
|
|
|
|
|
|
|
|
$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'];
|
2024-01-18 14:33:32 +13:00
|
|
|
|
|
|
|
// 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],
|
2024-01-18 14:33:32 +13:00
|
|
|
'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']);
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2023-09-28 02:38:35 +13:00
|
|
|
public function testSendEmail()
|
|
|
|
{
|
2023-10-31 10:04:18 +13:00
|
|
|
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'],
|
|
|
|
]), [
|
2023-10-05 00:57:56 +13:00
|
|
|
'providerId' => ID::unique(),
|
2024-01-11 13:06:15 +13:00
|
|
|
'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,
|
2023-12-07 23:25:19 +13:00
|
|
|
'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'],
|
|
|
|
], [
|
2023-10-05 00:57:56 +13:00
|
|
|
'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(),
|
2023-10-17 11:11:42 +13:00
|
|
|
'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']);
|
|
|
|
|
2024-01-11 13:06:15 +13:00
|
|
|
// Get target
|
2023-12-07 23:25:19 +13:00
|
|
|
$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()), [
|
2023-10-05 00:57:56 +13:00
|
|
|
'subscriberId' => ID::unique(),
|
2023-12-07 23:25:19 +13:00
|
|
|
'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'],
|
|
|
|
], [
|
2023-10-05 00:57:56 +13:00
|
|
|
'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-10-05 00:57:56 +13:00
|
|
|
|
2023-12-17 12:41:20 +13:00
|
|
|
\sleep(2);
|
2023-10-05 00:57:56 +13:00
|
|
|
|
|
|
|
$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-05 00:57:56 +13:00
|
|
|
$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(),
|
|
|
|
'status' => 'draft',
|
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'],
|
|
|
|
], [
|
|
|
|
'status' => 'processing',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2023-10-31 10:04:18 +13:00
|
|
|
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(),
|
|
|
|
'status' => 'draft',
|
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'],
|
|
|
|
], [
|
|
|
|
'status' => 'processing',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2023-10-31 10:04:18 +13:00
|
|
|
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(),
|
|
|
|
'status' => 'draft',
|
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'],
|
|
|
|
], [
|
|
|
|
'status' => 'processing',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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-18 14:33:32 +13:00
|
|
|
|
|
|
|
/**
|
2024-01-29 17:20:45 +13:00
|
|
|
* @depends testSendEmail
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception
|
2024-01-18 14:33:32 +13:00
|
|
|
*/
|
2024-01-29 17:20:45 +13:00
|
|
|
public function testDeleteMessage(array $params): void
|
2024-01-18 14:33:32 +13:00
|
|
|
{
|
2024-01-29 17:20:45 +13:00
|
|
|
$message = $params['message'];
|
|
|
|
$topic = $params['topic'];
|
2024-01-18 14:33:32 +13:00
|
|
|
|
2024-01-29 17:20:45 +13:00
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/messages/' . $message['$id'], [
|
2024-01-18 14:33:32 +13:00
|
|
|
'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-18 14:33:32 +13:00
|
|
|
|
2024-01-29 17:20:45 +13:00
|
|
|
// Test for FAILURE
|
2024-01-18 14:33:32 +13:00
|
|
|
$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
|
|
|
'status' => 'processing',
|
|
|
|
'topics' => [$topic['$id']],
|
|
|
|
'subject' => 'Test subject',
|
|
|
|
'content' => 'Test content',
|
2024-01-18 14:33:32 +13:00
|
|
|
]);
|
|
|
|
|
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-18 14:33:32 +13:00
|
|
|
|
2024-01-29 17:20:45 +13:00
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
2024-01-18 14:33:32 +13:00
|
|
|
|
2024-01-29 17:20:45 +13:00
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, '/messaging/messages/does_not_exist', [
|
2024-01-18 14:33:32 +13:00
|
|
|
'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']);
|
2024-01-18 14:33:32 +13:00
|
|
|
}
|
2023-08-25 11:06:41 +12:00
|
|
|
}
|