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
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
$auth_key = 'TestKey';
|
|
|
|
|
$auth_timestamp = time();
|
|
|
|
|
$auth_version = '1.0';
|
|
|
|
|
|
2018-12-04 21:22:33 +00:00
|
|
|
$queryParameters = http_build_query(compact('auth_key', 'auth_timestamp', 'auth_version'));
|
2018-12-03 09:21:35 +00:00
|
|
|
|
|
|
|
|
$signature =
|
2018-12-04 21:22:33 +00:00
|
|
|
"GET\n/apps/1234/channels\n".
|
|
|
|
|
"auth_key={$auth_key}".
|
|
|
|
|
"&auth_timestamp={$auth_timestamp}".
|
2018-12-03 09:21:35 +00:00
|
|
|
"&auth_version={$auth_version}";
|
|
|
|
|
|
|
|
|
|
$auth_signature = hash_hmac('sha256', $signature, 'InvalidSecret');
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}");
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_the_channel_information()
|
|
|
|
|
{
|
|
|
|
|
$this->joinPresenceChannel('presence-channel');
|
|
|
|
|
$this->joinPresenceChannel('presence-channel');
|
|
|
|
|
$this->joinPresenceChannel('presence-channel');
|
|
|
|
|
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$auth_key = 'TestKey';
|
|
|
|
|
$auth_timestamp = time();
|
|
|
|
|
$auth_version = '1.0';
|
|
|
|
|
|
2018-12-04 21:22:33 +00:00
|
|
|
$queryParameters = http_build_query(compact('auth_key', 'auth_timestamp', 'auth_version'));
|
2018-12-03 09:21:35 +00:00
|
|
|
|
|
|
|
|
$signature =
|
2018-12-04 21:22:33 +00:00
|
|
|
"GET\n/apps/1234/channels\n".
|
|
|
|
|
"auth_key={$auth_key}".
|
|
|
|
|
"&auth_timestamp={$auth_timestamp}".
|
2018-12-03 09:21:35 +00:00
|
|
|
"&auth_version={$auth_version}";
|
|
|
|
|
|
|
|
|
|
$auth_signature = hash_hmac('sha256', $signature, 'TestSecret');
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}");
|
|
|
|
|
|
|
|
|
|
$controller = app(FetchChannelsController::class);
|
|
|
|
|
|
|
|
|
|
$controller->onOpen($connection, $request);
|
|
|
|
|
|
|
|
|
|
/** @var JsonResponse $response */
|
|
|
|
|
$response = array_pop($connection->sentRawData);
|
|
|
|
|
|
|
|
|
|
$this->assertSame([
|
|
|
|
|
'channels' => [
|
|
|
|
|
'presence-channel' => [
|
2018-12-04 21:22:33 +00:00
|
|
|
'user_count' => 3,
|
|
|
|
|
],
|
|
|
|
|
],
|
2018-12-03 09:21:35 +00:00
|
|
|
], json_decode($response->getContent(), true));
|
|
|
|
|
}
|
2018-12-07 19:42:20 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function it_returns_empty_object_for_no_channels_found()
|
|
|
|
|
{
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$auth_key = 'TestKey';
|
|
|
|
|
$auth_timestamp = time();
|
|
|
|
|
$auth_version = '1.0';
|
|
|
|
|
|
|
|
|
|
$queryParameters = http_build_query(compact('auth_key', 'auth_timestamp', 'auth_version'));
|
|
|
|
|
|
|
|
|
|
$signature =
|
|
|
|
|
"GET\n/apps/1234/channels\n".
|
|
|
|
|
"auth_key={$auth_key}".
|
|
|
|
|
"&auth_timestamp={$auth_timestamp}".
|
|
|
|
|
"&auth_version={$auth_version}";
|
|
|
|
|
|
|
|
|
|
$auth_signature = hash_hmac('sha256', $signature, 'TestSecret');
|
|
|
|
|
|
|
|
|
|
$request = new Request('GET', "/apps/1234/channels?appId=1234&auth_signature={$auth_signature}&{$queryParameters}");
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
}
|