misc_pterodactyl-panel/tests/Unit/Http/Middleware/Api/Daemon/DaemonAuthenticateTest.php

103 lines
3.9 KiB
PHP
Raw Normal View History

2017-10-30 02:40:34 +00:00
<?php
namespace Tests\Unit\Http\Middleware\Api\Daemon;
2017-10-30 02:40:34 +00:00
use Mockery as m;
use Pterodactyl\Models\Node;
2017-11-02 01:45:43 +00:00
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
2017-10-30 02:40:34 +00:00
use Symfony\Component\HttpKernel\Exception\HttpException;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Http\Middleware\Api\Daemon\DaemonAuthenticate;
2017-10-30 02:40:34 +00:00
2017-11-02 01:45:43 +00:00
class DaemonAuthenticateTest extends MiddlewareTestCase
2017-10-30 02:40:34 +00:00
{
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
2017-10-30 02:40:34 +00:00
{
parent::setUp();
$this->repository = m::mock(NodeRepositoryInterface::class);
}
/**
* Test that if we are accessing the daemon.configuration route this middleware is not
* applied in order to allow an unauthenticated request to use a token to grab data.
*/
public function testResponseShouldContinueIfRouteIsExempted()
{
$this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('daemon.configuration');
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
}
/**
* Test that not passing in the bearer token will result in a HTTP/401 error with the
* proper response headers.
*/
public function testResponseShouldFailIfNoTokenIsProvided()
{
$this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.route');
$this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull();
try {
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
} catch (HttpException $exception) {
$this->assertEquals(401, $exception->getStatusCode(), 'Assert that a status code of 401 is returned.');
$this->assertTrue(is_array($exception->getHeaders()), 'Assert that an array of headers is returned.');
$this->assertArrayHasKey('WWW-Authenticate', $exception->getHeaders(), 'Assert exception headers contains WWW-Authenticate.');
$this->assertEquals('Bearer', $exception->getHeaders()['WWW-Authenticate']);
}
}
/**
* Test that passing in an invalid node daemon secret will result in a HTTP/403
* error response.
*
* @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
2017-10-30 02:40:34 +00:00
*/
public function testResponseShouldFailIfNoNodeIsFound()
{
$this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.route');
$this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturn('test1234');
$this->repository->shouldReceive('findFirstWhere')->with([['daemonSecret', '=', 'test1234']])->once()->andThrow(new RecordNotFoundException);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
2017-10-30 02:40:34 +00:00
}
/**
* Test a successful middleware process.
*/
public function testSuccessfulMiddlewareProcess()
{
$model = factory(Node::class)->make();
$this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.route');
$this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturn($model->daemonSecret);
$this->repository->shouldReceive('findFirstWhere')->with([['daemonSecret', '=', $model->daemonSecret]])->once()->andReturn($model);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
2017-11-02 01:45:43 +00:00
$this->assertRequestHasAttribute('node');
$this->assertRequestAttributeEquals($model, 'node');
2017-10-30 02:40:34 +00:00
}
/**
* Return an instance of the middleware using mocked dependencies.
*
* @return \Pterodactyl\Http\Middleware\Api\Daemon\DaemonAuthenticate
2017-10-30 02:40:34 +00:00
*/
private function getMiddleware(): DaemonAuthenticate
{
return new DaemonAuthenticate($this->repository);
}
}