2021-06-30 01:11:14 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Tests;
|
|
|
|
|
2021-12-17 07:12:06 +13:00
|
|
|
use Utopia\Database\Document;
|
2021-06-30 01:11:14 +12:00
|
|
|
use Appwrite\Messaging\Adapter\Realtime;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class MessagingTest extends TestCase
|
|
|
|
{
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown(): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUser()
|
|
|
|
{
|
|
|
|
$realtime = new Realtime();
|
|
|
|
|
|
|
|
$realtime->subscribe(
|
|
|
|
'1',
|
|
|
|
1,
|
2022-08-03 16:17:49 +12:00
|
|
|
['user:123', 'users', 'team:abc', 'team:abc/administrator', 'team:abc/moderator', 'team:def', 'team:def/guest'],
|
2021-06-30 01:11:14 +12:00
|
|
|
['files' => 0, 'documents' => 0, 'documents.789' => 0, 'account.123' => 0]
|
|
|
|
);
|
|
|
|
|
|
|
|
$event = [
|
|
|
|
'project' => '1',
|
2022-08-03 16:17:49 +12:00
|
|
|
'roles' => ['any'],
|
2021-06-30 01:11:14 +12:00
|
|
|
'data' => [
|
|
|
|
'channels' => [
|
|
|
|
0 => 'account.123',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
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-03 16:17:49 +12:00
|
|
|
$event['roles'] = ['users'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['user:123'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['team:abc'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['team:abc/administrator'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['team:abc/moderator'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['team:def'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['team:def/guest'];
|
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]);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['user:456'];
|
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);
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$event['roles'] = ['team:def/member'];
|
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-03 16:17:49 +12:00
|
|
|
$event['roles'] = ['any'];
|
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(7, $realtime->subscriptions['1']);
|
|
|
|
|
|
|
|
$realtime->unsubscribe(1);
|
|
|
|
|
|
|
|
$this->assertEmpty($realtime->connections);
|
|
|
|
$this->assertEmpty($realtime->subscriptions);
|
|
|
|
}
|
2021-06-30 23:36:58 +12:00
|
|
|
|
|
|
|
public function testConvertChannelsGuest()
|
|
|
|
{
|
|
|
|
$user = new Document([
|
|
|
|
'$id' => ''
|
|
|
|
]);
|
|
|
|
|
|
|
|
$channels = [
|
|
|
|
0 => 'files',
|
|
|
|
1 => 'documents',
|
|
|
|
2 => 'documents.789',
|
|
|
|
3 => 'account',
|
|
|
|
4 => 'account.456'
|
|
|
|
];
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$channels = Realtime::convertChannels($channels, $user->getId());
|
2021-08-27 20:20:44 +12:00
|
|
|
$this->assertCount(4, $channels);
|
2021-06-30 23:36:58 +12:00
|
|
|
$this->assertArrayHasKey('files', $channels);
|
|
|
|
$this->assertArrayHasKey('documents', $channels);
|
|
|
|
$this->assertArrayHasKey('documents.789', $channels);
|
2021-08-27 20:20:44 +12:00
|
|
|
$this->assertArrayHasKey('account', $channels);
|
2021-06-30 23:36:58 +12:00
|
|
|
$this->assertArrayNotHasKey('account.456', $channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConvertChannelsUser()
|
|
|
|
{
|
|
|
|
$user = new Document([
|
|
|
|
'$id' => '123',
|
|
|
|
'memberships' => [
|
|
|
|
[
|
|
|
|
'teamId' => 'abc',
|
|
|
|
'roles' => [
|
|
|
|
'administrator',
|
|
|
|
'moderator'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'teamId' => 'def',
|
|
|
|
'roles' => [
|
|
|
|
'guest'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$channels = [
|
|
|
|
0 => 'files',
|
|
|
|
1 => 'documents',
|
|
|
|
2 => 'documents.789',
|
|
|
|
3 => 'account',
|
|
|
|
4 => 'account.456'
|
|
|
|
];
|
|
|
|
|
2021-07-14 03:18:02 +12:00
|
|
|
$channels = Realtime::convertChannels($channels, $user->getId());
|
2021-06-30 23:36:58 +12:00
|
|
|
|
2021-08-27 20:20:44 +12:00
|
|
|
$this->assertCount(5, $channels);
|
2021-06-30 23:36:58 +12:00
|
|
|
$this->assertArrayHasKey('files', $channels);
|
|
|
|
$this->assertArrayHasKey('documents', $channels);
|
|
|
|
$this->assertArrayHasKey('documents.789', $channels);
|
|
|
|
$this->assertArrayHasKey('account.123', $channels);
|
2021-08-27 20:20:44 +12:00
|
|
|
$this->assertArrayHasKey('account', $channels);
|
2021-06-30 23:36:58 +12:00
|
|
|
$this->assertArrayNotHasKey('account.456', $channels);
|
|
|
|
}
|
2021-12-17 07:12:06 +13:00
|
|
|
|
|
|
|
public function testFromPayloadCollectionLevelPermissions(): void
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test Collection Level Permissions
|
|
|
|
*/
|
|
|
|
$result = Realtime::fromPayload(
|
2022-06-22 22:51:49 +12:00
|
|
|
event: 'databases.database_id.collections.collection_id.documents.document_id.create',
|
2021-12-17 07:12:06 +13:00
|
|
|
payload: new Document([
|
|
|
|
'$id' => 'test',
|
|
|
|
'$collection' => 'collection',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(admin)',
|
|
|
|
'write(admin)',
|
|
|
|
],
|
2021-12-17 07:12:06 +13:00
|
|
|
]),
|
|
|
|
collection: new Document([
|
|
|
|
'$id' => 'collection',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(any)',
|
|
|
|
'write(any)',
|
|
|
|
],
|
2022-06-22 22:51:49 +12:00
|
|
|
]),
|
|
|
|
database: new Document([
|
|
|
|
'$id' => 'database',
|
2021-12-17 07:12:06 +13:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2022-08-03 16:17:49 +12:00
|
|
|
\var_dump($result);
|
|
|
|
|
|
|
|
$this->assertContains('any', $result['roles']);
|
2021-12-17 07:12:06 +13:00
|
|
|
$this->assertNotContains('role:admin', $result['roles']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Document Level Permissions
|
|
|
|
*/
|
|
|
|
$result = Realtime::fromPayload(
|
2022-06-22 22:51:49 +12:00
|
|
|
event: 'databases.database_id.collections.collection_id.documents.document_id.create',
|
2021-12-17 07:12:06 +13:00
|
|
|
payload: new Document([
|
|
|
|
'$id' => 'test',
|
|
|
|
'$collection' => 'collection',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(any)',
|
|
|
|
'write(any)',
|
|
|
|
],
|
2021-12-17 07:12:06 +13:00
|
|
|
]),
|
|
|
|
collection: new Document([
|
|
|
|
'$id' => 'collection',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(admin)',
|
|
|
|
'write(admin)',
|
|
|
|
],
|
|
|
|
'documentSecurity' => true,
|
2022-06-22 22:51:49 +12:00
|
|
|
]),
|
|
|
|
database: new Document([
|
|
|
|
'$id' => 'database',
|
2021-12-17 07:12:06 +13:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertContains('any', $result['roles']);
|
2021-12-17 07:12:06 +13:00
|
|
|
$this->assertNotContains('role:admin', $result['roles']);
|
|
|
|
}
|
2022-04-19 04:21:45 +12:00
|
|
|
|
|
|
|
public function testFromPayloadBucketLevelPermissions(): void
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test Collection Level Permissions
|
|
|
|
*/
|
|
|
|
$result = Realtime::fromPayload(
|
|
|
|
event: 'buckets.bucket_id.files.file_id.create',
|
|
|
|
payload: new Document([
|
|
|
|
'$id' => 'test',
|
|
|
|
'$collection' => 'bucket',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(admin)',
|
|
|
|
'write(admin)',
|
|
|
|
],
|
2022-04-19 04:21:45 +12:00
|
|
|
]),
|
|
|
|
bucket: new Document([
|
|
|
|
'$id' => 'bucket',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(any)',
|
|
|
|
'write(any)',
|
|
|
|
],
|
2022-04-19 04:21:45 +12:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertContains('any', $result['roles']);
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->assertNotContains('role:admin', $result['roles']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Document Level Permissions
|
|
|
|
*/
|
|
|
|
$result = Realtime::fromPayload(
|
|
|
|
event: 'buckets.bucket_id.files.file_id.create',
|
|
|
|
payload: new Document([
|
|
|
|
'$id' => 'test',
|
|
|
|
'$collection' => 'bucket',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(any)',
|
|
|
|
'write(any)',
|
|
|
|
],
|
2022-04-19 04:21:45 +12:00
|
|
|
]),
|
|
|
|
bucket: new Document([
|
|
|
|
'$id' => 'bucket',
|
2022-08-02 21:21:53 +12:00
|
|
|
'$permissions' => [
|
|
|
|
'read(admin)',
|
|
|
|
'write(admin)',
|
|
|
|
],
|
2022-08-03 16:17:49 +12:00
|
|
|
'documentSecurity' => true
|
2022-04-19 04:21:45 +12:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2022-08-03 16:17:49 +12:00
|
|
|
$this->assertContains('any', $result['roles']);
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->assertNotContains('role:admin', $result['roles']);
|
|
|
|
}
|
2021-06-30 01:11:14 +12:00
|
|
|
}
|