|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BeyondCode\LaravelWebSockets\Tests\Dashboard; |
| 4 | + |
| 5 | +use BeyondCode\LaravelWebSockets\Tests\TestCase; |
| 6 | +use BeyondCode\LaravelWebSockets\Tests\Models\User; |
| 7 | +use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; |
| 8 | + |
| 9 | +class AuthTest extends TestCase |
| 10 | +{ |
| 11 | + /** @test */ |
| 12 | + public function can_authenticate_dashboard_over_channel() |
| 13 | + { |
| 14 | + $connection = $this->getConnectedWebSocketConnection(['test-channel']); |
| 15 | + |
| 16 | + $this->pusherServer->onOpen($connection); |
| 17 | + |
| 18 | + $this->actingAs(factory(User::class)->create()) |
| 19 | + ->json('POST', route('laravel-websockets.auth'), [ |
| 20 | + 'socket_id' => $connection->socketId, |
| 21 | + 'channel_name' => 'test-channel', |
| 22 | + ], ['x-app-id' => '1234']) |
| 23 | + ->seeJsonStructure([ |
| 24 | + 'auth', |
| 25 | + 'channel_data', |
| 26 | + ]); |
| 27 | + } |
| 28 | + |
| 29 | + /** @test */ |
| 30 | + public function can_authenticate_dashboard_over_private_channel() |
| 31 | + { |
| 32 | + $connection = $this->getWebSocketConnection(); |
| 33 | + |
| 34 | + $this->pusherServer->onOpen($connection); |
| 35 | + |
| 36 | + $signature = "{$connection->socketId}:private-channel"; |
| 37 | + |
| 38 | + $hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret); |
| 39 | + |
| 40 | + $message = new Message([ |
| 41 | + 'event' => 'pusher:subscribe', |
| 42 | + 'data' => [ |
| 43 | + 'auth' => "{$connection->app->key}:{$hashedAppSecret}", |
| 44 | + 'channel' => 'private-channel', |
| 45 | + ], |
| 46 | + ]); |
| 47 | + |
| 48 | + $this->pusherServer->onMessage($connection, $message); |
| 49 | + |
| 50 | + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ |
| 51 | + 'channel' => 'private-channel', |
| 52 | + ]); |
| 53 | + |
| 54 | + $this->actingAs(factory(User::class)->create()) |
| 55 | + ->json('POST', route('laravel-websockets.auth'), [ |
| 56 | + 'socket_id' => $connection->socketId, |
| 57 | + 'channel_name' => 'private-test-channel', |
| 58 | + ], ['x-app-id' => '1234']) |
| 59 | + ->seeJsonStructure([ |
| 60 | + 'auth', |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + /** @test */ |
| 65 | + public function can_authenticate_dashboard_over_presence_channel() |
| 66 | + { |
| 67 | + $connection = $this->getWebSocketConnection(); |
| 68 | + |
| 69 | + $this->pusherServer->onOpen($connection); |
| 70 | + |
| 71 | + $channelData = [ |
| 72 | + 'user_id' => 1, |
| 73 | + 'user_info' => [ |
| 74 | + 'name' => 'Marcel', |
| 75 | + ], |
| 76 | + ]; |
| 77 | + |
| 78 | + $signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); |
| 79 | + |
| 80 | + $message = new Message([ |
| 81 | + 'event' => 'pusher:subscribe', |
| 82 | + 'data' => [ |
| 83 | + 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), |
| 84 | + 'channel' => 'presence-channel', |
| 85 | + 'channel_data' => json_encode($channelData), |
| 86 | + ], |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->pusherServer->onMessage($connection, $message); |
| 90 | + |
| 91 | + $this->actingAs(factory(User::class)->create()) |
| 92 | + ->json('POST', route('laravel-websockets.auth'), [ |
| 93 | + 'socket_id' => $connection->socketId, |
| 94 | + 'channel_name' => 'presence-channel', |
| 95 | + ], ['x-app-id' => '1234']) |
| 96 | + ->seeJsonStructure([ |
| 97 | + 'auth', |
| 98 | + ]); |
| 99 | + } |
| 100 | +} |
0 commit comments