Skip to content

Commit e648e50

Browse files
committed
Write some example tests for @stanjg
1 parent 5a9d76a commit e648e50

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

app/Http/Middleware/MaintenanceMiddleware.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,40 @@
33
namespace Pterodactyl\Http\Middleware;
44

55
use Closure;
6+
use Illuminate\Contracts\Routing\ResponseFactory;
67

78
class MaintenanceMiddleware
89
{
10+
/**
11+
* @var \Illuminate\Contracts\Routing\ResponseFactory
12+
*/
13+
private $response;
14+
15+
/**
16+
* MaintenanceMiddleware constructor.
17+
*
18+
* @param \Illuminate\Contracts\Routing\ResponseFactory $response
19+
*/
20+
public function __construct(ResponseFactory $response)
21+
{
22+
$this->response = $response;
23+
}
24+
925
/**
1026
* Handle an incoming request.
1127
*
12-
* @param \Illuminate\Http\Request $request
13-
* @param \Closure $next
28+
* @param \Illuminate\Http\Request $request
29+
* @param \Closure $next
1430
* @return mixed
1531
*/
1632
public function handle($request, Closure $next)
1733
{
34+
/** @var \Pterodactyl\Models\Server $server */
1835
$server = $request->attributes->get('server');
19-
$node = $server->node;
36+
$node = $server->getRelation('node');
2037

2138
if ($node->maintenance) {
22-
return response(view('errors.maintenance'));
39+
return $this->response->view('errors.maintenance');
2340
}
2441

2542
return $next($request);

resources/lang/en/base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
],
2424
'maintenance' => [
2525
'header' => 'Node Under Maintenance',
26-
'title' => 'Maintenance',
27-
'desc' => 'This node is under maintenance, therefore your server can temporarily not be accessed.'
26+
'title' => 'Temporarily Unavailable',
27+
'desc' => 'This node is under maintenance, therefore your server can temporarily not be accessed.',
2828
],
2929
],
3030
'index' => [

resources/themes/pterodactyl/errors/maintenance.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;">@lang('base.errors.maintenance.title')</h1>
20+
<h1 class="text-red" style="font-size: 3em !important;font-weight: 100 !important;">@lang('base.errors.maintenance.title')</h1>
2121
<p class="text-muted">@lang('base.errors.maintenance.desc')</p>
2222
</div>
2323
<div class="box-footer with-border">
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Tests\Unit\Http\Middleware;
4+
5+
use Mockery as m;
6+
use Pterodactyl\Models\Node;
7+
use Illuminate\Http\Response;
8+
use Pterodactyl\Models\Server;
9+
use Illuminate\Contracts\Routing\ResponseFactory;
10+
use Pterodactyl\Http\Middleware\MaintenanceMiddleware;
11+
12+
class MaintenanceMiddlewareTest extends MiddlewareTestCase
13+
{
14+
/**
15+
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
16+
*/
17+
private $response;
18+
19+
/**
20+
* Setup tests.
21+
*/
22+
public function setUp()
23+
{
24+
parent::setUp();
25+
26+
$this->response = m::mock(ResponseFactory::class);
27+
}
28+
29+
/**
30+
* Test that a node not in maintenance mode continues through the request cycle.
31+
*/
32+
public function testHandle()
33+
{
34+
$server = factory(Server::class)->make();
35+
$node = factory(Node::class)->make(['maintenance' => 0]);
36+
37+
$server->setRelation('node', $node);
38+
$this->setRequestAttribute('server', $server);
39+
40+
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
41+
}
42+
43+
/**
44+
* Test that a node in maintenance mode returns an error view.
45+
*/
46+
public function testHandleInMaintenanceMode()
47+
{
48+
$server = factory(Server::class)->make();
49+
$node = factory(Node::class)->make(['maintenance' => 1]);
50+
51+
$server->setRelation('node', $node);
52+
$this->setRequestAttribute('server', $server);
53+
54+
$this->response->shouldReceive('view')
55+
->once()
56+
->with('errors.maintenance')
57+
->andReturn(new Response);
58+
59+
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
60+
$this->assertInstanceOf(Response::class, $response);
61+
}
62+
63+
/**
64+
* @return \Pterodactyl\Http\Middleware\MaintenanceMiddleware
65+
*/
66+
private function getMiddleware(): MaintenanceMiddleware
67+
{
68+
return new MaintenanceMiddleware($this->response);
69+
}
70+
}

0 commit comments

Comments
 (0)