PreAuthenticatedAuthenticationProviderTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Security\Authentication\Provider;
  11. use Symfony\Component\Security\Authentication\Provider\PreAuthenticatedAuthenticationProvider;
  12. class PreAuthenticatedAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testSupports()
  15. {
  16. $provider = $this->getProvider();
  17. $this->assertTrue($provider->supports($this->getSupportedToken()));
  18. $this->assertFalse($provider->supports($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
  19. }
  20. public function testAuthenticateWhenTokenIsNotSupported()
  21. {
  22. $provider = $this->getProvider();
  23. $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
  24. }
  25. /**
  26. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  27. */
  28. public function testAuthenticateWhenNoUserIsSet()
  29. {
  30. $provider = $this->getProvider();
  31. $provider->authenticate($this->getSupportedToken(''));
  32. }
  33. public function testAuthenticate()
  34. {
  35. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  36. $provider = $this->getProvider($user);
  37. $token = $provider->authenticate($this->getSupportedToken('fabien', 'pass'));
  38. $this->assertInstanceOf('Symfony\Component\Security\Authentication\Token\PreAuthenticatedToken', $token);
  39. $this->assertEquals('pass', $token->getCredentials());
  40. $this->assertEquals(array(), $token->getRoles());
  41. $this->assertSame($user, $token->getUser());
  42. }
  43. /**
  44. * @expectedException Symfony\Component\Security\Exception\LockedException
  45. */
  46. public function testAuthenticateWhenAccountCheckerThrowsException()
  47. {
  48. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  49. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  50. $userChecker->expects($this->once())
  51. ->method('checkPostAuth')
  52. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\LockedException', null, array(), '', false)))
  53. ;
  54. $provider = $this->getProvider($user, $userChecker);
  55. $provider->authenticate($this->getSupportedToken('fabien'));
  56. }
  57. protected function getSupportedToken($user = false, $credentials = false)
  58. {
  59. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\PreAuthenticatedToken', array('getUser', 'getCredentials'), array(), '', false);
  60. if (false !== $user) {
  61. $token->expects($this->once())
  62. ->method('getUser')
  63. ->will($this->returnValue($user))
  64. ;
  65. }
  66. if (false !== $credentials) {
  67. $token->expects($this->once())
  68. ->method('getCredentials')
  69. ->will($this->returnValue($credentials))
  70. ;
  71. }
  72. return $token;
  73. }
  74. protected function getProvider($user = false, $userChecker = false)
  75. {
  76. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  77. if (false !== $user) {
  78. $userProvider->expects($this->once())
  79. ->method('loadUserByUsername')
  80. ->will($this->returnValue($user))
  81. ;
  82. }
  83. if (false === $userChecker) {
  84. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  85. }
  86. return new PreAuthenticatedAuthenticationProvider($userProvider, $userChecker);
  87. }
  88. }