2020-06-28 20:50:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Schedule;
|
|
|
|
use Pterodactyl\Helpers\Utilities;
|
|
|
|
use Pterodactyl\Models\Permission;
|
|
|
|
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
|
|
|
|
|
|
|
class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
|
|
|
|
{
|
2020-10-04 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* The data to use when updating a schedule.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
private array $updateData = [
|
2020-10-04 02:47:52 +00:00
|
|
|
'name' => 'Updated Schedule Name',
|
|
|
|
'minute' => '5',
|
|
|
|
'hour' => '*',
|
|
|
|
'day_of_week' => '*',
|
2021-01-17 03:07:39 +00:00
|
|
|
'month' => '*',
|
2020-10-04 02:47:52 +00:00
|
|
|
'day_of_month' => '*',
|
|
|
|
'is_active' => false,
|
|
|
|
];
|
|
|
|
|
2020-06-28 20:50:07 +00:00
|
|
|
/**
|
|
|
|
* Test that a schedule can be updated.
|
|
|
|
*
|
|
|
|
* @dataProvider permissionsDataProvider
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function testScheduleCanBeUpdated(array $permissions)
|
2020-06-28 20:50:07 +00:00
|
|
|
{
|
|
|
|
[$user, $server] = $this->generateTestAccount($permissions);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
2021-01-23 20:09:16 +00:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
2021-01-17 03:07:39 +00:00
|
|
|
$expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*', '*');
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)
|
2020-10-04 02:47:52 +00:00
|
|
|
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
$schedule = $schedule->refresh();
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$this->assertSame('Updated Schedule Name', $schedule->name);
|
|
|
|
$this->assertFalse($schedule->is_active);
|
|
|
|
$this->assertJsonTransformedWith($response->json('attributes'), $schedule);
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->assertSame($expected->toAtomString(), $schedule->next_run_at->toAtomString());
|
2020-06-28 20:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an error is returned if the schedule exists but does not belong to this
|
|
|
|
* specific server instance.
|
|
|
|
*/
|
|
|
|
public function testErrorIsReturnedIfScheduleDoesNotBelongToServer()
|
|
|
|
{
|
|
|
|
[$user, $server] = $this->generateTestAccount();
|
2022-05-29 21:52:14 +00:00
|
|
|
$server2 = $this->createServerModel(['owner_id' => $user->id]);
|
2020-06-28 20:50:07 +00:00
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server2->id]);
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
$this->actingAs($user)
|
|
|
|
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
|
|
|
|
->assertNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an error is returned if the subuser does not have permission to modify a
|
|
|
|
* server schedule.
|
|
|
|
*/
|
|
|
|
public function testErrorIsReturnedIfSubuserDoesNotHavePermissionToModifySchedule()
|
|
|
|
{
|
|
|
|
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
$this->actingAs($user)
|
|
|
|
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:47:52 +00:00
|
|
|
/**
|
|
|
|
* Test that the "is_processing" field gets reset to false when the schedule is enabled
|
|
|
|
* or disabled so that an invalid state can be more easily fixed.
|
|
|
|
*
|
|
|
|
* @see https://github.com/pterodactyl/panel/issues/2425
|
|
|
|
*/
|
|
|
|
public function testScheduleIsProcessingIsSetToFalseWhenActiveStateChanges()
|
|
|
|
{
|
|
|
|
[$user, $server] = $this->generateTestAccount();
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
2021-01-23 20:09:16 +00:00
|
|
|
$schedule = Schedule::factory()->create([
|
2020-10-04 02:47:52 +00:00
|
|
|
'server_id' => $server->id,
|
|
|
|
'is_active' => true,
|
|
|
|
'is_processing' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($schedule->is_active);
|
|
|
|
$this->assertTrue($schedule->is_processing);
|
|
|
|
|
|
|
|
$response = $this->actingAs($user)
|
|
|
|
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
|
|
|
|
|
|
|
|
$schedule = $schedule->refresh();
|
|
|
|
|
|
|
|
$response->assertOk();
|
|
|
|
$this->assertFalse($schedule->is_active);
|
|
|
|
$this->assertFalse($schedule->is_processing);
|
|
|
|
}
|
|
|
|
|
2020-06-28 20:50:07 +00:00
|
|
|
public function permissionsDataProvider(): array
|
|
|
|
{
|
|
|
|
return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];
|
|
|
|
}
|
|
|
|
}
|