2021-06-30 01:11:14 +12:00
|
|
|
<?php
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
namespace Tests\Unit\Messaging;
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
use Appwrite\Messaging\Adapter\Realtime;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-12-15 04:42:25 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
2022-08-19 16:04:33 +12:00
|
|
|
use Utopia\Database\Role;
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
class MessagingGuestTest extends TestCase
|
|
|
|
{
|
2022-08-01 22:22:04 +12:00
|
|
|
public function testGuest(): void
|
2021-06-30 01:11:14 +12:00
|
|
|
{
|
|
|
|
$realtime = new Realtime();
|
|
|
|
|
|
|
|
$realtime->subscribe(
|
|
|
|
'1',
|
|
|
|
1,
|
2022-08-19 16:04:33 +12:00
|
|
|
[Role::guests()->toString()],
|
2021-06-30 01:11:14 +12:00
|
|
|
['files' => 0, 'documents' => 0, 'documents.789' => 0, 'account.123' => 0]
|
|
|
|
);
|
|
|
|
|
|
|
|
$event = [
|
|
|
|
'project' => '1',
|
2022-08-19 16:04:33 +12:00
|
|
|
'roles' => [Role::any()->toString()],
|
2021-06-30 01:11:14 +12:00
|
|
|
'data' => [
|
|
|
|
'channels' => [
|
|
|
|
0 => 'documents',
|
|
|
|
1 => 'documents',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertCount(1, $receivers);
|
|
|
|
$this->assertEquals(1, $receivers[0]);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::guests()->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertCount(1, $receivers);
|
|
|
|
$this->assertEquals(1, $receivers[0]);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::users()->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::user(ID::custom('123'))->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::team(ID::custom('abc'))->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::team(ID::custom('abc'), 'administrator')->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::team(ID::custom('abc'), 'god')->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::team(ID::custom('def'))->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::team(ID::custom('def'), 'guest')->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::user(ID::custom('456'))->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::team(ID::custom('def'), 'member')->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
2022-08-19 16:04:33 +12:00
|
|
|
$event['roles'] = [Role::any()->toString()];
|
2021-06-30 01:11:14 +12:00
|
|
|
$event['data']['channels'] = ['documents.123'];
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
|
|
|
$event['data']['channels'] = ['documents.789'];
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertCount(1, $receivers);
|
|
|
|
$this->assertEquals(1, $receivers[0]);
|
|
|
|
|
|
|
|
$event['project'] = '2';
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$receivers = $realtime->getSubscribers($event);
|
2021-06-30 01:11:14 +12:00
|
|
|
|
|
|
|
$this->assertEmpty($receivers);
|
|
|
|
|
|
|
|
$realtime->unsubscribe(2);
|
|
|
|
|
|
|
|
$this->assertCount(1, $realtime->connections);
|
|
|
|
$this->assertCount(1, $realtime->subscriptions['1']);
|
|
|
|
|
|
|
|
$realtime->unsubscribe(1);
|
|
|
|
|
|
|
|
$this->assertEmpty($realtime->connections);
|
|
|
|
$this->assertEmpty($realtime->subscriptions);
|
|
|
|
}
|
|
|
|
}
|