2017-10-30 02:40:34 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
namespace Pterodactyl\Tests\Unit\Http\Middleware\Server;
|
2017-10-30 02:40:34 +00:00
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2017-11-04 17:49:05 +00:00
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory;
|
2018-07-15 04:49:49 +00:00
|
|
|
use Pterodactyl\Http\Middleware\Server\AccessingValidServer;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Tests\Unit\Http\Middleware\MiddlewareTestCase;
|
2017-10-30 02:40:34 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
2020-06-24 04:59:37 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2017-10-30 02:40:34 +00:00
|
|
|
|
2017-11-02 01:45:43 +00:00
|
|
|
class AccessingValidServerTest extends MiddlewareTestCase
|
2017-10-30 02:40:34 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2017-11-04 17:49:05 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $response;
|
|
|
|
|
2017-10-30 02:40:34 +00:00
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-10-30 02:40:34 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->config = m::mock(Repository::class);
|
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
2017-11-04 17:49:05 +00:00
|
|
|
$this->response = m::mock(ResponseFactory::class);
|
2017-10-30 02:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the request is an API request and the server is suspended.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfServerIsSuspended()
|
|
|
|
{
|
2020-06-24 04:59:37 +00:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
$this->expectExceptionMessage('Server is suspended and cannot be accessed.');
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$model = Server::factory()->make(['suspended' => 1]);
|
2017-10-30 02:40:34 +00:00
|
|
|
|
|
|
|
$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()->andReturn($model);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the request is an API request and the server is not installed.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfServerIsNotInstalled()
|
|
|
|
{
|
2020-06-24 04:59:37 +00:00
|
|
|
$this->expectException(ConflictHttpException::class);
|
|
|
|
$this->expectExceptionMessage('Server is still completing the installation process.');
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$model = Server::factory()->make(['installed' => 0]);
|
2017-10-30 02:40:34 +00:00
|
|
|
|
|
|
|
$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()->andReturn($model);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the correct error pages are rendered depending on the status of the server.
|
|
|
|
*
|
|
|
|
* @dataProvider viewDataProvider
|
|
|
|
*/
|
2017-11-04 17:49:05 +00:00
|
|
|
public function testCorrectErrorPagesAreRendered(Server $model, string $page, int $httpCode)
|
2017-10-30 02:40:34 +00:00
|
|
|
{
|
|
|
|
$this->request->shouldReceive('route->parameter')->with('server')->once()->andReturn('123456');
|
|
|
|
$this->request->shouldReceive('expectsJson')->withNoArgs()->once()->andReturn(false);
|
|
|
|
$this->config->shouldReceive('get')->with('pterodactyl.json_routes', [])->once()->andReturn([]);
|
|
|
|
$this->request->shouldReceive('is')->with(...[])->once()->andReturn(false);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('getByUuid')->with('123456')->once()->andReturn($model);
|
2017-11-04 17:49:05 +00:00
|
|
|
$this->response->shouldReceive('view')->with($page, [], $httpCode)->once()->andReturn(true);
|
2017-10-30 02:40:34 +00:00
|
|
|
|
|
|
|
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
2017-11-04 17:49:05 +00:00
|
|
|
$this->assertTrue($response);
|
2017-10-30 02:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the full middleware works correctly.
|
|
|
|
*/
|
|
|
|
public function testValidServerProcess()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$model = Server::factory()->make();
|
2017-10-30 02:40:34 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('route->parameter')->with('server')->once()->andReturn('123456');
|
|
|
|
$this->request->shouldReceive('expectsJson')->withNoArgs()->once()->andReturn(false);
|
|
|
|
$this->config->shouldReceive('get')->with('pterodactyl.json_routes', [])->once()->andReturn([]);
|
|
|
|
$this->request->shouldReceive('is')->with(...[])->once()->andReturn(false);
|
|
|
|
|
|
|
|
$this->repository->shouldReceive('getByUuid')->with('123456')->once()->andReturn($model);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
2017-11-02 01:45:43 +00:00
|
|
|
$this->assertRequestHasAttribute('server');
|
|
|
|
$this->assertRequestAttributeEquals($model, 'server');
|
2017-10-30 02:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide test data that checks that the correct view is returned for each model type.
|
|
|
|
*/
|
|
|
|
public function viewDataProvider(): array
|
|
|
|
{
|
|
|
|
// Without this we are unable to instantiate the factory builders for some reason.
|
|
|
|
$this->refreshApplication();
|
|
|
|
|
|
|
|
return [
|
2021-01-23 20:09:16 +00:00
|
|
|
[Server::factory()->make(['suspended' => 1]), 'errors.suspended', 403],
|
|
|
|
[Server::factory()->make(['installed' => 0]), 'errors.installing', 409],
|
|
|
|
[Server::factory()->make(['installed' => 2]), 'errors.installing', 409],
|
2017-10-30 02:40:34 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the middleware using mocked dependencies.
|
|
|
|
*/
|
|
|
|
private function getMiddleware(): AccessingValidServer
|
|
|
|
{
|
2018-07-15 04:49:49 +00:00
|
|
|
return new AccessingValidServer($this->config, $this->response, $this->repository);
|
2017-10-30 02:40:34 +00:00
|
|
|
}
|
|
|
|
}
|