appwrite/tests/unit/Messaging/MessagingTest.php

332 lines
10 KiB
PHP
Raw Normal View History

2021-06-29 13:11:14 +00:00
<?php
2022-08-01 10:22:04 +00:00
namespace Tests\Unit\Messaging;
2021-06-29 13:11:14 +00:00
use Appwrite\Messaging\Adapter\Realtime;
use PHPUnit\Framework\TestCase;
2024-03-06 17:34:21 +00:00
use Utopia\Database\Document;
2022-12-14 15:42:25 +00:00
use Utopia\Database\Helpers\ID;
2022-12-14 16:04:06 +00:00
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2021-06-29 13:11:14 +00:00
class MessagingTest extends TestCase
{
public function setUp(): void
{
}
public function tearDown(): void
{
}
2022-08-01 10:22:04 +00:00
public function testUser(): void
2021-06-29 13:11:14 +00:00
{
$realtime = new Realtime();
$realtime->subscribe(
'1',
1,
2022-08-19 04:04:33 +00:00
[
Role::user(ID::custom('123'))->toString(),
Role::users()->toString(),
Role::team(ID::custom('abc'))->toString(),
Role::team(ID::custom('abc'), 'administrator')->toString(),
Role::team(ID::custom('abc'), 'moderator')->toString(),
Role::team(ID::custom('def'))->toString(),
Role::team(ID::custom('def'), 'guest')->toString(),
],
2025-04-27 05:06:21 +00:00
['files' => 0, 'rows' => 0, 'rows.789' => 0, 'account.123' => 0]
2021-06-29 13:11:14 +00:00
);
$event = [
'project' => '1',
2022-08-19 04:04:33 +00:00
'roles' => [Role::any()->toString()],
2021-06-29 13:11:14 +00:00
'data' => [
'channels' => [
0 => 'account.123',
]
]
];
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::users()->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::user(ID::custom('123'))->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::team(ID::custom('abc'))->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::team(ID::custom('abc'), 'administrator')->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::team(ID::custom('abc'), 'moderator')->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::team(ID::custom('def'))->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::team(ID::custom('def'), 'guest')->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::user(ID::custom('456'))->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertEmpty($receivers);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::team(ID::custom('def'), 'member')->toString()];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertEmpty($receivers);
2022-08-19 04:04:33 +00:00
$event['roles'] = [Role::any()->toString()];
2025-04-27 05:06:21 +00:00
$event['data']['channels'] = ['rows.123'];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertEmpty($receivers);
2025-04-27 05:06:21 +00:00
$event['data']['channels'] = ['rows.789'];
2021-06-29 13:11:14 +00:00
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00:00
$this->assertCount(1, $receivers);
$this->assertEquals(1, $receivers[0]);
$event['project'] = '2';
2021-07-13 15:18:02 +00:00
$receivers = $realtime->getSubscribers($event);
2021-06-29 13:11:14 +00: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 11:36:58 +00:00
2022-08-01 10:22:04 +00:00
public function testConvertChannelsGuest(): void
2021-06-30 11:36:58 +00:00
{
$user = new Document([
'$id' => ''
]);
$channels = [
0 => 'files',
2025-04-27 05:06:21 +00:00
1 => 'rows',
2 => 'rows.789',
2021-06-30 11:36:58 +00:00
3 => 'account',
4 => 'account.456'
];
2021-07-13 15:18:02 +00:00
$channels = Realtime::convertChannels($channels, $user->getId());
2021-08-27 08:20:44 +00:00
$this->assertCount(4, $channels);
2021-06-30 11:36:58 +00:00
$this->assertArrayHasKey('files', $channels);
2025-04-27 05:06:21 +00:00
$this->assertArrayHasKey('rows', $channels);
$this->assertArrayHasKey('rows.789', $channels);
2021-08-27 08:20:44 +00:00
$this->assertArrayHasKey('account', $channels);
2021-06-30 11:36:58 +00:00
$this->assertArrayNotHasKey('account.456', $channels);
}
2022-08-01 10:22:04 +00:00
public function testConvertChannelsUser(): void
2021-06-30 11:36:58 +00:00
{
$user = new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('123'),
2021-06-30 11:36:58 +00:00
'memberships' => [
[
2022-08-14 10:33:36 +00:00
'teamId' => ID::custom('abc'),
2021-06-30 11:36:58 +00:00
'roles' => [
'administrator',
'moderator'
]
],
[
2022-08-14 10:33:36 +00:00
'teamId' => ID::custom('def'),
2021-06-30 11:36:58 +00:00
'roles' => [
'guest'
]
]
]
]);
$channels = [
0 => 'files',
2025-04-27 05:06:21 +00:00
1 => 'rows',
2 => 'rows.789',
2021-06-30 11:36:58 +00:00
3 => 'account',
4 => 'account.456'
];
2021-07-13 15:18:02 +00:00
$channels = Realtime::convertChannels($channels, $user->getId());
2021-06-30 11:36:58 +00:00
2021-08-27 08:20:44 +00:00
$this->assertCount(5, $channels);
2021-06-30 11:36:58 +00:00
$this->assertArrayHasKey('files', $channels);
2025-04-27 05:06:21 +00:00
$this->assertArrayHasKey('rows', $channels);
$this->assertArrayHasKey('rows.789', $channels);
2021-06-30 11:36:58 +00:00
$this->assertArrayHasKey('account.123', $channels);
2021-08-27 08:20:44 +00:00
$this->assertArrayHasKey('account', $channels);
2021-06-30 11:36:58 +00:00
$this->assertArrayNotHasKey('account.456', $channels);
}
2021-12-16 18:12:06 +00:00
2022-08-19 04:04:33 +00:00
public function testFromPayloadPermissions(): void
2021-12-16 18:12:06 +00:00
{
/**
* Test Collection Level Permissions
*/
$result = Realtime::fromPayload(
2025-04-27 05:06:21 +00:00
event: 'databases.database_id.tables.collection_id.rows.document_id.create',
2021-12-16 18:12:06 +00:00
payload: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('test'),
2025-04-27 05:06:21 +00:00
'$collection' => ID::custom('table'),
'$permissions' => [
2022-08-19 04:04:33 +00:00
Permission::read(Role::team('123abc')),
Permission::update(Role::team('123abc')),
Permission::delete(Role::team('123abc')),
],
2021-12-16 18:12:06 +00:00
]),
2022-08-13 14:55:15 +00:00
database: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('database'),
2022-08-13 14:55:15 +00:00
]),
2025-04-27 05:06:21 +00:00
table: new Document([
'$id' => ID::custom('table'),
'$permissions' => [
2022-08-14 05:21:11 +00:00
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2021-12-16 18:12:06 +00:00
])
);
2022-08-19 04:04:33 +00:00
$this->assertContains(Role::any()->toString(), $result['roles']);
$this->assertNotContains(Role::team('123abc')->toString(), $result['roles']);
2021-12-16 18:12:06 +00:00
/**
* Test Document Level Permissions
*/
$result = Realtime::fromPayload(
2025-04-27 05:06:21 +00:00
event: 'databases.database_id.tables.collection_id.rows.document_id.create',
2021-12-16 18:12:06 +00:00
payload: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('test'),
2025-04-27 05:06:21 +00:00
'$collection' => ID::custom('table'),
'$permissions' => [
2022-08-14 05:21:11 +00:00
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2021-12-16 18:12:06 +00:00
]),
2022-08-13 14:55:15 +00:00
database: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('database'),
2022-08-13 14:55:15 +00:00
]),
2025-04-27 05:06:21 +00:00
table: new Document([
'$id' => ID::custom('table'),
'$permissions' => [
2022-08-19 04:04:33 +00:00
Permission::read(Role::team('123abc')),
Permission::update(Role::team('123abc')),
Permission::delete(Role::team('123abc')),
],
'documentSecurity' => true,
2021-12-16 18:12:06 +00:00
])
);
2022-08-19 04:04:33 +00:00
$this->assertContains(Role::any()->toString(), $result['roles']);
$this->assertContains(Role::team('123abc')->toString(), $result['roles']);
2021-12-16 18:12:06 +00:00
}
2022-04-18 16:21:45 +00:00
public function testFromPayloadBucketLevelPermissions(): void
{
/**
2022-08-13 14:55:15 +00:00
* Test Bucket Level Permissions
2022-04-18 16:21:45 +00:00
*/
$result = Realtime::fromPayload(
event: 'buckets.bucket_id.files.file_id.create',
payload: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('test'),
'$collection' => ID::custom('bucket'),
'$permissions' => [
2022-08-19 04:04:33 +00:00
Permission::read(Role::team('123abc')),
Permission::update(Role::team('123abc')),
Permission::delete(Role::team('123abc')),
],
2022-04-18 16:21:45 +00:00
]),
bucket: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('bucket'),
'$permissions' => [
2022-08-14 05:21:11 +00:00
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-04-18 16:21:45 +00:00
])
);
2022-08-19 04:04:33 +00:00
$this->assertContains(Role::any()->toString(), $result['roles']);
$this->assertNotContains(Role::team('123abc')->toString(), $result['roles']);
2022-04-18 16:21:45 +00:00
/**
2022-08-13 14:55:15 +00:00
* Test File Level Permissions
2022-04-18 16:21:45 +00:00
*/
$result = Realtime::fromPayload(
event: 'buckets.bucket_id.files.file_id.create',
payload: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('test'),
'$collection' => ID::custom('bucket'),
'$permissions' => [
2022-08-14 05:21:11 +00:00
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-04-18 16:21:45 +00:00
]),
bucket: new Document([
2022-08-14 10:33:36 +00:00
'$id' => ID::custom('bucket'),
'$permissions' => [
2022-08-19 04:04:33 +00:00
Permission::read(Role::team('123abc')),
Permission::update(Role::team('123abc')),
Permission::delete(Role::team('123abc')),
],
2022-08-13 14:55:15 +00:00
'fileSecurity' => true
2022-04-18 16:21:45 +00:00
])
);
2022-08-19 04:04:33 +00:00
$this->assertContains(Role::any()->toString(), $result['roles']);
$this->assertContains(Role::team('123abc')->toString(), $result['roles']);
2022-04-18 16:21:45 +00:00
}
2021-06-29 13:11:14 +00:00
}