2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Exceptions;
|
|
|
|
|
|
|
|
use App\Exceptions\Handler;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Contracts\Container\Container;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \App\Exceptions\Handler
|
|
|
|
*/
|
|
|
|
class HandlerTest extends TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*
|
|
|
|
* @dataProvider provideExceptionsforBadRequest
|
|
|
|
*/
|
|
|
|
public function test_exceptions_returns_badRequest_json_response($exception)
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$instance = new Handler($this->createMock(Container::class));
|
|
|
|
$class = new \ReflectionClass(Handler::class);
|
|
|
|
|
|
|
|
$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([
|
|
|
|
'message'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide Valid data for validation test
|
|
|
|
*/
|
|
|
|
public function provideExceptionsforBadRequest() : array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'\App\Exceptions\InvalidOtpParameterException'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'\App\Exceptions\InvalidQrCodeException'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'\App\Exceptions\InvalidSecretException'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'\App\Exceptions\DbEncryptionException'
|
|
|
|
],
|
2022-06-21 17:27:47 +02:00
|
|
|
[
|
2022-10-07 18:58:48 +02:00
|
|
|
'\App\Exceptions\InvalidMigrationDataException'
|
2022-06-21 17:27:47 +02:00
|
|
|
],
|
2022-07-05 10:10:24 +02:00
|
|
|
[
|
|
|
|
'\App\Exceptions\UndecipherableException'
|
|
|
|
],
|
2022-10-07 18:58:48 +02:00
|
|
|
[
|
|
|
|
'\App\Exceptions\UnsupportedMigrationException'
|
|
|
|
],
|
2022-07-05 10:10:24 +02:00
|
|
|
[
|
|
|
|
'\App\Exceptions\UnsupportedOtpTypeException'
|
|
|
|
],
|
2021-11-30 17:39:33 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*
|
|
|
|
* @dataProvider provideExceptionsforNotFound
|
|
|
|
*/
|
|
|
|
public function test_exceptions_returns_notFound_json_response($exception)
|
|
|
|
{
|
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
$instance = new Handler($this->createMock(Container::class));
|
|
|
|
$class = new \ReflectionClass(Handler::class);
|
|
|
|
|
|
|
|
$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([
|
|
|
|
'message'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide Valid data for validation test
|
|
|
|
*/
|
|
|
|
public function provideExceptionsforNotFound() : array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'\Illuminate\Database\Eloquent\ModelNotFoundException'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2022-07-05 10:10:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_authenticationException_returns_proxyAuthRequired_json_response_with_proxy_guard()
|
|
|
|
{
|
|
|
|
$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(['reverse-proxy-guard']);
|
|
|
|
|
|
|
|
$response = $method->invokeArgs($instance, [$request, $mockException]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response);
|
|
|
|
|
|
|
|
$response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
|
|
|
|
$response->assertStatus(407)
|
|
|
|
->assertJsonStructure([
|
|
|
|
'message'
|
|
|
|
]);
|
|
|
|
}
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|