2020-06-28 20:50:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Task;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
|
|
|
use Pterodactyl\Models\Permission;
|
|
|
|
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
|
|
|
|
|
|
|
class GetServerSchedulesTest extends ClientApiIntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cleanup after tests run.
|
|
|
|
*/
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
Task::query()->forceDelete();
|
|
|
|
Schedule::query()->forceDelete();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that schedules for a server are returned.
|
|
|
|
*
|
|
|
|
* @param array $permissions
|
2021-01-28 04:52:11 +00:00
|
|
|
* @param bool $individual
|
2020-06-28 20:50:07 +00:00
|
|
|
* @dataProvider permissionsDataProvider
|
|
|
|
*/
|
|
|
|
public function testServerSchedulesAreReturned($permissions, $individual)
|
|
|
|
{
|
|
|
|
[$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]);
|
2020-06-28 20:50:07 +00:00
|
|
|
/** @var \Pterodactyl\Models\Task $task */
|
2021-01-23 20:09:16 +00:00
|
|
|
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'sequence_id' => 1, 'time_offset' => 0]);
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
$response = $this->actingAs($user)
|
|
|
|
->getJson(
|
|
|
|
$individual
|
|
|
|
? "/api/client/servers/{$server->uuid}/schedules/{$schedule->id}"
|
|
|
|
: "/api/client/servers/{$server->uuid}/schedules"
|
|
|
|
)
|
|
|
|
->assertOk();
|
|
|
|
|
|
|
|
$prefix = $individual ? '' : 'data.0.';
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$individual) {
|
2020-06-28 20:50:07 +00:00
|
|
|
$response->assertJsonCount(1, 'data');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->assertJsonCount(1, $prefix . 'attributes.relationships.tasks.data');
|
|
|
|
|
|
|
|
$response->assertJsonPath($prefix . 'object', Schedule::RESOURCE_NAME);
|
|
|
|
$response->assertJsonPath($prefix . 'attributes.relationships.tasks.data.0.object', Task::RESOURCE_NAME);
|
|
|
|
|
|
|
|
$this->assertJsonTransformedWith($response->json($prefix . 'attributes'), $schedule);
|
|
|
|
$this->assertJsonTransformedWith($response->json($prefix . 'attributes.relationships.tasks.data.0.attributes'), $task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a schedule belonging to another server cannot be viewed.
|
|
|
|
*/
|
|
|
|
public function testScheduleBelongingToAnotherServerCannotBeViewed()
|
|
|
|
{
|
|
|
|
[$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)
|
|
|
|
->getJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
|
|
|
|
->assertNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a subuser without the required permissions is unable to access the schedules endpoint.
|
|
|
|
*/
|
|
|
|
public function testUserWithoutPermissionCannotViewSchedules()
|
|
|
|
{
|
|
|
|
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
|
|
|
|
|
|
|
|
$this->actingAs($user)
|
|
|
|
->getJson("/api/client/servers/{$server->uuid}/schedules")
|
|
|
|
->assertForbidden();
|
|
|
|
|
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)
|
|
|
|
->getJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function permissionsDataProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[[], false],
|
|
|
|
[[], true],
|
|
|
|
[[Permission::ACTION_SCHEDULE_READ], false],
|
|
|
|
[[Permission::ACTION_SCHEDULE_READ], true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|