diff --git a/tests/Channels/ChannelTest.php b/tests/Channels/ChannelTest.php new file mode 100644 index 0000000..88952b0 --- /dev/null +++ b/tests/Channels/ChannelTest.php @@ -0,0 +1,30 @@ +getWebSocketConnection(); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'channel' => 'basic-channel' + ], + ])); + + $this->pusherServer->onOpen($connection); + + $this->pusherServer->onMessage($connection, $message); + + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ + 'channel' => 'basic-channel' + ]); + } +} \ No newline at end of file diff --git a/tests/Channels/PresenceChannelTest.php b/tests/Channels/PresenceChannelTest.php new file mode 100644 index 0000000..f44fd9f --- /dev/null +++ b/tests/Channels/PresenceChannelTest.php @@ -0,0 +1,62 @@ +expectException(InvalidSignature::class); + + $connection = $this->getWebSocketConnection(); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'auth' => 'invalid', + 'channel' => 'presence-channel' + ], + ])); + + $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' => [ + '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) + ], + ])); + + $this->pusherServer->onMessage($connection, $message); + + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ + 'channel' => 'presence-channel', + ]); + } +} \ No newline at end of file diff --git a/tests/Channels/PrivateChannelTest.php b/tests/Channels/PrivateChannelTest.php new file mode 100644 index 0000000..57ff7f6 --- /dev/null +++ b/tests/Channels/PrivateChannelTest.php @@ -0,0 +1,54 @@ +expectException(InvalidSignature::class); + + $connection = $this->getWebSocketConnection(); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'auth' => 'invalid', + 'channel' => 'private-channel' + ], + ])); + + $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"; + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), + 'channel' => 'private-channel' + ], + ])); + + $this->pusherServer->onMessage($connection, $message); + + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ + 'channel' => 'private-channel' + ]); + } +} \ No newline at end of file diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 45c780a..c92ff80 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -3,22 +3,11 @@ namespace BeyondCode\LaravelWebSockets\Tests; use BeyondCode\LaravelWebSockets\ClientProviders\Client; -use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey; -use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; class ConnectionTest extends TestCase { - /** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */ - protected $pusherServer; - - public function setUp() - { - parent::setUp(); - - $this->pusherServer = app(WebSocketHandler::class); - } /** @test */ public function unknown_app_keys_can_not_connect() @@ -65,121 +54,4 @@ class ConnectionTest extends TestCase $connection->assertSentEvent('pusher:pong'); } - - /** @test */ - public function clients_can_subscribe_to_basic_channels() - { - $connection = $this->getWebSocketConnection(); - - $message = new Message(json_encode([ - 'event' => 'pusher:subscribe', - 'data' => [ - 'channel' => 'basic-channel' - ], - ])); - - $this->pusherServer->onOpen($connection); - - $this->pusherServer->onMessage($connection, $message); - - $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ - 'channel' => 'basic-channel' - ]); - } - - /** @test */ - public function clients_need_valid_auth_signatures_for_private_channels() - { - $this->expectException(InvalidSignature::class); - - $connection = $this->getWebSocketConnection(); - - $message = new Message(json_encode([ - 'event' => 'pusher:subscribe', - 'data' => [ - 'auth' => 'invalid', - 'channel' => 'private-channel' - ], - ])); - - $this->pusherServer->onOpen($connection); - - $this->pusherServer->onMessage($connection, $message); - } - - /** @test */ - public function clients_can_subscribe_to_private_channels() - { - $connection = $this->getWebSocketConnection(); - - $this->pusherServer->onOpen($connection); - - $signature = "{$connection->socketId}:private-channel"; - - $message = new Message(json_encode([ - 'event' => 'pusher:subscribe', - 'data' => [ - 'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), - 'channel' => 'private-channel' - ], - ])); - - $this->pusherServer->onMessage($connection, $message); - - $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ - 'channel' => 'private-channel' - ]); - } - - /** @test */ - public function clients_need_valid_auth_signatures_for_presence_channels() - { - $this->expectException(InvalidSignature::class); - - $connection = $this->getWebSocketConnection(); - - $message = new Message(json_encode([ - 'event' => 'pusher:subscribe', - 'data' => [ - 'auth' => 'invalid', - 'channel' => 'presence-channel' - ], - ])); - - $this->pusherServer->onOpen($connection); - - $this->pusherServer->onMessage($connection, $message); - } - - /** @test */ - public function clients_can_subscribe_to_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->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), - 'channel' => 'presence-channel', - 'channel_data' => json_encode($channelData) - ], - ])); - - $this->pusherServer->onMessage($connection, $message); - - $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ - 'channel' => 'presence-channel', - ]); - } } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 9636394..d50e27a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,12 +2,24 @@ namespace BeyondCode\LaravelWebSockets\Tests; +use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; use GuzzleHttp\Psr7\Request; use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection; use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider; abstract class TestCase extends \Orchestra\Testbench\TestCase { + + /** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */ + protected $pusherServer; + + public function setUp() + { + parent::setUp(); + + $this->pusherServer = app(WebSocketHandler::class); + } + protected function getPackageProviders($app) { return [WebSocketsServiceProvider::class];