laravel-websockets/tests/FetchUsersTest.php

118 lines
3.9 KiB
PHP
Raw Normal View History

2018-12-03 09:30:05 +00:00
<?php
2025-01-16 07:54:02 +00:00
namespace BlaxSoftware\LaravelWebSockets\Test;
2018-12-03 09:30:05 +00:00
2025-01-16 07:54:02 +00:00
use BlaxSoftware\LaravelWebSockets\API\FetchUsers;
2020-03-04 09:58:39 +00:00
use GuzzleHttp\Psr7\Request;
use Pusher\Pusher;
2018-12-03 09:30:05 +00:00
class FetchUsersTest extends TestCase
{
2020-09-10 19:59:26 +00:00
public function test_invalid_signatures_can_not_access_the_api()
2018-12-03 09:30:05 +00:00
{
$this->startServer();
2018-12-03 09:30:05 +00:00
$requestPath = '/apps/1234/channels/my-channel/users';
2018-12-03 09:30:05 +00:00
$queryString = http_build_query(Pusher::build_auth_query_params(
2020-09-10 19:59:26 +00:00
'TestKey', 'InvalidSecret', 'GET', $requestPath
));
2018-12-03 09:30:05 +00:00
$response = $this->await($this->browser->get('http://localhost:4000'."{$requestPath}?{$queryString}"));
2018-12-03 09:30:05 +00:00
$this->assertSame(401, $response->getStatusCode());
$this->assertSame('{"error":"Invalid auth signature provided."}', $response->getBody()->getContents());
2018-12-03 09:30:05 +00:00
}
2020-09-10 19:59:26 +00:00
public function test_it_only_returns_data_for_presence_channels()
2018-12-03 09:30:05 +00:00
{
$this->startServer();
2020-09-10 19:59:26 +00:00
$requestPath = '/apps/1234/channels/my-channel/users';
2018-12-03 09:30:05 +00:00
$queryString = http_build_query(Pusher::build_auth_query_params(
2020-09-10 19:59:26 +00:00
'TestKey', 'TestSecret', 'GET', $requestPath
));
2018-12-03 09:30:05 +00:00
$response = $this->await($this->browser->get('http://localhost:4000'."{$requestPath}?{$queryString}"));
2018-12-03 09:30:05 +00:00
$this->assertSame(400, $response->getStatusCode());
$this->assertSame('{"error":"Invalid presence channel `my-channel`"}', $response->getBody()->getContents());
2018-12-03 09:30:05 +00:00
}
public function test_it_returns_400_for_invalid_channels()
2018-12-03 09:30:05 +00:00
{
$this->startServer();
2018-12-03 09:30:05 +00:00
$requestPath = '/apps/1234/channels/invalid-channel/users';
2020-09-10 19:59:26 +00:00
$queryString = http_build_query(Pusher::build_auth_query_params(
2020-09-10 19:59:26 +00:00
'TestKey', 'TestSecret', 'GET', $requestPath
));
2018-12-03 09:30:05 +00:00
$response = $this->await($this->browser->get('http://localhost:4000'."{$requestPath}?{$queryString}"));
2018-12-03 09:30:05 +00:00
$this->assertSame(400, $response->getStatusCode());
$this->assertSame('{"error":"Invalid presence channel `invalid-channel`"}', $response->getBody()->getContents());
2018-12-03 09:30:05 +00:00
}
2020-09-10 19:59:26 +00:00
public function test_it_returns_connected_user_information()
2018-12-03 09:30:05 +00:00
{
2020-09-10 19:59:26 +00:00
$this->newPresenceConnection('presence-channel');
2020-08-14 16:53:30 +00:00
2020-09-10 19:59:26 +00:00
$connection = new Mocks\Connection;
2018-12-03 09:30:05 +00:00
$requestPath = '/apps/1234/channel/presence-channel/users';
2020-09-10 19:59:26 +00:00
$routeParams = [
'appId' => '1234',
'channelName' => 'presence-channel',
];
2018-12-03 09:30:05 +00:00
$queryString = http_build_query(Pusher::build_auth_query_params('TestKey', 'TestSecret', 'GET', $requestPath));
2018-12-03 09:30:05 +00:00
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
2018-12-03 09:30:05 +00:00
2020-09-10 19:59:26 +00:00
$controller = app(FetchUsers::class);
2018-12-03 09:30:05 +00:00
$controller->onOpen($connection, $request);
2018-12-04 20:07:35 +00:00
/** @var \Illuminate\Http\JsonResponse $response */
2018-12-03 09:30:05 +00:00
$response = array_pop($connection->sentRawData);
$this->assertSame([
2020-09-10 19:59:26 +00:00
'users' => [['id' => 1]],
2018-12-03 09:30:05 +00:00
], json_decode($response->getContent(), true));
}
public function test_multiple_clients_with_same_id_gets_counted_once()
{
$rick = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
$morty = $this->newPresenceConnection('presence-channel', ['user_id' => 1]);
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/channel/presence-channel/users';
$routeParams = [
'appId' => '1234',
'channelName' => 'presence-channel',
];
$queryString = http_build_query(Pusher::build_auth_query_params('TestKey', 'TestSecret', 'GET', $requestPath));
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(FetchUsers::class);
$controller->onOpen($connection, $request);
/** @var \Illuminate\Http\JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([
'users' => [['id' => 1]],
], json_decode($response->getContent(), true));
}
2018-12-04 21:22:33 +00:00
}