From eb1c0a13d55f8bc01687c98d890dc042faba22eb Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 26 Feb 2021 17:01:37 +0100 Subject: [PATCH] add unit tests --- tests/unit/Realtime/RealtimeChannelsTest.php | 310 +++++++++++++++++++ tests/unit/Realtime/RealtimeGuestTest.php | 205 ++++++++++++ tests/unit/Realtime/RealtimeTest.php | 223 +++++++++++++ 3 files changed, 738 insertions(+) create mode 100644 tests/unit/Realtime/RealtimeChannelsTest.php create mode 100644 tests/unit/Realtime/RealtimeGuestTest.php create mode 100644 tests/unit/Realtime/RealtimeTest.php diff --git a/tests/unit/Realtime/RealtimeChannelsTest.php b/tests/unit/Realtime/RealtimeChannelsTest.php new file mode 100644 index 0000000000..c28b308a5a --- /dev/null +++ b/tests/unit/Realtime/RealtimeChannelsTest.php @@ -0,0 +1,310 @@ +connectionsAuthenticated = count($this->allChannels) * $this->connectionsPerChannel; + $this->connectionsGuest = count($this->allChannels) * $this->connectionsPerChannel; + $this->connectionsTotal = $this->connectionsAuthenticated + $this->connectionsGuest; + + /** + * Add 100 Authenticated Clients + */ + for ($i = 0; $i < $this->connectionsPerChannel; $i++) { + foreach ($this->allChannels as $index => $channel) { + Realtime::setUser(new Document([ + '$id' => 'user' . $this->connectionsCount, + 'memberships' => [ + [ + 'teamId' => 'team' . $i, + 'roles' => [ + empty($index % 2) ? 'admin' : 'member' + ] + ] + ] + ])); + + Realtime::addSubscription( + '1', + $this->connectionsCount, + $this->subscriptions, + $this->connections, + Realtime::getRoles(), + Realtime::parseChannels([0 => $channel]) + ); + + $this->connectionsCount++; + } + } + + /** + * Add 100 Guest Clients + */ + for ($i = 0; $i < $this->connectionsPerChannel; $i++) { + foreach ($this->allChannels as $index => $channel) { + Realtime::setUser(new Document([ + '$id' => '' + ])); + + Realtime::addSubscription( + '1', + $this->connectionsCount, + $this->subscriptions, + $this->connections, + Realtime::getRoles(), + Realtime::parseChannels([0 => $channel]) + ); + + $this->connectionsCount++; + } + } + } + + public function tearDown(): void + { + $this->connections = []; + $this->subscriptions = []; + $this->connectionsCount = 0; + } + + public function testSubscriptions() + { + /** + * Check for 1 project. + */ + $this->assertCount(1, $this->subscriptions); + + /** + * Check for 133 subscriptions: + * - XXX users + * - 1 * + * - 1 role:guest + * - 1 role:member + * - 10 teams + * - 20 team roles (2 roles per team) + */ + $this->assertCount(($this->connectionsAuthenticated + (3 * $this->connectionsPerChannel) + 3), $this->subscriptions['1']); + + /** + * Check for 200 connections + * - 100 Authenticated + * - 100 Guests + */ + $this->assertCount($this->connectionsTotal, $this->connections); + } + + /** + * Tests Wildcard (*) Permissions on every channel. + */ + public function testWildcardPermission() + { + foreach ($this->allChannels as $index => $channel) { + $event = [ + 'project' => '1', + 'permissions' => ['*'], + 'data' => [ + 'channels' => [ + 0 => $channel, + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + /** + * Every Client subscribed to the Wildcard should receive this event. + */ + $this->assertCount($this->connectionsTotal / count($this->allChannels), $receivers, $channel); + + foreach ($receivers as $receiver) { + /** + * Making sure the right clients receive the event. + */ + $this->assertStringEndsWith($index, $receiver); + } + } + } + + public function testRolePermissions() + { + $roles = ['role:guest', 'role:member']; + foreach ($this->allChannels as $index => $channel) { + foreach ($roles as $role) { + $permissions = [$role]; + + $event = [ + 'project' => '1', + 'permissions' => $permissions, + 'data' => [ + 'channels' => [ + 0 => $channel, + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + /** + * Every Role subscribed to a Channel should receive this event. + */ + $this->assertCount($this->connectionsPerChannel, $receivers, $channel); + + foreach ($receivers as $receiver) { + /** + * Making sure the right clients receive the event. + */ + $this->assertStringEndsWith($index, $receiver); + } + } + } + } + + public function testUserPermissions() + { + foreach ($this->allChannels as $index => $channel) { + $permissions = []; + for ($i = 0; $i < $this->connectionsPerChannel; $i++) { + $permissions[] = 'user:user' . (!empty($i) ? $i : '') . $index; + } + $event = [ + 'project' => '1', + 'permissions' => $permissions, + 'data' => [ + 'channels' => [ + 0 => $channel, + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + /** + * Every Client subscribed to a Channel should receive this event. + */ + $this->assertCount($this->connectionsAuthenticated / count($this->allChannels), $receivers, $channel); + + foreach ($receivers as $receiver) { + /** + * Making sure the right clients receive the event. + */ + $this->assertStringEndsWith($index, $receiver); + } + } + } + + public function testTeamPermissions() + { + foreach ($this->allChannels as $index => $channel) { + $permissions = []; + + for ($i = 0; $i < $this->connectionsPerChannel; $i++) { + $permissions[] = 'team:team' . $i; + } + $event = [ + 'project' => '1', + 'permissions' => $permissions, + 'data' => [ + 'channels' => [ + 0 => $channel, + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + /** + * Every Team Member should receive this event. + */ + $this->assertCount($this->connectionsAuthenticated / count($this->allChannels), $receivers, $channel); + + foreach ($receivers as $receiver) { + /** + * Making sure the right clients receive the event. + */ + $this->assertStringEndsWith($index, $receiver); + } + + $permissions = ['team:team' . $index . '/' . (empty($index % 2) ? 'admin' : 'member')]; + + $event = [ + 'project' => '1', + 'permissions' => $permissions, + 'data' => [ + 'channels' => [ + 0 => $channel, + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + /** + * Only 1 Team Member of a role should have access to a specific channel. + */ + $this->assertCount(1, $receivers, $channel); + + foreach ($receivers as $receiver) { + /** + * Making sure the right clients receive the event. + */ + $this->assertStringEndsWith($index, $receiver); + } + } + } +} diff --git a/tests/unit/Realtime/RealtimeGuestTest.php b/tests/unit/Realtime/RealtimeGuestTest.php new file mode 100644 index 0000000000..35507a0d50 --- /dev/null +++ b/tests/unit/Realtime/RealtimeGuestTest.php @@ -0,0 +1,205 @@ + '' + ])); + + $roles = Realtime::getRoles(); + $this->assertCount(2, $roles); + $this->assertContains('*', $roles); + $this->assertContains('role:guest', $roles); + + $channels = [ + 0 => 'files', + 1 => 'documents', + 2 => 'documents.789', + 3 => 'account', + 4 => 'account.456' + ]; + + $channels = Realtime::parseChannels($channels); + $this->assertCount(3, $channels); + $this->assertArrayHasKey('files', $channels); + $this->assertArrayHasKey('documents', $channels); + $this->assertArrayHasKey('documents.789', $channels); + $this->assertArrayNotHasKey('account', $channels); + $this->assertArrayNotHasKey('account.456', $channels); + + Realtime::addSubscription('1', 1, $this->subscriptions, $this->connections, $roles, $channels); + + + $event = [ + 'project' => '1', + 'permissions' => ['*'], + 'data' => [ + 'channels' => [ + 0 => 'documents', + 1 => 'documents', + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['role:guest']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['role:member']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['user:123']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:abc']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:abc/administrator']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:abc/god']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:def']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:def/guest']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['user:456']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:def/member']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['*']; + $event['data']['channels'] = ['documents.123']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['data']['channels'] = ['documents.789']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['project'] = '2'; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + } +} diff --git a/tests/unit/Realtime/RealtimeTest.php b/tests/unit/Realtime/RealtimeTest.php new file mode 100644 index 0000000000..ea97d2c5c9 --- /dev/null +++ b/tests/unit/Realtime/RealtimeTest.php @@ -0,0 +1,223 @@ + '123', + 'memberships' => [ + [ + 'teamId' => 'abc', + 'roles' => [ + 'administrator', + 'god' + ] + ], + [ + 'teamId' => 'def', + 'roles' => [ + 'guest' + ] + ] + ] + ])); + + $roles = Realtime::getRoles(); + + $this->assertCount(8, $roles); + $this->assertContains('*', $roles); + $this->assertContains('user:123', $roles); + $this->assertContains('role:member', $roles); + $this->assertContains('team:abc', $roles); + $this->assertContains('team:abc/administrator', $roles); + $this->assertContains('team:abc/god', $roles); + $this->assertContains('team:def', $roles); + $this->assertContains('team:def/guest', $roles); + + $channels = [ + 0 => 'files', + 1 => 'documents', + 2 => 'documents.789', + 3 => 'account', + 4 => 'account.456' + ]; + + $channels = Realtime::parseChannels($channels); + + $this->assertCount(4, $channels); + $this->assertArrayHasKey('files', $channels); + $this->assertArrayHasKey('documents', $channels); + $this->assertArrayHasKey('documents.789', $channels); + $this->assertArrayHasKey('account.123', $channels); + $this->assertArrayNotHasKey('account', $channels); + $this->assertArrayNotHasKey('account.456', $channels); + + Realtime::addSubscription('1', 1, $this->subscriptions, $this->connections, $roles, $channels); + + $event = [ + 'project' => '1', + 'permissions' => ['*'], + 'data' => [ + 'channels' => [ + 0 => 'account.123', + ] + ] + ]; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['role:member']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['user:123']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['team:abc']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['team:abc/administrator']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['team:abc/god']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['team:def']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['team:def/guest']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['permissions'] = ['user:456']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['team:def/member']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['permissions'] = ['*']; + $event['data']['channels'] = ['documents.123']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + + $event['data']['channels'] = ['documents.789']; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertCount(1, $receivers); + $this->assertEquals(1, $receivers[0]); + + $event['project'] = '2'; + + $receivers = Realtime::identifyReceivers( + $event, + $this->connections, + $this->subscriptions + ); + + $this->assertEmpty($receivers); + } +}