Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Schedule/GetServerSchedulesTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;34use Pterodactyl\Models\Task;5use Pterodactyl\Models\Schedule;6use Pterodactyl\Models\Permission;7use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;89class GetServerSchedulesTest extends ClientApiIntegrationTestCase10{11/**12* Cleanup after tests run.13*/14protected function tearDown(): void15{16Task::query()->forceDelete();17Schedule::query()->forceDelete();1819parent::tearDown();20}2122/**23* Test that schedules for a server are returned.24*/25#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]26public function testServerSchedulesAreReturned(array $permissions, bool $individual)27{28[$user, $server] = $this->generateTestAccount($permissions);2930/** @var Schedule $schedule */31$schedule = Schedule::factory()->create(['server_id' => $server->id]);32/** @var Task $task */33$task = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 1, 'time_offset' => 0]);3435$response = $this->actingAs($user)36->getJson(37$individual38? "/api/client/servers/$server->uuid/schedules/$schedule->id"39: "/api/client/servers/$server->uuid/schedules"40)41->assertOk();4243$prefix = $individual ? '' : 'data.0.';44if (!$individual) {45$response->assertJsonCount(1, 'data');46}4748$response->assertJsonCount(1, $prefix . 'attributes.relationships.tasks.data');4950$response->assertJsonPath($prefix . 'object', Schedule::RESOURCE_NAME);51$response->assertJsonPath($prefix . 'attributes.relationships.tasks.data.0.object', Task::RESOURCE_NAME);5253$this->assertJsonTransformedWith($response->json($prefix . 'attributes'), $schedule);54$this->assertJsonTransformedWith($response->json($prefix . 'attributes.relationships.tasks.data.0.attributes'), $task);55}5657/**58* Test that a schedule belonging to another server cannot be viewed.59*/60public function testScheduleBelongingToAnotherServerCannotBeViewed()61{62[$user, $server] = $this->generateTestAccount();63$server2 = $this->createServerModel(['owner_id' => $user->id]);6465$schedule = Schedule::factory()->create(['server_id' => $server2->id]);6667$this->actingAs($user)68->getJson("/api/client/servers/$server->uuid/schedules/$schedule->id")69->assertNotFound();70}7172/**73* Test that a subuser without the required permissions is unable to access the schedules endpoint.74*/75public function testUserWithoutPermissionCannotViewSchedules()76{77[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);7879$this->actingAs($user)80->getJson("/api/client/servers/$server->uuid/schedules")81->assertForbidden();8283$schedule = Schedule::factory()->create(['server_id' => $server->id]);8485$this->actingAs($user)86->getJson("/api/client/servers/$server->uuid/schedules/$schedule->id")87->assertForbidden();88}8990public static function permissionsDataProvider(): array91{92return [93[[], false],94[[], true],95[[Permission::ACTION_SCHEDULE_READ], false],96[[Permission::ACTION_SCHEDULE_READ], true],97];98}99}100101102