2020-06-27 19:04:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use Illuminate\Http\Response;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
|
|
|
use GuzzleHttp\Exception\BadResponseException;
|
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse;
|
|
|
|
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
|
2020-07-26 17:55:30 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2020-06-27 19:04:41 +00:00
|
|
|
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();
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
|
2020-06-27 19:04:41 +00:00
|
|
|
'command' => '',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
2020-08-28 04:19:01 +00:00
|
|
|
$response->assertJsonPath('errors.0.meta.rule', 'required');
|
2020-06-27 19:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
|
2020-06-27 19:04:41 +00:00
|
|
|
'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]);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$mock = $this->mock(DaemonCommandRepository::class);
|
|
|
|
$mock->expects('setServer')
|
2023-02-23 19:30:16 +00:00
|
|
|
->with(\Mockery::on(fn (Server $value) => $value->is($server)))
|
2022-10-14 16:59:20 +00:00
|
|
|
->andReturnSelf();
|
|
|
|
|
|
|
|
$mock->expects('send')->with('say Test')->andReturn(new GuzzleResponse());
|
2020-06-27 19:04:41 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
|
2020-06-27 19:04:41 +00:00
|
|
|
'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();
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$mock = $this->mock(DaemonCommandRepository::class);
|
|
|
|
$mock->expects('setServer->send')->andThrows(
|
2020-07-26 17:55:30 +00:00
|
|
|
new DaemonConnectionException(
|
|
|
|
new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY))
|
|
|
|
)
|
2020-06-27 19:04:41 +00:00
|
|
|
);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
|
2020-06-27 19:04:41 +00:00
|
|
|
'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.');
|
|
|
|
}
|
|
|
|
}
|