runOnlyOnRedisReplication(); } /** @test */ public function clients_with_valid_auth_signatures_can_join_presence_channels() { $connection = $this->getWebSocketConnection(); $this->pusherServer->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->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), 'channel' => 'presence-channel', 'channel_data' => json_encode($channelData), ], ])); $this->pusherServer->onMessage($connection, $message); $this->getPublishClient() ->assertCalledWithArgs('hset', [ '1234:presence-channel', $connection->socketId, json_encode($channelData), ]) ->assertCalledWithArgs('hgetall', ['1234:presence-channel']) ->assertCalled('publish'); } /** @test */ public function clients_with_valid_auth_signatures_can_leave_presence_channels() { $connection = $this->getWebSocketConnection(); $this->pusherServer->onOpen($connection); $channelData = [ 'user_id' => 1, ]; $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); $message = new Message(json_encode([ 'event' => 'pusher:subscribe', 'data' => [ 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), 'channel' => 'presence-channel', 'channel_data' => json_encode($channelData), ], ])); $this->pusherServer->onMessage($connection, $message); $this->getSubscribeClient() ->assertEventDispatched('message'); $this->getPublishClient() ->assertCalled('hset') ->assertCalledWithArgs('hgetall', ['1234:presence-channel']) ->assertCalled('publish'); $this->getPublishClient() ->resetAssertions(); $message = new Message(json_encode([ 'event' => 'pusher:unsubscribe', 'data' => [ 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), 'channel' => 'presence-channel', ], ])); $this->pusherServer->onMessage($connection, $message); $this->getPublishClient() ->assertCalled('hdel') ->assertCalled('publish'); } /** @test */ public function clients_with_no_user_info_can_join_presence_channels() { $connection = $this->getWebSocketConnection(); $this->pusherServer->onOpen($connection); $channelData = [ 'user_id' => 1, ]; $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); $message = new Message(json_encode([ 'event' => 'pusher:subscribe', 'data' => [ 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), 'channel' => 'presence-channel', 'channel_data' => json_encode($channelData), ], ])); $this->pusherServer->onMessage($connection, $message); $this->getPublishClient() ->assertCalled('hset') ->assertcalledWithArgs('hgetall', ['1234:presence-channel']) ->assertCalled('publish'); } }