Path: blob/1.0-develop/tests/Integration/Api/Client/Server/ScheduleTask/CreateServerScheduleTaskTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\ScheduleTask;34use Pterodactyl\Models\Task;5use Illuminate\Http\Response;6use Pterodactyl\Models\Schedule;7use Pterodactyl\Models\Permission;8use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;910class CreateServerScheduleTaskTest extends ClientApiIntegrationTestCase11{12/**13* Test that a task can be created.14*/15#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]16public function testTaskCanBeCreated(array $permissions)17{18[$user, $server] = $this->generateTestAccount($permissions);1920/** @var Schedule $schedule */21$schedule = Schedule::factory()->create(['server_id' => $server->id]);22$this->assertEmpty($schedule->tasks);2324$response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [25'action' => 'command',26'payload' => 'say Test',27'time_offset' => 10,28'sequence_id' => 1,29]);3031$response->assertOk();32/** @var Task $task */33$task = Task::query()->findOrFail($response->json('attributes.id'));3435$this->assertSame($schedule->id, $task->schedule_id);36$this->assertSame(1, $task->sequence_id);37$this->assertSame('command', $task->action);38$this->assertSame('say Test', $task->payload);39$this->assertSame(10, $task->time_offset);40$this->assertJsonTransformedWith($response->json('attributes'), $task);41}4243/**44* Test that validation errors are returned correctly if bad data is passed into the API.45*/46public function testValidationErrorsAreReturned()47{48[$user, $server] = $this->generateTestAccount();4950/** @var Schedule $schedule */51$schedule = Schedule::factory()->create(['server_id' => $server->id]);5253$response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'))->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);5455foreach (['action', 'payload', 'time_offset'] as $i => $field) {56$response->assertJsonPath("errors.$i.meta.rule", $field === 'payload' ? 'required_unless' : 'required');57$response->assertJsonPath("errors.$i.meta.source_field", $field);58}5960$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [61'action' => 'hodor',62'payload' => 'say Test',63'time_offset' => 0,64])65->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)66->assertJsonPath('errors.0.meta.rule', 'in')67->assertJsonPath('errors.0.meta.source_field', 'action');6869$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [70'action' => 'command',71'time_offset' => 0,72])73->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)74->assertJsonPath('errors.0.meta.rule', 'required_unless')75->assertJsonPath('errors.0.meta.source_field', 'payload');7677$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [78'action' => 'command',79'payload' => 'say Test',80'time_offset' => 0,81'sequence_id' => 'hodor',82])83->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)84->assertJsonPath('errors.0.meta.rule', 'numeric')85->assertJsonPath('errors.0.meta.source_field', 'sequence_id');86}8788/**89* Test that backups can not be tasked when the backup limit is 0.90*/91public function testBackupsCanNotBeTaskedIfLimit0()92{93[$user, $server] = $this->generateTestAccount();9495/** @var Schedule $schedule */96$schedule = Schedule::factory()->create(['server_id' => $server->id]);9798$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [99'action' => 'backup',100'time_offset' => 0,101])102->assertStatus(Response::HTTP_FORBIDDEN)103->assertJsonPath('errors.0.detail', 'A backup task cannot be created when the server\'s backup limit is set to 0.');104105$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [106'action' => 'backup',107'payload' => "file.txt\nfile2.log",108'time_offset' => 0,109])110->assertStatus(Response::HTTP_FORBIDDEN)111->assertJsonPath('errors.0.detail', 'A backup task cannot be created when the server\'s backup limit is set to 0.');112}113114/**115* Test that an error is returned if the user attempts to create an additional task that116* would put the schedule over the task limit.117*/118public function testErrorIsReturnedIfTooManyTasksExistForSchedule()119{120config()->set('pterodactyl.client_features.schedules.per_schedule_task_limit', 2);121122[$user, $server] = $this->generateTestAccount();123124/** @var Schedule $schedule */125$schedule = Schedule::factory()->create(['server_id' => $server->id]);126Task::factory()->times(2)->create(['schedule_id' => $schedule->id]);127128$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [129'action' => 'command',130'payload' => 'say test',131'time_offset' => 0,132])133->assertStatus(Response::HTTP_BAD_REQUEST)134->assertJsonPath('errors.0.code', 'ServiceLimitExceededException')135->assertJsonPath('errors.0.detail', 'Schedules may not have more than 2 tasks associated with them. Creating this task would put this schedule over the limit.');136}137138/**139* Test that an error is returned if the targeted schedule does not belong to the server140* in the request.141*/142public function testErrorIsReturnedIfScheduleDoesNotBelongToServer()143{144[$user, $server] = $this->generateTestAccount();145$server2 = $this->createServerModel(['owner_id' => $user->id]);146147/** @var Schedule $schedule */148$schedule = Schedule::factory()->create(['server_id' => $server2->id]);149150$this->actingAs($user)151->postJson("/api/client/servers/$server->uuid/schedules/$schedule->id/tasks")152->assertNotFound();153}154155/**156* Test that an error is returned if the subuser making the request does not have permission157* to update a schedule.158*/159public function testErrorIsReturnedIfSubuserDoesNotHaveScheduleUpdatePermissions()160{161[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);162163/** @var Schedule $schedule */164$schedule = Schedule::factory()->create(['server_id' => $server->id]);165166$this->actingAs($user)167->postJson($this->link($schedule, '/tasks'))168->assertForbidden();169}170171public static function permissionsDataProvider(): array172{173return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];174}175}176177178