HandlerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Tests\Unit\Exceptions;
  3. use App\Exceptions\Handler;
  4. use Illuminate\Contracts\Container\Container;
  5. use Illuminate\Http\JsonResponse;
  6. use Illuminate\Http\Request;
  7. use Tests\TestCase;
  8. use App\Exceptions\InvalidOtpParameterException;
  9. use \App\Exceptions\InvalidQrCodeException;
  10. use App\Exceptions\InvalidSecretException;
  11. use App\Exceptions\DbEncryptionException;
  12. use App\Exceptions\InvalidMigrationDataException;
  13. use App\Exceptions\UndecipherableException;
  14. use App\Exceptions\UnsupportedMigrationException;
  15. use App\Exceptions\UnsupportedOtpTypeException;
  16. use App\Exceptions\EncryptedMigrationException;
  17. /**
  18. * @covers \App\Exceptions\Handler
  19. */
  20. class HandlerTest extends TestCase
  21. {
  22. /**
  23. * @test
  24. *
  25. * @dataProvider provideExceptionsforBadRequest
  26. */
  27. public function test_exceptions_returns_badRequest_json_response($exception)
  28. {
  29. $request = $this->createMock(Request::class);
  30. $instance = new Handler($this->createMock(Container::class));
  31. $class = new \ReflectionClass(Handler::class);
  32. $method = $class->getMethod('render');
  33. $method->setAccessible(true);
  34. $response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
  35. $this->assertInstanceOf(JsonResponse::class, $response);
  36. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  37. $response->assertStatus(400)
  38. ->assertJsonStructure([
  39. 'message',
  40. ]);
  41. }
  42. /**
  43. * Provide Valid data for validation test
  44. */
  45. public function provideExceptionsforBadRequest(): array
  46. {
  47. return [
  48. [
  49. InvalidOtpParameterException::class,
  50. ],
  51. [
  52. InvalidQrCodeException::class,
  53. ],
  54. [
  55. InvalidSecretException::class,
  56. ],
  57. [
  58. DbEncryptionException::class,
  59. ],
  60. [
  61. InvalidMigrationDataException::class,
  62. ],
  63. [
  64. UndecipherableException::class,
  65. ],
  66. [
  67. UnsupportedMigrationException::class,
  68. ],
  69. [
  70. UnsupportedOtpTypeException::class,
  71. ],
  72. [
  73. EncryptedMigrationException::class,
  74. ],
  75. ];
  76. }
  77. /**
  78. * @test
  79. *
  80. * @dataProvider provideExceptionsforNotFound
  81. */
  82. public function test_exceptions_returns_notFound_json_response($exception)
  83. {
  84. $request = $this->createMock(Request::class);
  85. $instance = new Handler($this->createMock(Container::class));
  86. $class = new \ReflectionClass(Handler::class);
  87. $method = $class->getMethod('render');
  88. $method->setAccessible(true);
  89. $response = $method->invokeArgs($instance, [$request, $this->createMock($exception)]);
  90. $this->assertInstanceOf(JsonResponse::class, $response);
  91. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  92. $response->assertStatus(404)
  93. ->assertJsonStructure([
  94. 'message',
  95. ]);
  96. }
  97. /**
  98. * Provide Valid data for validation test
  99. */
  100. public function provideExceptionsforNotFound(): array
  101. {
  102. return [
  103. [
  104. '\Illuminate\Database\Eloquent\ModelNotFoundException',
  105. ],
  106. [
  107. '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException',
  108. ],
  109. ];
  110. }
  111. /**
  112. * @test
  113. */
  114. public function test_authenticationException_returns_unauthorized_json_response()
  115. {
  116. $request = $this->createMock(Request::class);
  117. $instance = new Handler($this->createMock(Container::class));
  118. $class = new \ReflectionClass(Handler::class);
  119. $method = $class->getMethod('render');
  120. $method->setAccessible(true);
  121. $mockException = $this->createMock(\Illuminate\Auth\AuthenticationException::class);
  122. $mockException->method('guards')->willReturn(['web-guard']);
  123. $response = $method->invokeArgs($instance, [$request, $mockException]);
  124. $this->assertInstanceOf(JsonResponse::class, $response);
  125. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  126. $response->assertStatus(401)
  127. ->assertJsonStructure([
  128. 'message',
  129. ]);
  130. }
  131. /**
  132. * @test
  133. */
  134. public function test_authenticationException_returns_proxyAuthRequired_json_response_with_proxy_guard()
  135. {
  136. $request = $this->createMock(Request::class);
  137. $instance = new Handler($this->createMock(Container::class));
  138. $class = new \ReflectionClass(Handler::class);
  139. $method = $class->getMethod('render');
  140. $method->setAccessible(true);
  141. $mockException = $this->createMock(\Illuminate\Auth\AuthenticationException::class);
  142. $mockException->method('guards')->willReturn(['reverse-proxy-guard']);
  143. $response = $method->invokeArgs($instance, [$request, $mockException]);
  144. $this->assertInstanceOf(JsonResponse::class, $response);
  145. $response = \Illuminate\Testing\TestResponse::fromBaseResponse($response);
  146. $response->assertStatus(407)
  147. ->assertJsonStructure([
  148. 'message',
  149. ]);
  150. }
  151. }