2018-12-03 09:30:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;
|
|
|
|
|
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsersController;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\TestCase;
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use Pusher\Pusher;
|
2018-12-03 09:30:05 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
|
|
|
|
|
|
class FetchUsersTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
/** @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/channel/my-channel';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'my-channel',
|
|
|
|
|
];
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'InvalidSecret', 'GET', $requestPath);
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:30:05 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchUsersController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_only_returns_data_for_presence_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('Invalid presence channel');
|
|
|
|
|
|
|
|
|
|
$this->getConnectedWebSocketConnection(['my-channel']);
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channel/my-channel/users';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'my-channel',
|
|
|
|
|
];
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:30:05 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchUsersController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_404_for_invalid_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(HttpException::class);
|
|
|
|
|
$this->expectExceptionMessage('Unknown channel');
|
|
|
|
|
|
|
|
|
|
$this->getConnectedWebSocketConnection(['my-channel']);
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channel/invalid-channel/users';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'invalid-channel',
|
|
|
|
|
];
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:30:05 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchUsersController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_connected_user_information()
|
|
|
|
|
{
|
|
|
|
|
$this->joinPresenceChannel('presence-channel');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$requestPath = '/apps/1234/channel/presence-channel/users';
|
|
|
|
|
$routeParams = [
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'channelName' => 'presence-channel',
|
|
|
|
|
];
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$queryString = Pusher::build_auth_query_string('TestKey', 'TestSecret', 'GET', $requestPath);
|
2018-12-03 09:30:05 +00:00
|
|
|
|
2019-01-02 21:30:57 +00:00
|
|
|
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
|
2018-12-03 09:30:05 +00:00
|
|
|
|
|
|
|
|
$controller = app(FetchUsersController::class);
|
|
|
|
|
|
|
|
|
|
$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([
|
|
|
|
|
'users' => [
|
|
|
|
|
[
|
2018-12-04 21:22:33 +00:00
|
|
|
'id' => 1,
|
|
|
|
|
],
|
|
|
|
|
],
|
2018-12-03 09:30:05 +00:00
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|