Fix failing test suite

This commit is contained in:
Dane Everitt 2017-11-04 12:49:05 -05:00
parent 99aceaca38
commit 71b90650de
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
5 changed files with 41 additions and 46 deletions

View File

@ -6,9 +6,10 @@ use Closure;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class AccessingValidServer
@ -23,6 +24,11 @@ class AccessingValidServer
*/
private $repository;
/**
* @var \Illuminate\Contracts\Routing\ResponseFactory
*/
private $response;
/**
* @var \Illuminate\Contracts\Session\Session
*/
@ -32,16 +38,19 @@ class AccessingValidServer
* AccessingValidServer constructor.
*
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Illuminate\Contracts\Routing\ResponseFactory $response
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Illuminate\Contracts\Session\Session $session
*/
public function __construct(
ConfigRepository $config,
ResponseFactory $response,
ServerRepositoryInterface $repository,
Session $session
) {
$this->config = $config;
$this->repository = $repository;
$this->response = $response;
$this->session = $session;
}
@ -63,30 +72,22 @@ class AccessingValidServer
$isApiRequest = $request->expectsJson() || $request->is(...$this->config->get('pterodactyl.json_routes', []));
$server = $this->repository->getByUuid($attributes instanceof Server ? $attributes->uuid : $attributes);
if (! $server) {
if ($isApiRequest) {
throw new NotFoundHttpException('The requested server was not found on the system.');
}
return response()->view('errors.404', [], 404);
}
if ($server->suspended) {
if ($isApiRequest) {
throw new AccessDeniedHttpException('Server is suspended.');
throw new AccessDeniedHttpException('Server is suspended and cannot be accessed.');
}
return response()->view('errors.suspended', [], 403);
return $this->response->view('errors.suspended', [], 403);
}
// Servers can have install statuses other than 1 or 0, so don't check
// for a bool-type operator here.
if ($server->installed !== 1) {
if ($isApiRequest) {
throw new AccessDeniedHttpException('Server is not marked as installed.');
throw new ConflictHttpException('Server is still completing the installation process.');
}
return response()->view('errors.installing', [], 403);
return $this->response->view('errors.installing', [], 409);
}
// Store the server in the session.

View File

@ -54,6 +54,11 @@ class ServerConfigurationStructureService
$server = $this->repository->getDataForCreation($server);
}
$pack = $server->getRelation('pack');
if (! is_null($pack)) {
$pack = $server->getRelation('pack')->uuid;
}
return [
'uuid' => $server->uuid,
'build' => [
@ -74,7 +79,7 @@ class ServerConfigurationStructureService
],
'service' => [
'egg' => $server->egg->uuid,
'pack' => object_get($server, 'pack.uuid'),
'pack' => $pack,
'skip_scripts' => $server->skip_scripts,
],
'rebuild' => false,

View File

@ -17,7 +17,7 @@
<div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12">
<div class="box box-danger">
<div class="box-body text-center">
<h1 class="text-red" style="font-size: 160px !important;font-weight: 100 !important;">401</h1>
<h1 class="text-red" style="font-size: 160px !important;font-weight: 100 !important;">403</h1>
<p class="text-muted">@lang('base.errors.suspended.desc')</p>
</div>
<div class="box-footer with-border">

View File

@ -3,10 +3,10 @@
namespace Tests\Unit\Http\Middleware\Server;
use Mockery as m;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Routing\ResponseFactory;
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
use Pterodactyl\Http\Middleware\AccessingValidServer;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -23,6 +23,11 @@ class AccessingValidServerTest extends MiddlewareTestCase
*/
private $repository;
/**
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
*/
private $response;
/**
* @var \Illuminate\Contracts\Session\Session|\Mockery\Mock
*/
@ -37,30 +42,15 @@ class AccessingValidServerTest extends MiddlewareTestCase
$this->config = m::mock(Repository::class);
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->response = m::mock(ResponseFactory::class);
$this->session = m::mock(Session::class);
}
/**
* Test that an exception is thrown if the request is an API request and no server is found.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* @expectedExceptionMessage The requested server was not found on the system.
*/
public function testExceptionIsThrownIfNoServerIsFoundAndIsAPIRequest()
{
$this->request->shouldReceive('route->parameter')->with('server')->once()->andReturn('123456');
$this->request->shouldReceive('expectsJson')->withNoArgs()->once()->andReturn(true);
$this->repository->shouldReceive('getByUuid')->with('123456')->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
}
/**
* Test that an exception is thrown if the request is an API request and the server is suspended.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @expectedExceptionMessage Server is suspended.
* @expectedExceptionMessage Server is suspended and cannot be accessed.
*/
public function testExceptionIsThrownIfServerIsSuspended()
{
@ -77,8 +67,8 @@ class AccessingValidServerTest extends MiddlewareTestCase
/**
* Test that an exception is thrown if the request is an API request and the server is not installed.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @expectedExceptionMessage Server is not marked as installed.
* @expectedException \Symfony\Component\HttpKernel\Exception\ConflictHttpException
* @expectedExceptionMessage Server is still completing the installation process.
*/
public function testExceptionIsThrownIfServerIsNotInstalled()
{
@ -97,7 +87,7 @@ class AccessingValidServerTest extends MiddlewareTestCase
*
* @dataProvider viewDataProvider
*/
public function testCorrectErrorPagesAreRendered(Server $model = null, string $page, int $httpCode)
public function testCorrectErrorPagesAreRendered(Server $model, string $page, int $httpCode)
{
$this->request->shouldReceive('route->parameter')->with('server')->once()->andReturn('123456');
$this->request->shouldReceive('expectsJson')->withNoArgs()->once()->andReturn(false);
@ -105,11 +95,10 @@ class AccessingValidServerTest extends MiddlewareTestCase
$this->request->shouldReceive('is')->with(...[])->once()->andReturn(false);
$this->repository->shouldReceive('getByUuid')->with('123456')->once()->andReturn($model);
$this->response->shouldReceive('view')->with($page, [], $httpCode)->once()->andReturn(true);
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals($page, $response->getOriginalContent()->getName(), 'Assert that the correct view is returned.');
$this->assertEquals($httpCode, $response->getStatusCode(), 'Assert that the correct HTTP code is returned.');
$this->assertTrue($response);
}
/**
@ -143,10 +132,9 @@ class AccessingValidServerTest extends MiddlewareTestCase
$this->refreshApplication();
return [
[null, 'errors.404', 404],
[factory(Server::class)->make(['suspended' => 1]), 'errors.suspended', 403],
[factory(Server::class)->make(['installed' => 0]), 'errors.installing', 403],
[factory(Server::class)->make(['installed' => 2]), 'errors.installing', 403],
[factory(Server::class)->make(['installed' => 0]), 'errors.installing', 409],
[factory(Server::class)->make(['installed' => 2]), 'errors.installing', 409],
];
}
@ -157,6 +145,6 @@ class AccessingValidServerTest extends MiddlewareTestCase
*/
private function getMiddleware(): AccessingValidServer
{
return new AccessingValidServer($this->config, $this->repository, $this->session);
return new AccessingValidServer($this->config, $this->response, $this->repository, $this->session);
}
}

View File

@ -41,9 +41,10 @@ class ServerConfigurationStructureServiceTest extends TestCase
public function testCorrectStructureIsReturned()
{
$model = factory(Server::class)->make();
$model->allocation = factory(Allocation::class)->make();
$model->allocations = collect(factory(Allocation::class)->times(2)->make());
$model->egg = factory(Egg::class)->make();
$model->setRelation('pack', null);
$model->setRelation('allocation', factory(Allocation::class)->make());
$model->setRelation('allocations', collect(factory(Allocation::class)->times(2)->make()));
$model->setRelation('egg', factory(Egg::class)->make());
$portListing = $model->allocations->groupBy('ip')->map(function ($item) {
return $item->pluck('port');