Files

135 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2020-10-14 21:06:27 -07:00
<?php
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
use Pterodactyl\Models\Task;
use Illuminate\Http\Response;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Permission;
use Illuminate\Support\Facades\Bus;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class ExecuteScheduleTest extends ClientApiIntegrationTestCase
{
/**
* Test that a schedule can be executed and is updated in the database correctly.
*/
2024-10-21 19:18:20 -06:00
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
2020-10-14 21:06:27 -07:00
public function testScheduleIsExecutedRightAway(array $permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
Bus::fake();
2024-10-21 19:18:20 -06:00
/** @var Schedule $schedule */
2021-01-23 12:09:16 -08:00
$schedule = Schedule::factory()->create([
2020-10-14 21:06:27 -07:00
'server_id' => $server->id,
]);
$response = $this->actingAs($user)->postJson($this->link($schedule, '/execute'));
$response->assertStatus(Response::HTTP_BAD_REQUEST);
$response->assertJsonPath('errors.0.code', 'DisplayException');
$response->assertJsonPath('errors.0.detail', 'Cannot process schedule for task execution: no tasks are registered.');
2024-10-21 19:18:20 -06:00
/** @var Task $task */
2021-01-23 12:09:16 -08:00
$task = Task::factory()->create([
2020-10-14 21:06:27 -07:00
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'time_offset' => 2,
]);
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED);
Bus::assertDispatched(function (RunTaskJob $job) use ($task) {
// A task executed right now should not have any job delay associated with it.
$this->assertNull($job->delay);
2020-10-14 21:06:27 -07:00
$this->assertSame($task->id, $job->task->id);
return true;
});
}
/**
* Test that a user without the schedule update permission cannot execute it.
*/
public function testUserWithoutScheduleUpdatePermissionCannotExecute()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);
2024-10-21 19:18:20 -06:00
/** @var Schedule $schedule */
2021-01-23 12:09:16 -08:00
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
2020-10-14 21:06:27 -07:00
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden();
}
/**
* Test that a subuser can execute a schedule containing tasks they could not create.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('taskActionDataProvider')]
public function testSubuserCanExecuteScheduleWithoutTaskActionPermission(string $action, string $payload)
2026-06-28 17:51:28 -04:00
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]);
Bus::fake();
2026-06-28 17:51:28 -04:00
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var Task $task */
$task = Task::factory()->create([
2026-06-28 17:51:28 -04:00
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'action' => $action,
'payload' => $payload,
2026-06-28 17:51:28 -04:00
]);
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED);
Bus::assertDispatched(fn (RunTaskJob $job) => $job->task->id === $task->id);
}
/**
* Test that a task payload predating the current validation rules does not lock the owner
* out of their own schedule.
*/
public function testOwnerCanExecuteScheduleWithUnmappableTaskPayload()
{
[$user, $server] = $this->generateTestAccount();
Bus::fake();
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var Task $task */
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'sequence_id' => 1,
'action' => 'power',
'payload' => 'reboot',
]);
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED);
Bus::assertDispatched(fn (RunTaskJob $job) => $job->task->id === $task->id);
2026-06-28 17:51:28 -04:00
}
2023-02-23 12:30:16 -07:00
public static function permissionsDataProvider(): array
2020-10-14 21:06:27 -07:00
{
return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];
}
public static function taskActionDataProvider(): array
{
return [
['command', 'say Test'],
['power', 'start'],
['power', 'stop'],
['power', 'restart'],
['power', 'kill'],
['backup', ''],
];
2020-10-14 21:06:27 -07:00
}
}