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

@ -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);
}
}