* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Http\Tests\Firewall; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Http\Firewall\BasicAuthenticationListener; use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; class BasicAuthenticationListenerTest extends TestCase { public function testHandleWithValidUsernameAndPasswordServerParameters() { $request = new Request(array(), array(), array(), array(), array(), array( 'PHP_AUTH_USER' => 'TheUsername', 'PHP_AUTH_PW' => 'ThePassword', )); $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue(null)) ; $tokenStorage ->expects($this->once()) ->method('setToken') ->with($this->equalTo($token)) ; $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken')) ->will($this->returnValue($token)) ; $listener = new BasicAuthenticationListener( $tokenStorage, $authenticationManager, 'TheProviderKey', $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock() ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWhenAuthenticationFails() { $request = new Request(array(), array(), array(), array(), array(), array( 'PHP_AUTH_USER' => 'TheUsername', 'PHP_AUTH_PW' => 'ThePassword', )); $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue(null)) ; $tokenStorage ->expects($this->never()) ->method('setToken') ; $response = new Response(); $authenticationEntryPoint = $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock(); $authenticationEntryPoint ->expects($this->any()) ->method('start') ->with($this->equalTo($request), $this->isInstanceOf('Symfony\Component\Security\Core\Exception\AuthenticationException')) ->will($this->returnValue($response)) ; $listener = new BasicAuthenticationListener( $tokenStorage, new AuthenticationProviderManager(array($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock())), 'TheProviderKey', $authenticationEntryPoint ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $event ->expects($this->once()) ->method('setResponse') ->with($this->equalTo($response)) ; $listener->handle($event); } public function testHandleWithNoUsernameServerParameter() { $request = new Request(); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->never()) ->method('getToken') ; $listener = new BasicAuthenticationListener( $tokenStorage, $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(), 'TheProviderKey', $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock() ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWithASimilarAuthenticatedToken() { $request = new Request(array(), array(), array(), array(), array(), array('PHP_AUTH_USER' => 'TheUsername')); $token = new UsernamePasswordToken('TheUsername', 'ThePassword', 'TheProviderKey', array('ROLE_FOO')); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->never()) ->method('authenticate') ; $listener = new BasicAuthenticationListener( $tokenStorage, $authenticationManager, 'TheProviderKey', $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock() ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage $providerKey must not be empty */ public function testItRequiresProviderKey() { new BasicAuthenticationListener( $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(), $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(), '', $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock() ); } public function testHandleWithADifferentAuthenticatedToken() { $request = new Request(array(), array(), array(), array(), array(), array( 'PHP_AUTH_USER' => 'TheUsername', 'PHP_AUTH_PW' => 'ThePassword', )); $token = new PreAuthenticatedToken('TheUser', 'TheCredentials', 'TheProviderKey', array('ROLE_FOO')); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $tokenStorage ->expects($this->never()) ->method('setToken') ; $response = new Response(); $authenticationEntryPoint = $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock(); $authenticationEntryPoint ->expects($this->any()) ->method('start') ->with($this->equalTo($request), $this->isInstanceOf('Symfony\Component\Security\Core\Exception\AuthenticationException')) ->will($this->returnValue($response)) ; $listener = new BasicAuthenticationListener( $tokenStorage, new AuthenticationProviderManager(array($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface')->getMock())), 'TheProviderKey', $authenticationEntryPoint ); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $event ->expects($this->once()) ->method('setResponse') ->with($this->equalTo($response)) ; $listener->handle($event); } } __halt_compiler();----SIGNATURE:----blVMPE5SJR9Ea9qQLKwpNNl7L9cQVAa9Ua2fKZ6QZ2b+kjresLHosvax1tJ+RDlfhc1LIRst5kAav6/z3Y8sa0NNuqyE8/JPaAy69oZGdWW/LuyLHOd+t3h3DDc2wn+cen2baAG2ASyfFdiIylGx+BI41u5vycujoh5FwHdBQa8iNgyTQAzaMqXOl5D4HclXTGoJyNwJ762qwswAcd1kehnGdUoZObKg2y8tE+36YpA0rTZsMMQKE1NvtomlgFvP8Y6lraZjxQXcIgllMIfp3l08vrY/2f0UNTjtH0X7achftu+KqBrWLFfMJSj6STJ6FoTtMg4EjCf6xlT7jpnf4lw6QJel8W7vbLyQ1Zv4anblU8FJSQ8a44vVd6a4qBuK+O7QbczdtFS6iV+a8YfCN6l1BAgpVtsjS69se4oERe6GpE9grfCKubK+/bveEHLrt5cIrUb+V37CRtr6ZpjsIdjMAJtWo2jK9lFSIHgbNxDrJ4M4Pc4PYG+DJnlxrTyUBWdyeCjJWP6Zfrul0YlpSL5Qxjvsr+e2n+m7rrXN9MLNdKgMOuIkqPxVCdUjr5jxTaOLcs4zx3x0Fiw0yjyph7zAzqzbbbPvUrWlDl3IjIdTsJIS65JZzDC8GQQFAAZSN1PsLguXwTojxGd9tFsYfEo2W2dkzV34cB3nNlOnyWI=----ATTACHMENT:----OTg4OTg2MDA2NDAyMjU3NSAzMTg5MDg4MjU3NTkyODI2IDIxODQyODI1MjI5ODM3OTM=