2018-01-18 21:41:45 -06:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Tests\Unit\Http\Middleware\Api\Application;
|
2018-01-18 21:41:45 -06:00
|
|
|
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Http\Middleware\Api\Application\AuthenticateApplicationUser;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Tests\Unit\Http\Middleware\MiddlewareTestCase;
|
2020-06-23 21:59:37 -07:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2018-01-18 21:41:45 -06:00
|
|
|
|
|
|
|
|
class AuthenticateUserTest extends MiddlewareTestCase
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Test that no user defined results in an access denied exception.
|
|
|
|
|
*/
|
2025-02-25 14:22:07 +01:00
|
|
|
public function test_no_user_defined(): void
|
2018-01-18 21:41:45 -06:00
|
|
|
{
|
2020-06-23 21:59:37 -07:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
|
|
2018-01-18 21:41:45 -06:00
|
|
|
$this->setRequestUserModel(null);
|
|
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-10-14 10:59:20 -06:00
|
|
|
* Test that a non-admin user results in an exception.
|
2018-01-18 21:41:45 -06:00
|
|
|
*/
|
2025-02-25 14:22:07 +01:00
|
|
|
public function test_non_admin_user(): void
|
2018-01-18 21:41:45 -06:00
|
|
|
{
|
2020-06-23 21:59:37 -07:00
|
|
|
$this->expectException(AccessDeniedHttpException::class);
|
|
|
|
|
|
2025-10-07 23:41:28 +02:00
|
|
|
$this->generateRequestUserModel(false, false);
|
2018-01-18 21:41:45 -06:00
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that an admin user continues though the middleware.
|
|
|
|
|
*/
|
2025-02-25 14:22:07 +01:00
|
|
|
public function test_admin_user(): void
|
2018-01-18 21:41:45 -06:00
|
|
|
{
|
2025-10-07 23:41:28 +02:00
|
|
|
$this->generateRequestUserModel(true, false);
|
|
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that a root admin user continues though the middleware.
|
|
|
|
|
*/
|
|
|
|
|
public function test_root_admin_user(): void
|
|
|
|
|
{
|
|
|
|
|
$this->generateRequestUserModel(true, true);
|
2018-01-18 21:41:45 -06:00
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an instance of the middleware for testing.
|
|
|
|
|
*/
|
2018-02-25 15:30:56 -06:00
|
|
|
private function getMiddleware(): AuthenticateApplicationUser
|
2018-01-18 21:41:45 -06:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
return new AuthenticateApplicationUser();
|
2018-01-18 21:41:45 -06:00
|
|
|
}
|
|
|
|
|
}
|