2010-10-31 14:41:15 +00:00
|
|
|
<?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\Authentication\Provider;
|
|
|
|
|
|
|
|
use Symfony\Component\Security\Authentication\Provider\DaoAuthenticationProvider;
|
|
|
|
|
|
|
|
class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
|
|
|
|
*/
|
|
|
|
public function testRetrieveUserWhenProviderDoesNotReturnAnAccountInterface()
|
|
|
|
{
|
|
|
|
$provider = $this->getProvider('fabien');
|
|
|
|
$method = new \ReflectionMethod($provider, 'retrieveUser');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$method->invoke($provider, 'fabien', $this->getSupportedToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Security\Exception\UsernameNotFoundException
|
|
|
|
*/
|
|
|
|
public function testRetrieveUserWhenUsernameIsNotFound()
|
|
|
|
{
|
|
|
|
$userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
|
|
|
|
$userProvider->expects($this->once())
|
|
|
|
->method('loadUserByUsername')
|
|
|
|
->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\UsernameNotFoundException', null, array(), '', false)))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
|
|
|
|
$method = new \ReflectionMethod($provider, 'retrieveUser');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$method->invoke($provider, 'fabien', $this->getSupportedToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
|
|
|
|
*/
|
|
|
|
public function testRetrieveUserWhenAnExceptionOccurs()
|
|
|
|
{
|
|
|
|
$userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
|
|
|
|
$userProvider->expects($this->once())
|
|
|
|
->method('loadUserByUsername')
|
|
|
|
->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false)))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
|
|
|
|
$method = new \ReflectionMethod($provider, 'retrieveUser');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$method->invoke($provider, 'fabien', $this->getSupportedToken());
|
|
|
|
}
|
|
|
|
|
2010-12-08 12:51:26 +00:00
|
|
|
public function testRetrieveUserReturnsUserFromTokenOnReauthentication()
|
|
|
|
{
|
|
|
|
$userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
|
|
|
|
$userProvider->expects($this->never())
|
|
|
|
->method('loadUserByUsername')
|
|
|
|
;
|
|
|
|
|
|
|
|
$user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
|
|
|
|
$token = $this->getSupportedToken();
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
|
|
|
|
$reflection = new \ReflectionMethod($provider, 'retrieveUser');
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
$result = $reflection->invoke($provider, null, $token);
|
|
|
|
|
2010-12-16 21:27:16 +00:00
|
|
|
$this->assertSame($user, $result);
|
2010-12-08 12:51:26 +00:00
|
|
|
}
|
|
|
|
|
2010-10-31 14:41:15 +00:00
|
|
|
public function testRetrieveUser()
|
|
|
|
{
|
|
|
|
$user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
|
|
|
|
|
|
|
|
$userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
|
|
|
|
$userProvider->expects($this->once())
|
|
|
|
->method('loadUserByUsername')
|
2010-12-16 21:27:16 +00:00
|
|
|
->will($this->returnValue($user))
|
2010-10-31 14:41:15 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
|
|
|
|
$method = new \ReflectionMethod($provider, 'retrieveUser');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
2010-12-16 21:27:16 +00:00
|
|
|
$this->assertSame($user, $method->invoke($provider, 'fabien', $this->getSupportedToken()));
|
2010-10-31 14:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Security\Exception\BadCredentialsException
|
|
|
|
*/
|
|
|
|
public function testCheckAuthenticationWhenCredentialsAreEmpty()
|
|
|
|
{
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
$method = new \ReflectionMethod($provider, 'checkAuthentication');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$token = $this->getSupportedToken();
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getCredentials')
|
|
|
|
->will($this->returnValue(''))
|
|
|
|
;
|
|
|
|
|
|
|
|
$method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Security\Exception\BadCredentialsException
|
|
|
|
*/
|
|
|
|
public function testCheckAuthenticationWhenCredentialsAreNotValid()
|
|
|
|
{
|
|
|
|
$encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
|
|
|
|
$encoder->expects($this->once())
|
|
|
|
->method('isPasswordValid')
|
|
|
|
->will($this->returnValue(false))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = $this->getProvider(false, false, $encoder);
|
|
|
|
$method = new \ReflectionMethod($provider, 'checkAuthentication');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$token = $this->getSupportedToken();
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getCredentials')
|
|
|
|
->will($this->returnValue('foo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
|
|
|
|
}
|
|
|
|
|
2010-12-08 12:51:26 +00:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\Security\Exception\BadCredentialsException
|
|
|
|
*/
|
|
|
|
public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged()
|
|
|
|
{
|
|
|
|
$user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->will($this->returnValue('foo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$token = $this->getSupportedToken();
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
|
|
|
$dbUser = $this->getMock('Symfony\Component\Security\User\AccountInterface');
|
|
|
|
$dbUser->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->will($this->returnValue('newFoo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = $this->getProvider(false, false, null);
|
|
|
|
$reflection = new \ReflectionMethod($provider, 'checkAuthentication');
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
$reflection->invoke($provider, $dbUser, $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithoutOriginalCredentials()
|
|
|
|
{
|
|
|
|
$user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->will($this->returnValue('foo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$token = $this->getSupportedToken();
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
|
|
|
$dbUser = $this->getMock('Symfony\Component\Security\User\AccountInterface');
|
|
|
|
$dbUser->expects($this->once())
|
|
|
|
->method('getPassword')
|
|
|
|
->will($this->returnValue('foo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = $this->getProvider(false, false, null);
|
|
|
|
$reflection = new \ReflectionMethod($provider, 'checkAuthentication');
|
|
|
|
$reflection->setAccessible(true);
|
|
|
|
$reflection->invoke($provider, $dbUser, $token);
|
|
|
|
}
|
|
|
|
|
2010-10-31 14:41:15 +00:00
|
|
|
public function testCheckAuthentication()
|
|
|
|
{
|
|
|
|
$encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
|
|
|
|
$encoder->expects($this->once())
|
|
|
|
->method('isPasswordValid')
|
|
|
|
->will($this->returnValue(true))
|
|
|
|
;
|
|
|
|
|
|
|
|
$provider = $this->getProvider(false, false, $encoder);
|
|
|
|
$method = new \ReflectionMethod($provider, 'checkAuthentication');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
$token = $this->getSupportedToken();
|
|
|
|
$token->expects($this->once())
|
|
|
|
->method('getCredentials')
|
|
|
|
->will($this->returnValue('foo'))
|
|
|
|
;
|
|
|
|
|
|
|
|
$method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getSupportedToken()
|
|
|
|
{
|
2010-12-16 21:27:16 +00:00
|
|
|
return $this->getMock('Symfony\Component\Security\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getUser'), array(), '', false);
|
2010-10-31 14:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
|
|
|
|
{
|
|
|
|
$userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
|
|
|
|
if (false !== $user) {
|
|
|
|
$userProvider->expects($this->once())
|
|
|
|
->method('loadUserByUsername')
|
|
|
|
->will($this->returnValue($user))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $userChecker) {
|
|
|
|
$userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DaoAuthenticationProvider($userProvider, $userChecker, $passwordEncoder);
|
|
|
|
}
|
|
|
|
}
|