laravel-websockets/tests/Channels/PrivateChannelTest.php

57 lines
1.6 KiB
PHP
Raw Normal View History

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
use BeyondCode\LaravelWebSockets\Tests\TestCase;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
2018-11-27 20:56:25 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
class PrivateChannelTest extends TestCase
{
/** @test */
public function clients_need_valid_auth_signatures_to_join_private_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' => 'private-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_private_channels()
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$signature = "{$connection->socketId}:private-channel";
2018-12-01 13:12:15 +00:00
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
2018-11-28 22:59:58 +00:00
2018-11-27 20:56:25 +00:00
$message = new Message(json_encode([
'event' => 'pusher:subscribe',
'data' => [
2018-12-01 13:12:15 +00:00
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
2018-12-04 21:22:33 +00:00
'channel' => 'private-channel',
2018-11-27 20:56:25 +00:00
],
]));
$this->pusherServer->onMessage($connection, $message);
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
2018-12-04 21:22:33 +00:00
'channel' => 'private-channel',
2018-11-27 20:56:25 +00:00
]);
}
2018-12-04 21:22:33 +00:00
}