|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule; |
| 4 | + |
| 5 | +use Pterodactyl\Models\Task; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use Pterodactyl\Models\Schedule; |
| 8 | +use Pterodactyl\Models\Permission; |
| 9 | +use Illuminate\Support\Facades\Bus; |
| 10 | +use Pterodactyl\Jobs\Schedule\RunTaskJob; |
| 11 | +use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; |
| 12 | + |
| 13 | +class ExecuteScheduleTest extends ClientApiIntegrationTestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Test that a schedule can be executed and is updated in the database correctly. |
| 17 | + * |
| 18 | + * @param array $permissions |
| 19 | + * @dataProvider permissionsDataProvider |
| 20 | + */ |
| 21 | + public function testScheduleIsExecutedRightAway(array $permissions) |
| 22 | + { |
| 23 | + [$user, $server] = $this->generateTestAccount($permissions); |
| 24 | + |
| 25 | + Bus::fake(); |
| 26 | + |
| 27 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 28 | + $schedule = factory(Schedule::class)->create([ |
| 29 | + 'server_id' => $server->id, |
| 30 | + ]); |
| 31 | + |
| 32 | + $response = $this->actingAs($user)->postJson($this->link($schedule, '/execute')); |
| 33 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 34 | + $response->assertJsonPath('errors.0.code', 'DisplayException'); |
| 35 | + $response->assertJsonPath('errors.0.detail', 'Cannot process schedule for task execution: no tasks are registered.'); |
| 36 | + |
| 37 | + /** @var \Pterodactyl\Models\Task $task */ |
| 38 | + $task = factory(Task::class)->create([ |
| 39 | + 'schedule_id' => $schedule->id, |
| 40 | + 'sequence_id' => 1, |
| 41 | + 'time_offset' => 2, |
| 42 | + ]); |
| 43 | + |
| 44 | + $this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED); |
| 45 | + |
| 46 | + Bus::assertDispatched(function (RunTaskJob $job) use ($task) { |
| 47 | + $this->assertSame($task->time_offset, $job->delay); |
| 48 | + $this->assertSame($task->id, $job->task->id); |
| 49 | + |
| 50 | + return true; |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test that the schedule is not executed if it is not currently active. |
| 56 | + */ |
| 57 | + public function testScheduleIsNotExecutedIfNotActive() |
| 58 | + { |
| 59 | + [$user, $server] = $this->generateTestAccount(); |
| 60 | + |
| 61 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 62 | + $schedule = factory(Schedule::class)->create([ |
| 63 | + 'server_id' => $server->id, |
| 64 | + 'is_active' => false, |
| 65 | + ]); |
| 66 | + |
| 67 | + $response = $this->actingAs($user)->postJson($this->link($schedule, "/execute")); |
| 68 | + |
| 69 | + $response->assertStatus(Response::HTTP_BAD_REQUEST); |
| 70 | + $response->assertJsonPath('errors.0.code', 'BadRequestHttpException'); |
| 71 | + $response->assertJsonPath('errors.0.detail', 'Cannot trigger schedule exection for a schedule that is not currently active.'); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test that a user without the schedule update permission cannot execute it. |
| 76 | + */ |
| 77 | + public function testUserWithoutScheduleUpdatePermissionCannotExecute() |
| 78 | + { |
| 79 | + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]); |
| 80 | + |
| 81 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 82 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 83 | + |
| 84 | + $this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return array |
| 89 | + */ |
| 90 | + public function permissionsDataProvider(): array |
| 91 | + { |
| 92 | + return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]]; |
| 93 | + } |
| 94 | +} |
0 commit comments