Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Schedule/ExecuteScheduleTest.php
7461 views
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
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
19
public function testScheduleIsExecutedRightAway(array $permissions)
20
{
21
[$user, $server] = $this->generateTestAccount($permissions);
22
23
Bus::fake();
24
25
/** @var Schedule $schedule */
26
$schedule = Schedule::factory()->create([
27
'server_id' => $server->id,
28
]);
29
30
$response = $this->actingAs($user)->postJson($this->link($schedule, '/execute'));
31
$response->assertStatus(Response::HTTP_BAD_REQUEST);
32
$response->assertJsonPath('errors.0.code', 'DisplayException');
33
$response->assertJsonPath('errors.0.detail', 'Cannot process schedule for task execution: no tasks are registered.');
34
35
/** @var Task $task */
36
$task = Task::factory()->create([
37
'schedule_id' => $schedule->id,
38
'sequence_id' => 1,
39
'time_offset' => 2,
40
]);
41
42
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED);
43
44
Bus::assertDispatched(function (RunTaskJob $job) use ($task) {
45
// A task executed right now should not have any job delay associated with it.
46
$this->assertNull($job->delay);
47
$this->assertSame($task->id, $job->task->id);
48
49
return true;
50
});
51
}
52
53
/**
54
* Test that a user without the schedule update permission cannot execute it.
55
*/
56
public function testUserWithoutScheduleUpdatePermissionCannotExecute()
57
{
58
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);
59
60
/** @var Schedule $schedule */
61
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
62
63
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertForbidden();
64
}
65
66
public static function permissionsDataProvider(): array
67
{
68
return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];
69
}
70
}
71
72