UserAuthenticationProviderTest.php 6.6 KB

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