2017-11-03 23:16:49 +00:00
|
|
|
<?php
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
namespace Pterodactyl\Tests\Unit\Http\Middleware;
|
2017-11-03 23:16:49 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
use Pterodactyl\Http\Middleware\AdminAuthenticate;
|
2020-06-24 04:59:37 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2017-11-03 23:16:49 +00:00
|
|
|
|
|
|
|
class AdminAuthenticateTest extends MiddlewareTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test that an admin is authenticated.
|
|
|
|
*/
|
|
|
|
public function testAdminsAreAuthenticated()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->make(['root_admin' => 1]);
|
2017-11-03 23:16:49 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a missing user in the request triggers an error.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfUserDoesNotExist()
|
|
|
|
{
|
2020-06-24 04:59:37 +00:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
|
2017-11-03 23:16:49 +00:00
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
2017-11-03 23:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the user is not an admin.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfUserIsNotAnAdmin()
|
|
|
|
{
|
2020-06-24 04:59:37 +00:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$user = User::factory()->make(['root_admin' => 0]);
|
2017-11-03 23:16:49 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);
|
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
2017-11-03 23:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the middleware using mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Http\Middleware\AdminAuthenticate
|
|
|
|
*/
|
|
|
|
private function getMiddleware(): AdminAuthenticate
|
|
|
|
{
|
|
|
|
return new AdminAuthenticate();
|
|
|
|
}
|
|
|
|
}
|