DaoAuthenticationProviderTest.php 9.3 KB

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