8000 Renamed NotPwned to NotCompromisedPassword by javiereguiluz · Pull Request #30901 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Renamed NotPwned to NotCompromisedPassword #30901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
4.3.0
-----

* added `NotPwned` constraint
* added `NotCompromisedPassword` constraint
* added options `iban` and `ibanPropertyPath` to Bic constraint
* added UATP cards support to `CardSchemeValidator`
* added option `allowNull` to NotBlank constraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class NotPwned extends Constraint
class NotCompromisedPassword extends Constraint
{
const PWNED_ERROR = 'd9bcdbfe-a9d6-4bfa-a8ff-da5fd93e0f6d';
const COMPROMISED_PASSWORD_ERROR = 'd9bcdbfe-a9d6-4bfa-a8ff-da5fd93e0f6d';

protected static $errorNames = [self::PWNED_ERROR => 'PWNED_ERROR'];
protected static $errorNames = [self::COMPROMISED_PASSWORD_ERROR => 'COMPROMISED_PASSWORD_ERROR'];

public $message = 'This password has been leaked in a data breach, it must not be used. Please use another password.';
public $threshold = 1;
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class NotPwnedValidator extends ConstraintValidator
class NotCompromisedPasswordValidator extends ConstraintValidator
{
private const RANGE_API = 'https://api.pwnedpasswords.com/range/%s';

Expand All @@ -48,8 +48,8 @@ public function __construct(HttpClientInterface $httpClient = null)
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof NotPwned) {
throw new UnexpectedTypeException($constraint, NotPwned::class);
if (!$constraint instanceof NotCompromisedPassword) {
throw new UnexpectedTypeException($constraint, NotCompromisedPassword::class);
}

if (null !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
Expand Down Expand Up @@ -80,7 +80,7 @@ public function validate($value, Constraint $constraint)

if ($hashPrefix.$hashSuffix === $hash && $constraint->threshold <= (int) $count) {
$this->context->buildViolation($constraint->message)
->setCode(NotPwned::PWNED_ERROR)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->addViolation();

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
namespace Symfony\Component\Validator\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\NotPwned;
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class NotPwnedTest extends TestCase
class NotCompromisedPasswordTest extends TestCase
{
public function testDefaultValues()
{
$constraint = new NotPwned();
$constraint = new NotCompromisedPassword();
$this->assertSame(1, $constraint->threshold);
$this->assertFalse($constraint->skipOnError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\Luhn;
use Symfony\Component\Validator\Constraints\NotPwned;
use Symfony\Component\Validator\Constraints\NotPwnedValidator;
use Symfony\Component\Validator\Constraints\NotCompromisedPassword;
use Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -22,7 +22,7 @@
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class NotPwnedValidatorTest extends ConstraintValidatorTestCase
class NotCompromisedPasswordValidatorTest extends ConstraintValidatorTestCase
{
private const PASSWORD_TRIGGERING_AN_ERROR = 'apiError';
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"
Expand Down Expand Up @@ -61,53 +61,53 @@ public function getResponse(): ResponseInterface
);

// Pass HttpClient::create() instead of this mock to run the tests against the real API
return new NotPwnedValidator($httpClientStub);
return new NotCompromisedPasswordValidator($httpClientStub);
}

public function testNullIsValid()
{
$this->validator->validate(null, new NotPwned());
$this->validator->validate(null, new NotCompromisedPassword());

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new NotPwned());
$this->validator->validate('', new NotCompromisedPassword());

$this->assertNoViolation();
}

public function testInvalidPassword()
{
$constraint = new NotPwned();
$constraint = new NotCompromisedPassword();
$this->validator->validate(self::PASSWORD_LEAKED, $constraint);

$this->buildViolation($constraint->message)
->setCode(NotPwned::PWNED_ERROR)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->assertRaised();
}

public function testThresholdReached()
{
$constraint = new NotPwned(['threshold' => 3]);
$constraint = new NotCompromisedPassword(['threshold' => 3]);
$this->validator->validate(self::PASSWORD_LEAKED, $constraint);

$this->buildViolation($constraint->message)
->setCode(NotPwned::PWNED_ERROR)
->setCode(NotCompromisedPassword::COMPROMISED_PASSWORD_ERROR)
->assertRaised();
}

public function testThresholdNotReached()
{
$this->validator->validate(self::PASSWORD_LEAKED, new NotPwned(['threshold' => 10]));
$this->validator->validate(self::PASSWORD_LEAKED, new NotCompromisedPassword(['threshold' => 10]));

$this->assertNoViolation();
}

public function testValidPassword()
{
$this->validator->validate(self::PASSWORD_NOT_LEAKED, new NotPwned());
$this->validator->validate(self::PASSWORD_NOT_LEAKED, new NotCompromisedPassword());

$this->assertNoViolation();
}
Expand All @@ -125,7 +125,7 @@ public function testInvalidConstraint()
*/
public function testInvalidValue()
{
$this->validator->validate([], new NotPwned());
$this->validator->validate([], new NotCompromisedPassword());
}

/**
Expand All @@ -134,12 +134,12 @@ public function testInvalidValue()
*/
public function testApiError()
{
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotPwned());
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword());
}

public function testApiErrorSkipped()
{
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotPwned(['skipOnError' => true]));
$this->validator->validate(self::PASSWORD_TRIGGERING_AN_ERROR, new NotCompromisedPassword(['skipOnError' => true]));
$this->assertTrue(true); // No exception have been thrown
}
}
0