diff options
author | Ryan Weaver <ryan@thatsquality.com> | 2015-09-20 20:11:34 -0400 |
---|---|---|
committer | Ryan Weaver <ryan@thatsquality.com> | 2015-09-20 20:11:34 -0400 |
commit | 1aa1d1b25ee51760e703df10ca383cd3a6e957e4 (patch) | |
tree | 6c70c9674aabd02bf7f61bcd68de6aca945670f5 /Guard/Tests/GuardAuthenticatorHandlerTest.php | |
parent | 9143527a73e82b6335761a1dda73eb2c2d240269 (diff) | |
download | symfony-security-1aa1d1b25ee51760e703df10ca383cd3a6e957e4.zip symfony-security-1aa1d1b25ee51760e703df10ca383cd3a6e957e4.tar.gz symfony-security-1aa1d1b25ee51760e703df10ca383cd3a6e957e4.tar.bz2 |
Fixing a bug where having an authentication failure would log you out.
This solution is a copy of what AbstractAuthenticationListener does. Scenario:
1) Login
2) Go back to the log in page
3) Put in a bad user/pass
You *should* still be logged in after a failed attempt. This commit gives that behavior.
Diffstat (limited to 'Guard/Tests/GuardAuthenticatorHandlerTest.php')
-rw-r--r-- | Guard/Tests/GuardAuthenticatorHandlerTest.php | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/Guard/Tests/GuardAuthenticatorHandlerTest.php b/Guard/Tests/GuardAuthenticatorHandlerTest.php index 6b27e20..6f36702 100644 --- a/Guard/Tests/GuardAuthenticatorHandlerTest.php +++ b/Guard/Tests/GuardAuthenticatorHandlerTest.php @@ -18,9 +18,6 @@ use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; use Symfony\Component\Security\Http\SecurityEvents; -/** - * @author Ryan Weaver <weaverryan@gmail.com> - */ class GuardAuthenticatorHandlerTest extends \PHPUnit_Framework_TestCase { private $tokenStorage; @@ -63,7 +60,41 @@ class GuardAuthenticatorHandlerTest extends \PHPUnit_Framework_TestCase public function testHandleAuthenticationFailure() { + // setToken() not called - getToken() will return null, so there's nothing to clear + $this->tokenStorage->expects($this->never()) + ->method('setToken') + ->with(null); + $authException = new AuthenticationException('Bad password!'); + + $response = new Response('Try again, but with the right password!'); + $this->guardAuthenticator->expects($this->once()) + ->method('onAuthenticationFailure') + ->with($this->request, $authException) + ->will($this->returnValue($response)); + + $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher); + $actualResponse = $handler->handleAuthenticationFailure($authException, $this->request, $this->guardAuthenticator, 'firewall_provider_key'); + $this->assertSame($response, $actualResponse); + } + + /** + * @dataProvider getTokenClearingTests + */ + public function testHandleAuthenticationClearsToken($tokenClass, $tokenProviderKey, $actualProviderKey, $shouldTokenBeCleared) + { + $token = $this->getMockBuilder($tokenClass) + ->disableOriginalConstructor() + ->getMock(); + $token->expects($this->any()) + ->method('getProviderKey') + ->will($this->returnValue($tokenProviderKey)); + + // make the $token be the current token $this->tokenStorage->expects($this->once()) + ->method('getToken') + ->will($this->returnValue($token)); + + $this->tokenStorage->expects($shouldTokenBeCleared ? $this->once() : $this->never()) ->method('setToken') ->with(null); $authException = new AuthenticationException('Bad password!'); @@ -75,10 +106,21 @@ class GuardAuthenticatorHandlerTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue($response)); $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher); - $actualResponse = $handler->handleAuthenticationFailure($authException, $this->request, $this->guardAuthenticator); + $actualResponse = $handler->handleAuthenticationFailure($authException, $this->request, $this->guardAuthenticator, $actualProviderKey); $this->assertSame($response, $actualResponse); } + public function getTokenClearingTests() + { + $tests = array(); + // correct token class and matching firewall => clear the token + $tests[] = array('Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken', 'the_firewall_key', 'the_firewall_key', true); + $tests[] = array('Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken', 'the_firewall_key', 'different_key', false); + $tests[] = array('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', 'the_firewall_key', 'the_firewall_key', false); + + return $tests; + } + protected function setUp() { $this->tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); |