Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:beyondcode/laravel-websockets int…
Browse files Browse the repository at this point in the history
…o 2.x
  • Loading branch information
rennokki committed Aug 13, 2020
2 parents 8e6ca73 + 20d45e8 commit d76d7bb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -20,8 +22,11 @@ public function up()
$table->nullableTimestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Expand Down
8 changes: 7 additions & 1 deletion src/WebSockets/Channels/PresenceChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,18 @@ protected function getUserIds(array $users): array
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;
Expand Down
9 changes: 3 additions & 6 deletions src/WebSocketsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -33,11 +32,9 @@ public function boot()
__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'),
], 'migrations');
}
$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'),
], 'migrations');

$this
->registerRoutes()
Expand Down
34 changes: 30 additions & 4 deletions tests/Channels/PresenceChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()

$channelData = [
'user_id' => 1,
'user_info' => [
'name' => 'Marcel',
],
];

$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
Expand Down Expand Up @@ -102,6 +99,35 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
$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()
{
Expand All @@ -128,6 +154,6 @@ public function clients_with_valid_auth_signatures_cannot_leave_channels_they_ar

$this->pusherServer->onMessage($connection, $message);

$this->markTestAsPassed();
$this->assertTrue(true);
}
}
6 changes: 2 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public function setUp(): void
$this->channelManager,
Mockery::mock(Browser::class)
));

$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}

protected function getPackageProviders($app)
Expand All @@ -55,10 +57,6 @@ protected function getEnvironmentSetUp($app)
'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
Expand Down

0 comments on commit d76d7bb

Please sign in to comment.