DaoAuthenticationProviderTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Encoder\EncoderFactory;
  11. use Symfony\Component\Security\Encoder\PlaintextPasswordEncoder;
  12. use Symfony\Component\Security\Authentication\Provider\DaoAuthenticationProvider;
  13. class DaoAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
  17. */
  18. public function testRetrieveUserWhenProviderDoesNotReturnAnAccountInterface()
  19. {
  20. $provider = $this->getProvider('fabien');
  21. $method = new \ReflectionMethod($provider, 'retrieveUser');
  22. $method->setAccessible(true);
  23. $method->invoke($provider, 'fabien', $this->getSupportedToken());
  24. }
  25. /**
  26. * @expectedException Symfony\Component\Security\Exception\UsernameNotFoundException
  27. */
  28. public function testRetrieveUserWhenUsernameIsNotFound()
  29. {
  30. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  31. $userProvider->expects($this->once())
  32. ->method('loadUserByUsername')
  33. ->will($this->throwException($this->getMock('Symfony\Component\Security\Exception\UsernameNotFoundException', null, array(), '', false)))
  34. ;
  35. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'), $this->getMock('Symfony\Component\Security\Encoder\EncoderFactoryInterface'));
  36. $method = new \ReflectionMethod($provider, 'retrieveUser');
  37. $method->setAccessible(true);
  38. $method->invoke($provider, 'fabien', $this->getSupportedToken());
  39. }
  40. /**
  41. * @expectedException Symfony\Component\Security\Exception\AuthenticationServiceException
  42. */
  43. public function testRetrieveUserWhenAnExceptionOccurs()
  44. {
  45. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  46. $userProvider->expects($this->once())
  47. ->method('loadUserByUsername')
  48. ->will($this->throwException($this->getMock('RuntimeException', null, array(), '', false)))
  49. ;
  50. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'), $this->getMock('Symfony\Component\Security\Encoder\EncoderFactoryInterface'));
  51. $method = new \ReflectionMethod($provider, 'retrieveUser');
  52. $method->setAccessible(true);
  53. $method->invoke($provider, 'fabien', $this->getSupportedToken());
  54. }
  55. public function testRetrieveUserReturnsUserFromTokenOnReauthentication()
  56. {
  57. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  58. $userProvider->expects($this->never())
  59. ->method('loadUserByUsername')
  60. ;
  61. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  62. $token = $this->getSupportedToken();
  63. $token->expects($this->once())
  64. ->method('getUser')
  65. ->will($this->returnValue($user))
  66. ;
  67. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'), $this->getMock('Symfony\Component\Security\Encoder\EncoderFactoryInterface'));
  68. $reflection = new \ReflectionMethod($provider, 'retrieveUser');
  69. $reflection->setAccessible(true);
  70. $result = $reflection->invoke($provider, null, $token);
  71. $this->assertSame($user, $result);
  72. }
  73. public function testRetrieveUser()
  74. {
  75. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  76. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  77. $userProvider->expects($this->once())
  78. ->method('loadUserByUsername')
  79. ->will($this->returnValue($user))
  80. ;
  81. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'), $this->getMock('Symfony\Component\Security\Encoder\EncoderFactoryInterface'));
  82. $method = new \ReflectionMethod($provider, 'retrieveUser');
  83. $method->setAccessible(true);
  84. $this->assertSame($user, $method->invoke($provider, 'fabien', $this->getSupportedToken()));
  85. }
  86. /**
  87. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  88. */
  89. public function testCheckAuthenticationWhenCredentialsAreEmpty()
  90. {
  91. $provider = $this->getProvider();
  92. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  93. $method->setAccessible(true);
  94. $token = $this->getSupportedToken();
  95. $token->expects($this->once())
  96. ->method('getCredentials')
  97. ->will($this->returnValue(''))
  98. ;
  99. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  100. }
  101. /**
  102. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  103. */
  104. public function testCheckAuthenticationWhenCredentialsAreNotValid()
  105. {
  106. $encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
  107. $encoder->expects($this->once())
  108. ->method('isPasswordValid')
  109. ->will($this->returnValue(false))
  110. ;
  111. $provider = $this->getProvider(false, false, $encoder);
  112. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  113. $method->setAccessible(true);
  114. $token = $this->getSupportedToken();
  115. $token->expects($this->once())
  116. ->method('getCredentials')
  117. ->will($this->returnValue('foo'))
  118. ;
  119. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  120. }
  121. /**
  122. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  123. */
  124. public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged()
  125. {
  126. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  127. $user->expects($this->once())
  128. ->method('getPassword')
  129. ->will($this->returnValue('foo'))
  130. ;
  131. $token = $this->getSupportedToken();
  132. $token->expects($this->once())
  133. ->method('getUser')
  134. ->will($this->returnValue($user));
  135. $dbUser = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  136. $dbUser->expects($this->once())
  137. ->method('getPassword')
  138. ->will($this->returnValue('newFoo'))
  139. ;
  140. $provider = $this->getProvider(false, false, null);
  141. $reflection = new \ReflectionMethod($provider, 'checkAuthentication');
  142. $reflection->setAccessible(true);
  143. $reflection->invoke($provider, $dbUser, $token);
  144. }
  145. public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithoutOriginalCredentials()
  146. {
  147. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  148. $user->expects($this->once())
  149. ->method('getPassword')
  150. ->will($this->returnValue('foo'))
  151. ;
  152. $token = $this->getSupportedToken();
  153. $token->expects($this->once())
  154. ->method('getUser')
  155. ->will($this->returnValue($user));
  156. $dbUser = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  157. $dbUser->expects($this->once())
  158. ->method('getPassword')
  159. ->will($this->returnValue('foo'))
  160. ;
  161. $provider = $this->getProvider(false, false, null);
  162. $reflection = new \ReflectionMethod($provider, 'checkAuthentication');
  163. $reflection->setAccessible(true);
  164. $reflection->invoke($provider, $dbUser, $token);
  165. }
  166. public function testCheckAuthentication()
  167. {
  168. $encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
  169. $encoder->expects($this->once())
  170. ->method('isPasswordValid')
  171. ->will($this->returnValue(true))
  172. ;
  173. $provider = $this->getProvider(false, false, $encoder);
  174. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  175. $method->setAccessible(true);
  176. $token = $this->getSupportedToken();
  177. $token->expects($this->once())
  178. ->method('getCredentials')
  179. ->will($this->returnValue('foo'))
  180. ;
  181. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  182. }
  183. protected function getSupportedToken()
  184. {
  185. return $this->getMock('Symfony\Component\Security\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getUser'), array(), '', false);
  186. }
  187. protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
  188. {
  189. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  190. if (false !== $user) {
  191. $userProvider->expects($this->once())
  192. ->method('loadUserByUsername')
  193. ->will($this->returnValue($user))
  194. ;
  195. }
  196. if (false === $userChecker) {
  197. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  198. }
  199. if (null === $passwordEncoder) {
  200. $passwordEncoder = new PlaintextPasswordEncoder();
  201. }
  202. $encoderFactory = $this->getMock('Symfony\Component\Security\Encoder\EncoderFactoryInterface');
  203. $encoderFactory
  204. ->expects($this->any())
  205. ->method('getEncoder')
  206. ->will($this->returnValue($passwordEncoder))
  207. ;
  208. return new DaoAuthenticationProvider($userProvider, $userChecker, $encoderFactory);
  209. }
  210. }