From e7c64bc60e5e0e8e3ef2ecdc84ffb97b674f3226 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 14 Oct 2020 21:06:27 -0700 Subject: [PATCH] Add test coverage for schedule execution --- .../Schedules/TriggerScheduleRequest.php | 6 +- .../Schedules/ProcessScheduleService.php | 9 +- .../schedules/ScheduleEditContainer.tsx | 2 +- .../Server/Schedule/ExecuteScheduleTest.php | 94 +++++++++++++++++++ 4 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php diff --git a/app/Http/Requests/Api/Client/Servers/Schedules/TriggerScheduleRequest.php b/app/Http/Requests/Api/Client/Servers/Schedules/TriggerScheduleRequest.php index 7651b7419..d89f5ed30 100644 --- a/app/Http/Requests/Api/Client/Servers/Schedules/TriggerScheduleRequest.php +++ b/app/Http/Requests/Api/Client/Servers/Schedules/TriggerScheduleRequest.php @@ -3,9 +3,9 @@ namespace Pterodactyl\Http\Requests\Api\Client\Servers\Schedules; use Pterodactyl\Models\Permission; -use Illuminate\Foundation\Http\FormRequest; +use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest; -class TriggerScheduleRequest extends FormRequest +class TriggerScheduleRequest extends ClientApiRequest { /** * @return string @@ -18,7 +18,7 @@ class TriggerScheduleRequest extends FormRequest /** * @return array */ - public function rules() + public function rules(): array { return []; } diff --git a/app/Services/Schedules/ProcessScheduleService.php b/app/Services/Schedules/ProcessScheduleService.php index 5d4ad60cf..1f810d6f5 100644 --- a/app/Services/Schedules/ProcessScheduleService.php +++ b/app/Services/Schedules/ProcessScheduleService.php @@ -6,6 +6,7 @@ use Pterodactyl\Models\Schedule; use Illuminate\Contracts\Bus\Dispatcher; use Pterodactyl\Jobs\Schedule\RunTaskJob; use Illuminate\Database\ConnectionInterface; +use Pterodactyl\Exceptions\DisplayException; class ProcessScheduleService { @@ -42,7 +43,13 @@ class ProcessScheduleService public function handle(Schedule $schedule, bool $now = false) { /** @var \Pterodactyl\Models\Task $task */ - $task = $schedule->tasks()->where('sequence_id', 1)->firstOrFail(); + $task = $schedule->tasks()->where('sequence_id', 1)->first(); + + if (is_null($task)) { + throw new DisplayException( + 'Cannot process schedule for task execution: no tasks are registered.' + ); + } $this->connection->transaction(function () use ($schedule, $task) { $schedule->forceFill([ diff --git a/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx b/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx index 5b2265845..d7c5f2abf 100644 --- a/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx +++ b/resources/scripts/components/server/schedules/ScheduleEditContainer.tsx @@ -146,7 +146,7 @@ export default ({ match, history, location: { state } }: RouteComponentProps history.push(`/server/${id}/schedules`)} /> - {schedule.isActive && + {schedule.isActive && schedule.tasks.length > 0 && diff --git a/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php b/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php new file mode 100644 index 000000000..00c57d4a4 --- /dev/null +++ b/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php @@ -0,0 +1,94 @@ +generateTestAccount($permissions); + + Bus::fake(); + + /** @var \Pterodactyl\Models\Schedule $schedule */ + $schedule = factory(Schedule::class)->create([ + '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.'); + + /** @var \Pterodactyl\Models\Task $task */ + $task = factory(Task::class)->create([ + '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) { + $this->assertSame($task->time_offset, $job->delay); + $this->assertSame($task->id, $job->task->id); + + return true; + }); + } + + /** + * Test that the schedule is not executed if it is not currently active. + */ + public function testScheduleIsNotExecutedIfNotActive() + { + [$user, $server] = $this->generateTestAccount(); + + /** @var \Pterodactyl\Models\Schedule $schedule */ + $schedule = factory(Schedule::class)->create([ + 'server_id' => $server->id, + 'is_active' => false, + ]); + + $response = $this->actingAs($user)->postJson($this->link($schedule, "/execute")); + + $response->assertStatus(Response::HTTP_BAD_REQUEST); + $response->assertJsonPath('errors.0.code', 'BadRequestHttpException'); + $response->assertJsonPath('errors.0.detail', 'Cannot trigger schedule exection for a schedule that is not currently active.'); + } + + /** + * Test that a user without the schedule update permission cannot execute it. + */ + public function testUserWithoutScheduleUpdatePermissionCannotExecute() + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]); + + /** @var \Pterodactyl\Models\Schedule $schedule */ + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); + + $this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden(); + } + + /** + * @return array + */ + public function permissionsDataProvider(): array + { + return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]]; + } +}