DefaultAuthenticationFailureHandlerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\Authentication;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
  16. class DefaultAuthenticationFailureHandlerTest extends TestCase
  17. {
  18. private $httpKernel;
  19. private $httpUtils;
  20. private $logger;
  21. private $request;
  22. private $session;
  23. private $exception;
  24. protected function setUp()
  25. {
  26. $this->httpKernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
  27. $this->httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
  28. $this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
  29. $this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
  30. $this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  31. $this->request->expects($this->any())->method('getSession')->will($this->returnValue($this->session));
  32. $this->exception = $this->getMockBuilder('Symfony\Component\Security\Core\Exception\AuthenticationException')->setMethods(array('getMessage'))->getMock();
  33. }
  34. public function testForward()
  35. {
  36. $options = array('failure_forward' => true);
  37. $subRequest = $this->getRequest();
  38. $subRequest->attributes->expects($this->once())
  39. ->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
  40. $this->httpUtils->expects($this->once())
  41. ->method('createRequest')->with($this->request, '/login')
  42. ->will($this->returnValue($subRequest));
  43. $response = new Response();
  44. $this->httpKernel->expects($this->once())
  45. ->method('handle')->with($subRequest, HttpKernelInterface::SUB_REQUEST)
  46. ->will($this->returnValue($response));
  47. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
  48. $result = $handler->onAuthenticationFailure($this->request, $this->exception);
  49. $this->assertSame($response, $result);
  50. }
  51. public function testRedirect()
  52. {
  53. $response = new Response();
  54. $this->httpUtils->expects($this->once())
  55. ->method('createRedirectResponse')->with($this->request, '/login')
  56. ->will($this->returnValue($response));
  57. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, array(), $this->logger);
  58. $result = $handler->onAuthenticationFailure($this->request, $this->exception);
  59. $this->assertSame($response, $result);
  60. }
  61. public function testExceptionIsPersistedInSession()
  62. {
  63. $this->session->expects($this->once())
  64. ->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
  65. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, array(), $this->logger);
  66. $handler->onAuthenticationFailure($this->request, $this->exception);
  67. }
  68. public function testExceptionIsPassedInRequestOnForward()
  69. {
  70. $options = array('failure_forward' => true);
  71. $subRequest = $this->getRequest();
  72. $subRequest->attributes->expects($this->once())
  73. ->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
  74. $this->httpUtils->expects($this->once())
  75. ->method('createRequest')->with($this->request, '/login')
  76. ->will($this->returnValue($subRequest));
  77. $this->session->expects($this->never())->method('set');
  78. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
  79. $handler->onAuthenticationFailure($this->request, $this->exception);
  80. }
  81. public function testRedirectIsLogged()
  82. {
  83. $this->logger
  84. ->expects($this->once())
  85. ->method('debug')
  86. ->with('Authentication failure, redirect triggered.', array('failure_path' => '/login'));
  87. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, array(), $this->logger);
  88. $handler->onAuthenticationFailure($this->request, $this->exception);
  89. }
  90. public function testForwardIsLogged()
  91. {
  92. $options = array('failure_forward' => true);
  93. $this->httpUtils->expects($this->once())
  94. ->method('createRequest')->with($this->request, '/login')
  95. ->will($this->returnValue($this->getRequest()));
  96. $this->logger
  97. ->expects($this->once())
  98. ->method('debug')
  99. ->with('Authentication failure, forward triggered.', array('failure_path' => '/login'));
  100. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
  101. $handler->onAuthenticationFailure($this->request, $this->exception);
  102. }
  103. public function testFailurePathCanBeOverwritten()
  104. {
  105. $options = array('failure_path' => '/auth/login');
  106. $this->httpUtils->expects($this->once())
  107. ->method('createRedirectResponse')->with($this->request, '/auth/login');
  108. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
  109. $handler->onAuthenticationFailure($this->request, $this->exception);
  110. }
  111. public function testFailurePathCanBeOverwrittenWithRequest()
  112. {
  113. $this->request->expects($this->once())
  114. ->method('get')->with('_failure_path')
  115. ->will($this->returnValue('/auth/login'));
  116. $this->httpUtils->expects($this->once())
  117. ->method('createRedirectResponse')->with($this->request, '/auth/login');
  118. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, array(), $this->logger);
  119. $handler->onAuthenticationFailure($this->request, $this->exception);
  120. }
  121. public function testFailurePathCanBeOverwrittenWithNestedAttributeInRequest()
  122. {
  123. $this->request->expects($this->once())
  124. ->method('get')->with('_failure_path')
  125. ->will($this->returnValue(array('value' => '/auth/login')));
  126. $this->httpUtils->expects($this->once())
  127. ->method('createRedirectResponse')->with($this->request, '/auth/login');
  128. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, array('failure_path_parameter' => '_failure_path[value]'), $this->logger);
  129. $handler->onAuthenticationFailure($this->request, $this->exception);
  130. }
  131. public function testFailurePathParameterCanBeOverwritten()
  132. {
  133. $options = array('failure_path_parameter' => '_my_failure_path');
  134. $this->request->expects($this->once())
  135. ->method('get')->with('_my_failure_path')
  136. ->will($this->returnValue('/auth/login'));
  137. $this->httpUtils->expects($this->once())
  138. ->method('createRedirectResponse')->with($this->request, '/auth/login');
  139. $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
  140. $handler->onAuthenticationFailure($this->request, $this->exception);
  141. }
  142. private function getRequest()
  143. {
  144. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  145. $request->attributes = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')->getMock();
  146. return $request;
  147. }
  148. }