2018-12-03 09:06:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-12-03 09:22:13 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;
|
2018-12-03 09:06:40 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
use Pusher\Pusher;
|
2018-12-03 09:21:35 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
2018-12-03 09:21:35 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController;
|
2018-12-03 09:06:40 +00:00
|
|
|
|
2018-12-03 09:22:13 +00:00
|
|
|
class FetchChannelsTest extends TestCase
|
2018-12-03 09:06:40 +00:00
|
|
|
{
|
2018-12-03 09:21:35 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function invalid_signatures_can_not_access_the_api()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('Invalid auth signature provided.');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
2018-12-03 09:21:35 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'InvalidSecret', 'GET', $requestPath);
|
2018-12-03 09:21:35 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:21:35 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_the_channel_information()
|
|
|
|
|
{
|
|
|
|
|
$this->joinPresenceChannel('presence-channel');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
2018-12-03 09:21:35 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-03 09:21:35 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:21:35 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->assertSame([
|
|
|
|
|
'channels' => [
|
2019-05-11 06:52:04 +00:00
|
|
|
'presence-channel' => [],
|
2018-12-04 21:22:33 +00:00
|
|
|
],
|
2018-12-03 09:21:35 +00:00
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
2018-12-07 19:42:20 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
2019-01-02 21:30:57 +00:00
|
|
|
public function it_returns_the_channel_information_for_prefix()
|
2018-12-07 19:42:20 +00:00
|
|
|
{
|
2019-01-02 21:30:57 +00:00
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.2');
|
|
|
|
|
$this->joinPresenceChannel('presence-notglobal.2');
|
|
|
|
|
|
2018-12-07 19:42:20 +00:00
|
|
|
$connection = new Connection();
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath, [
|
|
|
|
|
'filter_by_prefix' => 'presence-global',
|
|
|
|
|
]);
|
2018-12-07 19:42:20 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
2019-05-11 06:52:04 +00:00
|
|
|
$this->assertSame([
|
|
|
|
|
'channels' => [
|
|
|
|
|
'presence-global.1' => [],
|
|
|
|
|
'presence-global.2' => [],
|
|
|
|
|
],
|
|
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_the_channel_information_for_prefix_with_user_count()
|
|
|
|
|
{
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.1');
|
|
|
|
|
$this->joinPresenceChannel('presence-global.2');
|
|
|
|
|
$this->joinPresenceChannel('presence-notglobal.2');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath, [
|
|
|
|
|
'filter_by_prefix' => 'presence-global',
|
|
|
|
|
'info' => 'user_count',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$this->assertSame([
|
|
|
|
|
'channels' => [
|
|
|
|
|
'presence-global.1' => [
|
|
|
|
|
'user_count' => 2,
|
|
|
|
|
],
|
|
|
|
|
'presence-global.2' => [
|
|
|
|
|
'user_count' => 1,
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 06:52:04 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function can_not_get_non_presence_channel_user_count()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('Request must be limited to presence channels in order to fetch user_count');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath, [
|
|
|
|
|
'info' => 'user_count',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_empty_object_for_no_channels_found()
|
|
|
|
|
{
|
|
|
|
|
$connection = new Connection();
|
2018-12-07 19:42:20 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channels';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
];
|
2018-12-07 19:42:20 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-07 19:42:20 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-07 19:42:20 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->assertSame('{"channels":{}}', $response->getContent());
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|