diff --git a/tests/Mocks/SignedMessage.php b/tests/Mocks/SignedMessage.php new file mode 100644 index 0000000..10db94d --- /dev/null +++ b/tests/Mocks/SignedMessage.php @@ -0,0 +1,32 @@ +socketId}:{$channelName}"; + + if ($encodedUser) { + $signature .= ":{$encodedUser}"; + } + + $hash = hash_hmac('sha256', $signature, $connection->app->secret); + + $this->payload['data']['auth'] = "{$connection->app->key}:{$hash}"; + } +} diff --git a/tests/PresenceChannelTest.php b/tests/PresenceChannelTest.php index 9d4bbcb..2bd54e2 100644 --- a/tests/PresenceChannelTest.php +++ b/tests/PresenceChannelTest.php @@ -40,17 +40,13 @@ class PresenceChannelTest extends TestCase $encodedUser = json_encode($user); - $signature = "{$connection->socketId}:presence-channel:".$encodedUser; - $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' => 'presence-channel', - 'channel_data' => json_encode($user), + 'channel_data' => $encodedUser, ], - ]); + ], $connection, 'presence-channel', $encodedUser); $this->pusherServer->onMessage($connection, $message); @@ -187,7 +183,7 @@ class PresenceChannelTest extends TestCase }); } - public function test_local_connections_for_private_channels() + public function test_local_connections_for_presence_channels() { $this->newPresenceConnection('presence-channel', ['user_id' => 1]); $this->newPresenceConnection('presence-channel-2', ['user_id' => 2]); diff --git a/tests/ReplicationTest.php b/tests/ReplicationTest.php index f08c6b0..e864c6b 100644 --- a/tests/ReplicationTest.php +++ b/tests/ReplicationTest.php @@ -4,15 +4,32 @@ namespace BeyondCode\LaravelWebSockets\Test; class ReplicationTest extends TestCase { + /** + * {@inheritdoc} + */ + public function setUp(): void + { + parent::setUp(); + + $this->runOnlyOnRedisReplication(); + } + + public function test_publishing_client_gets_subscribed() + { + $this->newActiveConnection(['public-channel']); + + $this->getSubscribeClient() + ->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234')]) + ->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]); + } + public function test_events_get_replicated_across_connections() { - $this->runOnlyOnRedisReplication(); - $connection = $this->newActiveConnection(['public-channel']); $message = [ 'appId' => '1234', - 'serverId' => 0, + 'serverId' => $this->channelManager->getServerId(), 'event' => 'some-event', 'data' => [ 'channel' => 'public-channel', @@ -31,12 +48,19 @@ class ReplicationTest extends TestCase 'serverId' => $this->channelManager->getServerId(), 'data' => ['channel' => 'public-channel', 'test' => 'yes'], ]); + + $this->getSubscribeClient() + ->assertNothingDispatched(); + + $this->getPublishClient() + ->assertCalledWithArgs('publish', [ + $this->channelManager->getRedisKey('1234', 'public-channel'), + json_encode($message), + ]); } public function test_not_ponged_connections_do_get_removed_for_public_channels() { - $this->runOnlyOnRedisReplication(); - $connection = $this->newActiveConnection(['public-channel']); // Make the connection look like it was lost 1 day ago. @@ -65,8 +89,6 @@ class ReplicationTest extends TestCase public function test_not_ponged_connections_do_get_removed_for_private_channels() { - $this->runOnlyOnRedisReplication(); - $connection = $this->newPrivateConnection('private-channel'); // Make the connection look like it was lost 1 day ago. @@ -95,8 +117,6 @@ class ReplicationTest extends TestCase public function test_not_ponged_connections_do_get_removed_for_presence_channels() { - $this->runOnlyOnRedisReplication(); - $connection = $this->newPresenceConnection('presence-channel'); // Make the connection look like it was lost 1 day ago. diff --git a/tests/TestCase.php b/tests/TestCase.php index e4b3064..da8dbae 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -331,18 +331,15 @@ abstract class TestCase extends Orchestra 'user_info' => ['name' => 'Rick'], ]; - $signature = "{$connection->socketId}:{$channel}:".json_encode($user); + $encodedUser = json_encode($user); - $hash = hash_hmac('sha256', $signature, $connection->app->secret); - - $message = new Mocks\Message([ + $message = new Mocks\SignedMessage([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => "{$connection->app->key}:{$hash}", 'channel' => $channel, - 'channel_data' => json_encode($user), + 'channel_data' => $encodedUser, ], - ]); + ], $connection, $channel, $encodedUser); $this->pusherServer->onMessage($connection, $message); @@ -363,17 +360,12 @@ abstract class TestCase extends Orchestra $this->pusherServer->onOpen($connection); - $signature = "{$connection->socketId}:{$channel}"; - - $hash = hash_hmac('sha256', $signature, $connection->app->secret); - - $message = new Mocks\Message([ + $message = new Mocks\SignedMessage([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => "{$connection->app->key}:{$hash}", 'channel' => $channel, ], - ]); + ], $connection, $channel); $this->pusherServer->onMessage($connection, $message);