123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace Tests\Unit\Exceptions;
- use App\Exceptions\Handler;
- use Illuminate\Contracts\Container\Container;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Tests\TestCase;
- use App\Exceptions\InvalidOtpParameterException;
- use \App\Exceptions\InvalidQrCodeException;
- use App\Exceptions\InvalidSecretException;
- use App\Exceptions\DbEncryptionException;
- use App\Exceptions\InvalidMigrationDataException;
- use App\Exceptions\UndecipherableException;
- use App\Exceptions\UnsupportedMigrationException;
- use App\Exceptions\UnsupportedOtpTypeException;
- use App\Exceptions\EncryptedMigrationException;
- /**
- * @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 [
- [
- InvalidOtpParameterException::class,
- ],
- [
- InvalidQrCodeException::class,
- ],
- [
- InvalidSecretException::class,
- ],
- [
- DbEncryptionException::class,
- ],
- [
- InvalidMigrationDataException::class,
- ],
- [
- UndecipherableException::class,
- ],
- [
- UnsupportedMigrationException::class,
- ],
- [
- UnsupportedOtpTypeException::class,
- ],
- [
- EncryptedMigrationException::class,
- ],
- ];
- }
- /**
- * @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',
- ],
- ];
- }
- /**
- * @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',
- ]);
- }
- /**
- * @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',
- ]);
- }
- }
|