|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Http\Middleware; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Pterodactyl\Models\Node; |
| 7 | +use Pterodactyl\Http\Middleware\DaemonAuthenticate; |
| 8 | +use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; |
| 9 | + |
| 10 | +class DaemonAuthenticateTest extends MiddlewareTestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface|\Mockery\Mock |
| 14 | + */ |
| 15 | + private $repository; |
| 16 | + |
| 17 | + /** |
| 18 | + * Setup tests. |
| 19 | + */ |
| 20 | + public function setUp() |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + $this->repository = m::mock(NodeRepositoryInterface::class); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test a valid daemon connection. |
| 29 | + */ |
| 30 | + public function testValidDaemonConnection() |
| 31 | + { |
| 32 | + $node = factory(Node::class)->make(); |
| 33 | + |
| 34 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.name'); |
| 35 | + $this->request->shouldReceive('header')->with('X-Access-Node')->twice()->andReturn($node->uuid); |
| 36 | + |
| 37 | + $this->repository->shouldReceive('findWhere')->with(['daemonSecret' => $node->uuid])->once()->andReturn($node); |
| 38 | + |
| 39 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 40 | + $this->assertRequestHasAttribute('node'); |
| 41 | + $this->assertRequestAttributeEquals($node, 'node'); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test that ignored routes do not continue through the middleware. |
| 46 | + */ |
| 47 | + public function testIgnoredRouteShouldContinue() |
| 48 | + { |
| 49 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('daemon.configuration'); |
| 50 | + |
| 51 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 52 | + $this->assertRequestMissingAttribute('node'); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test that a request missing a X-Access-Node header causes an exception. |
| 57 | + * |
| 58 | + * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException |
| 59 | + */ |
| 60 | + public function testExceptionThrownIfMissingHeader() |
| 61 | + { |
| 62 | + $this->request->shouldReceive('route->getName')->withNoArgs()->once()->andReturn('random.name'); |
| 63 | + $this->request->shouldReceive('header')->with('X-Access-Node')->once()->andReturn(false); |
| 64 | + |
| 65 | + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Return an instance of the middleware using mocked dependencies. |
| 70 | + * |
| 71 | + * @return \Pterodactyl\Http\Middleware\DaemonAuthenticate |
| 72 | + */ |
| 73 | + private function getMiddleware(): DaemonAuthenticate |
| 74 | + { |
| 75 | + return new DaemonAuthenticate($this->repository); |
| 76 | + } |
| 77 | +} |
0 commit comments