From 9c2ac543220c389083d4bbfbd8307e681a851ab5 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Mon, 3 Dec 2018 10:21:35 +0100 Subject: [PATCH] More tests --- tests/HttpApi/FetchChannelTest.php | 80 +++++++++++++++++++++++++++++- tests/Mocks/Connection.php | 3 ++ tests/TestCase.php | 29 +++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/tests/HttpApi/FetchChannelTest.php b/tests/HttpApi/FetchChannelTest.php index 0cda8fd..4636e2e 100644 --- a/tests/HttpApi/FetchChannelTest.php +++ b/tests/HttpApi/FetchChannelTest.php @@ -1,10 +1,86 @@ expectException(HttpException::class); + $this->expectExceptionMessage('Invalid auth signature provided.'); + + $connection = new Connection(); + + $auth_key = 'TestKey'; + $auth_timestamp = time(); + $auth_version = '1.0'; + + $queryParameters = http_build_query(compact('auth_key','auth_timestamp','auth_version')); + + $signature = + "GET\n/apps/1234/channels\n" . + "auth_key={$auth_key}" . + "&auth_timestamp={$auth_timestamp}" . + "&auth_version={$auth_version}"; + + $auth_signature = hash_hmac('sha256', $signature, 'InvalidSecret'); + + $request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}"); + + $controller = app(FetchChannelsController::class); + + $controller->onOpen($connection, $request); + } + + /** @test */ + public function it_returns_the_channel_information() + { + $this->joinPresenceChannel('presence-channel'); + $this->joinPresenceChannel('presence-channel'); + $this->joinPresenceChannel('presence-channel'); + + $connection = new Connection(); + + $auth_key = 'TestKey'; + $auth_timestamp = time(); + $auth_version = '1.0'; + + $queryParameters = http_build_query(compact('auth_key','auth_timestamp','auth_version')); + + $signature = + "GET\n/apps/1234/channels\n" . + "auth_key={$auth_key}" . + "&auth_timestamp={$auth_timestamp}" . + "&auth_version={$auth_version}"; + + $auth_signature = hash_hmac('sha256', $signature, 'TestSecret'); + + $request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}"); + + $controller = app(FetchChannelsController::class); + + $controller->onOpen($connection, $request); + + /** @var JsonResponse $response */ + $response = array_pop($connection->sentRawData); + + $this->assertSame([ + 'channels' => [ + 'presence-channel' => [ + 'user_count' => 3 + ] + ] + ], json_decode($response->getContent(), true)); + } + } \ No newline at end of file diff --git a/tests/Mocks/Connection.php b/tests/Mocks/Connection.php index 5717183..1ecfbca 100644 --- a/tests/Mocks/Connection.php +++ b/tests/Mocks/Connection.php @@ -13,11 +13,14 @@ class Connection implements ConnectionInterface public $sentData = []; + public $sentRawData = []; + public $closed = false; public function send($data) { $this->sentData[] = json_decode($data, true); + $this->sentRawData[] = $data; } public function close() diff --git a/tests/TestCase.php b/tests/TestCase.php index 1d3d965..07a3e3e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -76,6 +76,35 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase return $connection; } + protected function joinPresenceChannel($channel): Connection + { + $connection = $this->getWebSocketConnection(); + + $this->pusherServer->onOpen($connection); + + $channelData = [ + 'user_id' => 1, + 'user_info' => [ + 'name' => 'Marcel' + ] + ]; + + $signature = "{$connection->socketId}:{$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' => $channel, + 'channel_data' => json_encode($channelData) + ], + ])); + + $this->pusherServer->onMessage($connection, $message); + + return $connection; + } + protected function getChannel(ConnectionInterface $connection, string $channelName) { return $this->channelManager->findOrCreate($connection->app->id, $channelName);