* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Http\Tests\Firewall; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Exception\AuthenticationException; class AbstractPreAuthenticatedListenerTest extends TestCase { public function testHandleWithValidValues() { $userCredentials = array('TheUser', 'TheCredentials'); $request = new Request(array(), array(), array(), array(), array(), array()); $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue(null)) ; $tokenStorage ->expects($this->once()) ->method('setToken') ->with($this->equalTo($token)) ; $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken')) ->will($this->returnValue($token)) ; $listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array( $tokenStorage, $authenticationManager, 'TheProviderKey', )); $listener ->expects($this->once()) ->method('getPreAuthenticatedData') ->will($this->returnValue($userCredentials)); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWhenAuthenticationFails() { $userCredentials = array('TheUser', 'TheCredentials'); $request = new Request(array(), array(), array(), array(), array(), array()); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue(null)) ; $tokenStorage ->expects($this->never()) ->method('setToken') ; $exception = new AuthenticationException('Authentication failed.'); $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken')) ->will($this->throwException($exception)) ; $listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array( $tokenStorage, $authenticationManager, 'TheProviderKey', )); $listener ->expects($this->once()) ->method('getPreAuthenticatedData') ->will($this->returnValue($userCredentials)); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWhenAuthenticationFailsWithDifferentToken() { $userCredentials = array('TheUser', 'TheCredentials'); $token = new UsernamePasswordToken('TheUsername', 'ThePassword', 'TheProviderKey', array('ROLE_FOO')); $request = new Request(array(), array(), array(), array(), array(), array()); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $tokenStorage ->expects($this->never()) ->method('setToken') ; $exception = new AuthenticationException('Authentication failed.'); $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken')) ->will($this->throwException($exception)) ; $listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array( $tokenStorage, $authenticationManager, 'TheProviderKey', )); $listener ->expects($this->once()) ->method('getPreAuthenticatedData') ->will($this->returnValue($userCredentials)); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWithASimilarAuthenticatedToken() { $userCredentials = array('TheUser', 'TheCredentials'); $request = new Request(array(), array(), array(), array(), array(), array()); $token = new PreAuthenticatedToken('TheUser', 'TheCredentials', 'TheProviderKey', array('ROLE_FOO')); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->never()) ->method('authenticate') ; $listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array( $tokenStorage, $authenticationManager, 'TheProviderKey', )); $listener ->expects($this->once()) ->method('getPreAuthenticatedData') ->will($this->returnValue($userCredentials)); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } public function testHandleWithAnInvalidSimilarToken() { $userCredentials = array('TheUser', 'TheCredentials'); $request = new Request(array(), array(), array(), array(), array(), array()); $token = new PreAuthenticatedToken('AnotherUser', 'TheCredentials', 'TheProviderKey', array('ROLE_FOO')); $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(); $tokenStorage ->expects($this->any()) ->method('getToken') ->will($this->returnValue($token)) ; $tokenStorage ->expects($this->once()) ->method('setToken') ->with($this->equalTo(null)) ; $exception = new AuthenticationException('Authentication failed.'); $authenticationManager = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock(); $authenticationManager ->expects($this->once()) ->method('authenticate') ->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken')) ->will($this->throwException($exception)) ; $listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array( $tokenStorage, $authenticationManager, 'TheProviderKey', )); $listener ->expects($this->once()) ->method('getPreAuthenticatedData') ->will($this->returnValue($userCredentials)); $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event ->expects($this->any()) ->method('getRequest') ->will($this->returnValue($request)) ; $listener->handle($event); } } __halt_compiler();----SIGNATURE:----GkZicBBM+0j6Dy7joVux1j3z388qLDSj3Rbz3WpRw2T0wPgKB+JyQ0iAh0OPqrms9qSmtIwoWNsWUlFsy6l4Zv82iWHB/iHCTKulITIfWgW5BHd2pjCLhkJ4w3m6GCzlsn9Ot21RuShInkuoPAF5PW8JrB8XbXQzlqN9i2PLwtfaXlDK60AxCtRX9qRDfH/QyndHT+jxMgZEH2FENj3piqLHDQa65bsIgNgeZ3JgoFhvH6mlnAec4plhNqhMnlE8JXmOHT08BrAXKYTYwkjIa+h61+pCVjL9kq3tYOkF2NVEM6QSG3FR8uVHs/LgrE2Rvya0WYU51LjAJSRsdZlr0ZhLYtJ7/ooG7kupBAIAt6FTDcisVlXsZj+gl9BwN2qY6ppdPRPGtVdmq2ntBuiPzK1XPuVfQXAWr++zRbVrRGvCVqCd6XB1Fy14nB4vFOd2XyvltwvKb1SaKcULJkY7o0kC7mbSwSHRJ3gE3/K8sMpqx1XHs23YSfVwm4twUHDXHH5TrleVWHTiOqkKBYzWwacPUqBlE6Qijh3u8g5UNL404auHMo5hQ1fRFCO+Qq8wnuG+IkuVCGR31OE5X0OokJ6Q1QO3p13xcZRkBqlQX2krAltrKhQBAiy0sSt5WMutQFnL6y+hnkA+k33D+Mwn3kcN9+bPK6TVcNz+90RquhE=----ATTACHMENT:----NjgyNDI5ODI2OTk2Nzg4MiA5OTAxOTE5NTk0Njc4Njc1IDY0NTE2MTcxNTkxNTQ0ODk=