Skip to content

Commit

Permalink
implement terminate user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 19, 2023
1 parent 35336c7 commit ef7434a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Pusher/Http/Controllers/UsersTerminateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
use Laravel\Reverb\Http\Connection;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class UsersTerminateController extends Controller
{
/**
* Handle the request.
*/
public function handle(RequestInterface $request, Connection $connection, ...$args)
public function handle(RequestInterface $request, Connection $connection, ...$args): Response
{
if(! $connection = $this->connections->find($args['user'])) {
return new JsonResponse((object) [], 400);
}

$connection->disconnect();

return new JsonResponse((object) []);
}
}
2 changes: 1 addition & 1 deletion src/Servers/Reverb/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected static function routes(): RouteCollection
$routes->add('channels', Route::get('/apps/{appId}/channels', new ChannelsController));
$routes->add('channel', Route::get('/apps/{appId}/channels/{channel}', new ChannelController));
$routes->add('channel_users', Route::get('/apps/{appId}/channels/{channel}/users', new ChannelUsersController));
// $routes->add('users_terminate', Route::post('/apps/{appId}/users/{user}/terminate_connections', new UsersTerminateController));
$routes->add('users_terminate', Route::post('/apps/{appId}/users/{user}/terminate_connections', new UsersTerminateController));

return $routes;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/Ratchet/UsersTerminateControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Support\Arr;
use Laravel\Reverb\Channels\ChannelBroker;
use Laravel\Reverb\Tests\RatchetTestCase;
use React\Http\Message\ResponseException;

use function React\Async\await;

uses(RatchetTestCase::class);

it('returns an error when connection cannot be found', function () {
await($this->signedPostRequest('channels/users/not-a-user/terminate_connections'));
})->throws(ResponseException::class);

it('unsubscribes from all channels and terminates a user', function () {
$connection = $this->connect();
$this->subscribe('test-channel-one', connection: $connection);
$this->subscribe('test-channel-two', connection: $connection);

$this->subscribe('test-channel-one');
$this->subscribe('test-channel-two');

expect($connections = connectionManager()->all())->toHaveCount(3);
expect(channelManager()->connections(ChannelBroker::create('test-channel-one')))->toHaveCount(2);
expect(channelManager()->connections(ChannelBroker::create('test-channel-two')))->toHaveCount(2);

$connection = Arr::first($connections);

$response = await($this->signedPostRequest("users/{$connection->id()}/terminate_connections"));

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{}', $response->getBody()->getContents());
expect($connections = connectionManager()->all())->toHaveCount(2);
expect(channelManager()->connections(ChannelBroker::create('test-channel-one')))->toHaveCount(1);
expect(channelManager()->connections(ChannelBroker::create('test-channel-two')))->toHaveCount(1);
});

0 comments on commit ef7434a

Please sign in to comment.