DaoAuthenticationProviderTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $token->expects($this->once())
  66. ->method('getUserProviderName')
  67. ->will($this->returnValue('foo'))
  68. ;
  69. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
  70. $reflection = new \ReflectionMethod($provider, 'retrieveUser');
  71. $reflection->setAccessible(true);
  72. $result = $reflection->invoke($provider, null, $token);
  73. $this->assertSame(array($user, 'foo'), $result);
  74. }
  75. public function testRetrieveUser()
  76. {
  77. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  78. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  79. $userProvider->expects($this->once())
  80. ->method('loadUserByUsername')
  81. ->will($this->returnValue($result = array($user, 'foo')))
  82. ;
  83. $provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface'));
  84. $method = new \ReflectionMethod($provider, 'retrieveUser');
  85. $method->setAccessible(true);
  86. $this->assertSame($result, $method->invoke($provider, 'fabien', $this->getSupportedToken()));
  87. }
  88. /**
  89. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  90. */
  91. public function testCheckAuthenticationWhenCredentialsAreEmpty()
  92. {
  93. $provider = $this->getProvider();
  94. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  95. $method->setAccessible(true);
  96. $token = $this->getSupportedToken();
  97. $token->expects($this->once())
  98. ->method('getCredentials')
  99. ->will($this->returnValue(''))
  100. ;
  101. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  102. }
  103. /**
  104. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  105. */
  106. public function testCheckAuthenticationWhenCredentialsAreNotValid()
  107. {
  108. $encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
  109. $encoder->expects($this->once())
  110. ->method('isPasswordValid')
  111. ->will($this->returnValue(false))
  112. ;
  113. $provider = $this->getProvider(false, false, $encoder);
  114. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  115. $method->setAccessible(true);
  116. $token = $this->getSupportedToken();
  117. $token->expects($this->once())
  118. ->method('getCredentials')
  119. ->will($this->returnValue('foo'))
  120. ;
  121. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  122. }
  123. /**
  124. * @expectedException Symfony\Component\Security\Exception\BadCredentialsException
  125. */
  126. public function testCheckAuthenticationDoesNotReauthenticateWhenPasswordHasChanged()
  127. {
  128. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  129. $user->expects($this->once())
  130. ->method('getPassword')
  131. ->will($this->returnValue('foo'))
  132. ;
  133. $token = $this->getSupportedToken();
  134. $token->expects($this->once())
  135. ->method('getUser')
  136. ->will($this->returnValue($user));
  137. $dbUser = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  138. $dbUser->expects($this->once())
  139. ->method('getPassword')
  140. ->will($this->returnValue('newFoo'))
  141. ;
  142. $provider = $this->getProvider(false, false, null);
  143. $reflection = new \ReflectionMethod($provider, 'checkAuthentication');
  144. $reflection->setAccessible(true);
  145. $reflection->invoke($provider, $dbUser, $token);
  146. }
  147. public function testCheckAuthenticationWhenTokenNeedsReauthenticationWorksWithoutOriginalCredentials()
  148. {
  149. $user = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  150. $user->expects($this->once())
  151. ->method('getPassword')
  152. ->will($this->returnValue('foo'))
  153. ;
  154. $token = $this->getSupportedToken();
  155. $token->expects($this->once())
  156. ->method('getUser')
  157. ->will($this->returnValue($user));
  158. $dbUser = $this->getMock('Symfony\Component\Security\User\AccountInterface');
  159. $dbUser->expects($this->once())
  160. ->method('getPassword')
  161. ->will($this->returnValue('foo'))
  162. ;
  163. $provider = $this->getProvider(false, false, null);
  164. $reflection = new \ReflectionMethod($provider, 'checkAuthentication');
  165. $reflection->setAccessible(true);
  166. $reflection->invoke($provider, $dbUser, $token);
  167. }
  168. public function testCheckAuthentication()
  169. {
  170. $encoder = $this->getMock('Symfony\Component\Security\Encoder\PasswordEncoderInterface');
  171. $encoder->expects($this->once())
  172. ->method('isPasswordValid')
  173. ->will($this->returnValue(true))
  174. ;
  175. $provider = $this->getProvider(false, false, $encoder);
  176. $method = new \ReflectionMethod($provider, 'checkAuthentication');
  177. $method->setAccessible(true);
  178. $token = $this->getSupportedToken();
  179. $token->expects($this->once())
  180. ->method('getCredentials')
  181. ->will($this->returnValue('foo'))
  182. ;
  183. $method->invoke($provider, $this->getMock('Symfony\Component\Security\User\AccountInterface'), $token);
  184. }
  185. protected function getSupportedToken()
  186. {
  187. return $this->getMock('Symfony\Component\Security\Authentication\Token\UsernamePasswordToken', array('getCredentials', 'getUser', 'getUserProviderName'), array(), '', false);
  188. }
  189. protected function getProvider($user = false, $userChecker = false, $passwordEncoder = null)
  190. {
  191. $userProvider = $this->getMock('Symfony\Component\Security\User\UserProviderInterface');
  192. if (false !== $user) {
  193. $userProvider->expects($this->once())
  194. ->method('loadUserByUsername')
  195. ->will($this->returnValue($user))
  196. ;
  197. }
  198. if (false === $userChecker) {
  199. $userChecker = $this->getMock('Symfony\Component\Security\User\AccountCheckerInterface');
  200. }
  201. return new DaoAuthenticationProvider($userProvider, $userChecker, $passwordEncoder);
  202. }
  203. }