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