minor #11585 [SecurityBundle] Added UserPasswordValidator tests for the different Validation APIs (webmozart)

This PR was merged into the 2.5 branch.

Discussion
----------

[SecurityBundle] Added UserPasswordValidator tests for the different Validation APIs

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

0653426 [SecurityBundle] Added UserPasswordValidator tests for the different Validation APIs
This commit is contained in:
Bernhard Schussek 2014-08-06 15:00:40 +02:00
commit fba2393297
4 changed files with 127 additions and 59 deletions

View File

@ -17,7 +17,6 @@ use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Bundle\SecurityBundle\SecurityBundle; use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension; use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\ExpressionLanguage\Expression;
abstract class CompleteConfigurationTest extends \PHPUnit_Framework_TestCase abstract class CompleteConfigurationTest extends \PHPUnit_Framework_TestCase
{ {

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
use Symfony\Component\Validator\Validation;
/**
* @since 2.5.4
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class LegacyUserPasswordValidator2Dot4ApiTest extends UserPasswordValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_4;
}
}

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
use Symfony\Component\Validator\Validation;
/**
* @since 2.5.4
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class LegacyUserPasswordValidatorLegacyApiTest extends UserPasswordValidatorTest
{
protected function getApiVersion()
{
return Validation::API_VERSION_2_5_BC;
}
}

View File

@ -11,77 +11,102 @@
namespace Symfony\Component\Security\Core\Tests\Validator\Constraints; namespace Symfony\Component\Security\Core\Tests\Validator\Constraints;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator; use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
use Symfony\Component\Validator\Validation;
class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase /**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class UserPasswordValidatorTest extends AbstractConstraintValidatorTest
{ {
const PASSWORD_VALID = true; const PASSWORD = 's3Cr3t';
const PASSWORD_INVALID = false;
protected $context; const SALT = '^S4lt$';
/**
* @var SecurityContextInterface
*/
protected $securityContext;
/**
* @var PasswordEncoderInterface
*/
protected $encoder;
/**
* @var EncoderFactoryInterface
*/
protected $encoderFactory;
protected function getApiVersion()
{
return Validation::API_VERSION_2_5;
}
protected function createValidator()
{
return new UserPasswordValidator($this->securityContext, $this->encoderFactory);
}
protected function setUp() protected function setUp()
{ {
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); $user = $this->createUser();
} $this->securityContext = $this->createSecurityContext($user);
$this->encoder = $this->createPasswordEncoder();
$this->encoderFactory = $this->createEncoderFactory($this->encoder);
protected function tearDown() parent::setUp();
{
$this->context = null;
} }
public function testPasswordIsValid() public function testPasswordIsValid()
{ {
$user = $this->createUser(); $constraint = new UserPassword(array(
$securityContext = $this->createSecurityContext($user); 'message' => 'myMessage',
));
$encoder = $this->createPasswordEncoder(static::PASSWORD_VALID); $this->encoder->expects($this->once())
$encoderFactory = $this->createEncoderFactory($encoder); ->method('isPasswordValid')
->with(static::PASSWORD, 'secret', static::SALT)
->will($this->returnValue(true));
$validator = new UserPasswordValidator($securityContext, $encoderFactory); $this->validator->validate('secret', $constraint);
$validator->initialize($this->context);
$this $this->assertNoViolation();
->context
->expects($this->never())
->method('addViolation')
;
$validator->validate('secret', new UserPassword());
} }
public function testPasswordIsNotValid() public function testPasswordIsNotValid()
{ {
$user = $this->createUser(); $constraint = new UserPassword(array(
$securityContext = $this->createSecurityContext($user); 'message' => 'myMessage',
));
$encoder = $this->createPasswordEncoder(static::PASSWORD_INVALID); $this->encoder->expects($this->once())
$encoderFactory = $this->createEncoderFactory($encoder); ->method('isPasswordValid')
->with(static::PASSWORD, 'secret', static::SALT)
->will($this->returnValue(false));
$validator = new UserPasswordValidator($securityContext, $encoderFactory); $this->validator->validate('secret', $constraint);
$validator->initialize($this->context);
$this $this->assertViolation('myMessage');
->context
->expects($this->once())
->method('addViolation')
;
$validator->validate('secret', new UserPassword());
} }
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testUserIsNotValid() public function testUserIsNotValid()
{ {
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$user = $this->getMock('Foo\Bar\User'); $user = $this->getMock('Foo\Bar\User');
$encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
$securityContext = $this->createSecurityContext($user);
$validator = new UserPasswordValidator($securityContext, $encoderFactory); $this->securityContext = $this->createSecurityContext($user);
$validator->initialize($this->context); $this->validator = $this->createValidator();
$validator->validate('secret', new UserPassword()); $this->validator->initialize($this->context);
$this->validator->validate('secret', new UserPassword());
} }
protected function createUser() protected function createUser()
@ -89,15 +114,15 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
$mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); $mock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$mock $mock
->expects($this->once()) ->expects($this->any())
->method('getPassword') ->method('getPassword')
->will($this->returnValue('s3Cr3t')) ->will($this->returnValue(static::PASSWORD))
; ;
$mock $mock
->expects($this->once()) ->expects($this->any())
->method('getSalt') ->method('getSalt')
->will($this->returnValue('^S4lt$')) ->will($this->returnValue(static::SALT))
; ;
return $mock; return $mock;
@ -105,15 +130,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
protected function createPasswordEncoder($isPasswordValid = true) protected function createPasswordEncoder($isPasswordValid = true)
{ {
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface'); return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
$mock
->expects($this->once())
->method('isPasswordValid')
->will($this->returnValue($isPasswordValid))
;
return $mock;
} }
protected function createEncoderFactory($encoder = null) protected function createEncoderFactory($encoder = null)
@ -121,7 +138,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
$mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); $mock = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
$mock $mock
->expects($this->once()) ->expects($this->any())
->method('getEncoder') ->method('getEncoder')
->will($this->returnValue($encoder)) ->will($this->returnValue($encoder))
; ;
@ -135,7 +152,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
$mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); $mock = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$mock $mock
->expects($this->once()) ->expects($this->any())
->method('getToken') ->method('getToken')
->will($this->returnValue($token)) ->will($this->returnValue($token))
; ;
@ -147,7 +164,7 @@ class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
{ {
$mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); $mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
$mock $mock
->expects($this->once()) ->expects($this->any())
->method('getUser') ->method('getUser')
->will($this->returnValue($user)) ->will($this->returnValue($user))
; ;