|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use Symfony\Component\Validator\Constraints\Luhn; |
| 15 | +use Symfony\Component\Validator\Constraints\NotPwned; |
| 16 | +use Symfony\Component\Validator\Constraints\NotPwnedValidator; |
| 17 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 18 | +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 19 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 20 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Kévin Dunglas <[email protected]> |
| 24 | + */ |
| 25 | +class NotPwnedValidatorTest extends ConstraintValidatorTestCase |
| 26 | +{ |
| 27 | + private const PASSWORD_TRIGGERING_AN_ERROR = 'apiError'; |
| 28 | + private const PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL = 'https://api.pwnedpasswords.com/range/3EF27'; // https://api.pwnedpasswords.com/range/3EF27 is the range for the value "apiError" |
| 29 | + private const PASSWORD_LEAKED = 'maman'; |
| 30 | + private const PASSWORD_NOT_LEAKED = ']<0585"%sb^5aa$w6!b38",,72?dp3r4\45b28Hy'; |
| 31 | + |
| 32 | + private const RETURN = [ |
| 33 | + '35E033023A46402F94CFB4F654C5BFE44A1:1', |
| 34 | + '35F079CECCC31812288257CD770AA7968D7:53', |
| 35 | + '36039744C253F9B2A4E90CBEDB02EBFB82D:5', // this is the matching line, password: maman |
| 36 | + '3686792BBC66A72D40D928ED15621124CFE:7', |
| 37 | + '36EEC709091B810AA240179A44317ED415C:2', |
| 38 | + ]; |
| 39 | + |
| 40 | + protected function createValidator() |
| 41 | + { |
| 42 | + $httpClientStub = $this->createMock(HttpClientInterface::class); |
| 43 | + $httpClientStub->method('request')->will( |
| 44 | + $this->returnCallback(function (string $method, string $url): ResponseInterface { |
| 45 | + if (self::PASSWORD_TRIGGERING_AN_ERROR_RANGE_URL === $url) { |
| 46 | + throw new class('Problem contacting the Have I been Pwned API.') extends \Exception implements ServerExceptionInterface { |
| 47 | + public function getResponse(): ResponseInterface |
| 48 | + { |
| 49 | + throw new \RuntimeException('Not implemented'); |
| 50 | + } |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + $responseStub = $this->createMock(ResponseInterface::class); |
| 55 | + $responseStub |
| 56 | + ->method('getContent') |
| 57 | + ->willReturn(implode("\r\n", self::RETURN)); |
| 58 | + |
| 59 | + return $responseStub; |
| 60 | + }) |
| 61 | + ); |
| 62 | + |
| 63 | + // Pass HttpClient::create() instead of this mock to run the tests against the real API |
| 64 | + return new NotPwnedValidator($httpClientStub); |
| 65 | + } |
| 66 | + |
| 67 | + public function testNullIsValid() |
| 68 | + { |
| 69 | + $this->validator->validate(null, new NotPwned()); |
| 70 | + |
| 71 | + $this->assertNoViolation(); |
| 72 | + } |
| 73 | + |
| 74 | + public function testEmptyStringIsValid() |
| 75 | + { |
| 76 | + $this->validator->validate('', new NotPwned()); |
| 77 | + |
| 78 | + $this->assertNoViolation(); |
| 79 | + } |
| 80 | + |
| 81 | + public function testInvalidPassword() |
| 82 | + { |
| 83 | + $constraint = new NotPwned(); |
| 84 | + $this->validator->validate(self::PASSWORD_LEAKED, $constraint); |
| 85 | + |
| 86 | + $this->buildViolation($constraint->message) |
| 87 | + ->setCode(NotPwned::PWNED_ERROR) |
| 88 | + ->assertRaised(); |
| 89 | + } |
| 90 | + |
| 91 | + public function testThresholdReached() |
| 92 | + { |
| 93 | + $constraint = new NotPwned(['threshold' => 3]); |
| 94 | + $this->validator->validate(self::PASSWORD_LEAKED, $constraint); |
| 95 | + |
| 96 | + $this->buildViolation($constraint->message) |
| 97 | + ->setCode(NotPwned::PWNED_ERROR) |
| 98 | + ->assertRaised(); |
| 99 | + } |
| 100 | + |
| 101 | + public function testThresholdNotReached() |
| 102 | + { |
| 103 | + $this->validator->validate(self::PASSWORD_LEAKED, new NotPwned(['threshold' => 10])); |
| 104 | + |
| 105 | + $this->assertNoViolation(); |
| 106 | + } |
| 107 | + |
| 108 | + public function testValidPassword() |
| 109 | + { |
| 110 | + $this->validator->validate(self::PASSWORD_NOT_LEAKED, new NotPwned()); |
| 111 | + |
| 112 | + $this->assertNoViolation(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 117 | + */ |
| 118 | + public function testInvalidConstraint() |
| 119 | + { |
| 120 | + $this->validator->validate(null, new Luhn()); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 125 | + */ |
| 126 | + public function testInvalidValue() |
| 127 | + { |
| 128 | + $this->validator->validate([], new NotPwned()); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @expectedException \Symfony\Contracts\HttpClient\Exception\ExceptionInterface |
| 133 | + * @expectedExceptionMessage Problem contacting the Have I been Pwned API. |
| 134 | + */ |
| 135 | + public function testApiError() |
| 136 | + { |
| 137 | + $this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotPwned()); |
| 138 | + } |
| 139 | + |
| 140 | + public function testApiErrorSkipped() |
| 141 | + { |
| 142 | + $this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotPwned(['skipOnError' => true])); |
| 143 | + $this->assertTrue(true); // No exception have been thrown |
| 144 | + } |
| 145 | +} |
0 commit comments