Files
panel/tests/Unit/Http/Middleware/AdminAuthenticateTest.php

57 lines
1.6 KiB
PHP
Raw Normal View History

2017-11-03 18:16:49 -05:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Unit\Http\Middleware;
2017-11-03 18:16:49 -05:00
2024-03-12 22:39:16 -04:00
use App\Models\User;
use App\Http\Middleware\AdminAuthenticate;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
2017-11-03 18:16:49 -05:00
class AdminAuthenticateTest extends MiddlewareTestCase
{
/**
* Test that an admin is authenticated.
*/
2024-03-19 21:12:27 -04:00
public function testAdminsAreAuthenticated(): void
2017-11-03 18:16:49 -05:00
{
$user = User::factory()->make(['root_admin' => 1]);
2017-11-03 18:16:49 -05: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.
*/
2024-03-19 21:12:27 -04:00
public function testExceptionIsThrownIfUserDoesNotExist(): void
2017-11-03 18:16:49 -05:00
{
$this->expectException(AccessDeniedHttpException::class);
2017-11-03 18:16:49 -05:00
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
2017-11-03 18:16:49 -05:00
}
/**
* Test that an exception is thrown if the user is not an admin.
*/
2024-03-19 21:12:27 -04:00
public function testExceptionIsThrownIfUserIsNotAnAdmin(): void
2017-11-03 18:16:49 -05:00
{
$this->expectException(AccessDeniedHttpException::class);
$user = User::factory()->make(['root_admin' => 0]);
2017-11-03 18:16:49 -05:00
$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
2017-11-03 18:16:49 -05:00
}
/**
* Return an instance of the middleware using mocked dependencies.
*/
private function getMiddleware(): AdminAuthenticate
{
return new AdminAuthenticate();
}
}