From f75ed6d86c21ae8c6b4f5323b9429517d85f7715 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Mon, 26 Nov 2018 00:33:07 +0100 Subject: [PATCH] wip --- tests/ConnectionTest.php | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 7c350fa..142e521 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -141,4 +141,62 @@ class ConnectionTest extends TestCase 'channel' => 'private-channel' ]); } + + /** @test */ + public function clients_need_valid_auth_signatures_for_presence_channels() + { + $this->expectException(InvalidSignatureException::class); + + /** @var PusherServer $server */ + $server = app(PusherServer::class); + + $connection = $this->getWebSocketConnection(); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'auth' => 'invalid', + 'channel' => 'presence-channel' + ], + ])); + + $server->onOpen($connection); + + $server->onMessage($connection, $message); + } + + /** @test */ + public function clients_can_subscribe_to_presence_channels() + { + /** @var PusherServer $server */ + $server = app(PusherServer::class); + + $connection = $this->getWebSocketConnection(); + + $server->onOpen($connection); + + $channelData = [ + 'user_id' => 1, + 'user_info' => [ + 'name' => 'Marcel' + ] + ]; + + $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), + 'channel' => 'presence-channel', + 'channel_data' => json_encode($channelData) + ], + ])); + + $server->onMessage($connection, $message); + + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ + 'channel' => 'presence-channel', + ]); + } } \ No newline at end of file