forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateServerScheduleTaskTest.php
More file actions
177 lines (148 loc) · 6.65 KB
/
CreateServerScheduleTaskTest.php
File metadata and controls
177 lines (148 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Pterodactyl\Tests\Integration\Api\Client\Server\ScheduleTask;
use Pterodactyl\Models\Task;
use Illuminate\Http\Response;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Permission;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class CreateServerScheduleTaskTest extends ClientApiIntegrationTestCase
{
/**
* Test that a task can be created.
*
* @param array $permissions
* @dataProvider permissionsDataProvider
*/
public function testTaskCanBeCreated($permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
$this->assertEmpty($schedule->tasks);
$response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'command',
'payload' => 'say Test',
'time_offset' => 10,
'sequence_id' => 1,
]);
$response->assertOk();
/** @var \Pterodactyl\Models\Task $task */
$task = Task::query()->findOrFail($response->json('attributes.id'));
$this->assertSame($schedule->id, $task->schedule_id);
$this->assertSame(1, $task->sequence_id);
$this->assertSame('command', $task->action);
$this->assertSame('say Test', $task->payload);
$this->assertSame(10, $task->time_offset);
$this->assertJsonTransformedWith($response->json('attributes'), $task);
}
/**
* Test that validation errors are returned correctly if bad data is passed into the API.
*/
public function testValidationErrorsAreReturned()
{
[$user, $server] = $this->generateTestAccount();
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
$response = $this->actingAs($user)->postJson($this->link($schedule, '/tasks'))->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
foreach (['action', 'payload', 'time_offset'] as $i => $field) {
$response->assertJsonPath("errors.{$i}.code", $field === 'payload' ? 'required_unless' : 'required');
$response->assertJsonPath("errors.{$i}.source.field", $field);
}
$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'hodor',
'payload' => 'say Test',
'time_offset' => 0,
])
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJsonPath('errors.0.code', 'in')
->assertJsonPath('errors.0.source.field', 'action');
$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'command',
'time_offset' => 0,
])
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJsonPath('errors.0.code', 'required_unless')
->assertJsonPath('errors.0.source.field', 'payload');
$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'command',
'payload' => 'say Test',
'time_offset' => 0,
'sequence_id' => 'hodor',
])
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJsonPath('errors.0.code', 'numeric')
->assertJsonPath('errors.0.source.field', 'sequence_id');
}
/**
* Test that backups can be tasked out correctly since they do not require a payload.
*/
public function testBackupsCanBeTaskedCorrectly()
{
[$user, $server] = $this->generateTestAccount();
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'backup',
'time_offset' => 0,
])->assertOk();
$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'backup',
'payload' => "file.txt\nfile2.log",
'time_offset' => 0,
])->assertOk();
}
/**
* Test that an error is returned if the user attempts to create an additional task that
* would put the schedule over the task limit.
*/
public function testErrorIsReturnedIfTooManyTasksExistForSchedule()
{
config()->set('pterodactyl.client_features.schedules.per_schedule_task_limit', 2);
[$user, $server] = $this->generateTestAccount();
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
factory(Task::class)->times(2)->create(['schedule_id' => $schedule->id]);
$this->actingAs($user)->postJson($this->link($schedule, '/tasks'), [
'action' => 'command',
'payload' => 'say test',
'time_offset' => 0,
])
->assertStatus(Response::HTTP_BAD_REQUEST)
->assertJsonPath('errors.0.code', 'ServiceLimitExceededException')
->assertJsonPath('errors.0.detail', 'Schedules may not have more than 2 tasks associated with them. Creating this task would put this schedule over the limit.');
}
/**
* Test that an error is returned if the targeted schedule does not belong to the server
* in the request.
*/
public function testErrorIsReturnedIfScheduleDoesNotBelongToServer()
{
[$user, $server] = $this->generateTestAccount();
[, $server2] = $this->generateTestAccount(['user_id' => $user->id]);
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server2->id]);
$this->actingAs($user)
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}/tasks")
->assertNotFound();
}
/**
* Test that an error is returned if the subuser making the request does not have permission
* to update a schedule.
*/
public function testErrorIsReturnedIfSubuserDoesNotHaveScheduleUpdatePermissions()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_SCHEDULE_CREATE]);
/** @var \Pterodactyl\Models\Schedule $schedule */
$schedule = factory(Schedule::class)->create(['server_id' => $server->id]);
$this->actingAs($user)
->postJson($this->link($schedule, '/tasks'))
->assertForbidden();
}
/**
* @return array
*/
public function permissionsDataProvider(): array
{
return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];
}
}