laravel-websockets/tests/Dashboard/AuthTest.php

90 lines
2.7 KiB
PHP
Raw Normal View History

2020-08-23 16:12:22 +00:00
<?php
2020-09-10 19:59:26 +00:00
namespace BeyondCode\LaravelWebSockets\Test\Dashboard;
2020-08-23 16:12:22 +00:00
2020-09-18 09:15:49 +00:00
use BeyondCode\LaravelWebSockets\Test\Mocks\SignedMessage;
2020-09-10 19:59:26 +00:00
use BeyondCode\LaravelWebSockets\Test\Models\User;
use BeyondCode\LaravelWebSockets\Test\TestCase;
2020-08-23 16:12:22 +00:00
class AuthTest extends TestCase
{
2020-09-10 19:59:26 +00:00
public function test_can_authenticate_dashboard_over_channel()
2020-08-23 16:12:22 +00:00
{
2020-09-10 19:59:26 +00:00
$connection = $this->newActiveConnection(['test-channel']);
2020-08-23 16:12:22 +00:00
$this->pusherServer->onOpen($connection);
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.auth'), [
'socket_id' => $connection->socketId,
'channel_name' => 'test-channel',
], ['x-app-id' => '1234'])
->seeJsonStructure([
'auth',
'channel_data',
]);
}
2020-09-10 19:59:26 +00:00
public function test_can_authenticate_dashboard_over_private_channel()
2020-08-23 16:12:22 +00:00
{
2020-09-10 19:59:26 +00:00
$connection = $this->newConnection();
2020-08-23 16:12:22 +00:00
$this->pusherServer->onOpen($connection);
2020-09-18 09:15:49 +00:00
$message = new SignedMessage([
2020-08-23 16:12:22 +00:00
'event' => 'pusher:subscribe',
'data' => [
'channel' => 'private-channel',
],
2020-09-18 09:15:49 +00:00
], $connection, 'private-channel');
2020-08-23 16:12:22 +00:00
$this->pusherServer->onMessage($connection, $message);
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
'channel' => 'private-channel',
]);
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.auth'), [
'socket_id' => $connection->socketId,
'channel_name' => 'private-test-channel',
], ['x-app-id' => '1234'])
->seeJsonStructure([
'auth',
]);
}
2020-09-10 19:59:26 +00:00
public function test_can_authenticate_dashboard_over_presence_channel()
2020-08-23 16:12:22 +00:00
{
2020-09-10 19:59:26 +00:00
$connection = $this->newConnection();
2020-08-23 16:12:22 +00:00
$this->pusherServer->onOpen($connection);
2020-09-18 09:15:49 +00:00
$user = json_encode([
2020-08-23 16:12:22 +00:00
'user_id' => 1,
'user_info' => [
2020-09-10 19:59:26 +00:00
'name' => 'Rick',
2020-08-23 16:12:22 +00:00
],
2020-09-18 09:15:49 +00:00
]);
2020-08-23 16:12:22 +00:00
2020-09-18 09:15:49 +00:00
$message = new SignedMessage([
2020-08-23 16:12:22 +00:00
'event' => 'pusher:subscribe',
'data' => [
'channel' => 'presence-channel',
2020-09-18 09:15:49 +00:00
'channel_data' => $user,
2020-08-23 16:12:22 +00:00
],
2020-09-18 09:15:49 +00:00
], $connection, 'presence-channel', $user);
2020-08-23 16:12:22 +00:00
$this->pusherServer->onMessage($connection, $message);
$this->actingAs(factory(User::class)->create())
->json('POST', route('laravel-websockets.auth'), [
'socket_id' => $connection->socketId,
'channel_name' => 'presence-channel',
], ['x-app-id' => '1234'])
->seeJsonStructure([
'auth',
]);
}
}