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/CreateServerScheduleTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Schedule;
7
use Pterodactyl\Models\Permission;
8
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
9
10
class CreateServerScheduleTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* Test that a schedule can be created for the server.
14
*/
15
#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]
16
public function testScheduleCanBeCreatedForServer(array $permissions)
17
{
18
[$user, $server] = $this->generateTestAccount($permissions);
19
20
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/schedules", [
21
'name' => 'Test Schedule',
22
'is_active' => false,
23
'minute' => '0',
24
'hour' => '*/2',
25
'day_of_week' => '2',
26
'month' => '1',
27
'day_of_month' => '*',
28
]);
29
30
$response->assertOk();
31
32
$this->assertNotNull($id = $response->json('attributes.id'));
33
34
/** @var Schedule $schedule */
35
$schedule = Schedule::query()->findOrFail($id);
36
$this->assertFalse($schedule->is_active);
37
$this->assertFalse($schedule->is_processing);
38
$this->assertSame('0', $schedule->cron_minute);
39
$this->assertSame('*/2', $schedule->cron_hour);
40
$this->assertSame('2', $schedule->cron_day_of_week);
41
$this->assertSame('1', $schedule->cron_month);
42
$this->assertSame('*', $schedule->cron_day_of_month);
43
$this->assertSame('Test Schedule', $schedule->name);
44
45
$this->assertJsonTransformedWith($response->json('attributes'), $schedule);
46
$response->assertJsonCount(0, 'attributes.relationships.tasks.data');
47
}
48
49
/**
50
* Test that the validation rules for scheduling work as expected.
51
*/
52
public function testScheduleValidationRules()
53
{
54
[$user, $server] = $this->generateTestAccount();
55
56
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/schedules", []);
57
58
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
59
foreach (['name', 'minute', 'hour', 'day_of_month', 'day_of_week'] as $i => $field) {
60
$response->assertJsonPath("errors.$i.code", 'ValidationException');
61
$response->assertJsonPath("errors.$i.meta.rule", 'required');
62
$response->assertJsonPath("errors.$i.meta.source_field", $field);
63
}
64
65
$this->actingAs($user)
66
->postJson("/api/client/servers/$server->uuid/schedules", [
67
'name' => 'Testing',
68
'is_active' => 'no',
69
'minute' => '*',
70
'hour' => '*',
71
'day_of_month' => '*',
72
'month' => '*',
73
'day_of_week' => '*',
74
])
75
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
76
->assertJsonPath('errors.0.meta.rule', 'boolean');
77
}
78
79
/**
80
* Test that a subuser without required permissions cannot create a schedule.
81
*/
82
public function testSubuserCannotCreateScheduleWithoutPermissions()
83
{
84
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]);
85
86
$this->actingAs($user)
87
->postJson("/api/client/servers/$server->uuid/schedules", [])
88
->assertForbidden();
89
}
90
91
public static function permissionsDataProvider(): array
92
{
93
return [[[]], [[Permission::ACTION_SCHEDULE_CREATE]]];
94
}
95
}
96
97