Merge branch 'develop' into feature/vue-serverview

This commit is contained in:
Dane Everitt 2018-09-05 21:34:59 -07:00
commit 5ca13839cf
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
110 changed files with 7190 additions and 2093 deletions

View file

@ -10,7 +10,7 @@
namespace Tests\Unit\Commands\Schedule;
use Mockery as m;
use Carbon\Carbon;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\Schedule;
use Tests\Unit\Commands\CommandTestCase;
@ -20,11 +20,6 @@ use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ProcessRunnableCommandTest extends CommandTestCase
{
/**
* @var \Carbon\Carbon
*/
protected $carbon;
/**
* @var \Pterodactyl\Console\Commands\Schedule\ProcessRunnableCommand
*/
@ -47,11 +42,12 @@ class ProcessRunnableCommandTest extends CommandTestCase
{
parent::setUp();
$this->carbon = m::mock(Carbon::class);
Chronos::setTestNow(Chronos::now());
$this->processScheduleService = m::mock(ProcessScheduleService::class);
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->command = new ProcessRunnableCommand($this->carbon, $this->processScheduleService, $this->repository);
$this->command = new ProcessRunnableCommand($this->processScheduleService, $this->repository);
}
/**
@ -62,9 +58,7 @@ class ProcessRunnableCommandTest extends CommandTestCase
$schedule = factory(Schedule::class)->make();
$schedule->tasks = collect([factory(Task::class)->make()]);
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('toAtomString')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('getSchedulesToProcess')->with('00:00:00')->once()->andReturn(collect([$schedule]));
$this->repository->shouldReceive('getSchedulesToProcess')->with(Chronos::now()->toAtomString())->once()->andReturn(collect([$schedule]));
$this->processScheduleService->shouldReceive('handle')->with($schedule)->once()->andReturnNull();
$display = $this->runCommand($this->command);
@ -84,9 +78,7 @@ class ProcessRunnableCommandTest extends CommandTestCase
$schedule = factory(Schedule::class)->make();
$schedule->tasks = collect([]);
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('toAtomString')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('getSchedulesToProcess')->with('00:00:00')->once()->andReturn(collect([$schedule]));
$this->repository->shouldReceive('getSchedulesToProcess')->with(Chronos::now()->toAtomString())->once()->andReturn(collect([$schedule]));
$display = $this->runCommand($this->command);
@ -104,9 +96,7 @@ class ProcessRunnableCommandTest extends CommandTestCase
{
$schedule = factory(Schedule::class)->make(['tasks' => null]);
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('toAtomString')->withNoArgs()->once()->andReturn('00:00:00');
$this->repository->shouldReceive('getSchedulesToProcess')->with('00:00:00')->once()->andReturn(collect([$schedule]));
$this->repository->shouldReceive('getSchedulesToProcess')->with(Chronos::now()->toAtomString())->once()->andReturn(collect([$schedule]));
$display = $this->runCommand($this->command);

View file

@ -3,8 +3,8 @@
namespace Tests\Unit\Http\Middleware;
use Mockery as m;
use Pterodactyl\Models\User;
use Illuminate\Foundation\Application;
use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Http\Middleware\LanguageMiddleware;
class LanguageMiddlewareTest extends MiddlewareTestCase
@ -14,11 +14,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
*/
private $appMock;
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
private $config;
/**
* Setup tests.
*/
@ -27,20 +22,32 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
parent::setUp();
$this->appMock = m::mock(Application::class);
$this->config = m::mock(Repository::class);
}
/**
* Test that a language is defined via the middleware.
* Test that a language is defined via the middleware for guests.
*/
public function testLanguageIsSet()
public function testLanguageIsSetForGuest()
{
$this->config->shouldReceive('get')->with('app.locale', 'en')->once()->andReturn('en');
$this->request->shouldReceive('user')->withNoArgs()->andReturnNull();
$this->appMock->shouldReceive('setLocale')->with('en')->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
}
/**
* Test that a language is defined via the middleware for a user.
*/
public function testLanguageIsSetWithAuthenticatedUser()
{
$user = factory(User::class)->make(['language' => 'de']);
$this->request->shouldReceive('user')->withNoArgs()->andReturn($user);
$this->appMock->shouldReceive('setLocale')->with('de')->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
}
/**
* Return an instance of the middleware using mocked dependencies.
*
@ -48,6 +55,6 @@ class LanguageMiddlewareTest extends MiddlewareTestCase
*/
private function getMiddleware(): LanguageMiddleware
{
return new LanguageMiddleware($this->appMock, $this->config);
return new LanguageMiddleware($this->appMock);
}
}

View file

@ -3,8 +3,8 @@
namespace Tests\Unit\Jobs\Schedule;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
@ -58,7 +58,7 @@ class RunTaskJobTest extends TestCase
{
parent::setUp();
Bus::fake();
Carbon::setTestNow(Carbon::now());
Chronos::setTestNow(Chronos::now());
$this->commandRepository = m::mock(CommandRepositoryInterface::class);
$this->config = m::mock(Repository::class);
@ -94,7 +94,7 @@ class RunTaskJobTest extends TestCase
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
'last_run_at' => Chronos::now()->toDateTimeString(),
])->once()->andReturnNull();
$this->getJobInstance($task->id, $schedule->id);
@ -124,7 +124,7 @@ class RunTaskJobTest extends TestCase
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
'last_run_at' => Chronos::now()->toDateTimeString(),
])->once()->andReturnNull();
$this->getJobInstance($task->id, $schedule->id);
@ -202,7 +202,7 @@ class RunTaskJobTest extends TestCase
$this->scheduleRepository->shouldReceive('withoutFreshModel->update')->with($schedule->id, [
'is_processing' => false,
'last_run_at' => Carbon::now()->toDateTimeString(),
'last_run_at' => Chronos::now()->toDateTimeString(),
])->once()->andReturn(1);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturn(1);

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Nodes;
@ -63,17 +56,17 @@ class NodeUpdateServiceTest extends TestCase
->expects($this->once())->willReturn('random_string');
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
$this->repository->shouldReceive('update')->with($model->id, [
'name' => 'NewName',
'daemonSecret' => 'random_string',
])->andReturn(true);
])->andReturn($model);
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->returnUpdatedModel(false)->handle($model, ['name' => 'NewName', 'reset_secret' => true]);
$this->assertTrue($response);
$response = $this->getService()->handle($model, ['name' => 'NewName', 'reset_secret' => true]);
$this->assertInstanceOf(Node::class, $response);
}
/**
@ -83,37 +76,17 @@ class NodeUpdateServiceTest extends TestCase
{
$model = factory(Node::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'name' => 'NewName',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->returnUpdatedModel(false)->handle($model, ['name' => 'NewName']);
$this->assertTrue($response);
}
public function testUpdatedModelIsReturned()
{
$model = factory(Node::class)->make();
$updated = clone $model;
$updated->name = 'NewName';
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('update')->with($model->id, [
'name' => $updated->name,
])->andReturn($updated);
'name' => 'NewName',
])->andReturn($model);
$this->configRepository->shouldReceive('setNode')->with($model)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->returnUpdatedModel()->handle($model, ['name' => $updated->name]);
$response = $this->getService()->handle($model, ['name' => 'NewName']);
$this->assertInstanceOf(Node::class, $response);
$this->assertSame($updated, $response);
}
/**
@ -127,7 +100,7 @@ class NodeUpdateServiceTest extends TestCase
$model = factory(Node::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFreshModel->update')->andReturn(new Response);
$this->repository->shouldReceive('update')->andReturn($model);
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -146,7 +119,7 @@ class NodeUpdateServiceTest extends TestCase
$model = factory(Node::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFreshModel->update')->andReturn(new Response);
$this->repository->shouldReceive('update')->andReturn($model);
$this->configRepository->shouldReceive('setNode->update')->once()->andThrow($this->getExceptionMock());

View file

@ -3,36 +3,32 @@
namespace Tests\Unit\Services\Schedules;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Cron\CronExpression;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
use Illuminate\Contracts\Bus\Dispatcher;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Pterodactyl\Services\Schedules\ProcessScheduleService;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ProcessScheduleServiceTest extends TestCase
{
/**
* @var \Cron\CronExpression|\Mockery\Mock
* @var \Illuminate\Contracts\Bus\Dispatcher|\Mockery\Mock
*/
protected $cron;
private $dispatcher;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
*/
protected $repository;
private $scheduleRepository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService|\Mockery\Mock
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
*/
protected $runnerService;
/**
* @var \Pterodactyl\Services\Schedules\ProcessScheduleService
*/
protected $service;
private $taskRepository;
/**
* Setup tests.
@ -40,12 +36,10 @@ class ProcessScheduleServiceTest extends TestCase
public function setUp()
{
parent::setUp();
Carbon::setTestNow(Carbon::now());
$this->repository = m::mock(ScheduleRepositoryInterface::class);
$this->runnerService = m::mock(RunTaskService::class);
$this->service = new ProcessScheduleService($this->runnerService, $this->repository);
$this->dispatcher = m::mock(Dispatcher::class);
$this->scheduleRepository = m::mock(ScheduleRepositoryInterface::class);
$this->taskRepository = m::mock(TaskRepositoryInterface::class);
}
/**
@ -58,37 +52,36 @@ class ProcessScheduleServiceTest extends TestCase
'sequence_id' => 1,
])]));
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$this->scheduleRepository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$formatted = sprintf('%s %s %s * %s', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->repository->shouldReceive('update')->with($model->id, [
$this->scheduleRepository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once();
$this->service->handle($model);
$this->dispatcher->shouldReceive('dispatch')->with(m::on(function ($class) use ($model, $task) {
$this->assertInstanceOf(RunTaskJob::class, $class);
$this->assertSame($task->time_offset, $class->delay);
$this->assertSame($task->id, $class->task);
$this->assertSame($model->id, $class->schedule);
return true;
}))->once();
$this->getService()->handle($model);
$this->assertTrue(true);
}
public function testScheduleRunTimeCanBeOverridden()
/**
* Return an instance of the service for testing purposes.
*
* @return \Pterodactyl\Services\Schedules\ProcessScheduleService
*/
private function getService(): ProcessScheduleService
{
$model = factory(Schedule::class)->make();
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
'sequence_id' => 1,
])]));
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => Carbon::now()->addSeconds(15)->toDateTimeString(),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->setRunTimeOverride(Carbon::now()->addSeconds(15))->handle($model);
$this->assertTrue(true);
return new ProcessScheduleService($this->dispatcher, $this->scheduleRepository, $this->taskRepository);
}
}

View file

@ -1,90 +0,0 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Schedules\Tasks;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Task;
use Illuminate\Support\Facades\Bus;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
class RunTaskServiceTest extends TestCase
{
/**
* @var \Illuminate\Contracts\Bus\Dispatcher|\Mockery\Mock
*/
protected $dispatcher;
/**
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
Bus::fake();
$this->repository = m::mock(TaskRepositoryInterface::class);
$this->service = new RunTaskService($this->repository);
}
/**
* Test that a job is dispatched.
*/
public function testTaskIsDispatched()
{
$task = factory(Task::class)->make();
$this->repository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once()->andReturnNull();
$this->service->handle($task);
Bus::assertDispatched(RunTaskJob::class, function ($job) use ($task) {
$this->assertEquals($task->id, $job->task, 'Assert job task matches parent task model.');
$this->assertEquals($task->schedule_id, $job->schedule, 'Assert job is linked to correct schedule.');
$this->assertEquals($task->time_offset, $job->delay, 'Assert job delay is set correctly to match task.');
return true;
});
}
/**
* Test that passing an ID in place of a model works.
*/
public function testIdCanBePassedInPlaceOfModel()
{
$task = factory(Task::class)->make();
$this->repository->shouldReceive('find')->with($task->id)->once()->andReturn($task);
$this->repository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once()->andReturnNull();
$this->service->handle($task->id);
Bus::assertDispatched(RunTaskJob::class, function ($job) use ($task) {
$this->assertEquals($task->id, $job->task, 'Assert job task matches parent task model.');
$this->assertEquals($task->schedule_id, $job->schedule, 'Assert job is linked to correct schedule.');
$this->assertEquals($task->time_offset, $job->delay, 'Assert job delay is set correctly to match task.');
return true;
});
}
}