Add test coverage for schedule execution
This commit is contained in:
parent
c1ee0ac4f8
commit
e7c64bc60e
4 changed files with 106 additions and 5 deletions
|
@ -3,9 +3,9 @@
|
||||||
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Schedules;
|
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Schedules;
|
||||||
|
|
||||||
use Pterodactyl\Models\Permission;
|
use Pterodactyl\Models\Permission;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||||
|
|
||||||
class TriggerScheduleRequest extends FormRequest
|
class TriggerScheduleRequest extends ClientApiRequest
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -18,7 +18,7 @@ class TriggerScheduleRequest extends FormRequest
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ use Pterodactyl\Models\Schedule;
|
||||||
use Illuminate\Contracts\Bus\Dispatcher;
|
use Illuminate\Contracts\Bus\Dispatcher;
|
||||||
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
|
use Pterodactyl\Exceptions\DisplayException;
|
||||||
|
|
||||||
class ProcessScheduleService
|
class ProcessScheduleService
|
||||||
{
|
{
|
||||||
|
@ -42,7 +43,13 @@ class ProcessScheduleService
|
||||||
public function handle(Schedule $schedule, bool $now = false)
|
public function handle(Schedule $schedule, bool $now = false)
|
||||||
{
|
{
|
||||||
/** @var \Pterodactyl\Models\Task $task */
|
/** @var \Pterodactyl\Models\Task $task */
|
||||||
$task = $schedule->tasks()->where('sequence_id', 1)->firstOrFail();
|
$task = $schedule->tasks()->where('sequence_id', 1)->first();
|
||||||
|
|
||||||
|
if (is_null($task)) {
|
||||||
|
throw new DisplayException(
|
||||||
|
'Cannot process schedule for task execution: no tasks are registered.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$this->connection->transaction(function () use ($schedule, $task) {
|
$this->connection->transaction(function () use ($schedule, $task) {
|
||||||
$schedule->forceFill([
|
$schedule->forceFill([
|
||||||
|
|
|
@ -146,7 +146,7 @@ export default ({ match, history, location: { state } }: RouteComponentProps<Par
|
||||||
onDeleted={() => history.push(`/server/${id}/schedules`)}
|
onDeleted={() => history.push(`/server/${id}/schedules`)}
|
||||||
/>
|
/>
|
||||||
</Can>
|
</Can>
|
||||||
{schedule.isActive &&
|
{schedule.isActive && schedule.tasks.length > 0 &&
|
||||||
<Can action={'schedule.update'}>
|
<Can action={'schedule.update'}>
|
||||||
<RunScheduleButton schedule={schedule}/>
|
<RunScheduleButton schedule={schedule}/>
|
||||||
</Can>
|
</Can>
|
||||||
|
|
|
@ -0,0 +1,94 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Schedule;
|
||||||
|
|
||||||
|
use Pterodactyl\Models\Task;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
use Pterodactyl\Models\Schedule;
|
||||||
|
use Pterodactyl\Models\Permission;
|
||||||
|
use Illuminate\Support\Facades\Bus;
|
||||||
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
||||||
|
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
|
||||||
|
|
||||||
|
class ExecuteScheduleTest extends ClientApiIntegrationTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Test that a schedule can be executed and is updated in the database correctly.
|
||||||
|
*
|
||||||
|
* @param array $permissions
|
||||||
|
* @dataProvider permissionsDataProvider
|
||||||
|
*/
|
||||||
|
public function testScheduleIsExecutedRightAway(array $permissions)
|
||||||
|
{
|
||||||
|
[$user, $server] = $this->generateTestAccount($permissions);
|
||||||
|
|
||||||
|
Bus::fake();
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
||||||
|
$schedule = factory(Schedule::class)->create([
|
||||||
|
'server_id' => $server->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->actingAs($user)->postJson($this->link($schedule, '/execute'));
|
||||||
|
$response->assertStatus(Response::HTTP_BAD_REQUEST);
|
||||||
|
$response->assertJsonPath('errors.0.code', 'DisplayException');
|
||||||
|
$response->assertJsonPath('errors.0.detail', 'Cannot process schedule for task execution: no tasks are registered.');
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Task $task */
|
||||||
|
$task = factory(Task::class)->create([
|
||||||
|
'schedule_id' => $schedule->id,
|
||||||
|
'sequence_id' => 1,
|
||||||
|
'time_offset' => 2,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->actingAs($user)->postJson($this->link($schedule, '/execute'))->assertStatus(Response::HTTP_ACCEPTED);
|
||||||
|
|
||||||
|
Bus::assertDispatched(function (RunTaskJob $job) use ($task) {
|
||||||
|
$this->assertSame($task->time_offset, $job->delay);
|
||||||
|
$this->assertSame($task->id, $job->task->id);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the schedule is not executed if it is not currently active.
|
||||||
|
*/
|
||||||
|
public function testScheduleIsNotExecutedIfNotActive()
|
||||||
|
{
|
||||||
|
[$user, $server] = $this->generateTestAccount();
|
||||||
|
|
||||||
|
/** @var \Pterodactyl\Models\Schedule $schedule */
|
||||||
|
$schedule = factory(Schedule::class)->create([
|
||||||
|
'server_id' => $server->id,
|
||||||
|
'is_active' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->actingAs($user)->postJson($this->link($schedule, "/execute"));
|
||||||
|
|
||||||
|
$response->assertStatus(Response::HTTP_BAD_REQUEST);
|
||||||
|
$response->assertJsonPath('errors.0.code', 'BadRequestHttpException');
|
||||||
|
$response->assertJsonPath('errors.0.detail', 'Cannot trigger schedule exection for a schedule that is not currently active.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that a user without the schedule update permission cannot execute it.
|
||||||
|
*/
|
||||||
|
public function testUserWithoutScheduleUpdatePermissionCannotExecute()
|
||||||
|
{
|
||||||
|
[$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, '/execute'))->assertForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function permissionsDataProvider(): array
|
||||||
|
{
|
||||||
|
return [[[]], [[Permission::ACTION_SCHEDULE_UPDATE]]];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue