Merge branch '2.x' of github.com:beyondcode/laravel-websockets into refactor/tests
This commit is contained in:
commit
4c676cba8a
|
|
@ -1,13 +1,15 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateWebSocketsStatisticsEntriesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
|
|
@ -20,8 +22,11 @@ class CreateWebSocketsStatisticsEntriesTable extends Migration
|
|||
$table->nullableTimestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
|
|
@ -139,12 +139,18 @@ class PresenceChannel extends Channel
|
|||
return array_values($userIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the hash for the presence channel integrity.
|
||||
*
|
||||
* @param array $users
|
||||
* @return array
|
||||
*/
|
||||
protected function getHash(array $users): array
|
||||
{
|
||||
$hash = [];
|
||||
|
||||
foreach ($users as $socketId => $channelData) {
|
||||
$hash[$channelData->user_id] = $channelData->user_info;
|
||||
$hash[$channelData->user_id] = $channelData->user_info ?? [];
|
||||
}
|
||||
|
||||
return $hash;
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChanne
|
|||
use Illuminate\Broadcasting\BroadcastManager;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Pusher\Pusher;
|
||||
|
|
@ -33,11 +32,9 @@ class WebSocketsServiceProvider extends ServiceProvider
|
|||
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
|
||||
], 'config');
|
||||
|
||||
if (! Schema::hasTable('websockets_statistics_entries')) {
|
||||
$this->publishes([
|
||||
__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'),
|
||||
__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'),
|
||||
], 'migrations');
|
||||
}
|
||||
|
||||
$this
|
||||
->registerRoutes()
|
||||
|
|
|
|||
|
|
@ -69,9 +69,6 @@ class PresenceChannelTest extends TestCase
|
|||
|
||||
$channelData = [
|
||||
'user_id' => 1,
|
||||
'user_info' => [
|
||||
'name' => 'Marcel',
|
||||
],
|
||||
];
|
||||
|
||||
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
|
||||
|
|
@ -102,6 +99,35 @@ class PresenceChannelTest extends TestCase
|
|||
$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 */
|
||||
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->markTestAsPassed();
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
$this->channelManager,
|
||||
Mockery::mock(Browser::class)
|
||||
));
|
||||
|
||||
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
|
|
@ -55,10 +57,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'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
|
||||
|
|
|
|||
Loading…
Reference in New Issue