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/UpdateServerScheduleTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
4
5
use Pterodactyl\Models\Schedule;
6
use Pterodactyl\Helpers\Utilities;
7
use Pterodactyl\Models\Permission;
8
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
9
10
class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* The data to use when updating a schedule.
14
*/
15
private array $updateData = [
16
'name' => 'Updated Schedule Name',
17
'minute' => '5',
18
'hour' => '*',
19
'day_of_week' => '*',
20
'month' => '*',
21
'day_of_month' => '*',
22
'is_active' => false,
23
];
24
25
/**
26
* Test that a schedule can be updated.
27
*/
28
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
29
public function testScheduleCanBeUpdated(array $permissions)
30
{
31
[$user, $server] = $this->generateTestAccount($permissions);
32
33
/** @var Schedule $schedule */
34
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
35
$expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*', '*');
36
37
$response = $this->actingAs($user)
38
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
39
40
$schedule = $schedule->refresh();
41
42
$response->assertOk();
43
$this->assertSame('Updated Schedule Name', $schedule->name);
44
$this->assertFalse($schedule->is_active);
45
$this->assertJsonTransformedWith($response->json('attributes'), $schedule);
46
47
$this->assertSame($expected->toAtomString(), $schedule->next_run_at->toAtomString());
48
}
49
50
/**
51
* Test that an error is returned if the schedule exists but does not belong to this
52
* specific server instance.
53
*/
54
public function testErrorIsReturnedIfScheduleDoesNotBelongToServer()
55
{
56
[$user, $server] = $this->generateTestAccount();
57
$server2 = $this->createServerModel(['owner_id' => $user->id]);
58
59
$schedule = Schedule::factory()->create(['server_id' => $server2->id]);
60
61
$this->actingAs($user)
62
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
63
->assertNotFound();
64
}
65
66
/**
67
* Test that an error is returned if the subuser does not have permission to modify a
68
* server schedule.
69
*/
70
public function testErrorIsReturnedIfSubuserDoesNotHavePermissionToModifySchedule()
71
{
72
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);
73
74
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
75
76
$this->actingAs($user)
77
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
78
->assertForbidden();
79
}
80
81
/**
82
* Test that the "is_processing" field gets reset to false when the schedule is enabled
83
* or disabled so that an invalid state can be more easily fixed.
84
*
85
* @see https://github.com/pterodactyl/panel/issues/2425
86
*/
87
public function testScheduleIsProcessingIsSetToFalseWhenActiveStateChanges()
88
{
89
[$user, $server] = $this->generateTestAccount();
90
91
/** @var Schedule $schedule */
92
$schedule = Schedule::factory()->create([
93
'server_id' => $server->id,
94
'is_active' => true,
95
'is_processing' => true,
96
]);
97
98
$this->assertTrue($schedule->is_active);
99
$this->assertTrue($schedule->is_processing);
100
101
$response = $this->actingAs($user)
102
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
103
104
$schedule = $schedule->refresh();
105
106
$response->assertOk();
107
$this->assertFalse($schedule->is_active);
108
$this->assertFalse($schedule->is_processing);
109
}
110
111
public static function permissionsDataProvider(): array
112
{
113
return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];
114
}
115
}
116
117