|
| 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\Security\Core\Authentication\AuthenticationManagerInterface; |
| 16 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 17 | +use Symfony\Component\Security\Core\Security; |
| 18 | +use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
| 19 | +use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; |
| 20 | +use Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener; |
| 21 | +use Symfony\Component\Security\Http\HttpUtils; |
| 22 | +use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Kévin Dunglas <dunglas@gmail.com> |
| 26 | + */ |
| 27 | +class UsernamePasswordJsonAuthenticationListenerTest extends \PHPUnit_Framework_TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var UsernamePasswordJsonAuthenticationListener |
| 31 | + */ |
| 32 | + private $listener; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var \ReflectionMethod |
| 36 | + */ |
| 37 | + private $attemptAuthenticationMethod; |
| 38 | + |
| 39 | + protected function setUp() { |
| 40 | + $tokenStorage = $this->getMock(TokenStorageInterface::class); |
| 41 | + $authenticationManager = $this->getMock(AuthenticationManagerInterface::class); |
| 42 | + $authenticationManager->method('authenticate')->willReturn(true); |
| 43 | + $sessionAuthenticationStrategyInterface = $this->getMock(SessionAuthenticationStrategyInterface::class); |
| 44 | + $httpUtils = $this->getMock(HttpUtils::class); |
| 45 | + $authenticationSuccessHandler = $this->getMock(AuthenticationSuccessHandlerInterface::class); |
| 46 | + $authenticationFailureHandler = $this->getMock(AuthenticationFailureHandlerInterface::class); |
| 47 | + |
| 48 | + $this->listener = new UsernamePasswordJsonAuthenticationListener($tokenStorage, $authenticationManager, $sessionAuthenticationStrategyInterface, $httpUtils, 'providerKey', $authenticationSuccessHandler, $authenticationFailureHandler); |
| 49 | + $this->attemptAuthenticationMethod = new \ReflectionMethod($this->listener, 'attemptAuthentication'); |
| 50 | + $this->attemptAuthenticationMethod->setAccessible(true); |
| 51 | + } |
| 52 | + |
| 53 | + public function testAttemptAuthentication() |
| 54 | + { |
| 55 | + $request = new Request(array(), array(), array(), array(), array(), array(), '{"_username": "dunglas", "_password": "foo"}'); |
| 56 | + |
| 57 | + $result = $this->attemptAuthenticationMethod->invokeArgs($this->listener, array($request)); |
| 58 | + $this->assertTrue($result); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
| 63 | + */ |
| 64 | + public function testAttemptAuthenticationNoUsername() |
| 65 | + { |
| 66 | + $request = new Request(array(), array(), array(), array(), array(), array(), '{"usr": "dunglas", "_password": "foo"}'); |
| 67 | + $this->attemptAuthenticationMethod->invokeArgs($this->listener, array($request)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
| 72 | + */ |
| 73 | + public function testAttemptAuthenticationNoPassword() |
| 74 | + { |
| 75 | + $request = new Request(array(), array(), array(), array(), array(), array(), '{"_username": "dunglas", "pass": "foo"}'); |
| 76 | + $this->attemptAuthenticationMethod->invokeArgs($this->listener, array($request)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
| 81 | + */ |
| 82 | + public function testAttemptAuthenticationUsernameNotAString() |
| 83 | + { |
| 84 | + $request = new Request(array(), array(), array(), array(), array(), array(), '{"_username": 1, "_password": "foo"}'); |
| 85 | + $this->attemptAuthenticationMethod->invokeArgs($this->listener, array($request)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
| 90 | + */ |
| 91 | + public function testAttemptAuthenticationPasswordNotAString() |
| 92 | + { |
| 93 | + $request = new Request(array(), array(), array(), array(), array(), array(), '{"_username": "dunglas", "_password": 1}'); |
| 94 | + $this->attemptAuthenticationMethod->invokeArgs($this->listener, array($request)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException |
| 99 | + */ |
| 100 | + public function testAttemptAuthenticationUsernameTooLong() |
| 101 | + { |
| 102 | + $username = str_repeat('x', Security::MAX_USERNAME_LENGTH + 1); |
| 103 | + $request = new Request(array(), array(), array(), array(), array(), array(), sprintf('{"_username": "%s", "_password": 1}', $username)); |
| 104 | + |
| 105 | + $this->attemptAuthenticationMethod->invokeArgs($this->listener, array($request)); |
| 106 | + } |
| 107 | +} |
0 commit comments