|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Api\Client\Server\ScheduleTask; |
| 4 | + |
| 5 | +use Pterodactyl\Models\Task; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use Pterodactyl\Models\Schedule; |
| 8 | +use Pterodactyl\Models\Permission; |
| 9 | +use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; |
| 10 | + |
| 11 | +class CreateServerScheduleTaskTest extends ClientApiIntegrationTestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Test that a task can be created. |
| 15 | + * |
| 16 | + * @param array $permissions |
| 17 | + * @dataProvider permissionsDataProvider |
| 18 | + */ |
| 19 | + public function testTaskCanBeCreated($permissions) |
| 20 | + { |
| 21 | + [$user, $server] = $this->generateTestAccount($permissions); |
| 22 | + |
| 23 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 24 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 25 | + $this->assertEmpty($schedule->tasks); |
| 26 | + |
| 27 | + $response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 28 | + 'action' => 'command', |
| 29 | + 'payload' => 'say Test', |
| 30 | + 'time_offset' => 10, |
| 31 | + 'sequence_id' => 1, |
| 32 | + ]); |
| 33 | + |
| 34 | + $response->assertOk(); |
| 35 | + /** @var \Pterodactyl\Models\Task $task */ |
| 36 | + $task = Task::query()->findOrFail($response->json('attributes.id')); |
| 37 | + |
| 38 | + $this->assertSame($schedule->id, $task->schedule_id); |
| 39 | + $this->assertSame(1, $task->sequence_id); |
| 40 | + $this->assertSame('command', $task->action); |
| 41 | + $this->assertSame('say Test', $task->payload); |
| 42 | + $this->assertSame(10, $task->time_offset); |
| 43 | + $this->assertJsonTransformedWith($response->json('attributes'), $task); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test that validation errors are returned correctly if bad data is passed into the API. |
| 48 | + */ |
| 49 | + public function testValidationErrorsAreReturned() |
| 50 | + { |
| 51 | + [$user, $server] = $this->generateTestAccount(); |
| 52 | + |
| 53 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 54 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 55 | + |
| 56 | + $response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'))->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY); |
| 57 | + |
| 58 | + foreach (['action', 'payload', 'time_offset'] as $i => $field) { |
| 59 | + $response->assertJsonPath("errors.{$i}.code", $field === 'payload' ? 'required_unless' : 'required'); |
| 60 | + $response->assertJsonPath("errors.{$i}.source.field", $field); |
| 61 | + } |
| 62 | + |
| 63 | + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 64 | + 'action' => 'hodor', |
| 65 | + 'payload' => 'say Test', |
| 66 | + 'time_offset' => 0, |
| 67 | + ]) |
| 68 | + ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY) |
| 69 | + ->assertJsonPath('errors.0.code', 'in') |
| 70 | + ->assertJsonPath('errors.0.source.field', 'action'); |
| 71 | + |
| 72 | + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 73 | + 'action' => 'command', |
| 74 | + 'time_offset' => 0, |
| 75 | + ]) |
| 76 | + ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY) |
| 77 | + ->assertJsonPath('errors.0.code', 'required_unless') |
| 78 | + ->assertJsonPath('errors.0.source.field', 'payload'); |
| 79 | + |
| 80 | + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 81 | + 'action' => 'command', |
| 82 | + 'payload' => 'say Test', |
| 83 | + 'time_offset' => 0, |
| 84 | + 'sequence_id' => 'hodor', |
| 85 | + ]) |
| 86 | + ->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY) |
| 87 | + ->assertJsonPath('errors.0.code', 'numeric') |
| 88 | + ->assertJsonPath('errors.0.source.field', 'sequence_id'); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test that backups can be tasked out correctly since they do not require a payload. |
| 93 | + */ |
| 94 | + public function testBackupsCanBeTaskedCorrectly() |
| 95 | + { |
| 96 | + [$user, $server] = $this->generateTestAccount(); |
| 97 | + |
| 98 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 99 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 100 | + |
| 101 | + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 102 | + 'action' => 'backup', |
| 103 | + 'time_offset' => 0, |
| 104 | + ])->assertOk(); |
| 105 | + |
| 106 | + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 107 | + 'action' => 'backup', |
| 108 | + 'payload' => "file.txt\nfile2.log", |
| 109 | + 'time_offset' => 0, |
| 110 | + ])->assertOk(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Test that an error is returned if the user attempts to create an additional task that |
| 115 | + * would put the schedule over the task limit. |
| 116 | + */ |
| 117 | + public function testErrorIsReturnedIfTooManyTasksExistForSchedule() |
| 118 | + { |
| 119 | + config()->set('pterodactyl.client_features.schedules.per_schedule_task_limit', 2); |
| 120 | + |
| 121 | + [$user, $server] = $this->generateTestAccount(); |
| 122 | + |
| 123 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 124 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 125 | + factory(Task::class)->times(2)->create(['schedule_id' => $schedule->id]); |
| 126 | + |
| 127 | + $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [ |
| 128 | + 'action' => 'command', |
| 129 | + 'payload' => 'say test', |
| 130 | + 'time_offset' => 0, |
| 131 | + ]) |
| 132 | + ->assertStatus(Response::HTTP_BAD_REQUEST) |
| 133 | + ->assertJsonPath('errors.0.code', 'ServiceLimitExceededException') |
| 134 | + ->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.'); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test that an error is returned if the targeted schedule does not belong to the server |
| 139 | + * in the request. |
| 140 | + */ |
| 141 | + public function testErrorIsReturnedIfScheduleDoesNotBelongToServer() |
| 142 | + { |
| 143 | + [$user, $server] = $this->generateTestAccount(); |
| 144 | + [, $server2] = $this->generateTestAccount(['user_id' => $user->id]); |
| 145 | + |
| 146 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 147 | + $schedule = factory(Schedule::class)->create(['server_id' => $server2->id]); |
| 148 | + |
| 149 | + $this->actingAs($user) |
| 150 | + ->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}/tasks") |
| 151 | + ->assertNotFound(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Test that an error is returned if the subuser making the request does not have permission |
| 156 | + * to update a schedule. |
| 157 | + */ |
| 158 | + public function testErrorIsReturnedIfSubuserDoesNotHaveScheduleUpdatePermissions() |
| 159 | + { |
| 160 | + [$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]); |
| 161 | + |
| 162 | + /** @var \Pterodactyl\Models\Schedule $schedule */ |
| 163 | + $schedule = factory(Schedule::class)->create(['server_id' => $server->id]); |
| 164 | + |
| 165 | + $this->actingAs($user) |
| 166 | + ->postJson($this->link($schedule, '/tasks')) |
| 167 | + ->assertForbidden(); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return array |
| 172 | + */ |
| 173 | + public function permissionsDataProvider(): array |
| 174 | + { |
| 175 | + return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]]; |
| 176 | + } |
| 177 | +} |
0 commit comments