Skip to content

Commit 71b9065

Browse files
committed
Fix failing test suite
1 parent 99aceac commit 71b9065

File tree

5 files changed

+41
-46
lines changed

5 files changed

+41
-46
lines changed

app/Http/Middleware/Server/AccessingValidServer.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use Illuminate\Http\Request;
77
use Pterodactyl\Models\Server;
88
use Illuminate\Contracts\Session\Session;
9+
use Illuminate\Contracts\Routing\ResponseFactory;
910
use Illuminate\Contracts\Config\Repository as ConfigRepository;
1011
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
11-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12+
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
1213
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1314

1415
class AccessingValidServer
@@ -23,6 +24,11 @@ class AccessingValidServer
2324
*/
2425
private $repository;
2526

27+
/**
28+
* @var \Illuminate\Contracts\Routing\ResponseFactory
29+
*/
30+
private $response;
31+
2632
/**
2733
* @var \Illuminate\Contracts\Session\Session
2834
*/
@@ -32,16 +38,19 @@ class AccessingValidServer
3238
* AccessingValidServer constructor.
3339
*
3440
* @param \Illuminate\Contracts\Config\Repository $config
41+
* @param \Illuminate\Contracts\Routing\ResponseFactory $response
3542
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
3643
* @param \Illuminate\Contracts\Session\Session $session
3744
*/
3845
public function __construct(
3946
ConfigRepository $config,
47+
ResponseFactory $response,
4048
ServerRepositoryInterface $repository,
4149
Session $session
4250
) {
4351
$this->config = $config;
4452
$this->repository = $repository;
53+
$this->response = $response;
4554
$this->session = $session;
4655
}
4756

@@ -63,30 +72,22 @@ public function handle(Request $request, Closure $next)
6372
$isApiRequest = $request->expectsJson() || $request->is(...$this->config->get('pterodactyl.json_routes', []));
6473
$server = $this->repository->getByUuid($attributes instanceof Server ? $attributes->uuid : $attributes);
6574

66-
if (! $server) {
67-
if ($isApiRequest) {
68-
throw new NotFoundHttpException('The requested server was not found on the system.');
69-
}
70-
71-
return response()->view('errors.404', [], 404);
72-
}
73-
7475
if ($server->suspended) {
7576
if ($isApiRequest) {
76-
throw new AccessDeniedHttpException('Server is suspended.');
77+
throw new AccessDeniedHttpException('Server is suspended and cannot be accessed.');
7778
}
7879

79-
return response()->view('errors.suspended', [], 403);
80+
return $this->response->view('errors.suspended', [], 403);
8081
}
8182

8283
// Servers can have install statuses other than 1 or 0, so don't check
8384
// for a bool-type operator here.
8485
if ($server->installed !== 1) {
8586
if ($isApiRequest) {
86-
throw new AccessDeniedHttpException('Server is not marked as installed.');
87+
throw new ConflictHttpException('Server is still completing the installation process.');
8788
}
8889

89-
return response()->view('errors.installing', [], 403);
90+
return $this->response->view('errors.installing', [], 409);
9091
}
9192

9293
// Store the server in the session.

app/Services/Servers/ServerConfigurationStructureService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function handle(Server $server): array
5454
$server = $this->repository->getDataForCreation($server);
5555
}
5656

57+
$pack = $server->getRelation('pack');
58+
if (! is_null($pack)) {
59+
$pack = $server->getRelation('pack')->uuid;
60+
}
61+
5762
return [
5863
'uuid' => $server->uuid,
5964
'build' => [
@@ -74,7 +79,7 @@ public function handle(Server $server): array
7479
],
7580
'service' => [
7681
'egg' => $server->egg->uuid,
77-
'pack' => object_get($server, 'pack.uuid'),
82+
'pack' => $pack,
7883
'skip_scripts' => $server->skip_scripts,
7984
],
8085
'rebuild' => false,

resources/themes/pterodactyl/errors/suspended.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12">
1818
<div class="box box-danger">
1919
<div class="box-body text-center">
20-
<h1 class="text-red" style="font-size: 160px !important;font-weight: 100 !important;">401</h1>
20+
<h1 class="text-red" style="font-size: 160px !important;font-weight: 100 !important;">403</h1>
2121
<p class="text-muted">@lang('base.errors.suspended.desc')</p>
2222
</div>
2323
<div class="box-footer with-border">

tests/Unit/Http/Middleware/Server/AccessingValidServerTest.php

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Tests\Unit\Http\Middleware\Server;
44

55
use Mockery as m;
6-
use Illuminate\Http\Response;
76
use Pterodactyl\Models\Server;
87
use Illuminate\Contracts\Session\Session;
98
use Illuminate\Contracts\Config\Repository;
9+
use Illuminate\Contracts\Routing\ResponseFactory;
1010
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
1111
use Pterodactyl\Http\Middleware\AccessingValidServer;
1212
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@@ -23,6 +23,11 @@ class AccessingValidServerTest extends MiddlewareTestCase
2323
*/
2424
private $repository;
2525

26+
/**
27+
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
28+
*/
29+
private $response;
30+
2631
/**
2732
* @var \Illuminate\Contracts\Session\Session|\Mockery\Mock
2833
*/
@@ -37,30 +42,15 @@ public function setUp()
3742

3843
$this->config = m::mock(Repository::class);
3944
$this->repository = m::mock(ServerRepositoryInterface::class);
45+
$this->response = m::mock(ResponseFactory::class);
4046
$this->session = m::mock(Session::class);
4147
}
4248

43-
/**
44-
* Test that an exception is thrown if the request is an API request and no server is found.
45-
*
46-
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
47-
* @expectedExceptionMessage The requested server was not found on the system.
48-
*/
49-
public function testExceptionIsThrownIfNoServerIsFoundAndIsAPIRequest()
50-
{
51-
$this->request->shouldReceive('route->parameter')->with('server')->once()->andReturn('123456');
52-
$this->request->shouldReceive('expectsJson')->withNoArgs()->once()->andReturn(true);
53-
54-
$this->repository->shouldReceive('getByUuid')->with('123456')->once()->andReturnNull();
55-
56-
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
57-
}
58-
5949
/**
6050
* Test that an exception is thrown if the request is an API request and the server is suspended.
6151
*
6252
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
63-
* @expectedExceptionMessage Server is suspended.
53+
* @expectedExceptionMessage Server is suspended and cannot be accessed.
6454
*/
6555
public function testExceptionIsThrownIfServerIsSuspended()
6656
{
@@ -77,8 +67,8 @@ public function testExceptionIsThrownIfServerIsSuspended()
7767
/**
7868
* Test that an exception is thrown if the request is an API request and the server is not installed.
7969
*
80-
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
81-
* @expectedExceptionMessage Server is not marked as installed.
70+
* @expectedException \Symfony\Component\HttpKernel\Exception\ConflictHttpException
71+
* @expectedExceptionMessage Server is still completing the installation process.
8272
*/
8373
public function testExceptionIsThrownIfServerIsNotInstalled()
8474
{
@@ -97,19 +87,18 @@ public function testExceptionIsThrownIfServerIsNotInstalled()
9787
*
9888
* @dataProvider viewDataProvider
9989
*/
100-
public function testCorrectErrorPagesAreRendered(Server $model = null, string $page, int $httpCode)
90+
public function testCorrectErrorPagesAreRendered(Server $model, string $page, int $httpCode)
10191
{
10292
$this->request->shouldReceive('route->parameter')->with('server')->once()->andReturn('123456');
10393
$this->request->shouldReceive('expectsJson')->withNoArgs()->once()->andReturn(false);
10494
$this->config->shouldReceive('get')->with('pterodactyl.json_routes', [])->once()->andReturn([]);
10595
$this->request->shouldReceive('is')->with(...[])->once()->andReturn(false);
10696

10797
$this->repository->shouldReceive('getByUuid')->with('123456')->once()->andReturn($model);
98+
$this->response->shouldReceive('view')->with($page, [], $httpCode)->once()->andReturn(true);
10899

109100
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
110-
$this->assertInstanceOf(Response::class, $response);
111-
$this->assertEquals($page, $response->getOriginalContent()->getName(), 'Assert that the correct view is returned.');
112-
$this->assertEquals($httpCode, $response->getStatusCode(), 'Assert that the correct HTTP code is returned.');
101+
$this->assertTrue($response);
113102
}
114103

115104
/**
@@ -143,10 +132,9 @@ public function viewDataProvider(): array
143132
$this->refreshApplication();
144133

145134
return [
146-
[null, 'errors.404', 404],
147135
[factory(Server::class)->make(['suspended' => 1]), 'errors.suspended', 403],
148-
[factory(Server::class)->make(['installed' => 0]), 'errors.installing', 403],
149-
[factory(Server::class)->make(['installed' => 2]), 'errors.installing', 403],
136+
[factory(Server::class)->make(['installed' => 0]), 'errors.installing', 409],
137+
[factory(Server::class)->make(['installed' => 2]), 'errors.installing', 409],
150138
];
151139
}
152140

@@ -157,6 +145,6 @@ public function viewDataProvider(): array
157145
*/
158146
private function getMiddleware(): AccessingValidServer
159147
{
160-
return new AccessingValidServer($this->config, $this->repository, $this->session);
148+
return new AccessingValidServer($this->config, $this->response, $this->repository, $this->session);
161149
}
162150
}

tests/Unit/Services/Servers/ServerConfigurationStructureServiceTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ public function setUp()
4141
public function testCorrectStructureIsReturned()
4242
{
4343
$model = factory(Server::class)->make();
44-
$model->allocation = factory(Allocation::class)->make();
45-
$model->allocations = collect(factory(Allocation::class)->times(2)->make());
46-
$model->egg = factory(Egg::class)->make();
44+
$model->setRelation('pack', null);
45+
$model->setRelation('allocation', factory(Allocation::class)->make());
46+
$model->setRelation('allocations', collect(factory(Allocation::class)->times(2)->make()));
47+
$model->setRelation('egg', factory(Egg::class)->make());
4748

4849
$portListing = $model->allocations->groupBy('ip')->map(function ($item) {
4950
return $item->pluck('port');

0 commit comments

Comments
 (0)