|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Tests\Http\Firewall; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener; |
| 17 | +use Symfony\Component\Security\Core\SecurityContextInterface; |
| 18 | + |
| 19 | +class UsernamePasswordFormAuthenticationListenerTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @dataProvider getUsernameForLength |
| 23 | + */ |
| 24 | + public function testHandleWhenUsernameLength($username, $ok) |
| 25 | + { |
| 26 | + $request = Request::create('/login_check', 'POST', array('_username' => $username)); |
| 27 | + $request->setSession($this->getMock('Symfony\Component\HttpFoundation\Session\SessionInterface')); |
| 28 | + |
| 29 | + $httpUtils = $this->getMock('Symfony\Component\Security\Http\HttpUtils'); |
| 30 | + $httpUtils |
| 31 | + ->expects($this->any()) |
| 32 | + ->method('checkRequestPath') |
| 33 | + ->will($this->returnValue(true)) |
| 34 | + ; |
| 35 | + |
| 36 | + $failureHandler = $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface'); |
| 37 | + $failureHandler |
| 38 | + ->expects($ok ? $this->never() : $this->once()) |
| 39 | + ->method('onAuthenticationFailure') |
| 40 | + ->will($this->returnValue(new Response())) |
| 41 | + ; |
| 42 | + |
| 43 | + $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager')->disableOriginalConstructor()->getMock(); |
| 44 | + $authenticationManager |
| 45 | + ->expects($
F58A
ok ? $this->once() : $this->never()) |
| 46 | + ->method('authenticate') |
| 47 | + ->will($this->returnValue(new Response())) |
| 48 | + ; |
| 49 | + |
| 50 | + $listener = new UsernamePasswordFormAuthenticationListener( |
| 51 | + $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'), |
| 52 | + $authenticationManager, |
| 53 | + $this->getMock('Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface'), |
| 54 | + $httpUtils, |
| 55 | + 'TheProviderKey', |
| 56 | + $this->getMock('Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface'), |
| 57 | + $failureHandler, |
| 58 | + array('require_previous_session' => false) |
| 59 | + ); |
| 60 | + |
| 61 | + $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false); |
| 62 | + $event |
| 63 | + ->expects($this->any()) |
| 64 | + ->method('getRequest') |
| 65 | + ->will($this->returnValue($request)) |
| 66 | + ; |
| 67 | + |
| 68 | + $listener->handle($event); |
| 69 | + } |
| 70 | + |
| 71 | + public function getUsernameForLength() |
| 72 | + { |
| 73 | + return array( |
| 74 | + array(str_repeat('x', SecurityContextInterface::MAX_USERNAME_LENGTH + 1), false), |
| 75 | + array(str_repeat('x', SecurityContextInterface::MAX_USERNAME_LENGTH - 1), true), |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
0 commit comments