UserAuthenticationProviderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\UserAuthenticationProvider;
  11. use Symfony\Component\Security\Role\Role;
  12. class UserAuthenticationProviderTest 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\UsernameNotFoundException
  27. */
  28. public function testAuthenticateWhenUsernameIsNotFound()
  29. {
  30. $provider = $this->getProvider(false, false);
  31. $provider->expects($this->once())
  32. ->method('retrieveUser')
  33. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\UsernameNotFoundException', null, array(), '', false)))
  34. ;
  35. $provider->authenticate($this->getSupportedToken());
  36. }
  37. /**
  38. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  39. */
  40. public function testAuthenticateWhenUsernameIsNotFoundAndHideIsTrue()
  41. {
  42. $provider = $this->getProvider(false, true);
  43. $provider->expects($this->once())
  44. ->method('retrieveUser')
  45. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\UsernameNotFoundException', null, array(), '', false)))
  46. ;
  47. $provider->authenticate($this->getSupportedToken());
  48. }
  49. /**
  50. * @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
  51. */
  52. public function testAuthenticateWhenProviderDoesNotReturnAnAccountInterface()
  53. {
  54. $provider = $this->getProvider(false, true);
  55. $provider->expects($this->once())
  56. ->method('retrieveUser')
  57. ->will($this->returnValue(null))
  58. ;
  59. $provider->authenticate($this->getSupportedToken());
  60. }
  61. /**
  62. * @expectedException Symfony\Component\Security\Exception\CredentialsExpiredException
  63. */
  64. public function testAuthenticateWhenPreChecksFails()
  65. {
  66. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  67. $userChecker->expects($this->once())
  68. ->method('checkPreAuth')
  69. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\CredentialsExpiredException', null, array(), '', false)))
  70. ;
  71. $provider = $this->getProvider($userChecker);
  72. $provider->expects($this->once())
  73. ->method('retrieveUser')
  74. ->will($this->returnValue($this->getMock('Symfony\Component\Security\User\AccountInterface')))
  75. ;
  76. $provider->authenticate($this->getSupportedToken());
  77. }
  78. /**
  79. * @expectedException Symfony\Component\Security\Exception\AccountExpiredException
  80. */
  81. public function testAuthenticateWhenPostChecksFails()
  82. {
  83. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  84. $userChecker->expects($this->once())
  85. ->method('checkPostAuth')
  86. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\AccountExpiredException', null, array(), '', false)))
  87. ;
  88. $provider = $this->getProvider($userChecker);
  89. $provider->expects($this->once())
  90. ->method('retrieveUser')
  91. ->will($this->returnValue($this->getMock('Symfony\Component\Security\User\AccountInterface')))
  92. ;
  93. $provider->authenticate($this->getSupportedToken());
  94. }
  95. /**
  96. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  97. */
  98. public function testAuthenticateWhenPostCheckAuthenticationFails()
  99. {
  100. $provider = $this->getProvider();
  101. $provider->expects($this->once())
  102. ->method('retrieveUser')
  103. ->will($this->returnValue($this->getMock('Symfony\Component\Security\User\AccountInterface')))
  104. ;
  105. $provider->expects($this->once())
  106. ->method('checkAuthentication')
  107. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\BadCredentialsException', null, array(), '', false)))
  108. ;
  109. $provider->authenticate($this->getSupportedToken());
  110. }
  111. public function testAuthenticate()
  112. {
  113. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  114. $user->expects($this->once())
  115. ->method('getRoles')
  116. ->will($this->returnValue(array('ROLE_FOO')))
  117. ;
  118. $provider = $this->getProvider();
  119. $provider->expects($this->once())
  120. ->method('retrieveUser')
  121. ->will($this->returnValue($user))
  122. ;
  123. $token = $this->getSupportedToken();
  124. $token->expects($this->once())
  125. ->method('getCredentials')
  126. ->will($this->returnValue('foo'))
  127. ;
  128. $authToken = $provider->authenticate($token);
  129. $this->assertInstanceOf('Symfony\Component\Security\Authentication\Token\UsernamePasswordToken', $authToken);
  130. $this->assertSame($user, $authToken->getUser());
  131. $this->assertEquals(array(new Role('ROLE_FOO')), $authToken->getRoles());
  132. $this->assertEquals('foo', $authToken->getCredentials());
  133. }
  134. protected function getSupportedToken()
  135. {
  136. return $this->getMock('Symfony\Component\Security\Authentication\Token\UsernamePasswordToken', array('getCredentials'), array(), '', false);
  137. }
  138. protected function getProvider($userChecker = false, $hide = true)
  139. {
  140. if (false === $userChecker) {
  141. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  142. }
  143. return $this->getMockForAbstractClass('Symfony\Component\Security\Authentication\Provider\UserAuthenticationProvider', array($userChecker, $hide));
  144. }
  145. }