[Security] simplified tests

This commit is contained in:
Fabien Potencier 2017-07-19 07:42:44 +02:00
parent 3387612451
commit b1f1ae26b4
1 changed files with 69 additions and 124 deletions

View File

@ -15,138 +15,83 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\HttpUtils;
class DefaultAuthenticationSuccessHandlerTest extends TestCase class DefaultAuthenticationSuccessHandlerTest extends TestCase
{ {
private $httpUtils = null; /**
private $token = null; * @dataProvider getRequestRedirections
*/
protected function setUp() public function testRequestRedirections(Request $request, $options, $redirectedUrl)
{ {
$this->httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock(); $httpUtils = new HttpUtils();
$this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
$handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options);
if ($request->hasSession()) {
$handler->setProviderKey('admin');
}
$this->assertSame('http://localhost'.$redirectedUrl, $handler->onAuthenticationSuccess($request, $token)->getTargetUrl());
} }
public function testRequestIsRedirected() public function getRequestRedirections()
{
$request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result);
}
public function testDefaultTargetPathCanBeForced()
{
$options = array(
'always_use_default_target_path' => true,
'default_target_path' => '/dashboard',
);
$request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result);
}
public function testTargetPathIsPassedWithRequest()
{
$request = Request::create('/?_target_path=/dashboard');
$response = $this->expectRedirectResponse($request, '/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result);
}
public function testTargetPathParameterIsCustomised()
{
$options = array('target_path_parameter' => '_my_target_path');
$request = Request::create('/?_my_target_path=/dashboard');
$response = $this->expectRedirectResponse($request, '/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result);
}
public function testTargetPathIsTakenFromTheSession()
{ {
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock(); $session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
$session->expects($this->once()) $session->expects($this->once())->method('get')->with('_security.admin.target_path')->will($this->returnValue('/admin/dashboard'));
->method('get')->with('_security.admin.target_path') $session->expects($this->once())->method('remove')->with('_security.admin.target_path');
->will($this->returnValue('/admin/dashboard')); $requestWithSession = Request::create('/');
$session->expects($this->once()) $requestWithSession->setSession($session);
->method('remove')->with('_security.admin.target_path');
$request = Request::create('/?_my_target_path=/dashboard'); return array(
$request->setSession($session); 'default' => array(
$response = $this->expectRedirectResponse($request, '/admin/dashboard'); Request::create('/'),
array(),
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array()); '/',
$handler->setProviderKey('admin'); ),
'forced target path' => array(
$result = $handler->onAuthenticationSuccess($request, $this->token); Request::create('/'),
array('always_use_default_target_path' => true, 'default_target_path' => '/dashboard'),
$this->assertSame($response, $result); '/dashboard',
} ),
'target path as query string' => array(
public function testTargetPathIsPassedAsReferer() Request::create('/?_target_path=/dashboard'),
{ array(),
$options = array('use_referer' => true); '/dashboard',
$request = Request::create('/'); ),
$request->headers->set('Referer', '/dashboard'); 'target path name as query string is customized' => array(
$response = $this->expectRedirectResponse($request, '/dashboard'); Request::create('/?_my_target_path=/dashboard'),
array('target_path_parameter' => '_my_target_path'),
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options); '/dashboard',
$result = $handler->onAuthenticationSuccess($request, $this->token); ),
'target path name as query string is customized and nested' => array(
$this->assertSame($response, $result); Request::create('/?_target_path[value]=/dashboard'),
} array('target_path_parameter' => '_target_path[value]'),
'/dashboard',
public function testRefererHasToBeDifferentThatLoginUrl() ),
{ 'target path in session' => array(
$options = array('use_referer' => true); $requestWithSession,
$request = Request::create('/'); array(),
$request->headers->set('Referer', '/login'); '/admin/dashboard',
$this->httpUtils->expects($this->once()) ),
->method('generateUri')->with($request, '/login') 'target path as referer' => array(
->will($this->returnValue('/login')); Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
array('use_referer' => true),
$response = $this->expectRedirectResponse($request, '/'); '/dashboard',
),
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options); 'target path as referer is ignored if not configured' => array(
$result = $handler->onAuthenticationSuccess($request, $this->token); Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/dashboard')),
array(),
$this->assertSame($response, $result); '/',
} ),
'target path should be different than login URL' => array(
public function testRefererTargetPathIsIgnoredByDefault() Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login')),
{ array('use_referer' => true, 'login_path' => '/login'),
$request = Request::create('/'); '/',
$response = $this->expectRedirectResponse($request, '/'); ),
'target path should be different than login URL (query string does not matter)' => array(
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array()); Request::create('/', 'GET', array(), array(), array(), array('HTTP_REFERER' => 'http://localhost/login?t=1&p=2')),
$result = $handler->onAuthenticationSuccess($request, $this->token); array('use_referer' => true, 'login_path' => '/login'),
'/',
$this->assertSame($response, $result); ),
} );
private function expectRedirectResponse(Request $request, $path)
{
$response = new Response();
$this->httpUtils->expects($this->once())
->method('createRedirectResponse')
->with($request, $path)
->will($this->returnValue($response));
return $response;
} }
} }