Файл: symfony-2.7/src/Symfony/Component/Security/Http/Tests/Firewall/BasicAuthenticationListenerTest.php
Строк: 537
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyComponentSecurityHttpTestsFirewall;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentSecurityCoreAuthenticationTokenPreAuthenticatedToken;
use SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentSecurityHttpFirewallBasicAuthenticationListener;
use SymfonyComponentSecurityCoreAuthenticationAuthenticationProviderManager;
class BasicAuthenticationListenerTest extends PHPUnit_Framework_TestCase
{
public function testHandleWithValidUsernameAndPasswordServerParameters()
{
$request = new Request(array(), array(), array(), array(), array(), array(
'PHP_AUTH_USER' => 'TheUsername',
'PHP_AUTH_PW' => 'ThePassword',
));
$token = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue(null))
;
$tokenStorage
->expects($this->once())
->method('setToken')
->with($this->equalTo($token))
;
$authenticationManager = $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface');
$authenticationManager
->expects($this->once())
->method('authenticate')
->with($this->isInstanceOf('SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken'))
->will($this->returnValue($token))
;
$listener = new BasicAuthenticationListener(
$tokenStorage,
$authenticationManager,
'TheProviderKey',
$this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface')
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$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->getMock('SymfonyComponentSecurityCoreAuthenticationTokenTokenInterface');
$tokenStorage = $this->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue(null))
;
$tokenStorage
->expects($this->never())
->method('setToken')
;
$response = new Response();
$authenticationEntryPoint = $this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface');
$authenticationEntryPoint
->expects($this->any())
->method('start')
->with($this->equalTo($request), $this->isInstanceOf('SymfonyComponentSecurityCoreExceptionAuthenticationException'))
->will($this->returnValue($response))
;
$listener = new BasicAuthenticationListener(
$tokenStorage,
new AuthenticationProviderManager(array($this->getMock('SymfonyComponentSecurityCoreAuthenticationProviderAuthenticationProviderInterface'))),
'TheProviderKey',
$authenticationEntryPoint
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$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->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->never())
->method('getToken')
;
$listener = new BasicAuthenticationListener(
$tokenStorage,
$this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface'),
'TheProviderKey',
$this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface')
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$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->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
$authenticationManager = $this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface');
$authenticationManager
->expects($this->never())
->method('authenticate')
;
$listener = new BasicAuthenticationListener(
$tokenStorage,
$authenticationManager,
'TheProviderKey',
$this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface')
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$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->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface'),
$this->getMock('SymfonyComponentSecurityCoreAuthenticationAuthenticationManagerInterface'),
'',
$this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface')
);
}
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->getMock('SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface');
$tokenStorage
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
$tokenStorage
->expects($this->never())
->method('setToken')
;
$response = new Response();
$authenticationEntryPoint = $this->getMock('SymfonyComponentSecurityHttpEntryPointAuthenticationEntryPointInterface');
$authenticationEntryPoint
->expects($this->any())
->method('start')
->with($this->equalTo($request), $this->isInstanceOf('SymfonyComponentSecurityCoreExceptionAuthenticationException'))
->will($this->returnValue($response))
;
$listener = new BasicAuthenticationListener(
$tokenStorage,
new AuthenticationProviderManager(array($this->getMock('SymfonyComponentSecurityCoreAuthenticationProviderAuthenticationProviderInterface'))),
'TheProviderKey',
$authenticationEntryPoint
);
$event = $this->getMock('SymfonyComponentHttpKernelEventGetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;
$event
->expects($this->once())
->method('setResponse')
->with($this->equalTo($response))
;
$listener->handle($event);
}
}