[Security] Update access listener constructor's prototype and add tests
This commit is contained in:
parent
81afd77ed9
commit
3c2affb7e7
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Security\Http\Firewall;
|
namespace Symfony\Component\Security\Http\Firewall;
|
||||||
|
|
||||||
use Symfony\Component\Security\Core\SecurityContext;
|
use Symfony\Component\Security\Core\SecurityContextInterface;
|
||||||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
|
||||||
use Symfony\Component\Security\Http\AccessMap;
|
use Symfony\Component\Security\Http\AccessMap;
|
||||||
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
|
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
|
||||||
@ -33,7 +33,7 @@ class AccessListener implements ListenerInterface
|
|||||||
private $authManager;
|
private $authManager;
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
public function __construct(SecurityContext $context, AccessDecisionManagerInterface $accessDecisionManager, AccessMap $map, AuthenticationManagerInterface $authManager, LoggerInterface $logger = null)
|
public function __construct(SecurityContextInterface $context, AccessDecisionManagerInterface $accessDecisionManager, AccessMap $map, AuthenticationManagerInterface $authManager, LoggerInterface $logger = null)
|
||||||
{
|
{
|
||||||
$this->context = $context;
|
$this->context = $context;
|
||||||
$this->accessDecisionManager = $accessDecisionManager;
|
$this->accessDecisionManager = $accessDecisionManager;
|
||||||
|
@ -0,0 +1,199 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Symfony\Tests\Component\Security\Http\Firewall;
|
||||||
|
|
||||||
|
use Symfony\Component\Security\Http\Firewall\AccessListener;
|
||||||
|
|
||||||
|
class AccessListenerTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
|
||||||
|
*/
|
||||||
|
public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess()
|
||||||
|
{
|
||||||
|
$request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
|
||||||
|
|
||||||
|
$accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMap');
|
||||||
|
$accessMap
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getPatterns')
|
||||||
|
->with($this->equalTo($request))
|
||||||
|
->will($this->returnValue(array(array('foo' => 'bar'), null)))
|
||||||
|
;
|
||||||
|
|
||||||
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||||
|
$token
|
||||||
|
->expects($this->any())
|
||||||
|
->method('isAuthenticated')
|
||||||
|
->will($this->returnValue(true))
|
||||||
|
;
|
||||||
|
|
||||||
|
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
||||||
|
$context
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getToken')
|
||||||
|
->will($this->returnValue($token))
|
||||||
|
;
|
||||||
|
|
||||||
|
$accessDecisionManager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');
|
||||||
|
$accessDecisionManager
|
||||||
|
->expects($this->once())
|
||||||
|
->method('decide')
|
||||||
|
->with($this->equalTo($token), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request))
|
||||||
|
->will($this->returnValue(false))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener = new AccessListener(
|
||||||
|
$context,
|
||||||
|
$accessDecisionManager,
|
||||||
|
$accessMap,
|
||||||
|
$this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
||||||
|
$event
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getRequest')
|
||||||
|
->will($this->returnValue($request))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener->handle($event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandleWhenTheTokenIsNotAuthenticated()
|
||||||
|
{
|
||||||
|
$request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
|
||||||
|
|
||||||
|
$accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMap');
|
||||||
|
$accessMap
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getPatterns')
|
||||||
|
->with($this->equalTo($request))
|
||||||
|
->will($this->returnValue(array(array('foo' => 'bar'), null)))
|
||||||
|
;
|
||||||
|
|
||||||
|
$notAuthenticatedToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||||
|
$notAuthenticatedToken
|
||||||
|
->expects($this->any())
|
||||||
|
->method('isAuthenticated')
|
||||||
|
->will($this->returnValue(false))
|
||||||
|
;
|
||||||
|
|
||||||
|
$authenticatedToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||||
|
$authenticatedToken
|
||||||
|
->expects($this->any())
|
||||||
|
->method('isAuthenticated')
|
||||||
|
->will($this->returnValue(true))
|
||||||
|
;
|
||||||
|
|
||||||
|
$authManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
|
||||||
|
$authManager
|
||||||
|
->expects($this->once())
|
||||||
|
->method('authenticate')
|
||||||
|
->with($this->equalTo($notAuthenticatedToken))
|
||||||
|
->will($this->returnValue($authenticatedToken))
|
||||||
|
;
|
||||||
|
|
||||||
|
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
||||||
|
$context
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getToken')
|
||||||
|
->will($this->returnValue($notAuthenticatedToken))
|
||||||
|
;
|
||||||
|
$context
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setToken')
|
||||||
|
->with($this->equalTo($authenticatedToken))
|
||||||
|
;
|
||||||
|
|
||||||
|
$accessDecisionManager = $this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface');
|
||||||
|
$accessDecisionManager
|
||||||
|
->expects($this->once())
|
||||||
|
->method('decide')
|
||||||
|
->with($this->equalTo($authenticatedToken), $this->equalTo(array('foo' => 'bar')), $this->equalTo($request))
|
||||||
|
->will($this->returnValue(true))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener = new AccessListener(
|
||||||
|
$context,
|
||||||
|
$accessDecisionManager,
|
||||||
|
$accessMap,
|
||||||
|
$authManager
|
||||||
|
);
|
||||||
|
|
||||||
|
$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
||||||
|
$event
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getRequest')
|
||||||
|
->will($this->returnValue($request))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener->handle($event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest()
|
||||||
|
{
|
||||||
|
$request = $this->getMock('Symfony\Component\HttpFoundation\Request', array(), array(), '', false, false);
|
||||||
|
|
||||||
|
$accessMap = $this->getMock('Symfony\Component\Security\Http\AccessMap');
|
||||||
|
$accessMap
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getPatterns')
|
||||||
|
->with($this->equalTo($request))
|
||||||
|
->will($this->returnValue(array(null, null)))
|
||||||
|
;
|
||||||
|
|
||||||
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||||
|
$token
|
||||||
|
->expects($this->never())
|
||||||
|
->method('isAuthenticated')
|
||||||
|
;
|
||||||
|
|
||||||
|
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
||||||
|
$context
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getToken')
|
||||||
|
->will($this->returnValue($token))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener = new AccessListener(
|
||||||
|
$context,
|
||||||
|
$this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'),
|
||||||
|
$accessMap,
|
||||||
|
$this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
||||||
|
$event
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getRequest')
|
||||||
|
->will($this->returnValue($request))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener->handle($event);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
|
||||||
|
*/
|
||||||
|
public function testHandleWhenTheSecurityContextHasNoToken()
|
||||||
|
{
|
||||||
|
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
|
||||||
|
$context
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getToken')
|
||||||
|
->will($this->returnValue(null))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener = new AccessListener(
|
||||||
|
$context,
|
||||||
|
$this->getMock('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface'),
|
||||||
|
$this->getMock('Symfony\Component\Security\Http\AccessMap'),
|
||||||
|
$this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')
|
||||||
|
);
|
||||||
|
|
||||||
|
$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
||||||
|
|
||||||
|
$listener->handle($event);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user