Files
panel-pelican-dev/tests/Integration/Api/Client/Server/Schedule/UpdateServerScheduleTest.php

115 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2020-06-28 13:50:07 -07:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Api\Client\Server\Schedule;
2020-06-28 13:50:07 -07:00
use App\Enums\SubuserPermission;
2024-03-12 22:39:16 -04:00
use App\Helpers\Utilities;
2025-09-24 13:34:19 +02:00
use App\Models\Schedule;
2024-03-12 22:39:16 -04:00
use App\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
2020-06-28 13:50:07 -07:00
class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
{
/**
* The data to use when updating a schedule.
*/
private array $updateData = [
'name' => 'Updated Schedule Name',
'minute' => '5',
'hour' => '*',
'day_of_week' => '*',
'month' => '*',
'day_of_month' => '*',
'is_active' => false,
];
2020-06-28 13:50:07 -07:00
/**
* Test that a schedule can be updated.
*/
#[DataProvider('permissionsDataProvider')]
public function test_schedule_can_be_updated(array $permissions): void
2020-06-28 13:50:07 -07:00
{
[$user, $server] = $this->generateTestAccount($permissions);
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
$expected = Utilities::getScheduleNextRunDate('5', '*', '*', '*', '*');
2020-06-28 13:50:07 -07:00
$response = $this->actingAs($user)
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $this->updateData);
2020-06-28 13:50:07 -07:00
$schedule = $schedule->refresh();
$response->assertOk();
$this->assertSame('Updated Schedule Name', $schedule->name);
$this->assertFalse($schedule->is_active);
$this->assertJsonTransformedWith($response->json('attributes'), $schedule);
$this->assertSame($expected->toAtomString(), $schedule->next_run_at->toAtomString());
2020-06-28 13:50:07 -07:00
}
/**
* Test that an error is returned if the schedule exists but does not belong to this
* specific server instance.
*/
public function test_error_is_returned_if_schedule_does_not_belong_to_server(): void
2020-06-28 13:50:07 -07:00
{
[$user, $server] = $this->generateTestAccount();
$server2 = $this->createServerModel(['owner_id' => $user->id]);
2020-06-28 13:50:07 -07:00
$schedule = Schedule::factory()->create(['server_id' => $server2->id]);
2020-06-28 13:50:07 -07: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 test_error_is_returned_if_subuser_does_not_have_permission_to_modify_schedule(): void
2020-06-28 13:50:07 -07:00
{
[$user, $server] = $this->generateTestAccount([SubuserPermission::ScheduleCreate]);
2020-06-28 13:50:07 -07:00
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
2020-06-28 13:50:07 -07:00
$this->actingAs($user)
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
->assertForbidden();
}
/**
* 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.
*/
public function test_schedule_is_processing_is_set_to_false_when_active_state_changes(): void
{
[$user, $server] = $this->generateTestAccount();
/** @var Schedule $schedule */
$schedule = Schedule::factory()->create([
'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);
}
2023-02-23 12:30:16 -07:00
public static function permissionsDataProvider(): array
2020-06-28 13:50:07 -07:00
{
return [[[]], [[SubuserPermission::ScheduleUpdate]]];
2020-06-28 13:50:07 -07:00
}
}