|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Tests\Authentication\Provider; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Security\Core\Exception\DisabledException; |
| 16 | +use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider; |
| 17 | +use Symfony\Component\Security\Core\Exception\LockedException; |
| 18 | + |
| 19 | +class SimpleAuthenticationProviderTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @expectedException \Symfony\Component\Security\Core\Exception\DisabledException |
| 23 | + */ |
| 24 | + public function testAuthenticateWhenPreChecksFails() |
| 25 | + { |
| 26 | + $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); |
| 27 | + |
| 28 | + $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); |
| 29 | + $token->expects($this->any()) |
| 30 | + ->method('getUser') |
| 31 | + ->will($this->returnValue($user)); |
| 32 | + |
| 33 | + $userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); |
| 34 | + $userChecker->expects($this->once()) |
| 35 | + ->method('checkPreAuth') |
| 36 | + ->will($this->throwException(new DisabledException())); |
| 37 | + |
| 38 | + $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); |
| 39 | + $authenticator->expects($this->once()) |
| 40 | + ->method('authenticateToken') |
| 41 | + ->will($this->returnValue($token)); |
| 42 | + |
| 43 | + $provider = $this->getProvider($authenticator, null, $userChecker); |
| 44 | + |
| 45 | + $provider->authenticate($token); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @expectedException \Symfony\Component\Security\Core\Exception\LockedException |
| 50 | + */ |
| 51 | + public function testAuthenticateWhenPostChecksFails() |
| 52 | + { |
| 53 | + $user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); |
| 54 | + |
| 55 | + $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); |
| 56 | + $token->expects($this->any()) |
| 57 | + ->method('getUser') |
| 58 | + ->will($this->returnValue($user)); |
| 59 | + |
| 60 | + $userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); |
| 61 | + $userChecker->expects($this->once()) |
| 62 | + ->method('checkPostAuth') |
| 63 | + ->will($this->throwException(new LockedException())); |
| 64 | + |
| 65 | + $authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); |
| 66 | + $authenticator->expects($this->once()) |
| 67 | + ->method('authenticateToken') |
| 68 | + ->will($this->returnValue($token)); |
| 69 | + |
| 70 | + $provider = $this->getProvider($authenticator, null, $userChecker); |
| 71 | + |
| 72 | + $provider->authenticate($token); |
| 73 | + } |
| 74 | + |
| 75 | + protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test') |
| 76 | + { |
| 77 | + if (null === $userChecker) { |
| 78 | + $userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); |
| 79 | + } |
| 80 | + if (null === $simpleAuthenticator) { |
| 81 | + $simpleAuthenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); |
| 82 | + } |
| 83 | + if (null === $userProvider) { |
| 84 | + $userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock(); |
| 85 | + } |
| 86 | + |
| 87 | + return new SimpleAuthenticationProvider($simpleAuthenticator, $userProvider, $key, $userChecker); |
| 88 | + } |
| 89 | +} |
0 commit comments