PreAuthenticatedAuthenticationProviderTest.php 3.9 KB

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