Return Http test cases to a passing state
This commit is contained in:
parent
eaae74fe33
commit
536180ed0c
26 changed files with 140 additions and 1113 deletions
|
@ -1,110 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Stan
|
||||
* Date: 26-5-2018
|
||||
* Time: 21:06.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Http\Controllers\Admin;
|
||||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Tests\Assertions\ControllerAssertionsTrait;
|
||||
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Http\Controllers\Admin\StatisticsController;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
|
||||
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(): void
|
||||
{
|
||||
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, ]
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,181 +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\Http\Controllers\Base;
|
||||
|
||||
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;
|
||||
|
||||
class IndexControllerTest extends ControllerTestCase
|
||||
{
|
||||
use ControllerAssertionsTrait;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Http\Controllers\Base\IndexController
|
||||
*/
|
||||
protected $controller;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
|
||||
*/
|
||||
protected $keyProviderService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
|
||||
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
|
||||
$this->controller = new IndexController($this->keyProviderService, $this->daemonRepository, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the index controller.
|
||||
*/
|
||||
public function testIndexController()
|
||||
{
|
||||
$paginator = m::mock(LengthAwarePaginator::class);
|
||||
$model = $this->generateRequestUserModel();
|
||||
|
||||
$this->request->shouldReceive('input')->with('query')->once()->andReturn('searchTerm');
|
||||
$this->repository->shouldReceive('setSearchTerm')->with('searchTerm')->once()->andReturnSelf()
|
||||
->shouldReceive('filterUserAccessServers')->with($model, User::FILTER_LEVEL_ALL, config('pterodactyl.paginate.frontend.servers'))
|
||||
->once()->andReturn($paginator);
|
||||
|
||||
$response = $this->controller->index($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('templates.base.core', $response);
|
||||
$this->assertViewHasKey('servers', $response);
|
||||
$this->assertViewKeyEquals('servers', $paginator, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status controller.
|
||||
*/
|
||||
public function testStatusController()
|
||||
{
|
||||
$user = $this->generateRequestUserModel();
|
||||
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
|
||||
$psrResponse = new Response;
|
||||
|
||||
$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()->andReturn($psrResponse);
|
||||
|
||||
$response = $this->controller->status($this->request, $server->uuidShort);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseJsonEquals(json_encode($psrResponse->getBody()), $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status controller if a server is not installed.
|
||||
*/
|
||||
public function testStatusControllerWhenServerNotInstalled()
|
||||
{
|
||||
$user = $this->generateRequestUserModel();
|
||||
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 0]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
|
||||
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
|
||||
|
||||
$response = $this->controller->status($this->request, $server->uuidShort);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(200, $response);
|
||||
$this->assertResponseJsonEquals(['status' => 20], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status controller when a server is suspended.
|
||||
*/
|
||||
public function testStatusControllerWhenServerIsSuspended()
|
||||
{
|
||||
$user = factory(User::class)->make();
|
||||
$server = factory(Server::class)->make(['suspended' => 1, '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');
|
||||
|
||||
$response = $this->controller->status($this->request, $server->uuidShort);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$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);
|
||||
}
|
||||
}
|
|
@ -1,156 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Http\Controllers\Base;
|
||||
|
||||
use Mockery as m;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Collection;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||
use Pterodactyl\Services\Users\TwoFactorSetupService;
|
||||
use Pterodactyl\Services\Users\ToggleTwoFactorService;
|
||||
use Pterodactyl\Http\Controllers\Base\SecurityController;
|
||||
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
|
||||
|
||||
class SecurityControllerTest extends ControllerTestCase
|
||||
{
|
||||
/**
|
||||
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
||||
*/
|
||||
protected $alert;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SessionRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService|\Mockery\Mock
|
||||
*/
|
||||
protected $toggleTwoFactorService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\TwoFactorSetupService|\Mockery\Mock
|
||||
*/
|
||||
protected $twoFactorSetupService;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->alert = m::mock(AlertsMessageBag::class);
|
||||
$this->config = m::mock(Repository::class);
|
||||
$this->repository = m::mock(SessionRepositoryInterface::class);
|
||||
$this->toggleTwoFactorService = m::mock(ToggleTwoFactorService::class);
|
||||
$this->twoFactorSetupService = m::mock(TwoFactorSetupService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test TOTP generation controller.
|
||||
*/
|
||||
public function testIndexWithout2FactorEnabled()
|
||||
{
|
||||
$model = $this->generateRequestUserModel(['use_totp' => 0]);
|
||||
|
||||
$this->twoFactorSetupService->shouldReceive('handle')->with($model)->once()->andReturn(new Collection([
|
||||
'image' => 'test-image',
|
||||
'secret' => 'secret-code',
|
||||
]));
|
||||
|
||||
$response = $this->getController()->index($this->request);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
||||
$this->assertResponseJsonEquals(['enabled' => false, 'qr_image' => 'test-image', 'secret' => 'secret-code'], $response);
|
||||
$this->assertResponseJsonEquals(['qrImage' => 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=qrCodeImage'], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test TOTP setting controller when no exception is thrown by the service.
|
||||
*/
|
||||
public function testIndexWith2FactorEnabled()
|
||||
{
|
||||
$this->generateRequestUserModel(['use_totp' => 1]);
|
||||
|
||||
$response = $this->getController()->index($this->request);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
||||
$this->assertResponseJsonEquals(['enabled' => true], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a 2FA token can be stored or deleted.
|
||||
*
|
||||
* @param string $func
|
||||
* @dataProvider functionCallDataProvider
|
||||
*/
|
||||
public function testStore(string $func)
|
||||
{
|
||||
$model = $this->generateRequestUserModel();
|
||||
|
||||
$this->mockRequestInput('token', 'some-token');
|
||||
|
||||
if ($func === 'delete') {
|
||||
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'some-token', false);
|
||||
} else {
|
||||
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'some-token');
|
||||
}
|
||||
|
||||
$response = $this->getController()->{$func}($this->request);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
||||
$this->assertResponseJsonEquals(['success' => true], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test an invalid token exception is handled.
|
||||
*
|
||||
* @param string $func
|
||||
* @dataProvider functionCallDataProvider
|
||||
*/
|
||||
public function testStoreWithInvalidTokenException(string $func)
|
||||
{
|
||||
$this->generateRequestUserModel();
|
||||
|
||||
$this->mockRequestInput('token');
|
||||
$this->toggleTwoFactorService->shouldReceive('handle')->andThrow(new TwoFactorAuthenticationTokenInvalid);
|
||||
|
||||
$response = $this->getController()->{$func}($this->request);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
||||
$this->assertResponseJsonEquals(['success' => false], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function functionCallDataProvider()
|
||||
{
|
||||
return [['store'], ['delete']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the controller for testing with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Http\Controllers\Base\SecurityController
|
||||
*/
|
||||
private function getController(): SecurityController
|
||||
{
|
||||
return new SecurityController(
|
||||
$this->alert,
|
||||
$this->config,
|
||||
$this->repository,
|
||||
$this->toggleTwoFactorService,
|
||||
$this->twoFactorSetupService
|
||||
);
|
||||
}
|
||||
}
|
Reference in a new issue