Merge branch 'develop' of https://github.com/stanjg/panel into feature/user-specific-language

This commit is contained in:
stanjg 2018-06-01 15:58:09 +02:00
commit 3bb9e5e8a8
199 changed files with 2390 additions and 1179 deletions

View file

@ -124,7 +124,7 @@ class BulkPowerActionCommandTest extends CommandTestCase
}
/**
* Test that validation occurrs correctly.
* Test that validation occurs correctly.
*
* @param array $data
*

View file

@ -24,7 +24,7 @@ class HumanReadableTest extends TestCase
}
/**
* Provide data to test aganist the helper function.
* Provide data to test against the helper function.
*
* @return array
*/

View file

@ -24,7 +24,7 @@ class IsDigitTest extends TestCase
}
/**
* Provide data to test aganist the helper function.
* Provide data to test against the helper function.
*
* @return array
*/

View file

@ -0,0 +1,113 @@
<?php
/**
* Created by PhpStorm.
* User: Stan
* Date: 26-5-2018
* Time: 21:06
*/
namespace Tests\Unit\Http\Controllers\Admin;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Routing\Controller;
use Mockery as m;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Http\Controllers\Admin\StatisticsController;
use Pterodactyl\Models\Node;
use Tests\Assertions\ControllerAssertionsTrait;
use Tests\Unit\Http\Controllers\ControllerTestCase;
class StatisticsControllerTest extends ControllerTestCase
{
use ControllerAssertionsTrait;
/**
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
*/
private $allocationRepository;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
*/
private $databaseRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface|\Mockery\Mock
*/
private $eggRepository;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
*/
private $nodeRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
private $serverRepository;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
*/
private $userRepository;
public function setUp()
{
parent::setUp();
$this->allocationRepository = m::mock(AllocationRepositoryInterface::class);
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
$this->eggRepository = m::mock(EggRepositoryInterface::class);
$this->nodeRepository = m::mock(NodeRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->userRepository = m::mock(UserRepositoryInterface::class);
}
public function testIndexController()
{
$controller = $this->getController();
$this->serverRepository->shouldReceive('all')->withNoArgs();
$this->nodeRepository->shouldReceive('all')->withNoArgs()->andReturn(collect([factory(Node::class)->make(), factory(Node::class)->make()]));
$this->userRepository->shouldReceive('count')->withNoArgs();
$this->eggRepository->shouldReceive('count')->withNoArgs();
$this->databaseRepository->shouldReceive('count')->withNoArgs();
$this->allocationRepository->shouldReceive('count')->withNoArgs();
$this->serverRepository->shouldReceive('getSuspendedServersCount')->withNoArgs();
$this->nodeRepository->shouldReceive('getUsageStatsRaw')->twice()->andReturn([
'memory' => [
'value' => 1024,
'max' => 512,
],
'disk' => [
'value' => 1024,
'max' => 512,
]
]);
$controller->shouldReceive('injectJavascript')->once();
$response = $controller->index();
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('admin.statistics', $response);
}
private function getController()
{
return $this->buildMockedController(StatisticsController::class, [$this->allocationRepository,
$this->databaseRepository,
$this->eggRepository,
$this->nodeRepository,
$this->serverRepository,
$this->userRepository]
);
}
}

View file

@ -13,10 +13,14 @@ use Mockery as m;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Tests\Assertions\ControllerAssertionsTrait;
use Tests\Unit\Http\Controllers\ControllerTestCase;
use Pterodactyl\Http\Controllers\Base\IndexController;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -134,4 +138,44 @@ class IndexControllerTest extends ControllerTestCase
$this->assertResponseCodeEquals(200, $response);
$this->assertResponseJsonEquals(['status' => 30], $response);
}
/**
* Test the status controller with a ServerConnectionException.
*/
public function testStatusControllerWithServerConnectionException()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andThrow(new ConnectException('bad connection', new ServerRequest('', '')));
$this->expectExceptionObject(new HttpException(500, 'bad connection'));
$this->controller->status($this->request, $server->uuidShort);
}
/**
* Test the status controller with a RequestException.
*/
public function testStatusControllerWithRequestException()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andThrow(new RequestException('bad request', new ServerRequest('', '')));
$this->expectExceptionObject(new HttpException(500, 'bad request'));
$this->controller->status($this->request, $server->uuidShort);
}
}

View file

@ -98,6 +98,36 @@ class SecurityControllerTest extends ControllerTestCase
$this->assertResponseJsonEquals(['qrImage' => 'qrCodeImage'], $response);
}
/**
* Test TOTP setting controller when no exception is thrown by the service.
*/
public function testSetTotpControllerSuccess()
{
$model = $this->generateRequestUserModel();
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once();
$response = $this->getController()->setTotp($this->request);
$this->assertIsResponse($response);
$this->assertSame('true', $response->getContent());
}
/**
* Test TOTP setting controller when an exception is thrown by the service.
*/
public function testSetTotpControllerWhenExceptionIsThrown()
{
$model = $this->generateRequestUserModel();
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once()->andThrow(new TwoFactorAuthenticationTokenInvalid());
$response = $this->getController()->setTotp($this->request);
$this->assertIsResponse($response);
$this->assertSame('false', $response->getContent());
}
/**
* Test the disable totp controller when no exception is thrown by the service.
*/

View file

@ -79,7 +79,7 @@ class RemoteRequestControllerTest extends ControllerTestCase
}
/**
* Test that the controller properly handles an exception thrown by the daemon conneciton.
* Test that the controller properly handles an exception thrown by the daemon connection.
*/
public function testExceptionThrownByDaemonConnectionIsHandledByDisplayController()
{
@ -125,7 +125,7 @@ class RemoteRequestControllerTest extends ControllerTestCase
}
/**
* Test that the controller properly handles an exception thrown by the daemon conneciton.
* Test that the controller properly handles an exception thrown by the daemon connection.
*/
public function testExceptionThrownByDaemonConnectionIsHandledByStoreController()
{

View file

@ -36,7 +36,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
/**
* Test that a CIDR range can be used.
*/
public function testValidIPAganistCIDRRange()
public function testValidIPAgainstCIDRRange()
{
$model = factory(ApiKey::class)->make(['allowed_ips' => '["192.168.1.1/28"]']);
$this->setRequestAttribute('api_key', $model);

View file

@ -59,7 +59,7 @@ class AuthenticateKeyTest extends MiddlewareTestCase
}
/**
* Test that an invalid API identifer throws an exception.
* Test that an invalid API identifier throws an exception.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/

View file

@ -0,0 +1,70 @@
<?php
namespace Tests\Unit\Http\Middleware;
use Mockery as m;
use Pterodactyl\Models\Node;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Routing\ResponseFactory;
use Pterodactyl\Http\Middleware\MaintenanceMiddleware;
class MaintenanceMiddlewareTest extends MiddlewareTestCase
{
/**
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
*/
private $response;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->response = m::mock(ResponseFactory::class);
}
/**
* Test that a node not in maintenance mode continues through the request cycle.
*/
public function testHandle()
{
$server = factory(Server::class)->make();
$node = factory(Node::class)->make(['maintenance' => 0]);
$server->setRelation('node', $node);
$this->setRequestAttribute('server', $server);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
}
/**
* Test that a node in maintenance mode returns an error view.
*/
public function testHandleInMaintenanceMode()
{
$server = factory(Server::class)->make();
$node = factory(Node::class)->make(['maintenance_mode' => 1]);
$server->setRelation('node', $node);
$this->setRequestAttribute('server', $server);
$this->response->shouldReceive('view')
->once()
->with('errors.maintenance')
->andReturn(new Response);
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
$this->assertInstanceOf(Response::class, $response);
}
/**
* @return \Pterodactyl\Http\Middleware\MaintenanceMiddleware
*/
private function getMiddleware(): MaintenanceMiddleware
{
return new MaintenanceMiddleware($this->response);
}
}

View file

@ -103,7 +103,7 @@ class RunTaskJobTest extends TestCase
}
/**
* Test commmand action passed to job.
* Test command action passed to job.
*/
public function testCommandAction()
{

View file

@ -19,7 +19,7 @@ class AdminAclTest extends TestCase
}
/**
* Test that checking aganist a model works as expected.
* Test that checking against a model works as expected.
*/
public function testCheck()
{

View file

@ -103,7 +103,7 @@ class AssignmentServiceTest extends TestCase
}
/**
* Test a non-CIRD IP address with a single port and an alias.
* Test a non-CIDR IP address with a single port and an alias.
*/
public function testIndividualIPAddressWithAlias()
{

View file

@ -11,10 +11,10 @@ namespace Tests\Unit\Services\DaemonKeys;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\DaemonKey;
use Psr\Log\LoggerInterface as Writer;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
@ -57,7 +57,7 @@ class DaemonKeyDeletionServiceTest extends TestCase
protected $service;
/**
* @var \Illuminate\Log\Writer|\Mockery\Mock
* @var \Psr\Log\LoggerInterface|\Mockery\Mock
*/
protected $writer;

View file

@ -65,7 +65,7 @@ class InstallScriptServiceTest extends TestCase
{
$this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
$this->repository->shouldReceive('isCopyableScript')->with(1, $this->model->nest_id)->once()->andReturn(true);
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->model->id, $this->data)->andReturnNull();
@ -73,13 +73,13 @@ class InstallScriptServiceTest extends TestCase
}
/**
* Test that an exception gets raised when the script is not copiable.
* Test that an exception gets raised when the script is not copyable.
*/
public function testUpdateWithInvalidCopyScriptFromAttribute()
{
$this->data['copy_script_from'] = 1;
$this->repository->shouldReceive('isCopiableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
$this->repository->shouldReceive('isCopyableScript')->with(1, $this->model->nest_id)->once()->andReturn(false);
try {
$this->service->handle($this->model, $this->data);
} catch (Exception $exception) {

View file

@ -68,7 +68,7 @@ class VariableCreationServiceTest extends TestCase
/**
* Test that an empty (null) value passed in the option key is handled
* properly as an array. Also tests the same case aganist the default_value.
* properly as an array. Also tests the same case against the default_value.
*
* @see https://github.com/Pterodactyl/Panel/issues/841
* @see https://github.com/Pterodactyl/Panel/issues/943

View file

@ -153,7 +153,7 @@ class SoftwareVersionServiceTest extends TestCase
}
/**
* Provide data for testing booklean response for daemon version.
* Provide data for testing boolean response for daemon version.
*
* @return array
*/

View file

@ -154,7 +154,7 @@ class PackCreationServiceTest extends TestCase
}
/**
* Return an array of valid mimetypes to test aganist.
* Return an array of valid mimetypes to test against.
*
* @return array
*/
@ -167,7 +167,7 @@ class PackCreationServiceTest extends TestCase
}
/**
* Provide invalid mimetypes to test exceptions aganist.
* Provide invalid mimetypes to test exceptions against.
*
* @return array
*/

View file

@ -60,7 +60,7 @@ class ProcessScheduleServiceTest extends TestCase
$this->repository->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);
$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, [
'is_processing' => true,
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),

View file

@ -64,7 +64,7 @@ class ScheduleCreationServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'next_run_at' => CronExpression::factory('* * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -85,7 +85,7 @@ class ScheduleCreationServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('create')->with([
'server_id' => $server->id,
'next_run_at' => CronExpression::factory('* * * * * *')->getNextRunDate(),
'next_run_at' => CronExpression::factory('* * * * *')->getNextRunDate(),
'test_key' => 'value',
])->once()->andReturn($schedule);

View file

@ -64,7 +64,7 @@ class ScheduleUpdateServiceTest extends TestCase
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs();
$this->repository->shouldReceive('update')->once()->with($schedule->id, array_merge($data, [
'next_run_at' => CronExpression::factory('1 2 3 * 4 *')->getNextRunDate(),
'next_run_at' => CronExpression::factory('1 2 3 * 4')->getNextRunDate(),
]))->andReturn($schedule);
$this->taskRepository->shouldReceive('deleteWhere')->once()->with([['schedule_id', '=', $schedule->id]]);

View file

@ -149,7 +149,7 @@ class TaskCreationServiceTest extends TestCase
}
/**
* Test that exceptions are thrown if the Scheudle module or ID is invalid.
* Test that exceptions are thrown if the Schedule module or ID is invalid.
*
* @dataProvider invalidScheduleArgumentProvider
* @expectedException \InvalidArgumentException
@ -190,7 +190,7 @@ class TaskCreationServiceTest extends TestCase
}
/**
* Return an array of invalid schedule data to test aganist.
* Return an array of invalid schedule data to test against.
*
* @return array
*/

View file

@ -36,7 +36,7 @@ class EnvironmentServiceTest extends TestCase
}
/**
* Test that set environment key stores the key into a retreviable array.
* Test that set environment key stores the key into a retrievable array.
*/
public function testSettingEnvironmentKeyPersistsItInArray()
{
@ -92,7 +92,7 @@ class EnvironmentServiceTest extends TestCase
/**
* Test that duplicate variables provided in config override the defaults.
*/
public function testProcessShouldAllowOverwritingVaraiblesWithConfigurationFile()
public function testProcessShouldAllowOverwritingVariablesWithConfigurationFile()
{
$model = $this->getServerModel();
$this->repository->shouldReceive('getVariablesWithValues')->with($model->id)->once()->andReturn([]);

View file

@ -4,9 +4,9 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Psr\Log\LoggerInterface as Writer;
use Tests\Traits\MocksRequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Servers\ServerDeletionService;
@ -45,7 +45,7 @@ class ServerDeletionServiceTest extends TestCase
private $repository;
/**
* @var \Illuminate\Log\Writer|\Mockery\Mock
* @var \Psr\Log\LoggerInterface|\Mockery\Mock
*/
private $writer;

View file

@ -12,9 +12,9 @@ namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Psr\Log\LoggerInterface as Writer;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
@ -55,7 +55,7 @@ class SuspensionServiceTest extends TestCase
protected $service;
/**
* @var \Illuminate\Log\Writer
* @var \Psr\Log\LoggerInterface
*/
protected $writer;