Path: blob/1.0-develop/tests/Integration/Api/Client/Server/PowerControllerTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server;34use Illuminate\Http\Response;5use Pterodactyl\Models\Permission;6use Pterodactyl\Repositories\Wings\DaemonPowerRepository;7use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;89class PowerControllerTest extends ClientApiIntegrationTestCase10{11/**12* Test that a subuser without permission to send a command to the server receives13* an error in response. This checks against the specific permission needed to send14* the command to the server.15*16* @param string[] $permissions17*/18#[\PHPUnit\Framework\Attributes\DataProvider('invalidPermissionDataProvider')]19public function testSubuserWithoutPermissionsReceivesError(string $action, array $permissions)20{21[$user, $server] = $this->generateTestAccount($permissions);2223$this->actingAs($user)24->postJson("/api/client/servers/$server->uuid/power", ['signal' => $action])25->assertStatus(Response::HTTP_FORBIDDEN);26}2728/**29* Test that sending an invalid power signal returns an error.30*/31public function testInvalidPowerSignalResultsInError()32{33[$user, $server] = $this->generateTestAccount();3435$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/power", [36'signal' => 'invalid',37]);3839$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);40$response->assertJsonPath('errors.0.meta.rule', 'in');41$response->assertJsonPath('errors.0.detail', 'The selected signal is invalid.');42}4344/**45* Test that sending a valid power actions works.46*/47#[\PHPUnit\Framework\Attributes\DataProvider('validPowerActionDataProvider')]48public function testActionCanBeSentToServer(string $action, string $permission)49{50$service = \Mockery::mock(DaemonPowerRepository::class);51$this->app->instance(DaemonPowerRepository::class, $service);5253[$user, $server] = $this->generateTestAccount([$permission]);5455$service->expects('setServer')56->with(\Mockery::on(function ($value) use ($server) {57return $server->uuid === $value->uuid;58}))59->andReturnSelf()60->getMock()61->expects('send')62->with(trim($action));6364$this->actingAs($user)65->postJson("/api/client/servers/$server->uuid/power", ['signal' => $action])66->assertStatus(Response::HTTP_NO_CONTENT);67}6869/**70* Returns invalid permission combinations for a given power action.71*/72public static function invalidPermissionDataProvider(): array73{74return [75['start', [Permission::ACTION_CONTROL_STOP, Permission::ACTION_CONTROL_RESTART]],76['stop', [Permission::ACTION_CONTROL_START]],77['kill', [Permission::ACTION_CONTROL_START, Permission::ACTION_CONTROL_RESTART]],78['restart', [Permission::ACTION_CONTROL_STOP, Permission::ACTION_CONTROL_START]],79['random', [Permission::ACTION_CONTROL_START]],80];81}8283public static function validPowerActionDataProvider(): array84{85return [86['start', Permission::ACTION_CONTROL_START],87['stop', Permission::ACTION_CONTROL_STOP],88['restart', Permission::ACTION_CONTROL_RESTART],89['kill', Permission::ACTION_CONTROL_STOP],90// Yes, these spaces are intentional. You should be able to send values with or without91// a space on the start/end since we should be trimming the values.92[' restart', Permission::ACTION_CONTROL_RESTART],93['kill ', Permission::ACTION_CONTROL_STOP],94];95}96}979899