|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Http\Middleware\Daemon; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Mockery as m; |
| 7 | +use Tests\TestCase; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Pterodactyl\Models\Node; |
| 10 | +use Symfony\Component\HttpFoundation\ParameterBag; |
| 11 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 12 | +use Pterodactyl\Http\Middleware\Daemon\DaemonAuthenticate; |
| 13 | +use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; |
| 14 | +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; |
| 15 | + |
| 16 | +class DaemonAuthenticateTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock |
| 20 | + */ |
| 21 | + private $repository; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Illuminate\Http\Request|\Mockery\Mock |
| 25 | + */ |
| 26 | + private $request; |
| 27 | + |
| 28 | + /** |
| 29 | + * Setup tests. |
| 30 | + */ |
| 31 | + public function setUp() |
| 32 | + { |
| 33 | + parent::setUp(); |
| 34 | + |
| 35 | + $this->repository = m::mock(NodeRepositoryInterface::class); |
| 36 | + $this->request = m::mock(Request::class); |
| 37 | + $this->request->attributes = new ParameterBag(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Test that if we are accessing the daemon.configuration route this middleware is not |
| 42 | + * applied in order to allow an unauthenticated request to use a token to grab data. |
| 43 | + */ |
| 44 | + public function testResponseShouldContinueIfRouteIsExempted() |
| 45 | + { |
| 46 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('daemon.configuration'); |
| 47 | + |
| 48 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test that not passing in the bearer token will result in a HTTP/401 error with the |
| 53 | + * proper response headers. |
| 54 | + */ |
| 55 | + public function testResponseShouldFailIfNoTokenIsProvided() |
| 56 | + { |
| 57 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.route'); |
| 58 | + $this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull(); |
| 59 | + |
| 60 | + try { |
| 61 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 62 | + } catch (HttpException $exception) { |
| 63 | + $this->assertEquals(401, $exception->getStatusCode(), 'Assert that a status code of 401 is returned.'); |
| 64 | + $this->assertTrue(is_array($exception->getHeaders()), 'Assert that an array of headers is returned.'); |
| 65 | + $this->assertArrayHasKey('WWW-Authenticate', $exception->getHeaders(), 'Assert exception headers contains WWW-Authenticate.'); |
| 66 | + $this->assertEquals('Bearer', $exception->getHeaders()['WWW-Authenticate']); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test that passing in an invalid node daemon secret will result in a HTTP/403 |
| 72 | + * error response. |
| 73 | + */ |
| 74 | + public function testResponseShouldFailIfNoNodeIsFound() |
| 75 | + { |
| 76 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.route'); |
| 77 | + $this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturn('test1234'); |
| 78 | + |
| 79 | + $this->repository->shouldReceive('findFirstWhere')->with([['daemonSecret', '=', 'test1234']])->once()->andThrow(new RecordNotFoundException); |
| 80 | + |
| 81 | + try { |
| 82 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 83 | + } catch (HttpException $exception) { |
| 84 | + $this->assertEquals(403, $exception->getStatusCode(), 'Assert that a status code of 403 is returned.'); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test a successful middleware process. |
| 90 | + */ |
| 91 | + public function testSuccessfulMiddlewareProcess() |
| 92 | + { |
| 93 | + $model = factory(Node::class)->make(); |
| 94 | + |
| 95 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.route'); |
| 96 | + $this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturn($model->daemonSecret); |
| 97 | + |
| 98 | + $this->repository->shouldReceive('findFirstWhere')->with([['daemonSecret', '=', $model->daemonSecret]])->once()->andReturn($model); |
| 99 | + |
| 100 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 101 | + $this->assertTrue($this->request->attributes->has('node'), 'Assert request attributes contains node.'); |
| 102 | + $this->assertSame($model, $this->request->attributes->get('node')); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Return an instance of the middleware using mocked dependencies. |
| 107 | + * |
| 108 | + * @return \Pterodactyl\Http\Middleware\Daemon\DaemonAuthenticate |
| 109 | + */ |
| 110 | + private function getMiddleware(): DaemonAuthenticate |
| 111 | + { |
| 112 | + return new DaemonAuthenticate($this->repository); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Provide a closure to be used when validating that the response from the middleware |
| 117 | + * is the same request object we passed into it. |
| 118 | + */ |
| 119 | + private function getClosureAssertions(): Closure |
| 120 | + { |
| 121 | + return function ($response) { |
| 122 | + $this->assertInstanceOf(Request::class, $response); |
| 123 | + $this->assertSame($this->request, $response); |
| 124 | + }; |
| 125 | + } |
| 126 | +} |
0 commit comments