[Security] refactored tests

This commit is contained in:
Fabien Potencier 2017-07-19 07:07:01 +02:00
parent 2040770da5
commit 3387612451

View File

@ -12,31 +12,28 @@
namespace Symfony\Component\Security\Http\Tests\Authentication; namespace Symfony\Component\Security\Http\Tests\Authentication;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
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;
class DefaultAuthenticationSuccessHandlerTest extends TestCase class DefaultAuthenticationSuccessHandlerTest extends TestCase
{ {
private $httpUtils = null; private $httpUtils = null;
private $request = null;
private $token = null; private $token = null;
protected function setUp() protected function setUp()
{ {
$this->httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock(); $this->httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$this->request->headers = $this->getMockBuilder('Symfony\Component\HttpFoundation\HeaderBag')->getMock();
$this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
} }
public function testRequestIsRedirected() public function testRequestIsRedirected()
{ {
$response = $this->expectRedirectResponse('/'); $request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array()); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
@ -48,24 +45,22 @@ class DefaultAuthenticationSuccessHandlerTest extends TestCase
'default_target_path' => '/dashboard', 'default_target_path' => '/dashboard',
); );
$response = $this->expectRedirectResponse('/dashboard'); $request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
public function testTargetPathIsPassedWithRequest() public function testTargetPathIsPassedWithRequest()
{ {
$this->request->expects($this->once()) $request = Request::create('/?_target_path=/dashboard');
->method('get')->with('_target_path') $response = $this->expectRedirectResponse($request, '/dashboard');
->will($this->returnValue('/dashboard'));
$response = $this->expectRedirectResponse('/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array()); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
@ -73,15 +68,11 @@ class DefaultAuthenticationSuccessHandlerTest extends TestCase
public function testTargetPathParameterIsCustomised() public function testTargetPathParameterIsCustomised()
{ {
$options = array('target_path_parameter' => '_my_target_path'); $options = array('target_path_parameter' => '_my_target_path');
$request = Request::create('/?_my_target_path=/dashboard');
$this->request->expects($this->once()) $response = $this->expectRedirectResponse($request, '/dashboard');
->method('get')->with('_my_target_path')
->will($this->returnValue('/dashboard'));
$response = $this->expectRedirectResponse('/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
@ -95,16 +86,14 @@ class DefaultAuthenticationSuccessHandlerTest extends TestCase
$session->expects($this->once()) $session->expects($this->once())
->method('remove')->with('_security.admin.target_path'); ->method('remove')->with('_security.admin.target_path');
$this->request->expects($this->any()) $request = Request::create('/?_my_target_path=/dashboard');
->method('getSession') $request->setSession($session);
->will($this->returnValue($session)); $response = $this->expectRedirectResponse($request, '/admin/dashboard');
$response = $this->expectRedirectResponse('/admin/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array()); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$handler->setProviderKey('admin'); $handler->setProviderKey('admin');
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
@ -112,15 +101,12 @@ class DefaultAuthenticationSuccessHandlerTest extends TestCase
public function testTargetPathIsPassedAsReferer() public function testTargetPathIsPassedAsReferer()
{ {
$options = array('use_referer' => true); $options = array('use_referer' => true);
$request = Request::create('/');
$this->request->headers->expects($this->once()) $request->headers->set('Referer', '/dashboard');
->method('get')->with('Referer') $response = $this->expectRedirectResponse($request, '/dashboard');
->will($this->returnValue('/dashboard'));
$response = $this->expectRedirectResponse('/dashboard');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
@ -128,41 +114,37 @@ class DefaultAuthenticationSuccessHandlerTest extends TestCase
public function testRefererHasToBeDifferentThatLoginUrl() public function testRefererHasToBeDifferentThatLoginUrl()
{ {
$options = array('use_referer' => true); $options = array('use_referer' => true);
$request = Request::create('/');
$this->request->headers->expects($this->any()) $request->headers->set('Referer', '/login');
->method('get')->with('Referer')
->will($this->returnValue('/login'));
$this->httpUtils->expects($this->once()) $this->httpUtils->expects($this->once())
->method('generateUri')->with($this->request, '/login') ->method('generateUri')->with($request, '/login')
->will($this->returnValue('/login')); ->will($this->returnValue('/login'));
$response = $this->expectRedirectResponse('/'); $response = $this->expectRedirectResponse($request, '/');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
public function testRefererTargetPathIsIgnoredByDefault() public function testRefererTargetPathIsIgnoredByDefault()
{ {
$this->request->headers->expects($this->never())->method('get'); $request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/');
$response = $this->expectRedirectResponse('/');
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array()); $handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($this->request, $this->token); $result = $handler->onAuthenticationSuccess($request, $this->token);
$this->assertSame($response, $result); $this->assertSame($response, $result);
} }
private function expectRedirectResponse($path) private function expectRedirectResponse(Request $request, $path)
{ {
$response = new Response(); $response = new Response();
$this->httpUtils->expects($this->once()) $this->httpUtils->expects($this->once())
->method('createRedirectResponse') ->method('createRedirectResponse')
->with($this->request, $path) ->with($request, $path)
->will($this->returnValue($response)); ->will($this->returnValue($response));
return $response; return $response;