Merge branch 'refactor/tests' of github.com:beyondcode/laravel-websockets into refactor/pubsub-replication-tests
This commit is contained in:
commit
7053600ed0
|
|
@ -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()
|
||||||
{
|
{
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
@ -34,11 +33,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()
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
@ -67,10 +69,6 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||||
'websockets.replication.driver',
|
'websockets.replication.driver',
|
||||||
getenv('REPLICATION_DRIVER') ?: 'local'
|
getenv('REPLICATION_DRIVER') ?: 'local'
|
||||||
);
|
);
|
||||||
|
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue