* * 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:----FhO8JM7ML91Jaz1zRoJ7JcAfOGLFTN0+UUBzlME4u/Xpn/4aMKYyCvkHz/vIZg4zrdqn2ZPG1WZ6o3bw6CgLJkcFWVyCMNS4fvSi/qlaFCPlmmupq9oSgVWElyCcJ1P/QL4GObijFfb+R+tQALvZVnHBLA0u/LefYclWunys4ognr7A79vDs9nh1oNvWTGX4AZrXsaee3LRjP5jBpMNB79+qhJuVTsmL2vLcDEDYTWJw4oemjJqk3QxizKYhor/edy2rMcI8X7Mll5MzyH5waaMkwMKtqg4CNPKoVAGbd3qB0A8KMKw93QBRmKL1LjpdSCAiGIIgT0rJ1nHr/kPh1Qw2fGi+bHpeYl58GlkGs6v85lG5blX1T3AFTeftFRKut9WkonVZ7H0V2qg3m4Q8m+cLk/QXOb+C6kkkOBN3gUj8Osqx07+AM7NLFyFedS89AMNb79dznlrGv1ekXEOpfFY2VbcKheWiser8bl0++W5FTHyLZjHwEiESQ04asX8SyUXobNLCWEr72vpc2L8Hch1rbi2dzSVYZGGaz1ukYy0HZZPSz/DurKRZ1chMBTunQ81Thb9M5V8gAo3DWwsgeoVpI74SyHrSt73itncJafcKaua2kFo1Y4yobnaCme7aBNRLE6wG9SoSHolwyMdxSogJEaxoq45WS3HyZhOQxN4=----ATTACHMENT:----Nzk5ODIzNjU5NDI4OTg0NyAxMjUyNTMyMDgzOTU5NDYgNTA1OTA5ODU1NjkxODg2NQ==