SimpleAuthenticationHandlerTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Security\Http\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  17. use Symfony\Component\Security\Http\Authentication\SimpleAuthenticationHandler;
  18. class SimpleAuthenticationHandlerTest extends TestCase
  19. {
  20. private $successHandler;
  21. private $failureHandler;
  22. private $request;
  23. private $token;
  24. private $authenticationException;
  25. private $response;
  26. protected function setUp()
  27. {
  28. $this->successHandler = $this->getMockBuilder('Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface')->getMock();
  29. $this->failureHandler = $this->getMockBuilder('Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface')->getMock();
  30. $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  31. $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
  32. // No methods are invoked on the exception; we just assert on its class
  33. $this->authenticationException = new AuthenticationException();
  34. $this->response = new Response();
  35. }
  36. public function testOnAuthenticationSuccessFallsBackToDefaultHandlerIfSimpleIsNotASuccessHandler()
  37. {
  38. $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
  39. $this->successHandler->expects($this->once())
  40. ->method('onAuthenticationSuccess')
  41. ->with($this->request, $this->token)
  42. ->will($this->returnValue($this->response));
  43. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  44. $result = $handler->onAuthenticationSuccess($this->request, $this->token);
  45. $this->assertSame($this->response, $result);
  46. }
  47. public function testOnAuthenticationSuccessCallsSimpleAuthenticator()
  48. {
  49. $this->successHandler->expects($this->never())
  50. ->method('onAuthenticationSuccess');
  51. $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestSuccessHandlerInterface');
  52. $authenticator->expects($this->once())
  53. ->method('onAuthenticationSuccess')
  54. ->with($this->request, $this->token)
  55. ->will($this->returnValue($this->response));
  56. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  57. $result = $handler->onAuthenticationSuccess($this->request, $this->token);
  58. $this->assertSame($this->response, $result);
  59. }
  60. /**
  61. * @expectedException \UnexpectedValueException
  62. * @expectedExceptionMessage onAuthenticationSuccess method must return null to use the default success handler, or a Response object
  63. */
  64. public function testOnAuthenticationSuccessThrowsAnExceptionIfNonResponseIsReturned()
  65. {
  66. $this->successHandler->expects($this->never())
  67. ->method('onAuthenticationSuccess');
  68. $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestSuccessHandlerInterface');
  69. $authenticator->expects($this->once())
  70. ->method('onAuthenticationSuccess')
  71. ->with($this->request, $this->token)
  72. ->will($this->returnValue(new \stdClass()));
  73. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  74. $handler->onAuthenticationSuccess($this->request, $this->token);
  75. }
  76. public function testOnAuthenticationSuccessFallsBackToDefaultHandlerIfNullIsReturned()
  77. {
  78. $this->successHandler->expects($this->once())
  79. ->method('onAuthenticationSuccess')
  80. ->with($this->request, $this->token)
  81. ->will($this->returnValue($this->response));
  82. $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestSuccessHandlerInterface');
  83. $authenticator->expects($this->once())
  84. ->method('onAuthenticationSuccess')
  85. ->with($this->request, $this->token)
  86. ->will($this->returnValue(null));
  87. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  88. $result = $handler->onAuthenticationSuccess($this->request, $this->token);
  89. $this->assertSame($this->response, $result);
  90. }
  91. public function testOnAuthenticationFailureFallsBackToDefaultHandlerIfSimpleIsNotAFailureHandler()
  92. {
  93. $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
  94. $this->failureHandler->expects($this->once())
  95. ->method('onAuthenticationFailure')
  96. ->with($this->request, $this->authenticationException)
  97. ->will($this->returnValue($this->response));
  98. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  99. $result = $handler->onAuthenticationFailure($this->request, $this->authenticationException);
  100. $this->assertSame($this->response, $result);
  101. }
  102. public function testOnAuthenticationFailureCallsSimpleAuthenticator()
  103. {
  104. $this->failureHandler->expects($this->never())
  105. ->method('onAuthenticationFailure');
  106. $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestFailureHandlerInterface');
  107. $authenticator->expects($this->once())
  108. ->method('onAuthenticationFailure')
  109. ->with($this->request, $this->authenticationException)
  110. ->will($this->returnValue($this->response));
  111. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  112. $result = $handler->onAuthenticationFailure($this->request, $this->authenticationException);
  113. $this->assertSame($this->response, $result);
  114. }
  115. /**
  116. * @expectedException \UnexpectedValueException
  117. * @expectedExceptionMessage onAuthenticationFailure method must return null to use the default failure handler, or a Response object
  118. */
  119. public function testOnAuthenticationFailureThrowsAnExceptionIfNonResponseIsReturned()
  120. {
  121. $this->failureHandler->expects($this->never())
  122. ->method('onAuthenticationFailure');
  123. $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestFailureHandlerInterface');
  124. $authenticator->expects($this->once())
  125. ->method('onAuthenticationFailure')
  126. ->with($this->request, $this->authenticationException)
  127. ->will($this->returnValue(new \stdClass()));
  128. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  129. $handler->onAuthenticationFailure($this->request, $this->authenticationException);
  130. }
  131. public function testOnAuthenticationFailureFallsBackToDefaultHandlerIfNullIsReturned()
  132. {
  133. $this->failureHandler->expects($this->once())
  134. ->method('onAuthenticationFailure')
  135. ->with($this->request, $this->authenticationException)
  136. ->will($this->returnValue($this->response));
  137. $authenticator = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Tests\TestFailureHandlerInterface');
  138. $authenticator->expects($this->once())
  139. ->method('onAuthenticationFailure')
  140. ->with($this->request, $this->authenticationException)
  141. ->will($this->returnValue(null));
  142. $handler = new SimpleAuthenticationHandler($authenticator, $this->successHandler, $this->failureHandler);
  143. $result = $handler->onAuthenticationFailure($this->request, $this->authenticationException);
  144. $this->assertSame($this->response, $result);
  145. }
  146. }
  147. interface TestSuccessHandlerInterface extends AuthenticationSuccessHandlerInterface, SimpleAuthenticatorInterface
  148. {
  149. }
  150. interface TestFailureHandlerInterface extends AuthenticationFailureHandlerInterface, SimpleAuthenticatorInterface
  151. {
  152. }