2FAuth/tests/Unit/Exceptions/HandlerTest.php

204 lines
6.2 KiB
PHP
Raw Normal View History

2021-11-30 17:39:33 +01:00
<?php
namespace Tests\Unit\Exceptions;
2022-12-13 12:07:29 +01:00
use App\Exceptions\DbEncryptionException;
use App\Exceptions\EncryptedMigrationException;
2021-11-30 17:39:33 +01:00
use App\Exceptions\Handler;
2022-12-13 12:07:29 +01:00
use App\Exceptions\InvalidMigrationDataException;
2022-12-09 10:52:17 +01:00
use App\Exceptions\InvalidOtpParameterException;
2022-12-13 12:07:29 +01:00
use App\Exceptions\InvalidQrCodeException;
2022-12-09 10:52:17 +01:00
use App\Exceptions\InvalidSecretException;
use App\Exceptions\UndecipherableException;
use App\Exceptions\UnsupportedMigrationException;
use App\Exceptions\UnsupportedOtpTypeException;
2022-12-13 12:07:29 +01:00
use Illuminate\Contracts\Container\Container;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
2023-08-01 11:28:27 +02:00
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
2022-12-13 12:07:29 +01:00
use Tests\TestCase;
2021-11-30 17:39:33 +01:00
/**
2023-08-01 11:28:27 +02:00
* HandlerTest test class
2021-11-30 17:39:33 +01:00
*/
2023-08-01 11:28:27 +02:00
#[CoversClass(Handler::class)]
2021-11-30 17:39:33 +01:00
class HandlerTest extends TestCase
{
/**
2022-11-22 15:15:52 +01:00
* @test
*/
2023-08-01 11:28:27 +02:00
#[DataProvider('provideExceptionsforBadRequest')]
2021-11-30 17:39:33 +01:00
public function test_exceptions_returns_badRequest_json_response($exception)
{
2022-11-22 15:15:52 +01:00
$request = $this->createMock(Request::class);
2021-11-30 17:39:33 +01:00
$instance = new Handler($this->createMock(Container::class));
2022-11-22 15:15:52 +01:00
$class = new \ReflectionClass(Handler::class);
2021-11-30 17:39:33 +01:00
$method = $class->getMethod('render');
$method->setAccessible(true);
$response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
$this->assertInstanceOf(JsonResponse::class, $response);
$response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
$response->assertStatus(400)
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-30 17:39:33 +01:00
]);
}
/**
* Provide Valid data for validation test
*/
2023-08-01 11:28:27 +02:00
public static function provideExceptionsforBadRequest() : array
2021-11-30 17:39:33 +01:00
{
return [
[
2022-12-09 10:52:17 +01:00
InvalidOtpParameterException::class,
2021-11-30 17:39:33 +01:00
],
[
2022-12-09 10:52:17 +01:00
InvalidQrCodeException::class,
2021-11-30 17:39:33 +01:00
],
[
2022-12-09 10:52:17 +01:00
InvalidSecretException::class,
2021-11-30 17:39:33 +01:00
],
[
2022-12-09 10:52:17 +01:00
DbEncryptionException::class,
2021-11-30 17:39:33 +01:00
],
[
2022-12-09 10:52:17 +01:00
InvalidMigrationDataException::class,
],
[
2022-12-09 10:52:17 +01:00
UndecipherableException::class,
],
[
2022-12-09 10:52:17 +01:00
UnsupportedMigrationException::class,
],
[
2022-12-09 10:52:17 +01:00
UnsupportedOtpTypeException::class,
],
[
EncryptedMigrationException::class,
],
2021-11-30 17:39:33 +01:00
];
}
/**
2022-11-22 15:15:52 +01:00
* @test
*/
2023-08-01 11:28:27 +02:00
#[DataProvider('provideExceptionsforNotFound')]
2021-11-30 17:39:33 +01:00
public function test_exceptions_returns_notFound_json_response($exception)
{
2022-11-22 15:15:52 +01:00
$request = $this->createMock(Request::class);
2021-11-30 17:39:33 +01:00
$instance = new Handler($this->createMock(Container::class));
2022-11-22 15:15:52 +01:00
$class = new \ReflectionClass(Handler::class);
2021-11-30 17:39:33 +01:00
$method = $class->getMethod('render');
$method->setAccessible(true);
$response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
$this->assertInstanceOf(JsonResponse::class, $response);
$response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
$response->assertStatus(404)
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
2021-11-30 17:39:33 +01:00
]);
}
/**
* Provide Valid data for validation test
*/
2023-08-01 11:28:27 +02:00
public static function provideExceptionsforNotFound() : array
2021-11-30 17:39:33 +01:00
{
return [
[
2022-11-22 15:15:52 +01:00
'\Illuminate\Database\Eloquent\ModelNotFoundException',
2021-11-30 17:39:33 +01:00
],
[
2022-11-22 15:15:52 +01:00
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException',
2021-11-30 17:39:33 +01:00
],
];
}
2022-12-09 10:52:17 +01:00
/**
* @test
*/
public function test_authenticationException_returns_unauthorized_json_response()
{
$request = $this->createMock(Request::class);
$instance = new Handler($this->createMock(Container::class));
$class = new \ReflectionClass(Handler::class);
$method = $class->getMethod('render');
$method->setAccessible(true);
$mockException = $this->createMock(\Illuminate\Auth\AuthenticationException::class);
$mockException->method('guards')->willReturn(['web-guard']);
$response = $method->invokeArgs($instance, [$request, $mockException]);
$this->assertInstanceOf(JsonResponse::class, $response);
$response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
$response->assertStatus(401)
->assertJsonStructure([
'message',
]);
}
/**
2022-11-22 15:15:52 +01:00
* @test
*/
public function test_authenticationException_returns_proxyAuthRequired_json_response_with_proxy_guard()
{
2022-11-22 15:15:52 +01:00
$request = $this->createMock(Request::class);
$instance = new Handler($this->createMock(Container::class));
2022-11-22 15:15:52 +01:00
$class = new \ReflectionClass(Handler::class);
$method = $class->getMethod('render');
$method->setAccessible(true);
$mockException = $this->createMock(\Illuminate\Auth\AuthenticationException::class);
2022-11-22 15:15:52 +01:00
$mockException->method('guards')->willReturn(['reverse-proxy-guard']);
$response = $method->invokeArgs($instance, [$request, $mockException]);
$this->assertInstanceOf(JsonResponse::class, $response);
$response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
$response->assertStatus(407)
->assertJsonStructure([
2022-11-22 15:15:52 +01:00
'message',
]);
}
2023-03-18 17:33:43 +01:00
/**
* @test
*/
public function test_AccessDeniedException_returns_forbidden_json_response()
{
$request = $this->createMock(Request::class);
$instance = new Handler($this->createMock(Container::class));
$class = new \ReflectionClass(Handler::class);
$method = $class->getMethod('render');
$method->setAccessible(true);
$mockException = $this->createMock(\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException::class);
$response = $method->invokeArgs($instance, [$request, $mockException]);
$this->assertInstanceOf(JsonResponse::class, $response);
$response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
$response->assertStatus(403)
->assertJsonStructure([
'message',
]);
}
2022-11-22 15:15:52 +01:00
}