2018-11-27 20:56:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-12-06 19:55:48 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests\Channels;
|
2018-11-27 20:56:25 +00:00
|
|
|
|
2020-08-14 12:35:36 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
2018-11-27 20:56:25 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
|
|
|
|
|
|
|
|
|
|
class PresenceChannelTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
/** @test */
|
|
|
|
|
public function clients_need_valid_auth_signatures_to_join_presence_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(InvalidSignature::class);
|
|
|
|
|
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => 'invalid',
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => 'presence-channel',
|
2018-11-27 20:56:25 +00:00
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @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' => [
|
2018-12-04 21:22:33 +00:00
|
|
|
'name' => 'Marcel',
|
|
|
|
|
],
|
2018-11-27 20:56:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
2018-12-01 13:12:15 +00:00
|
|
|
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
|
2018-11-27 20:56:25 +00:00
|
|
|
'channel' => 'presence-channel',
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel_data' => json_encode($channelData),
|
2018-11-27 20:56:25 +00:00
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
2020-08-14 12:51:57 +00:00
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'presence-channel',
|
|
|
|
|
]);
|
2018-11-27 20:56:25 +00:00
|
|
|
}
|
2019-04-05 19:30:41 +00:00
|
|
|
|
|
|
|
|
/** @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);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'presence-channel',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 16:17:06 +00:00
|
|
|
/** @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);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'presence-channel',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2020-08-13 17:02:29 +00:00
|
|
|
|
2019-04-05 19:30:41 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function clients_with_valid_auth_signatures_cannot_leave_channels_they_are_not_in()
|
|
|
|
|
{
|
|
|
|
|
$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:unsubscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
|
|
|
|
|
'channel' => 'presence-channel',
|
|
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
2020-08-13 17:02:29 +00:00
|
|
|
$this->assertTrue(true);
|
2019-04-05 19:30:41 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|