Merge branch 'master' of github.com:beyondcode/laravel-websockets into 2.x

This commit is contained in:
Alex Renoki 2020-08-13 20:02:29 +03:00
commit d76d7bbe67
5 changed files with 49 additions and 17 deletions

View File

@ -1,13 +1,15 @@
<?php <?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateWebSocketsStatisticsEntriesTable extends Migration class CreateWebSocketsStatisticsEntriesTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
*
* @return void
*/ */
public function up() public function up()
{ {
@ -20,8 +22,11 @@ class CreateWebSocketsStatisticsEntriesTable extends Migration
$table->nullableTimestamps(); $table->nullableTimestamps();
}); });
} }
/** /**
* Reverse the migrations. * Reverse the migrations.
*
* @return void
*/ */
public function down() public function down()
{ {

View File

@ -139,12 +139,18 @@ class PresenceChannel extends Channel
return array_values($userIds); return array_values($userIds);
} }
/**
* Compute the hash for the presence channel integrity.
*
* @param array $users
* @return array
*/
protected function getHash(array $users): array protected function getHash(array $users): array
{ {
$hash = []; $hash = [];
foreach ($users as $socketId => $channelData) { foreach ($users as $socketId => $channelData) {
$hash[$channelData->user_id] = $channelData->user_info; $hash[$channelData->user_id] = $channelData->user_info ?? [];
} }
return $hash; return $hash;

View File

@ -20,7 +20,6 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChanne
use Illuminate\Broadcasting\BroadcastManager; use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Pusher\Pusher; use Pusher\Pusher;
@ -33,11 +32,9 @@ class WebSocketsServiceProvider extends ServiceProvider
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), __DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
], 'config'); ], 'config');
if (! Schema::hasTable('websockets_statistics_entries')) { $this->publishes([
$this->publishes([ __DIR__.'/../database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php' => database_path('migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php'),
__DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_statistics_entries_table.php'), ], 'migrations');
], 'migrations');
}
$this $this
->registerRoutes() ->registerRoutes()

View File

@ -69,9 +69,6 @@ class PresenceChannelTest extends TestCase
$channelData = [ $channelData = [
'user_id' => 1, 'user_id' => 1,
'user_info' => [
'name' => 'Marcel',
],
]; ];
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
@ -102,6 +99,35 @@ class PresenceChannelTest extends TestCase
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
} }
/** @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',
]);
}
/** @test */ /** @test */
public function clients_with_valid_auth_signatures_cannot_leave_channels_they_are_not_in() public function clients_with_valid_auth_signatures_cannot_leave_channels_they_are_not_in()
{ {
@ -128,6 +154,6 @@ class PresenceChannelTest extends TestCase
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
$this->markTestAsPassed(); $this->assertTrue(true);
} }
} }

View File

@ -34,6 +34,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
$this->channelManager, $this->channelManager,
Mockery::mock(Browser::class) Mockery::mock(Browser::class)
)); ));
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
} }
protected function getPackageProviders($app) protected function getPackageProviders($app)
@ -55,10 +57,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'enable_statistics' => true, 'enable_statistics' => true,
], ],
]); ]);
include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub';
(new \CreateWebSocketsStatisticsEntriesTable())->up();
} }
protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection