|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Api\Client\Server; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use GuzzleHttp\Psr7\Request; |
| 7 | +use Illuminate\Http\Response; |
| 8 | +use Pterodactyl\Models\Permission; |
| 9 | +use GuzzleHttp\Exception\BadResponseException; |
| 10 | +use GuzzleHttp\Psr7\Response as GuzzleResponse; |
| 11 | +use Pterodactyl\Repositories\Wings\DaemonCommandRepository; |
| 12 | +use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; |
| 13 | + |
| 14 | +class CommandControllerTest extends ClientApiIntegrationTestCase |
| 15 | +{ |
| 16 | + /** @var \Mockery\MockInterface */ |
| 17 | + private $repository; |
| 18 | + |
| 19 | + /** |
| 20 | + * Setup tests. |
| 21 | + */ |
| 22 | + public function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->repository = Mockery::mock(DaemonCommandRepository::class); |
| 27 | + $this->app->instance(DaemonCommandRepository::class, $this->repository); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Test that a validation error is returned if there is no command present in the |
| 32 | + * request. |
| 33 | + */ |
| 34 | + public function testValidationErrorIsReturnedIfNoCommandIsPresent() |
| 35 | + { |
| 36 | + [$user, $server] = $this->generateTestAccount(); |
| 37 | + |
| 38 | + $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/command", [ |
| 39 | + 'command' => '', |
| 40 | + ]); |
| 41 | + |
| 42 | + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY); |
| 43 | + $response->assertJsonPath('errors.0.code', 'required'); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test that a subuser without the required permission receives an error when trying to |
| 48 | + * execute the command. |
| 49 | + */ |
| 50 | + public function testSubuserWithoutPermissionReceivesError() |
| 51 | + { |
| 52 | + [$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]); |
| 53 | + |
| 54 | + $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/command", [ |
| 55 | + 'command' => 'say Test', |
| 56 | + ]); |
| 57 | + |
| 58 | + $response->assertStatus(Response::HTTP_FORBIDDEN); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Test that a command can be sent to the server. |
| 63 | + */ |
| 64 | + public function testCommandCanSendToServer() |
| 65 | + { |
| 66 | + [$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]); |
| 67 | + |
| 68 | + $this->repository->expects('setServer')->with(Mockery::on(function ($value) use ($server) { |
| 69 | + return $value->uuid === $server->uuid; |
| 70 | + }))->andReturnSelf(); |
| 71 | + $this->repository->expects('send')->with('say Test')->andReturn(new GuzzleResponse); |
| 72 | + |
| 73 | + $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/command", [ |
| 74 | + 'command' => 'say Test', |
| 75 | + ]); |
| 76 | + |
| 77 | + $response->assertStatus(Response::HTTP_NO_CONTENT); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test that an error is returned when the server is offline that is more specific than the |
| 82 | + * regular daemon connection error. |
| 83 | + */ |
| 84 | + public function testErrorIsReturnedWhenServerIsOffline() |
| 85 | + { |
| 86 | + [$user, $server] = $this->generateTestAccount(); |
| 87 | + |
| 88 | + $this->repository->expects('setServer->send')->andThrows( |
| 89 | + new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY)) |
| 90 | + ); |
| 91 | + |
| 92 | + $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/command", [ |
| 93 | + 'command' => 'say Test', |
| 94 | + ]); |
| 95 | + |
| 96 | + $response->assertStatus(Response::HTTP_BAD_GATEWAY); |
| 97 | + $response->assertJsonPath('errors.0.code', 'HttpException'); |
| 98 | + $response->assertJsonPath('errors.0.detail', 'Server must be online in order to send commands.'); |
| 99 | + } |
| 100 | +} |
0 commit comments