PreAuthenticatedAuthenticationProviderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $token = $this->getMockBuilder('Symfony\Component\Security\Authentication\Token\PreAuthenticatedToken')
  20. ->disableOriginalConstructor()
  21. ->getMock()
  22. ;
  23. $token
  24. ->expects($this->once())
  25. ->method('getProviderKey')
  26. ->will($this->returnValue('foo'))
  27. ;
  28. $this->assertFalse($provider->supports($token));
  29. }
  30. public function testAuthenticateWhenTokenIsNotSupported()
  31. {
  32. $provider = $this->getProvider();
  33. $this->assertNull($provider->authenticate($this->getMock('Symfony\Component\Security\Authentication\Token\TokenInterface')));
  34. }
  35. /**
  36. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  37. */
  38. public function testAuthenticateWhenNoUserIsSet()
  39. {
  40. $provider = $this->getProvider();
  41. $provider->authenticate($this->getSupportedToken(''));
  42. }
  43. public function testAuthenticate()
  44. {
  45. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  46. $provider = $this->getProvider($user);
  47. $token = $provider->authenticate($this->getSupportedToken('fabien', 'pass'));
  48. $this->assertInstanceOf('Symfony\Component\Security\Authentication\Token\PreAuthenticatedToken', $token);
  49. $this->assertEquals('pass', $token->getCredentials());
  50. $this->assertEquals(array(), $token->getRoles());
  51. $this->assertSame($user, $token->getUser());
  52. }
  53. /**
  54. * @expectedException Symfony\Component\Security\Exception\LockedException
  55. */
  56. public function testAuthenticateWhenAccountCheckerThrowsException()
  57. {
  58. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  59. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  60. $userChecker->expects($this->once())
  61. ->method('checkPostAuth')
  62. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\LockedException', null, array(), '', false)))
  63. ;
  64. $provider = $this->getProvider($user, $userChecker);
  65. $provider->authenticate($this->getSupportedToken('fabien'));
  66. }
  67. protected function getSupportedToken($user = false, $credentials = false)
  68. {
  69. $token = $this->getMock('Symfony\Component\Security\Authentication\Token\PreAuthenticatedToken', array('getUser', 'getCredentials', 'getProviderKey'), array(), '', false);
  70. if (false !== $user) {
  71. $token->expects($this->once())
  72. ->method('getUser')
  73. ->will($this->returnValue($user))
  74. ;
  75. }
  76. if (false !== $credentials) {
  77. $token->expects($this->once())
  78. ->method('getCredentials')
  79. ->will($this->returnValue($credentials))
  80. ;
  81. }
  82. $token
  83. ->expects($this->any())
  84. ->method('getProviderKey')
  85. ->will($this->returnValue('key'))
  86. ;
  87. return $token;
  88. }
  89. protected function getProvider($user = false, $userChecker = false)
  90. {
  91. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  92. if (false !== $user) {
  93. $userProvider->expects($this->once())
  94. ->method('loadUserByUsername')
  95. ->will($this->returnValue($user))
  96. ;
  97. }
  98. if (false === $userChecker) {
  99. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  100. }
  101. return new PreAuthenticatedAuthenticationProvider($userProvider, $userChecker, 'key');
  102. }
  103. }