summaryrefslogtreecommitdiffstats
path: root/Tests/Core/Authentication/Provider
diff options
context:
space:
mode:
Diffstat (limited to 'Tests/Core/Authentication/Provider')
-rw-r--r--Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php66
-rw-r--r--Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php300
-rw-r--r--Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php133
-rw-r--r--Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php111
-rw-r--r--Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php206
5 files changed, 0 insertions, 816 deletions
diff --git a/Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php
deleted file mode 100644
index d0da147..0000000
--- a/Tests/Core/Authentication/Provider/AnonymousAuthenticationProviderTest.php
+++ /dev/null
@@ -1,66 +0,0 @@
-<?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\Tests\Core\Authentication\Provider;
-
-use Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider;
-
-class AnonymousAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
-{
- public function testSupports()
- {
- $provider = $this->getProvider('foo');
-
- $this->assertTrue($provider->supports($this->getSupportedToken('foo')));
- $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
- }
-
- public function testAuthenticateWhenTokenIsNotSupported()
- {
- $provider = $this->getProvider('foo');
-
- $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testAuthenticateWhenKeyIsNotValid()
- {
- $provider = $this->getProvider('foo');
-
- $this->assertNull($provider->authenticate($this->getSupportedToken('bar')));
- }
-
- public function testAuthenticate()
- {
- $provider = $this->getProvider('foo');
- $token = $this->getSupportedToken('foo');
-
- $this->assertSame($token, $provider->authenticate($token));
- }
-
- protected function getSupportedToken($key)
- {
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken', array('getKey'), array(), '', false);
- $token->expects($this->any())
- ->method('getKey')
- ->will($this->returnValue($key))
- ;
-
- return $token;
- }
-
- protected function getProvider($key)
- {
- return new AnonymousAuthenticationProvider($key);
- }
-}
diff --git a/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php
deleted file mode 100644
index 8b27061..0000000
--- a/Tests/Core/Authentication/Provider/DaoAuthenticationProviderTest.php
+++ /dev/null
@@ -1,300 +0,0 @@
-<?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\Tests\Core\Authentication\Provider;
-
-use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
-
-use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
-
-class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
-{
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
- */
- public function testRetrieveUserWhenProviderDoesNotReturnAnUserInterface()
- {
- $provider = $this->getProvider('fabien');
- $method = new \ReflectionMethod($provider, 'retrieveUser');
- $method->setAccessible(true);
-
- $method->invoke($provider, 'fabien', $this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
- */
- public function testRetrieveUserWhenUsernameIsNotFound()
- {
- $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
- $userProvider->expects($this->once())
- ->method('loadUserByUsername')
- ->will($this->throwException($this->getMock('Symfony\\Component\\Security\\Core\\Exception\\UsernameNotFoundException', null, array(), '', false)))
- ;
-
- $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
- $method = new \ReflectionMethod($provider, 'retrieveUser');
- $method->setAccessible(true);
-
- $method->invoke($provider, 'fabien', $this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
- */
- public function testRetrieveUserWhenAnExceptionOccurs()
- {
- $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\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\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
- $method = new \ReflectionMethod($provider, 'retrieveUser');
- $method->setAccessible(true);
-
- $method->invoke($provider, 'fabien', $this->getSupportedToken());
- }
-
- public function testRetrieveUserReturnsUserFromTokenOnReauthentication()
- {
- $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
- $userProvider->expects($this->never())
- ->method('loadUserByUsername')
- ;
-
- $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
- $token = $this->getSupportedToken();
- $token->expects($this->once())
- ->method('getUser')
- ->will($this->returnValue($user))
- ;
-
- $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
- $reflection = new \ReflectionMethod($provider, 'retrieveUser');
- $reflection->setAccessible(true);
- $result = $reflection->invoke($provider, null, $token);
-
- $this->assertSame($user, $result);
- }
-
- public function testRetrieveUser()
- {
- $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
-
- $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
- $userProvider->expects($this->once())
- ->method('loadUserByUsername')
- ->will($this->returnValue($user))
- ;
-
- $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
- $method = new \ReflectionMethod($provider, 'retrieveUser');
- $method->setAccessible(true);
-
- $this->assertSame($user, $method->invoke($provider, 'fabien', $this->getSupportedToken()));
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testCheckAuthenticationWhenCredentialsAreEmpty()
- {
- $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface');
- $encoder
- ->expects($this->never())
- ->method('isPasswordValid')
- ;
-
- $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(''))
- ;
-
- $method->invoke(
- $provider,
- $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'),
- $token
- );
- }
-
- public function testCheckAuthenticationWhenCredentialsAre0()
- {
- $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\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('0'))
- ;
-
- $method->invoke(
- $provider,
- $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'),
- $token
- );
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testCheckAuthenticationWhenCredentialsAreNotValid()
- {
- $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\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\\Core\\User\\UserInterface'), $token);
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged()
- {
- $user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface');
- $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\\Core\\User\\UserInterface');
- $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\\Core\\User\\UserInterface');
- $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\\Core\\User\\UserInterface');
- $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);
- }
-
- public function testCheckAuthentication()
- {
- $encoder = $this->getMock('Symfony\\Component\\Security\\Core\\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\\Core\\User\\UserInterface'), $token);
- }
-
- protected function getSupportedToken()
- {
- $mock = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false);
- $mock
- ->expects($this->any())
- ->method('getProviderKey')
- ->will($this->returnValue('key'))
- ;
-
- return $mock;
- }
-
- protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
- {
- $userProvider = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserProviderInterface');
- if (false !== $user) {
- $userProvider->expects($this->once())
- ->method('loadUserByUsername')
- ->will($this->returnValue($user))
- ;
- }
-
- if (false === $userChecker) {
- $userChecker = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface');
- }
-
- if (null === $passwordEncoder) {
- $passwordEncoder = new PlaintextPasswordEncoder();
- }
-
- $encoderFactory = $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface');
- $encoderFactory
- ->expects($this->any())
- ->method('getEncoder')
- ->will($this->returnValue($passwordEncoder))
- ;
-
- return new DaoAuthenticationProvider($userProvider, $userChecker, 'key', $encoderFactory);
- }
-}
diff --git a/Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
deleted file mode 100644
index f7ffb1e..0000000
--- a/Tests/Core/Authentication/Provider/PreAuthenticatedAuthenticationProviderTest.php
+++ /dev/null
@@ -1,133 +0,0 @@
-<?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\Tests\Core\Authentication\Provider;
-
-use Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider;
-
-class PreAuthenticatedAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
-{
- public function testSupports()
- {
- $provider = $this->getProvider();
-
- $this->assertTrue($provider->supports($this->getSupportedToken()));
- $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
-
- $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken')
- ->disableOriginalConstructor()
- ->getMock()
- ;
- $token
- ->expects($this->once())
- ->method('getProviderKey')
- ->will($this->returnValue('foo'))
- ;
- $this->assertFalse($provider->supports($token));
- }
-
- public function testAuthenticateWhenTokenIsNotSupported()
- {
- $provider = $this->getProvider();
-
- $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testAuthenticateWhenNoUserIsSet()
- {
- $provider = $this->getProvider();
- $provider->authenticate($this->getSupportedToken(''));
- }
-
- public function testAuthenticate()
- {
- $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
- $user
- ->expects($this->once())
- ->method('getRoles')
- ->will($this->returnValue(array()))
- ;
- $provider = $this->getProvider($user);
-
- $token = $provider->authenticate($this->getSupportedToken('fabien', 'pass'));
- $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', $token);
- $this->assertEquals('pass', $token->getCredentials());
- $this->assertEquals('key', $token->getProviderKey());
- $this->assertEquals(array(), $token->getRoles());
- $this->assertEquals(array('foo' => 'bar'), $token->getAttributes(), '->authenticate() copies token attributes');
- $this->assertSame($user, $token->getUser());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\LockedException
- */
- public function testAuthenticateWhenUserCheckerThrowsException()
- {
- $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
-
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- $userChecker->expects($this->once())
- ->method('checkPostAuth')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\LockedException', null, array(), '', false)))
- ;
-
- $provider = $this->getProvider($user, $userChecker);
-
- $provider->authenticate($this->getSupportedToken('fabien'));
- }
-
- protected function getSupportedToken($user = false, $credentials = false)
- {
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken', array('getUser', 'getCredentials', 'getProviderKey'), array(), '', false);
- if (false !== $user) {
- $token->expects($this->once())
- ->method('getUser')
- ->will($this->returnValue($user))
- ;
- }
- if (false !== $credentials) {
- $token->expects($this->once())
- ->method('getCredentials')
- ->will($this->returnValue($credentials))
- ;
- }
-
- $token
- ->expects($this->any())
- ->method('getProviderKey')
- ->will($this->returnValue('key'))
- ;
-
- $token->setAttributes(array('foo' => 'bar'));
-
- return $token;
- }
-
- protected function getProvider($user = false, $userChecker = false)
- {
- $userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
- if (false !== $user) {
- $userProvider->expects($this->once())
- ->method('loadUserByUsername')
- ->will($this->returnValue($user))
- ;
- }
-
- if (false === $userChecker) {
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- }
-
- return new PreAuthenticatedAuthenticationProvider($userProvider, $userChecker, 'key');
- }
-}
diff --git a/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php
deleted file mode 100644
index 5e250e0..0000000
--- a/Tests/Core/Authentication/Provider/RememberMeAuthenticationProviderTest.php
+++ /dev/null
@@ -1,111 +0,0 @@
-<?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\Tests\Core\Authentication\Provider;
-
-use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
-use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
-use Symfony\Component\Security\Core\Role\Role;
-
-class RememberMeAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
-{
- public function testSupports()
- {
- $provider = $this->getProvider();
-
- $this->assertTrue($provider->supports($this->getSupportedToken()));
- $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
- }
-
- public function testAuthenticateWhenTokenIsNotSupported()
- {
- $provider = $this->getProvider();
-
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
- $this->assertNull($provider->authenticate($token));
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testAuthenticateWhenKeysDoNotMatch()
- {
- $provider = $this->getProvider(null, 'key1');
- $token = $this->getSupportedToken(null, 'key2');
-
- $provider->authenticate($token);
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
- */
- public function testAuthenticateWhenPostChecksFails()
- {
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- $userChecker->expects($this->once())
- ->method('checkPostAuth')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\AccountExpiredException', null, array(), '', false)))
- ;
-
- $provider = $this->getProvider($userChecker);
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- public function testAuthenticate()
- {
- $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
- $user->expects($this->exactly(2))
- ->method('getRoles')
- ->will($this->returnValue(array('ROLE_FOO')))
- ;
-
- $provider = $this->getProvider();
-
- $token = $this->getSupportedToken($user);
- $authToken = $provider->authenticate($token);
-
- $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', $authToken);
- $this->assertSame($user, $authToken->getUser());
- $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
- $this->assertEquals('', $authToken->getCredentials());
- }
-
- protected function getSupportedToken($user = null, $key = 'test')
- {
- if (null === $user) {
- $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
- $user
- ->expects($this->any())
- ->method('getRoles')
- ->will($this->returnValue(array()))
- ;
- }
-
- $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('getProviderKey'), array($user, 'foo', $key));
- $token
- ->expects($this->once())
- ->method('getProviderKey')
- ->will($this->returnValue('foo'))
- ;
-
- return $token;
- }
-
- protected function getProvider($userChecker = null, $key = 'test')
- {
- if (null === $userChecker) {
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- }
-
- return new RememberMeAuthenticationProvider($userChecker, $key, 'foo');
- }
-}
diff --git a/Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php b/Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php
deleted file mode 100644
index 1516a5f..0000000
--- a/Tests/Core/Authentication/Provider/UserAuthenticationProviderTest.php
+++ /dev/null
@@ -1,206 +0,0 @@
-<?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\Tests\Core\Authentication\Provider;
-
-use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
-use Symfony\Component\Security\Core\Role\Role;
-use Symfony\Component\Security\Core\Exception\BadCredentialsException;
-
-class UserAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
-{
- public function testSupports()
- {
- $provider = $this->getProvider();
-
- $this->assertTrue($provider->supports($this->getSupportedToken()));
- $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
- }
-
- public function testAuthenticateWhenTokenIsNotSupported()
- {
- $provider = $this->getProvider();
-
- $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')));
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
- */
- public function testAuthenticateWhenUsernameIsNotFound()
- {
- $provider = $this->getProvider(false, false);
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- */
- public function testAuthenticateWhenUsernameIsNotFoundAndHideIsTrue()
- {
- $provider = $this->getProvider(false, true);
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\UsernameNotFoundException', null, array(), '', false)))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationServiceException
- */
- public function testAuthenticateWhenProviderDoesNotReturnAnUserInterface()
- {
- $provider = $this->getProvider(false, true);
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->returnValue(null))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\CredentialsExpiredException
- */
- public function testAuthenticateWhenPreChecksFails()
- {
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- $userChecker->expects($this->once())
- ->method('checkPreAuth')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\CredentialsExpiredException', null, array(), '', false)))
- ;
-
- $provider = $this->getProvider($userChecker);
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
- */
- public function testAuthenticateWhenPostChecksFails()
- {
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- $userChecker->expects($this->once())
- ->method('checkPostAuth')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\AccountExpiredException', null, array(), '', false)))
- ;
-
- $provider = $this->getProvider($userChecker);
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- * @expectedExceptionMessage Bad credentials
- */
- public function testAuthenticateWhenPostCheckAuthenticationFails()
- {
- $provider = $this->getProvider();
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
- ;
- $provider->expects($this->once())
- ->method('checkAuthentication')
- ->will($this->throwException($this->getMock('Symfony\Component\Security\Core\Exception\BadCredentialsException', null, array(), '', false)))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- /**
- * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
- * @expectedExceptionMessage Foo
- */
- public function testAuthenticateWhenPostCheckAuthenticationFailsWithHideFalse()
- {
- $provider = $this->getProvider(false, false);
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->returnValue($this->getMock('Symfony\Component\Security\Core\User\UserInterface')))
- ;
- $provider->expects($this->once())
- ->method('checkAuthentication')
- ->will($this->throwException(new BadCredentialsException('Foo')))
- ;
-
- $provider->authenticate($this->getSupportedToken());
- }
-
- public function testAuthenticate()
- {
- $user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
- $user->expects($this->once())
- ->method('getRoles')
- ->will($this->returnValue(array('ROLE_FOO')))
- ;
-
- $provider = $this->getProvider();
- $provider->expects($this->once())
- ->method('retrieveUser')
- ->will($this->returnValue($user))
- ;
-
- $token = $this->getSupportedToken();
- $token->expects($this->once())
- ->method('getCredentials')
- ->will($this->returnValue('foo'))
- ;
-
- $authToken = $provider->authenticate($token);
-
- $this->assertInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $authToken);
- $this->assertSame($user, $authToken->getUser());
- $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
- $this->assertEquals('foo', $authToken->getCredentials());
- $this->assertEquals(array('foo' => 'bar'), $authToken->getAttributes(), '->authenticate() copies token attributes');
- }
-
- protected function getSupportedToken()
- {
- $mock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getProviderKey'), array(), '', false);
- $mock
- ->expects($this->any())
- ->method('getProviderKey')
- ->will($this->returnValue('key'))
- ;
-
- $mock->setAttributes(array('foo' => 'bar'));
-
- return $mock;
- }
-
- protected function getProvider($userChecker = false, $hide = true)
- {
- if (false === $userChecker) {
- $userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
- }
-
- return $this->getMockForAbstractClass('Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider', array($userChecker, 'key', $hide));
- }
-}