summaryrefslogtreecommitdiffstats
path: root/Guard/Tests
diff options
context:
space:
mode:
authorRyan Weaver <ryan@thatsquality.com>2015-05-17 14:39:26 -0400
committerRyan Weaver <ryan@thatsquality.com>2015-09-20 19:24:20 -0400
commite7d6919203ec4e60e71bba1f4c84c9aee0a4c816 (patch)
treec60a9af360303ca2fac0a7eb553ecdf5dd71709f /Guard/Tests
parent85fb36d5cc968c432e0f0013d0eaab0c7c9f78c6 (diff)
downloadsymfony-security-e7d6919203ec4e60e71bba1f4c84c9aee0a4c816.zip
symfony-security-e7d6919203ec4e60e71bba1f4c84c9aee0a4c816.tar.gz
symfony-security-e7d6919203ec4e60e71bba1f4c84c9aee0a4c816.tar.bz2
Initial commit (but after some polished work) of the new Guard authentication system
Diffstat (limited to 'Guard/Tests')
-rw-r--r--Guard/Tests/Firewall/GuardAuthenticationListenerTest.php222
-rw-r--r--Guard/Tests/GuardAuthenticatorHandlerTest.php99
-rw-r--r--Guard/Tests/Provider/GuardAuthenticationProviderTest.php93
3 files changed, 414 insertions, 0 deletions
diff --git a/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php b/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php
new file mode 100644
index 0000000..b8939a9
--- /dev/null
+++ b/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php
@@ -0,0 +1,222 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Guard\Tests\Firewall;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Guard\Firewall\GuardAuthenticationListener;
+use Symfony\Component\Security\Guard\Token\NonAuthenticatedGuardToken;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+
+/**
+ * @author Ryan Weaver <weaverryan@gmail.com>
+ */
+class GuardAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
+{
+ private $authenticationManager;
+ private $guardAuthenticatorHandler;
+ private $event;
+ private $logger;
+ private $request;
+ private $rememberMeServices;
+
+ public function testHandleSuccess()
+ {
+ $authenticator = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $authenticateToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $providerKey = 'my_firewall';
+
+ $credentials = array('username' => 'weaverryan', 'password' => 'all_your_base');
+ $authenticator
+ ->expects($this->once())
+ ->method('getCredentialsFromRequest')
+ ->with($this->equalTo($this->request))
+ ->will($this->returnValue($credentials));
+
+ // a clone of the token that should be created internally
+ $uniqueGuardKey = 'my_firewall_0';
+ $nonAuthedToken = new NonAuthenticatedGuardToken($credentials, $uniqueGuardKey);
+
+ $this->authenticationManager
+ ->expects($this->once())
+ ->method('authenticate')
+ ->with($this->equalTo($nonAuthedToken))
+ ->will($this->returnValue($authenticateToken));
+
+ $this->guardAuthenticatorHandler
+ ->expects($this->once())
+ ->method('authenticateWithToken')
+ ->with($authenticateToken, $this->request);
+
+ $this->guardAuthenticatorHandler
+ ->expects($this->once())
+ ->method('handleAuthenticationSuccess')
+ ->with($authenticateToken, $this->request, $authenticator, $providerKey);
+
+ $listener = new GuardAuthenticationListener(
+ $this->guardAuthenticatorHandler,
+ $this->authenticationManager,
+ $providerKey,
+ array($authenticator),
+ $this->logger
+ );
+
+ $listener->setRememberMeServices($this->rememberMeServices);
+ // should never be called - our handleAuthenticationSuccess() does not return a Response
+ $this->rememberMeServices
+ ->expects($this->never())
+ ->method('loginSuccess');
+
+ $listener->handle($this->event);
+ }
+
+ public function testHandleSuccessWithRememberMe()
+ {
+ $authenticator = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $authenticateToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $providerKey = 'my_firewall_with_rememberme';
+
+ $authenticator
+ ->expects($this->once())
+ ->method('getCredentialsFromRequest')
+ ->with($this->equalTo($this->request))
+ ->will($this->returnValue(array('username' => 'anything_not_empty')));
+
+ $this->authenticationManager
+ ->expects($this->once())
+ ->method('authenticate')
+ ->will($this->returnValue($authenticateToken));
+
+ $successResponse = new Response('Success!');
+ $this->guardAuthenticatorHandler
+ ->expects($this->once())
+ ->method('handleAuthenticationSuccess')
+ ->will($this->returnValue($successResponse));
+
+ $listener = new GuardAuthenticationListener(
+ $this->guardAuthenticatorHandler,
+ $this->authenticationManager,
+ $providerKey,
+ array($authenticator),
+ $this->logger
+ );
+
+ $listener->setRememberMeServices($this->rememberMeServices);
+ $authenticator->expects($this->once())
+ ->method('supportsRememberMe')
+ ->will($this->returnValue(true));
+ // should be called - we do have a success Response
+ $this->rememberMeServices
+ ->expects($this->once())
+ ->method('loginSuccess');
+
+ $listener->handle($this->event);
+ }
+
+ public function testHandleCatchesAuthenticationException()
+ {
+ $authenticator = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $providerKey = 'my_firewall2';
+
+ $authException = new AuthenticationException('Get outta here crazy user with a bad password!');
+ $authenticator
+ ->expects($this->once())
+ ->method('getCredentialsFromRequest')
+ ->will($this->throwException($authException));
+
+ // this is not called
+ $this->authenticationManager
+ ->expects($this->never())
+ ->method('authenticate');
+
+ $this->guardAuthenticatorHandler
+ ->expects($this->once())
+ ->method('handleAuthenticationFailure')
+ ->with($authException, $this->request, $authenticator);
+
+ $listener = new GuardAuthenticationListener(
+ $this->guardAuthenticatorHandler,
+ $this->authenticationManager,
+ $providerKey,
+ array($authenticator),
+ $this->logger
+ );
+
+ $listener->handle($this->event);
+ }
+
+ public function testReturnNullToSkipAuth()
+ {
+ $authenticatorA = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $authenticatorB = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $providerKey = 'my_firewall3';
+
+ $authenticatorA
+ ->expects($this->once())
+ ->method('getCredentialsFromRequest')
+ ->will($this->returnValue(null));
+ $authenticatorB
+ ->expects($this->once())
+ ->method('getCredentialsFromRequest')
+ ->will($this->returnValue(null));
+
+ // this is not called
+ $this->authenticationManager
+ ->expects($this->never())
+ ->method('authenticate');
+
+ $this->guardAuthenticatorHandler
+ ->expects($this->never())
+ ->method('handleAuthenticationSuccess');
+
+ $listener = new GuardAuthenticationListener(
+ $this->guardAuthenticatorHandler,
+ $this->authenticationManager,
+ $providerKey,
+ array($authenticatorA, $authenticatorB),
+ $this->logger
+ );
+
+ $listener->handle($this->event);
+ }
+
+ protected function setUp()
+ {
+ $this->authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->guardAuthenticatorHandler = $this->getMockBuilder('Symfony\Component\Security\Guard\GuardAuthenticatorHandler')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->request = new Request(array(), array(), array(), array(), array(), array());
+
+ $this->event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $this->event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($this->request));
+
+ $this->logger = $this->getMock('Psr\Log\LoggerInterface');
+ $this->rememberMeServices = $this->getMock('Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface');
+ }
+
+ protected function tearDown()
+ {
+ $this->authenticationManager = null;
+ $this->guardAuthenticatorHandler = null;
+ $this->event = null;
+ $this->logger = null;
+ $this->request = null;
+ }
+}
diff --git a/Guard/Tests/GuardAuthenticatorHandlerTest.php b/Guard/Tests/GuardAuthenticatorHandlerTest.php
new file mode 100644
index 0000000..6b27e20
--- /dev/null
+++ b/Guard/Tests/GuardAuthenticatorHandlerTest.php
@@ -0,0 +1,99 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Guard\Tests;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
+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;
+ private $dispatcher;
+ private $token;
+ private $request;
+ private $guardAuthenticator;
+
+ public function testAuthenticateWithToken()
+ {
+ $this->tokenStorage->expects($this->once())
+ ->method('setToken')
+ ->with($this->token);
+
+ $loginEvent = new InteractiveLoginEvent($this->request, $this->token);
+
+ $this->dispatcher
+ ->expects($this->once())
+ ->method('dispatch')
+ ->with($this->equalTo(SecurityEvents::INTERACTIVE_LOGIN), $this->equalTo($loginEvent))
+ ;
+
+ $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
+ $handler->authenticateWithToken($this->token, $this->request);
+ }
+
+ public function testHandleAuthenticationSuccess()
+ {
+ $providerKey = 'my_handleable_firewall';
+ $response = new Response('Guard all the things!');
+ $this->guardAuthenticator->expects($this->once())
+ ->method('onAuthenticationSuccess')
+ ->with($this->request, $this->token, $providerKey)
+ ->will($this->returnValue($response));
+
+ $handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
+ $actualResponse = $handler->handleAuthenticationSuccess($this->token, $this->request, $this->guardAuthenticator, $providerKey);
+ $this->assertSame($response, $actualResponse);
+ }
+
+ public function testHandleAuthenticationFailure()
+ {
+ $this->tokenStorage->expects($this->once())
+ ->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);
+ $this->assertSame($response, $actualResponse);
+ }
+
+ protected function setUp()
+ {
+ $this->tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
+ $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+ $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $this->request = new Request(array(), array(), array(), array(), array(), array());
+ $this->guardAuthenticator = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ }
+
+ protected function tearDown()
+ {
+ $this->tokenStorage = null;
+ $this->dispatcher = null;
+ $this->token = null;
+ $this->request = null;
+ $this->guardAuthenticator = null;
+ }
+}
diff --git a/Guard/Tests/Provider/GuardAuthenticationProviderTest.php b/Guard/Tests/Provider/GuardAuthenticationProviderTest.php
new file mode 100644
index 0000000..0ef6818
--- /dev/null
+++ b/Guard/Tests/Provider/GuardAuthenticationProviderTest.php
@@ -0,0 +1,93 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Guard\Tests\Provider;
+
+use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProvider;
+
+/**
+ * @author Ryan Weaver <weaverryan@gmail.com>
+ */
+class GuardAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
+{
+ private $userProvider;
+ private $userChecker;
+ private $nonAuthedToken;
+
+ public function testAuthenticate()
+ {
+ $providerKey = 'my_cool_firewall';
+
+ $authenticatorA = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $authenticatorB = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $authenticatorC = $this->getMock('Symfony\Component\Security\Guard\GuardAuthenticatorInterface');
+ $authenticators = array($authenticatorA, $authenticatorB, $authenticatorC);
+
+ // called 2 times - for authenticator A and B (stops on B because of match)
+ $this->nonAuthedToken->expects($this->exactly(2))
+ ->method('getGuardProviderKey')
+ // it will return the "1" index, which will match authenticatorB
+ ->will($this->returnValue('my_cool_firewall_1'));
+
+ $enteredCredentials = array(
+ 'username' => '_weaverryan_test_user',
+ 'password' => 'guard_auth_ftw',
+ );
+ $this->nonAuthedToken->expects($this->once())
+ ->method('getCredentials')
+ ->will($this->returnValue($enteredCredentials));
+
+ // authenticators A and C are never called
+ $authenticatorA->expects($this->never())
+ ->method('authenticate');
+ $authenticatorC->expects($this->never())
+ ->method('authenticate');
+
+ $mockedUser = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
+ $authenticatorB->expects($this->once())
+ ->method('authenticate')
+ ->with($enteredCredentials, $this->userProvider)
+ ->will($this->returnValue($mockedUser));
+ $authedToken = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ $authenticatorB->expects($this->once())
+ ->method('createAuthenticatedToken')
+ ->with($mockedUser, $providerKey)
+ ->will($this->returnValue($authedToken));
+
+ // user checker should be called
+ $this->userChecker->expects($this->once())
+ ->method('checkPreAuth')
+ ->with($mockedUser);
+ $this->userChecker->expects($this->once())
+ ->method('checkPostAuth')
+ ->with($mockedUser);
+
+ $provider = new GuardAuthenticationProvider($authenticators, $this->userProvider, $providerKey, $this->userChecker);
+ $actualAuthedToken = $provider->authenticate($this->nonAuthedToken);
+ $this->assertSame($authedToken, $actualAuthedToken);
+ }
+
+ protected function setUp()
+ {
+ $this->userProvider = $this->getMock('Symfony\Component\Security\Core\User\UserProviderInterface');
+ $this->userChecker = $this->getMock('Symfony\Component\Security\Core\User\UserCheckerInterface');
+ $this->nonAuthedToken = $this->getMockBuilder('Symfony\Component\Security\Guard\Token\NonAuthenticatedGuardToken')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ protected function tearDown()
+ {
+ $this->userProvider = null;
+ $this->userChecker = null;
+ $this->nonAuthedToken = null;
+ }
+}