Write some example tests for @stanjg
This commit is contained in:
parent
5a9d76a7e1
commit
e648e50d90
4 changed files with 94 additions and 7 deletions
|
@ -3,23 +3,40 @@
|
|||
namespace Pterodactyl\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
|
||||
class MaintenanceMiddleware
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Routing\ResponseFactory
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* MaintenanceMiddleware constructor.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Routing\ResponseFactory $response
|
||||
*/
|
||||
public function __construct(ResponseFactory $response)
|
||||
{
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
/** @var \Pterodactyl\Models\Server $server */
|
||||
$server = $request->attributes->get('server');
|
||||
$node = $server->node;
|
||||
$node = $server->getRelation('node');
|
||||
|
||||
if ($node->maintenance) {
|
||||
return response(view('errors.maintenance'));
|
||||
return $this->response->view('errors.maintenance');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
|
|
|
@ -23,8 +23,8 @@ return [
|
|||
],
|
||||
'maintenance' => [
|
||||
'header' => 'Node Under Maintenance',
|
||||
'title' => 'Maintenance',
|
||||
'desc' => 'This node is under maintenance, therefore your server can temporarily not be accessed.'
|
||||
'title' => 'Temporarily Unavailable',
|
||||
'desc' => 'This node is under maintenance, therefore your server can temporarily not be accessed.',
|
||||
],
|
||||
],
|
||||
'index' => [
|
||||
|
|
|
@ -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;">@lang('base.errors.maintenance.title')</h1>
|
||||
<h1 class="text-red" style="font-size: 3em !important;font-weight: 100 !important;">@lang('base.errors.maintenance.title')</h1>
|
||||
<p class="text-muted">@lang('base.errors.maintenance.desc')</p>
|
||||
</div>
|
||||
<div class="box-footer with-border">
|
||||
|
|
70
tests/Unit/Http/Middleware/MaintenanceMiddlewareTest.php
Normal file
70
tests/Unit/Http/Middleware/MaintenanceMiddlewareTest.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Http\Middleware;
|
||||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Contracts\Routing\ResponseFactory;
|
||||
use Pterodactyl\Http\Middleware\MaintenanceMiddleware;
|
||||
|
||||
class MaintenanceMiddlewareTest extends MiddlewareTestCase
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Mockery\Mock
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->response = m::mock(ResponseFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a node not in maintenance mode continues through the request cycle.
|
||||
*/
|
||||
public function testHandle()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$node = factory(Node::class)->make(['maintenance' => 0]);
|
||||
|
||||
$server->setRelation('node', $node);
|
||||
$this->setRequestAttribute('server', $server);
|
||||
|
||||
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a node in maintenance mode returns an error view.
|
||||
*/
|
||||
public function testHandleInMaintenanceMode()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$node = factory(Node::class)->make(['maintenance' => 1]);
|
||||
|
||||
$server->setRelation('node', $node);
|
||||
$this->setRequestAttribute('server', $server);
|
||||
|
||||
$this->response->shouldReceive('view')
|
||||
->once()
|
||||
->with('errors.maintenance')
|
||||
->andReturn(new Response);
|
||||
|
||||
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Pterodactyl\Http\Middleware\MaintenanceMiddleware
|
||||
*/
|
||||
private function getMiddleware(): MaintenanceMiddleware
|
||||
{
|
||||
return new MaintenanceMiddleware($this->response);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue