misc_pterodactyl-panel/tests/Integration/Api/Client/Server/CommandControllerTest.php
2023-02-23 12:30:16 -07:00

92 lines
3.2 KiB
PHP

<?php
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Permission;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class CommandControllerTest extends ClientApiIntegrationTestCase
{
/**
* Test that a validation error is returned if there is no command present in the
* request.
*/
public function testValidationErrorIsReturnedIfNoCommandIsPresent()
{
[$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.
*/
public function testSubuserWithoutPermissionReceivesError()
{
[$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.
*/
public function testCommandCanSendToServer()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]);
$mock = $this->mock(DaemonCommandRepository::class);
$mock->expects('setServer')
->with(\Mockery::on(fn (Server $value) => $value->is($server)))
->andReturnSelf();
$mock->expects('send')->with('say Test')->andReturn(new GuzzleResponse());
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => 'say Test',
]);
$response->assertStatus(Response::HTTP_NO_CONTENT);
}
/**
* Test that an error is returned when the server is offline that is more specific than the
* regular daemon connection error.
*/
public function testErrorIsReturnedWhenServerIsOffline()
{
[$user, $server] = $this->generateTestAccount();
$mock = $this->mock(DaemonCommandRepository::class);
$mock->expects('setServer->send')->andThrows(
new DaemonConnectionException(
new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY))
)
);
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => 'say Test',
]);
$response->assertStatus(Response::HTTP_BAD_GATEWAY);
$response->assertJsonPath('errors.0.code', 'HttpException');
$response->assertJsonPath('errors.0.detail', 'Server must be online in order to send commands.');
}
}