Skip to content

Commit b9a451b

Browse files
committed
Add test coverage for schedules
1 parent 63bc408 commit b9a451b

File tree

7 files changed

+414
-2
lines changed

7 files changed

+414
-2
lines changed

app/Http/Controllers/Api/Client/Servers/ScheduleController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function delete(DeleteScheduleRequest $request, Server $server, Schedule
147147
{
148148
$this->repository->delete($schedule->id);
149149

150-
return JsonResponse::create([], Response::HTTP_NO_CONTENT);
150+
return new JsonResponse([], Response::HTTP_NO_CONTENT);
151151
}
152152

153153
/**

app/Http/Requests/Api/Client/Servers/Schedules/StoreScheduleRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function rules(): array
2121
{
2222
return [
2323
'name' => 'required|string|min:1',
24-
'is_active' => 'boolean',
24+
'is_active' => 'filled|boolean',
2525
'minute' => 'required|string',
2626
'hour' => 'required|string',
2727
'day_of_month' => 'required|string',

tests/Integration/Api/Client/ClientApiIntegrationTestCase.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace Pterodactyl\Tests\Integration\Api\Client;
44

5+
use Carbon\Carbon;
6+
use ReflectionClass;
7+
use Carbon\CarbonImmutable;
58
use Pterodactyl\Models\Node;
69
use Pterodactyl\Models\User;
10+
use Pterodactyl\Models\Model;
711
use Pterodactyl\Models\Server;
812
use Pterodactyl\Models\Subuser;
913
use Pterodactyl\Models\Location;
14+
use Illuminate\Support\Collection;
1015
use Pterodactyl\Tests\Integration\IntegrationTestCase;
16+
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
1117

1218
abstract class ClientApiIntegrationTestCase extends IntegrationTestCase
1319
{
@@ -24,6 +30,17 @@ protected function tearDown(): void
2430
parent::tearDown();
2531
}
2632

33+
/**
34+
* Setup tests and ensure all of the times are always the same.
35+
*/
36+
public function setUp(): void
37+
{
38+
parent::setUp();
39+
40+
Carbon::setTestNow(Carbon::now());
41+
CarbonImmutable::setTestNow(Carbon::now());
42+
}
43+
2744
/**
2845
* Generates a user and a server for that user. If an array of permissions is passed it
2946
* is assumed that the user is actually a subuser of the server.
@@ -50,4 +67,25 @@ protected function generateTestAccount(array $permissions = []): array
5067

5168
return [$user, $server];
5269
}
70+
71+
/**
72+
* Asserts that the data passed through matches the output of the data from the transformer. This
73+
* will remove the "relationships" key when performing the comparison.
74+
*
75+
* @param array $data
76+
* @param \Pterodactyl\Models\Model|\Illuminate\Database\Eloquent\Model $model
77+
*/
78+
protected function assertJsonTransformedWith(array $data, $model)
79+
{
80+
$reflect = new ReflectionClass($model);
81+
$transformer = sprintf('\\Pterodactyl\\Transformers\\Api\\Client\\%sTransformer', $reflect->getShortName());
82+
83+
$transformer = new $transformer;
84+
$this->assertInstanceOf(BaseClientTransformer::class, $transformer);
85+
86+
$this->assertSame(
87+
$transformer->transform($model),
88+
Collection::make($data)->except(['relationships'])->toArray()
89+
);
90+
}
5391
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
* @param array $permissions
16+
* @dataProvider permissionsDataProvider
17+
*/
18+
public function testScheduleCanBeCreatedForServer($permissions)
19+
{
20+
[$user, $server] = $this->generateTestAccount($permissions);
21+
22+
$response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/schedules", [
23+
'name' => 'Test Schedule',
24+
'is_active' => false,
25+
'minute' => '0',
26+
'hour' => '*/2',
27+
'day_of_week' => '2',
28+
'day_of_month' => '*',
29+
]);
30+
31+
$response->assertOk();
32+
33+
$this->assertNotNull($id = $response->json('attributes.id'));
34+
35+
/** @var \Pterodactyl\Models\Schedule $schedule */
36+
$schedule = Schedule::query()->findOrFail($id);
37+
$this->assertFalse($schedule->is_active);
38+
$this->assertFalse($schedule->is_processing);
39+
$this->assertSame('0', $schedule->cron_minute);
40+
$this->assertSame('*/2', $schedule->cron_hour);
41+
$this->assertSame('2', $schedule->cron_day_of_week);
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", 'required');
61+
$response->assertJsonPath("errors.{$i}.source.field", $field);
62+
}
63+
64+
$this->actingAs($user)
65+
->postJson("/api/client/servers/{$server->uuid}/schedules", [
66+
'name' => 'Testing',
67+
'is_active' => 'no',
68+
'minute' => '*',
69+
'hour' => '*',
70+
'day_of_month' => '*',
71+
'day_of_week' => '*',
72+
])
73+
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
74+
->assertJsonPath('errors.0.code', 'boolean');
75+
}
76+
77+
/**
78+
* Test that a subuser without required permissions cannot create a schedule.
79+
*/
80+
public function testSubuserCannotCreateScheduleWithoutPermissions()
81+
{
82+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]);
83+
84+
$this->actingAs($user)
85+
->postJson("/api/client/servers/{$server->uuid}/schedules", [])
86+
->assertForbidden();
87+
}
88+
89+
/**
90+
* @return array
91+
*/
92+
public function permissionsDataProvider(): array
93+
{
94+
return [[[]], [[Permission::ACTION_SCHEDULE_CREATE]]];
95+
}
96+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
4+
5+
use Pterodactyl\Models\Task;
6+
use Illuminate\Http\Response;
7+
use Pterodactyl\Models\Schedule;
8+
use Pterodactyl\Models\Permission;
9+
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
10+
11+
class DeleteServerScheduleTest extends ClientApiIntegrationTestCase
12+
{
13+
/**
14+
* Test that a schedule can be deleted from the system.
15+
*
16+
* @param array $permissions
17+
* @dataProvider permissionsDataProvider
18+
*/
19+
public function testScheduleCanBeDeleted($permissions)
20+
{
21+
[$user, $server] = $this->generateTestAccount($permissions);
22+
23+
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
24+
$task = factory(Task::class)->create(['schedule_id' => $schedule->id]);
25+
26+
$this->actingAs($user)
27+
->deleteJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
28+
->assertStatus(Response::HTTP_NO_CONTENT);
29+
30+
$this->assertDatabaseMissing('schedules', ['id' => $schedule->id]);
31+
$this->assertDatabaseMissing('tasks', ['id' => $task->id]);
32+
}
33+
34+
/**
35+
* Test that no error is returned if the schedule does not exist on the system at all.
36+
*/
37+
public function testNotFoundErrorIsReturnedIfScheduleDoesNotExistAtAll()
38+
{
39+
[$user, $server] = $this->generateTestAccount();
40+
41+
$this->actingAs($user)
42+
->deleteJson("/api/client/servers/{$server->uuid}/schedules/123456789")
43+
->assertStatus(Response::HTTP_NOT_FOUND);
44+
}
45+
46+
/**
47+
* Ensure that a schedule belonging to another server cannot be deleted and its presence is not
48+
* revealed to the user.
49+
*/
50+
public function testNotFoundErrorIsReturnedIfScheduleDoesNotBelongToServer()
51+
{
52+
[$user, $server] = $this->generateTestAccount();
53+
[, $server2] = $this->generateTestAccount();
54+
55+
$schedule = factory(Schedule::class)->create(['server_id' => $server2->id]);
56+
57+
$this->actingAs($user)
58+
->deleteJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
59+
->assertStatus(Response::HTTP_NOT_FOUND);
60+
61+
$this->assertDatabaseHas('schedules', ['id' => $schedule->id]);
62+
}
63+
64+
/**
65+
* Test that an error is returned if the subuser does not have the required permissions to
66+
* delete the schedule from the server.
67+
*/
68+
public function testErrorIsReturnedIfSubuserDoesNotHaveRequiredPermissions()
69+
{
70+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_UPDATE]);
71+
72+
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
73+
74+
$this->actingAs($user)
75+
->deleteJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
76+
->assertStatus(Response::HTTP_FORBIDDEN);
77+
78+
$this->assertDatabaseHas('schedules', ['id' => $schedule->id]);
79+
}
80+
81+
/**
82+
* @return array
83+
*/
84+
public function permissionsDataProvider(): array
85+
{
86+
return [[[]], [[Permission::ACTION_SCHEDULE_DELETE]]];
87+
}
88+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
4+
5+
use Pterodactyl\Models\Task;
6+
use Pterodactyl\Models\Schedule;
7+
use Pterodactyl\Models\Permission;
8+
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
9+
10+
class GetServerSchedulesTest extends ClientApiIntegrationTestCase
11+
{
12+
/**
13+
* Cleanup after tests run.
14+
*/
15+
protected function tearDown(): void
16+
{
17+
Task::query()->forceDelete();
18+
Schedule::query()->forceDelete();
19+
20+
parent::tearDown();
21+
}
22+
23+
/**
24+
* Test that schedules for a server are returned.
25+
*
26+
* @param array $permissions
27+
* @param bool $individual
28+
* @dataProvider permissionsDataProvider
29+
*/
30+
public function testServerSchedulesAreReturned($permissions, $individual)
31+
{
32+
[$user, $server] = $this->generateTestAccount($permissions);
33+
34+
/** @var \Pterodactyl\Models\Schedule $schedule */
35+
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
36+
/** @var \Pterodactyl\Models\Task $task */
37+
$task = factory(Task::class)->create(['schedule_id' => $schedule->id, 'sequence_id' => 1, 'time_offset' => 0]);
38+
39+
$response = $this->actingAs($user)
40+
->getJson(
41+
$individual
42+
? "/api/client/servers/{$server->uuid}/schedules/{$schedule->id}"
43+
: "/api/client/servers/{$server->uuid}/schedules"
44+
)
45+
->assertOk();
46+
47+
$prefix = $individual ? '' : 'data.0.';
48+
if (! $individual) {
49+
$response->assertJsonCount(1, 'data');
50+
}
51+
52+
$response->assertJsonCount(1, $prefix . 'attributes.relationships.tasks.data');
53+
54+
$response->assertJsonPath($prefix . 'object', Schedule::RESOURCE_NAME);
55+
$response->assertJsonPath($prefix . 'attributes.relationships.tasks.data.0.object', Task::RESOURCE_NAME);
56+
57+
$this->assertJsonTransformedWith($response->json($prefix . 'attributes'), $schedule);
58+
$this->assertJsonTransformedWith($response->json($prefix . 'attributes.relationships.tasks.data.0.attributes'), $task);
59+
}
60+
61+
/**
62+
* Test that a schedule belonging to another server cannot be viewed.
63+
*/
64+
public function testScheduleBelongingToAnotherServerCannotBeViewed()
65+
{
66+
[$user, $server] = $this->generateTestAccount();
67+
[, $server2] = $this->generateTestAccount();
68+
69+
$schedule = factory(Schedule::class)->create(['server_id' => $server2->id]);
70+
71+
$this->actingAs($user)
72+
->getJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
73+
->assertNotFound();
74+
}
75+
76+
/**
77+
* Test that a subuser without the required permissions is unable to access the schedules endpoint.
78+
*/
79+
public function testUserWithoutPermissionCannotViewSchedules()
80+
{
81+
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
82+
83+
$this->actingAs($user)
84+
->getJson("/api/client/servers/{$server->uuid}/schedules")
85+
->assertForbidden();
86+
87+
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
88+
89+
$this->actingAs($user)
90+
->getJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}")
91+
->assertForbidden();
92+
}
93+
94+
/**
95+
* @return array
96+
*/
97+
public function permissionsDataProvider(): array
98+
{
99+
return [
100+
[[], false],
101+
[[], true],
102+
[[Permission::ACTION_SCHEDULE_READ], false],
103+
[[Permission::ACTION_SCHEDULE_READ], true],
104+
];
105+
}
106+
}

0 commit comments

Comments
 (0)