[Security] added more unit tests

This commit is contained in:
Fabien Potencier 2010-10-22 17:48:58 +02:00
parent d2b184e058
commit 4027f751e3
12 changed files with 682 additions and 2 deletions

View File

@ -3,6 +3,7 @@
namespace Symfony\Component\Security;
use Symfony\Component\Security\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Authorization\AccessDecisionManager;
/*
* This file is part of the Symfony package.
@ -29,12 +30,17 @@ class SecurityContext
protected $token;
protected $accessDecisionManager;
public function __construct($accessDecisionManager = null)
/**
* Constructor.
*
* @param AccessDecisionManager|null $accessDecisionManager An AccessDecisionManager instance
*/
public function __construct(AccessDecisionManager $accessDecisionManager = null)
{
$this->accessDecisionManager = $accessDecisionManager;
}
protected function getUser()
public function getUser()
{
return null === $this->token ? null : $this->token->getUser();
}

View File

@ -15,6 +15,60 @@ use Symfony\Component\Security\Authorization\Voter\VoterInterface;
class AccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
{
public function testSupportsClass()
{
$manager = new AccessDecisionManager(array(
$this->getVoterSupportsClass(true),
$this->getVoterSupportsClass(false),
));
$this->assertTrue($manager->supportsClass('FooClass'));
$manager = new AccessDecisionManager(array(
$this->getVoterSupportsClass(false),
$this->getVoterSupportsClass(false),
));
$this->assertFalse($manager->supportsClass('FooClass'));
}
public function testSupportsAttribute()
{
$manager = new AccessDecisionManager(array(
$this->getVoterSupportsAttribute(true),
$this->getVoterSupportsAttribute(false),
));
$this->assertTrue($manager->supportsAttribute('foo'));
$manager = new AccessDecisionManager(array(
$this->getVoterSupportsAttribute(false),
$this->getVoterSupportsAttribute(false),
));
$this->assertFalse($manager->supportsAttribute('foo'));
}
/**
* @expectedException LogicException
*/
public function testSetVotersEmpty()
{
$manager = new AccessDecisionManager();
$manager->setVoters(array());
}
public function testSetVoters()
{
$manager = new AccessDecisionManager();
$manager->setVoters(array($voter = $this->getVoterSupportsAttribute(true)));
$this->assertSame(array($voter), $manager->getVoters());
}
public function testGetVoters()
{
$manager = new AccessDecisionManager(array($voter = $this->getVoterSupportsAttribute(true)));
$this->assertSame(array($voter), $manager->getVoters());
}
/**
* @dataProvider getStrategyTests
*/
@ -93,4 +147,26 @@ class AccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
return $voter;
}
protected function getVoterSupportsClass($ret)
{
$voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
$voter->expects($this->any())
->method('supportsClass')
->will($this->returnValue($ret));
;
return $voter;
}
protected function getVoterSupportsAttribute($ret)
{
$voter = $this->getMock('Symfony\Component\Security\Authorization\Voter\VoterInterface');
$voter->expects($this->any())
->method('supportsAttribute')
->will($this->returnValue($ret));
;
return $voter;
}
}

View File

@ -16,6 +16,13 @@ use Symfony\Component\Security\Role\Role;
class RoleVoterTest extends \PHPUnit_Framework_TestCase
{
public function testSupportsClass()
{
$voter = new RoleVoter();
$this->assertTrue($voter->supportsClass('Foo'));
}
/**
* @dataProvider getVoteTests
*/

View File

@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\Encoder;
use Symfony\Component\Security\Encoder\BasePasswordEncoder;
class PasswordEncoder extends BasePasswordEncoder
{
public function encodePassword($raw, $salt)
{
}
public function isPasswordValid($encoded, $raw, $salt)
{
}
}
class BasePasswordEncoderTest extends \PHPUnit_Framework_TestCase
{
public function testComparePassword()
{
$this->assertTrue($this->invokeComparePasswords('password', 'password'));
$this->assertFalse($this->invokeComparePasswords('password', 'foo'));
}
public function testDemergePasswordAndSalt()
{
$this->assertEquals(array('password', 'salt'), $this->invokeDemergePasswordAndSalt('password{salt}'));
$this->assertEquals(array('password', ''), $this->invokeDemergePasswordAndSalt('password'));
$this->assertEquals(array('', ''), $this->invokeDemergePasswordAndSalt(''));
}
public function testMergePasswordAndSalt()
{
$this->assertEquals('password{salt}', $this->invokeMergePasswordAndSalt('password', 'salt'));
$this->assertEquals('password', $this->invokeMergePasswordAndSalt('password', ''));
}
/**
* @expectedException InvalidArgumentException
*/
public function testMergePasswordAndSaltWithException()
{
$this->invokeMergePasswordAndSalt('password', '{foo}');
}
protected function invokeDemergePasswordAndSalt($password)
{
$encoder = new PasswordEncoder();
$r = new \ReflectionObject($encoder);
$m = $r->getMethod('demergePasswordAndSalt');
$m->setAccessible(true);
return $m->invoke($encoder, $password);
}
protected function invokeMergePasswordAndSalt($password, $salt)
{
$encoder = new PasswordEncoder();
$r = new \ReflectionObject($encoder);
$m = $r->getMethod('mergePasswordAndSalt');
$m->setAccessible(true);
return $m->invoke($encoder, $password, $salt);
}
protected function invokeComparePasswords($p1, $p2)
{
$encoder = new PasswordEncoder();
$r = new \ReflectionObject($encoder);
$m = $r->getMethod('comparePasswords');
$m->setAccessible(true);
return $m->invoke($encoder, $p1, $p2);
}
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\Encoder;
use Symfony\Component\Security\Encoder\MessageDigestPasswordEncoder;
class MessageDigestPasswordEncoderTest extends \PHPUnit_Framework_TestCase
{
public function testIsPasswordValid()
{
$encoder = new MessageDigestPasswordEncoder();
$this->assertTrue($encoder->isPasswordValid(hash('sha256', 'password'), 'password', ''));
}
public function testEncodePassword()
{
$encoder = new MessageDigestPasswordEncoder();
$this->assertSame(hash('sha256', 'password'), $encoder->encodePassword('password', ''));
$encoder = new MessageDigestPasswordEncoder('sha256', true);
$this->assertSame(base64_encode(hash('sha256', 'password')), $encoder->encodePassword('password', ''));
$encoder = new MessageDigestPasswordEncoder('sha256', false, 2);
$this->assertSame(hash('sha256', hash('sha256', 'password')), $encoder->encodePassword('password', ''));
}
/**
* @expectedException LogicException
*/
public function testEncodePasswordAlgorithmDoesNotExist()
{
$encoder = new MessageDigestPasswordEncoder('foobar');
$encoder->encodePassword('password', '');
}
}

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\Role;
use Symfony\Component\Security\Role\RoleHierarchy;
use Symfony\Component\Security\Role\Role;
class RoleHierarchyTest extends \PHPUnit_Framework_TestCase
{
public function testGetReachableRoles()
{
$role = new RoleHierarchy(array(
'ROLE_ADMIN' => array('ROLE_USER'),
'ROLE_SUPER_ADMIN' => array('ROLE_ADMIN', 'ROLE_FOO'),
));
$this->assertEquals(array(new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_USER'))));
$this->assertEquals(array(new Role('ROLE_FOO')), $role->getReachableRoles(array(new Role('ROLE_FOO'))));
$this->assertEquals(array(new Role('ROLE_ADMIN'), new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_ADMIN'))));
$this->assertEquals(array(new Role('ROLE_FOO'), new Role('ROLE_ADMIN'), new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_FOO'), new Role('ROLE_ADMIN'))));
$this->assertEquals(array(new Role('ROLE_SUPER_ADMIN'), new Role('ROLE_ADMIN'), new Role('ROLE_FOO'), new Role('ROLE_USER')), $role->getReachableRoles(array(new Role('ROLE_SUPER_ADMIN'))));
}
}

View File

@ -0,0 +1,23 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\Role;
use Symfony\Component\Security\Role\Role;
class RoleTest extends \PHPUnit_Framework_TestCase
{
public function testGetRole()
{
$role = new Role('FOO');
$this->assertEquals('FOO', $role->getRole());
}
}

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\Role;
use Symfony\Component\Security\Role\SwitchUserRole;
class SwitchUserRoleTest extends \PHPUnit_Framework_TestCase
{
public function testGetSource()
{
$role = new SwitchUserRole('FOO', $token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertSame($token, $role->getSource());
}
public function testGetRole()
{
$role = new SwitchUserRole('FOO', $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertEquals('FOO', $role->getRole());
}
}

View File

@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security;
use Symfony\Component\Security\SecurityContext;
class SecurityContextTest extends \PHPUnit_Framework_TestCase
{
public function testIsAuthenticated()
{
$context = new SecurityContext();
$this->assertFalse($context->isAuthenticated());
$token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
$token->expects($this->once())->method('isAuthenticated')->will($this->returnValue(false));
$context->setToken($token);
$this->assertFalse($context->isAuthenticated());
$token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
$token->expects($this->once())->method('isAuthenticated')->will($this->returnValue(true));
$context->setToken($token);
$this->assertTrue($context->isAuthenticated());
}
public function testGetUser()
{
$context = new SecurityContext();
$this->assertNull($context->getUser());
$token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface');
$token->expects($this->once())->method('getUser')->will($this->returnValue('foo'));
$context->setToken($token);
$this->assertEquals('foo', $context->getUser());
}
public function testVote()
{
$context = new SecurityContext();
$this->assertFalse($context->vote('ROLE_FOO'));
$context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertFalse($context->vote('ROLE_FOO'));
$manager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManager');
$manager->expects($this->once())->method('decide')->will($this->returnValue(false));
$context = new SecurityContext($manager);
$context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertFalse($context->vote('ROLE_FOO'));
$manager = $this->getMock('Symfony\Component\Security\Authorization\AccessDecisionManager');
$manager->expects($this->once())->method('decide')->will($this->returnValue(true));
$context = new SecurityContext($manager);
$context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertTrue($context->vote('ROLE_FOO'));
}
public function testGetSetToken()
{
$context = new SecurityContext();
$this->assertNull($context->getToken());
$context->setToken($token = $this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface'));
$this->assertSame($token, $context->getToken());
}
}

View File

@ -0,0 +1,107 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\User;
use Symfony\Component\Security\User\AccountChecker;
class AccountCheckerTest extends \PHPUnit_Framework_TestCase
{
public function testCheckPreAuthNotAdvancedAccountInterface()
{
$checker = new AccountChecker();
$this->assertNull($checker->checkPreAuth($this->getMock('Symfony\Component\Security\User\AccountInterface')));
}
public function testCheckPreAuthPass()
{
$checker = new AccountChecker();
$account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
$account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(true));
$this->assertNull($checker->checkPreAuth($account));
}
/**
* @expectedException Symfony\Component\Security\Exception\CredentialsExpiredException
*/
public function testCheckPreAuthCredentialsExpired()
{
$checker = new AccountChecker();
$account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
$account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(false));
$checker->checkPreAuth($account);
}
public function testCheckPostAuthNotAdvancedAccountInterface()
{
$checker = new AccountChecker();
$this->assertNull($checker->checkPostAuth($this->getMock('Symfony\Component\Security\User\AccountInterface')));
}
public function testCheckPostAuthPass()
{
$checker = new AccountChecker();
$account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
$account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
$account->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
$account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(true));
$this->assertNull($checker->checkPostAuth($account));
}
/**
* @expectedException Symfony\Component\Security\Exception\LockedException
*/
public function testCheckPostAuthAccountLocked()
{
$checker = new AccountChecker();
$account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
$account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(false));
$checker->checkPostAuth($account);
}
/**
* @expectedException Symfony\Component\Security\Exception\DisabledException
*/
public function testCheckPostAuthDisabled()
{
$checker = new AccountChecker();
$account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
$account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
$account->expects($this->once())->method('isEnabled')->will($this->returnValue(false));
$checker->checkPostAuth($account);
}
/**
* @expectedException Symfony\Component\Security\Exception\AccountExpiredException
*/
public function testCheckPostAuthAccountExpired()
{
$checker = new AccountChecker();
$account = $this->getMock('Symfony\Component\Security\User\AdvancedAccountInterface');
$account->expects($this->once())->method('isAccountNonLocked')->will($this->returnValue(true));
$account->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
$account->expects($this->once())->method('isAccountNonExpired')->will($this->returnValue(false));
$checker->checkPostAuth($account);
}
}

View File

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\User;
use Symfony\Component\Security\User\InMemoryUserProvider;
use Symfony\Component\Security\User\User;
class InMemoryUserProviderTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$provider = new InMemoryUserProvider(array(
'fabien' => array(
'password' => 'foo',
'enabled' => false,
'roles' => array('ROLE_USER'),
),
));
$user = $provider->loadUserByUsername('fabien');
$this->assertEquals('foo', $user->getPassword());
$this->assertEquals(array('ROLE_USER'), $user->getRoles());
$this->assertFalse($user->isEnabled());
}
public function testCreateUser()
{
$provider = new InMemoryUserProvider();
$provider->createUser(new User('fabien', 'foo'));
$this->assertEquals('foo', $provider->loadUserByUsername('fabien')->getPassword());
}
/**
* @expectedException LogicException
*/
public function testCreateUserAlreadyExist()
{
$provider = new InMemoryUserProvider();
$provider->createUser(new User('fabien', 'foo'));
$provider->createUser(new User('fabien', 'foo'));
}
/**
* @expectedException Symfony\Component\Security\Exception\UsernameNotFoundException
*/
public function testLoadUserByUsernameDoesNotExist()
{
$provider = new InMemoryUserProvider();
$provider->loadUserByUsername('fabien');
}
}

View File

@ -0,0 +1,134 @@
<?php
/*
* This file is part of the Symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Security\User;
use Symfony\Component\Security\User\User;
class UserTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\Security\User\User::__construct
* @expectedException InvalidArgumentException
*/
public function testConstructorException()
{
new User('', 'superpass');
}
/**
* @covers Symfony\Component\Security\User\User::__construct
* @covers Symfony\Component\Security\User\User::getRoles
*/
public function testGetRoles()
{
$user = new User('fabien', 'superpass');
$this->assertEquals(array(), $user->getRoles());
$user = new User('fabien', 'superpass', array('ROLE_ADMIN'));
$this->assertEquals(array('ROLE_ADMIN'), $user->getRoles());
}
/**
* @covers Symfony\Component\Security\User\User::__construct
* @covers Symfony\Component\Security\User\User::getPassword
*/
public function testGetPassord()
{
$user = new User('fabien', 'superpass');
$this->assertEquals('superpass', $user->getPassword());
}
/**
* @covers Symfony\Component\Security\User\User::__construct
* @covers Symfony\Component\Security\User\User::getUsername
*/
public function testGetUsername()
{
$user = new User('fabien', 'superpass');
$this->assertEquals('fabien', $user->getUsername());
}
/**
* @covers Symfony\Component\Security\User\User::getSalt
*/
public function testGetSalt()
{
$user = new User('fabien', 'superpass');
$this->assertEquals('', $user->getSalt());
}
/**
* @covers Symfony\Component\Security\User\User::isAccountNonExpired
*/
public function testIsAccountNonExpired()
{
$user = new User('fabien', 'superpass');
$this->assertTrue($user->isAccountNonExpired());
$user = new User('fabien', 'superpass', array(), true, false);
$this->assertFalse($user->isAccountNonExpired());
}
/**
* @covers Symfony\Component\Security\User\User::isCredentialsNonExpired
*/
public function testIsCredentialsNonExpired()
{
$user = new User('fabien', 'superpass');
$this->assertTrue($user->isCredentialsNonExpired());
$user = new User('fabien', 'superpass', array(), true, true, false);
$this->assertFalse($user->isCredentialsNonExpired());
}
/**
* @covers Symfony\Component\Security\User\User::isAccountNonLocked
*/
public function testIsAccountNonLocked()
{
$user = new User('fabien', 'superpass');
$this->assertTrue($user->isAccountNonLocked());
$user = new User('fabien', 'superpass', array(), true, true, true, false);
$this->assertFalse($user->isAccountNonLocked());
}
/**
* @covers Symfony\Component\Security\User\User::isEnabled
*/
public function testIsEnabled()
{
$user = new User('fabien', 'superpass');
$this->assertTrue($user->isEnabled());
$user = new User('fabien', 'superpass', array(), false);
$this->assertFalse($user->isEnabled());
}
/**
* @covers Symfony\Component\Security\User\User::eraseCredentials
*/
public function testEraseCredentials()
{
$user = new User('fabien', 'superpass');
$user->eraseCredentials();
$this->assertNull($user->getPassword());
}
/**
* @covers Symfony\Component\Security\User\User::__toString
*/
public function testMagicToString()
{
$user = new User('fabien', 'superpass');
$this->assertEquals('fabien', (string) $user);
}
}