Files
panel-pelican-dev/tests/Integration/Api/Client/Server/CommandControllerTest.php

99 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Api\Client\Server;
use App\Http\Controllers\Api\Client\Servers\CommandController;
use App\Http\Requests\Api\Client\Servers\SendCommandRequest;
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\Response;
2024-03-12 22:39:16 -04:00
use App\Models\Server;
use App\Models\Permission;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
2024-03-12 22:39:16 -04:00
use App\Exceptions\Http\Connection\DaemonConnectionException;
use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
use Symfony\Component\HttpKernel\Exception\HttpException;
class CommandControllerTest extends ClientApiIntegrationTestCase
{
/**
* Test that a validation error is returned if there is no command present in the
* request.
*/
2024-03-19 21:12:27 -04:00
public function testValidationErrorIsReturnedIfNoCommandIsPresent(): void
{
[$user, $server] = $this->generateTestAccount();
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => '',
]);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.meta.rule', 'required');
}
/**
* Test that a subuser without the required permission receives an error when trying to
* execute the command.
*/
2024-03-19 21:12:27 -04:00
public function testSubuserWithoutPermissionReceivesError(): void
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => 'say Test',
]);
$response->assertStatus(Response::HTTP_FORBIDDEN);
}
/**
* Test that a command can be sent to the server.
*/
2024-03-19 21:12:27 -04:00
public function testCommandCanSendToServer(): void
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]);
2024-03-16 15:11:10 -04:00
$server = \Mockery::mock($server)->makePartial();
2024-03-16 15:11:10 -04:00
$this->instance(Server::class, $server);
$server->expects('send')->with('say Test')->andReturn(new GuzzleResponse());
$request = new SendCommandRequest(['command' => 'say Test']);
$cc = resolve(CommandController::class);
$response = $cc->index($request, $server);
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
}
/**
* Test that an error is returned when the server is offline that is more specific than the
* regular daemon connection error.
*/
2024-03-19 21:12:27 -04:00
public function testErrorIsReturnedWhenServerIsOffline(): void
{
[$user, $server] = $this->generateTestAccount();
2024-03-16 15:11:10 -04:00
$server = \Mockery::mock($server)->makePartial();
$server->expects('send')->andThrows(
2020-07-26 10:55:30 -07:00
new DaemonConnectionException(
new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY))
)
);
2024-03-16 15:11:10 -04:00
$this->instance(Server::class, $server);
$request = new SendCommandRequest(['command' => 'say Test']);
$cc = resolve(CommandController::class);
$this->expectException(HttpException::class);
$this->expectExceptionMessageMatches('/Server must be online in order to send commands\./');
$response = $cc->index($request, $server);
$this->assertEquals(Response::HTTP_BAD_GATEWAY, $response->getStatusCode());
}
}