8000 Fix login redirect when referer contains a query string by fabpot · Pull Request #23580 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix login redirect when referer contains a query string #23580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 19, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Security] refactored tests
  • Loading branch information
fabpot committed Jul 19, 2017
commit 338761245100e09dafdce47f7646ef7d4fc14f64
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,28 @@
namespace Symfony\Component\Security\Http\Tests\Authentication;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;

class DefaultAuthenticationSuccessHandlerTest extends TestCase
{
private $httpUtils = null;

private $request = null;

private $token = null;

protected function setUp()
{
$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();
}

public function testRequestIsRedirected()
{
$response = $this->expectRedirectResponse('/');
$request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}
Expand All @@ -48,40 +45,34 @@ public function testDefaultTargetPathCanBeForced()
'default_target_path' => '/dashboard',
);

$response = $this->expectRedirectResponse('/dashboard');
$request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/dashboard');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}

public function testTargetPathIsPassedWithRequest()
{
$this->request->expects($this->once())
->method('get')->with('_target_path')
->will($this->returnValue('/dashboard'));

$response = $this->expectRedirectResponse('/dashboard');
$request = Request::create('/?_target_path=/dashboard');
$response = $this->expectRedirectResponse($request, '/dashboard');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}

public function testTargetPathParameterIsCustomised()
{
$options = array('target_path_parameter' => '_my_target_path');

$this->request->expects($this->once())
->method('get')->with('_my_target_path')
->will($this->returnValue('/dashboard'));

$response = $this->expectRedirectResponse('/dashboard');
$request = Request::create('/?_my_target_path=/dashboard');
$response = $this->expectRedirectResponse($request, '/dashboard');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}
Expand All @@ -95,74 +86,65 @@ public function testTargetPathIsTakenFromTheSession()
$session->expects($this->once())
->method('remove')->with('_security.admin.target_path');

$this->request->expects($this->any())
->method('getSession')
->will($this->returnValue($session));

$response = $this->expectRedirectResponse('/admin/dashboard');
$request = Request::create('/?_my_target_path=/dashboard');
$request->setSession($session);
$response = $this->expectRedirectResponse($request, '/admin/dashboard');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$handler->setProviderKey('admin');

$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}

public function testTargetPathIsPassedAsReferer()
{
$options = array('use_referer' => true);

$this->request->headers->expects($this->once())
->method('get')->with('Referer')
->will($this->returnValue('/dashboard'));

$response = $this->expectRedirectResponse('/dashboard');
$request = Request::create('/');
$request->headers->set('Referer', '/dashboard');
$response = $this->expectRedirectResponse($request, '/dashboard');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}

public function testRefererHasToBeDifferentThatLoginUrl()
{
$options = array('use_referer' => true);

$this->request->headers->expects($this->any())
->method('get')->with('Referer')
->will($this->returnValue('/login'));

$request = Request::create('/');
$request->headers->set('Referer', '/login');
$this->httpUtils->expects($this->once())
->method('generateUri')->with($this->request, '/login')
->method('generateUri')->with($request, '/login')
->will($this->returnValue('/login'));

$response = $this->expectRedirectResponse('/');
$response = $this->expectRedirectResponse($request, '/');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}

public function testRefererTargetPathIsIgnoredByDefault()
{
$this->request->headers->expects($this->never())->method('get');

$response = $this->expectRedirectResponse('/');
$request = Request::create('/');
$response = $this->expectRedirectResponse($request, '/');

$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
$result = $handler->onAuthenticationSuccess($request, $this->token);

$this->assertSame($response, $result);
}

private function expectRedirectResponse($path)
private function expectRedirectResponse(Request $request, $path)
{
$response = new Response();
$this->httpUtils->expects($this->once())
->method('createRedirectResponse')
->with($this->request, $path)
->with($request, $path)
->will($this->returnValue($response));

return $response;
Expand Down
0