Path: blob/1.0-develop/tests/Integration/Api/Client/Server/CommandControllerTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server;34use GuzzleHttp\Psr7\Request;5use Illuminate\Http\Response;6use Pterodactyl\Models\Server;7use Pterodactyl\Models\Permission;8use GuzzleHttp\Exception\BadResponseException;9use GuzzleHttp\Psr7\Response as GuzzleResponse;10use Pterodactyl\Repositories\Wings\DaemonCommandRepository;11use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;12use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;1314class CommandControllerTest extends ClientApiIntegrationTestCase15{16/**17* Test that a validation error is returned if there is no command present in the18* request.19*/20public function testValidationErrorIsReturnedIfNoCommandIsPresent()21{22[$user, $server] = $this->generateTestAccount();2324$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [25'command' => '',26]);2728$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);29$response->assertJsonPath('errors.0.meta.rule', 'required');30}3132/**33* Test that a subuser without the required permission receives an error when trying to34* execute the command.35*/36public function testSubuserWithoutPermissionReceivesError()37{38[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);3940$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [41'command' => 'say Test',42]);4344$response->assertStatus(Response::HTTP_FORBIDDEN);45}4647/**48* Test that a command can be sent to the server.49*/50public function testCommandCanSendToServer()51{52[$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]);5354$mock = $this->mock(DaemonCommandRepository::class);55$mock->expects('setServer')56->with(\Mockery::on(fn (Server $value) => $value->is($server)))57->andReturnSelf();5859$mock->expects('send')->with('say Test')->andReturn(new GuzzleResponse());6061$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [62'command' => 'say Test',63]);6465$response->assertStatus(Response::HTTP_NO_CONTENT);66}6768/**69* Test that an error is returned when the server is offline that is more specific than the70* regular daemon connection error.71*/72public function testErrorIsReturnedWhenServerIsOffline()73{74[$user, $server] = $this->generateTestAccount();7576$mock = $this->mock(DaemonCommandRepository::class);77$mock->expects('setServer->send')->andThrows(78new DaemonConnectionException(79new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY))80)81);8283$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [84'command' => 'say Test',85]);8687$response->assertStatus(Response::HTTP_BAD_GATEWAY);88$response->assertJsonPath('errors.0.code', 'HttpException');89$response->assertJsonPath('errors.0.detail', 'Server must be online in order to send commands.');90}91}929394