ValidationNotPwnedVerifierTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Illuminate\Tests\Validation;
  3. use Illuminate\Container\Container;
  4. use Illuminate\Contracts\Debug\ExceptionHandler;
  5. use Illuminate\Http\Client\ConnectionException;
  6. use Illuminate\Http\Client\Factory as HttpFactory;
  7. use Illuminate\Http\Client\Response;
  8. use Illuminate\Validation\NotPwnedVerifier;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. class ValidationNotPwnedVerifierTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. Container::setInstance(null);
  17. }
  18. public function testEmptyValues()
  19. {
  20. $httpFactory = m::mock(HttpFactory::class);
  21. $verifier = new NotPwnedVerifier($httpFactory);
  22. foreach (['', false, 0] as $password) {
  23. $this->assertFalse($verifier->verify([
  24. 'value' => $password,
  25. 'threshold' => 0,
  26. ]));
  27. }
  28. }
  29. public function testApiResponseGoesWrong()
  30. {
  31. $httpFactory = m::mock(HttpFactory::class);
  32. $response = m::mock(Response::class);
  33. $httpFactory = m::mock(HttpFactory::class);
  34. $httpFactory
  35. ->shouldReceive('withHeaders')
  36. ->once()
  37. ->with(['Add-Padding' => true])
  38. ->andReturn($httpFactory);
  39. $httpFactory
  40. ->shouldReceive('timeout')
  41. ->once()
  42. ->with(30)
  43. ->andReturn($httpFactory);
  44. $httpFactory->shouldReceive('get')
  45. ->once()
  46. ->andReturn($response);
  47. $response->shouldReceive('successful')
  48. ->once()
  49. ->andReturn(true);
  50. $response->shouldReceive('body')
  51. ->once()
  52. ->andReturn('');
  53. $verifier = new NotPwnedVerifier($httpFactory);
  54. $this->assertTrue($verifier->verify([
  55. 'value' => 123123123,
  56. 'threshold' => 0,
  57. ]));
  58. }
  59. public function testApiGoesDown()
  60. {
  61. $httpFactory = m::mock(HttpFactory::class);
  62. $response = m::mock(Response::class);
  63. $httpFactory
  64. ->shouldReceive('withHeaders')
  65. ->once()
  66. ->with(['Add-Padding' => true])
  67. ->andReturn($httpFactory);
  68. $httpFactory
  69. ->shouldReceive('timeout')
  70. ->once()
  71. ->with(30)
  72. ->andReturn($httpFactory);
  73. $httpFactory->shouldReceive('get')
  74. ->once()
  75. ->andReturn($response);
  76. $response->shouldReceive('successful')
  77. ->once()
  78. ->andReturn(false);
  79. $verifier = new NotPwnedVerifier($httpFactory);
  80. $this->assertTrue($verifier->verify([
  81. 'value' => 123123123,
  82. 'threshold' => 0,
  83. ]));
  84. }
  85. public function testDnsDown()
  86. {
  87. $container = Container::getInstance();
  88. $exception = new ConnectionException();
  89. $exceptionHandler = m::mock(ExceptionHandler::class);
  90. $exceptionHandler->shouldReceive('report')->once()->with($exception);
  91. $container->bind(ExceptionHandler::class, function () use ($exceptionHandler) {
  92. return $exceptionHandler;
  93. });
  94. $httpFactory = m::mock(HttpFactory::class);
  95. $httpFactory
  96. ->shouldReceive('withHeaders')
  97. ->once()
  98. ->with(['Add-Padding' => true])
  99. ->andReturn($httpFactory);
  100. $httpFactory
  101. ->shouldReceive('timeout')
  102. ->once()
  103. ->with(30)
  104. ->andReturn($httpFactory);
  105. $httpFactory
  106. ->shouldReceive('get')
  107. ->once()
  108. ->andThrow($exception);
  109. $verifier = new NotPwnedVerifier($httpFactory);
  110. $this->assertTrue($verifier->verify([
  111. 'value' => 123123123,
  112. 'threshold' => 0,
  113. ]));
  114. unset($container[ExceptionHandler::class]);
  115. }
  116. }