Path: blob/1.0-develop/tests/Unit/Http/Middleware/AdminAuthenticateTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Unit\Http\Middleware;34use Pterodactyl\Models\User;5use Pterodactyl\Http\Middleware\AdminAuthenticate;6use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;78class AdminAuthenticateTest extends MiddlewareTestCase9{10/**11* Test that an admin is authenticated.12*/13public function testAdminsAreAuthenticated()14{15$user = User::factory()->make(['root_admin' => 1]);1617$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);1819$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());20}2122/**23* Test that a missing user in the request triggers an error.24*/25public function testExceptionIsThrownIfUserDoesNotExist()26{27$this->expectException(AccessDeniedHttpException::class);2829$this->request->shouldReceive('user')->withNoArgs()->once()->andReturnNull();3031$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());32}3334/**35* Test that an exception is thrown if the user is not an admin.36*/37public function testExceptionIsThrownIfUserIsNotAnAdmin()38{39$this->expectException(AccessDeniedHttpException::class);4041$user = User::factory()->make(['root_admin' => 0]);4243$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);4445$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());46}4748/**49* Return an instance of the middleware using mocked dependencies.50*/51private function getMiddleware(): AdminAuthenticate52{53return new AdminAuthenticate();54}55}565758