* * 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:----BUlJeBRsGRyEf43hcbibqXBkmT39LQ1BqJRUVDSEtKRUYJGLnEhKXpuBFJAsDNiym50VMuhdSFl9k/8zc2vf/X72EcUSRdl26IH+i0TpayhVdIQUehv7CknfNxF19tqOJZym2y17SMxcxIEJchYiBKgxpRU1g4UuWejoa4LPLQng9+W8bJBg75B7oe/DA49ea8yT7vSEXzseC6AnRYjCSIJsSzzqfuJbpn4x2j8qXaKrQyT5DhBoan2bpcDoOOLHBSPMDO0b02EciZXZo+sOUHk2QBkBtpIjV0necssrmtg0ThV5PHcl/ZVlxDctChUkgMuLdeSrDMe9UgnCV/MxpLXLgmAoBCgoeG8XdWrBeQLWGMxpqfcEcTar/sgj5Ggwz9MRKcOhnJhVo9vyRN6D6dzf9aO25wedWR1KwFKi1FxWPmnNULCoeC9RB1/JgY69sz3NtTpfNKmDAbuepzuHCHJGVLofZfBY1YSnPU0KRuLU2cNgb4ribmtFzp6VES23Qvxc/EAnBE4056jsC4/j+96jCDbPp6Gs0ll53StZtbca/kDFKoeyJ5ebnbsmKm9j/w6WNcEq+dDOSZ3mFEhvsqqxM0gxCiruB3VefayCYkz0rBAXphFjH2FZ4puoEBeR0a2uuEsHeqIsP+KxK6+yPL7K4a8y3nMdTrtKAf+ykJI=----ATTACHMENT:----MTU3NDYxMjYxODEyNDQwNCA0OTI0MDI4OTA3MDg4Njk0IDM3MDgyNTg0MDg5NTAwOTM=