diff --git a/tests/Dashboard/AuthTest.php b/tests/Dashboard/AuthTest.php index 5522bca..bc67361 100644 --- a/tests/Dashboard/AuthTest.php +++ b/tests/Dashboard/AuthTest.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Test\Dashboard; -use BeyondCode\LaravelWebSockets\Test\Mocks\Message; +use BeyondCode\LaravelWebSockets\Test\Mocks\SignedMessage; use BeyondCode\LaravelWebSockets\Test\Models\User; use BeyondCode\LaravelWebSockets\Test\TestCase; @@ -31,17 +31,12 @@ class AuthTest extends TestCase $this->pusherServer->onOpen($connection); - $signature = "{$connection->socketId}:private-channel"; - - $hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret); - - $message = new Message([ + $message = new SignedMessage([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => "{$connection->app->key}:{$hashedAppSecret}", 'channel' => 'private-channel', ], - ]); + ], $connection, 'private-channel'); $this->pusherServer->onMessage($connection, $message); @@ -65,23 +60,20 @@ class AuthTest extends TestCase $this->pusherServer->onOpen($connection); - $channelData = [ + $user = json_encode([ 'user_id' => 1, 'user_info' => [ 'name' => 'Rick', ], - ]; + ]); - $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); - - $message = new Message([ + $message = new SignedMessage([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), 'channel' => 'presence-channel', - 'channel_data' => json_encode($channelData), + 'channel_data' => $user, ], - ]); + ], $connection, 'presence-channel', $user); $this->pusherServer->onMessage($connection, $message); diff --git a/tests/Mocks/Message.php b/tests/Mocks/Message.php index 2915494..04a5a1a 100644 --- a/tests/Mocks/Message.php +++ b/tests/Mocks/Message.php @@ -33,4 +33,24 @@ class Message extends BaseMessage { return json_encode($this->payload); } + + /** + * Get the payload as object. + * + * @return stdClass + */ + public function getPayloadAsObject() + { + return json_decode($this->getPayload()); + } + + /** + * Get the payload as array. + * + * @return stdClass + */ + public function getPayloadAsArray(): array + { + return $this->payload; + } } diff --git a/tests/PrivateChannelTest.php b/tests/PrivateChannelTest.php index f28ce6d..53f325b 100644 --- a/tests/PrivateChannelTest.php +++ b/tests/PrivateChannelTest.php @@ -31,16 +31,12 @@ class PrivateChannelTest extends TestCase $this->pusherServer->onOpen($connection); - $signature = "{$connection->socketId}:private-channel"; - $hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret); - - $message = new Mocks\Message([ + $message = new Mocks\SignedMessage([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => "{$connection->app->key}:{$hashedAppSecret}", 'channel' => 'private-channel', ], - ]); + ], $connection, 'private-channel'); $this->pusherServer->onMessage($connection, $message); diff --git a/tests/ReplicationTest.php b/tests/ReplicationTest.php index 8eaad91..d23d86d 100644 --- a/tests/ReplicationTest.php +++ b/tests/ReplicationTest.php @@ -25,11 +25,12 @@ class ReplicationTest extends TestCase ->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]); } - public function test_events_get_replicated_across_connections() + public function test_events_get_replicated_across_connections_for_public_channels() { $connection = $this->newActiveConnection(['public-channel']); + $receiver = $this->newActiveConnection(['public-channel']); - $message = [ + $message = new Mocks\Message([ 'appId' => '1234', 'serverId' => $this->channelManager->getServerId(), 'event' => 'some-event', @@ -37,19 +38,16 @@ class ReplicationTest extends TestCase 'channel' => 'public-channel', 'test' => 'yes', ], - ]; + 'socketId' => $connection->socketId, + ]); $channel = $this->channelManager->find('1234', 'public-channel'); $channel->broadcastToEveryoneExcept( - (object) $message, null, '1234', true + $message->getPayloadAsObject(), $connection->socketId, '1234', true ); - $connection->assertSentEvent('some-event', [ - 'appId' => '1234', - 'serverId' => $this->channelManager->getServerId(), - 'data' => ['channel' => 'public-channel', 'test' => 'yes'], - ]); + $receiver->assertSentEvent('some-event', $message->getPayloadAsArray()); $this->getSubscribeClient() ->assertNothingDispatched(); @@ -57,7 +55,85 @@ class ReplicationTest extends TestCase $this->getPublishClient() ->assertCalledWithArgs('publish', [ $this->channelManager->getRedisKey('1234', 'public-channel'), - json_encode($message), + $message->getPayload() + ]); + } + + public function test_events_get_replicated_across_connections_for_private_channels() + { + $connection = $this->newPrivateConnection('private-channel'); + $receiver = $this->newPrivateConnection('private-channel'); + + $message = new Mocks\SignedMessage([ + 'appId' => '1234', + 'serverId' => $this->channelManager->getServerId(), + 'event' => 'some-event', + 'data' => [ + 'channel' => 'private-channel', + 'test' => 'yes', + ], + 'socketId' => $connection->socketId, + ], $connection, 'private-channel'); + + $channel = $this->channelManager->find('1234', 'private-channel'); + + $channel->broadcastToEveryoneExcept( + $message->getPayloadAsObject(), $connection->socketId, '1234', true + ); + + $receiver->assertSentEvent('some-event', $message->getPayloadAsArray()); + + $this->getSubscribeClient() + ->assertNothingDispatched(); + + $this->getPublishClient() + ->assertCalledWithArgs('publish', [ + $this->channelManager->getRedisKey('1234', 'private-channel'), + $message->getPayload() + ]); + } + + public function test_events_get_replicated_across_connections_for_presence_channels() + { + $connection = $this->newPresenceConnection('presence-channel'); + $receiver = $this->newPresenceConnection('presence-channel', ['user_id' => 2]); + + $user = [ + 'user_id' => 1, + 'user_info' => [ + 'name' => 'Rick', + ], + ]; + + $encodedUser = json_encode($user); + + $message = new Mocks\SignedMessage([ + 'appId' => '1234', + 'serverId' => $this->channelManager->getServerId(), + 'event' => 'some-event', + 'data' => [ + 'channel' => 'presence-channel', + 'channel_data' => $encodedUser, + 'test' => 'yes', + ], + 'socketId' => $connection->socketId, + ], $connection, 'presence-channel', $encodedUser); + + $channel = $this->channelManager->find('1234', 'presence-channel'); + + $channel->broadcastToEveryoneExcept( + $message->getPayloadAsObject(), $connection->socketId, '1234', true + ); + + $receiver->assertSentEvent('some-event', $message->getPayloadAsArray()); + + $this->getSubscribeClient() + ->assertNothingDispatched(); + + $this->getPublishClient() + ->assertCalledWithArgs('publish', [ + $this->channelManager->getRedisKey('1234', 'presence-channel'), + $message->getPayload() ]); } @@ -186,4 +262,108 @@ class ReplicationTest extends TestCase $this->assertCount(1, $members); }); } + + public function test_events_are_processed_by_on_message_on_public_channels() + { + $connection = $this->newActiveConnection(['public-channel']); + + $message = new Mocks\Message([ + 'appId' => '1234', + 'serverId' => 'different_server_id', + 'event' => 'some-event', + 'data' => [ + 'channel' => 'public-channel', + 'test' => 'yes', + ], + ]); + + $this->channelManager->onMessage( + $this->channelManager->getRedisKey('1234', 'public-channel'), + $message->getPayload() + ); + + // The message does not contain appId and serverId anymore. + $message = new Mocks\Message([ + 'event' => 'some-event', + 'data' => [ + 'channel' => 'public-channel', + 'test' => 'yes', + ], + ]); + + $connection->assertSentEvent('some-event', $message->getPayloadAsArray()); + } + + public function test_events_are_processed_by_on_message_on_private_channels() + { + $connection = $this->newPrivateConnection('private-channel'); + + $message = new Mocks\SignedMessage([ + 'appId' => '1234', + 'serverId' => 'different_server_id', + 'event' => 'some-event', + 'data' => [ + 'channel' => 'private-channel', + 'test' => 'yes', + ], + ], $connection, 'private-channel'); + + $this->channelManager->onMessage( + $this->channelManager->getRedisKey('1234', 'private-channel'), + $message->getPayload() + ); + + // The message does not contain appId and serverId anymore. + $message = new Mocks\SignedMessage([ + 'event' => 'some-event', + 'data' => [ + 'channel' => 'private-channel', + 'test' => 'yes', + ], + ], $connection, 'private-channel'); + + $connection->assertSentEvent('some-event', $message->getPayloadAsArray()); + } + + public function test_events_are_processed_by_on_message_on_presence_channels() + { + $user = [ + 'user_id' => 1, + 'user_info' => [ + 'name' => 'Rick', + ], + ]; + + $connection = $this->newPresenceConnection('presence-channel', $user); + + $encodedUser = json_encode($user); + + $message = new Mocks\SignedMessage([ + 'appId' => '1234', + 'serverId' => 'different_server_id', + 'event' => 'some-event', + 'data' => [ + 'channel' => 'presence-channel', + 'channel_data' => $encodedUser, + 'test' => 'yes', + ], + ], $connection, 'presence-channel', $encodedUser); + + $this->channelManager->onMessage( + $this->channelManager->getRedisKey('1234', 'presence-channel'), + $message->getPayload() + ); + + // The message does not contain appId and serverId anymore. + $message = new Mocks\SignedMessage([ + 'event' => 'some-event', + 'data' => [ + 'channel' => 'presence-channel', + 'channel_data' => $encodedUser, + 'test' => 'yes', + ], + ], $connection, 'presence-channel', $encodedUser); + + $connection->assertSentEvent('some-event', $message->getPayloadAsArray()); + } } diff --git a/tests/TriggerEventTest.php b/tests/TriggerEventTest.php index 9b087bd..8952198 100644 --- a/tests/TriggerEventTest.php +++ b/tests/TriggerEventTest.php @@ -190,10 +190,11 @@ class TriggerEventTest extends TestCase ->assertCalledWithArgs('publish', [ $this->channelManager->getRedisKey('1234', 'public-channel'), json_encode([ - 'channel' => 'public-channel', 'event' => null, + 'channel' => 'public-channel', 'data' => null, 'appId' => '1234', + 'socketId' => null, 'serverId' => $this->channelManager->getServerId(), ]), ]);