|
| 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\Guard\Tests\Authenticator; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 18 | +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
| 19 | +use Symfony\Component\Security\Core\User\User; |
| 20 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 21 | +use Symfony\Component\Security\Guard\Authenticator\GuardBridgeAuthenticator; |
| 22 | +use Symfony\Component\Security\Guard\AuthenticatorInterface; |
| 23 | +use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; |
| 24 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; |
| 25 | +use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials; |
| 26 | +use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 27 | + |
| 28 | +class GuardBridgeAuthenticatorTest extends TestCase |
| 29 | +{ |
| 30 | + private $guardAuthenticator; |
| 31 | + private $userProvider; |
| 32 | + private $authenticator; |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + if (!interface_exists(\Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface::class)) { |
| 37 | + $this->markTestSkipped('Authenticator system not installed.'); |
| 38 | + } |
| 39 | + |
| 40 | + $this->guardAuthenticator = $this->createMock(AuthenticatorInterface::class); |
| 41 | + $this->userProvider = $this->createMock(UserProviderInterface::class); |
| 42 | + $this->authenticator = new GuardBridgeAuthenticator($this->guardAuthenticator, $this->userProvider); |
| 43 | + } |
| 44 | + |
| 45 | + public function testSupports() |
| 46 | + { |
| 47 | + $request = new Request(); |
| 48 | + |
| 49 | + $this->guardAuthenticator->expects($this->once()) |
| 50 | + ->method('supports') |
| 51 | + ->with($request) |
| 52 | + ->willReturn(true); |
| 53 | + |
| 54 | + $this->assertTrue($this->authenticator->supports($request)); |
| 55 | + } |
| 56 | + |
| 57 | + public function testNoSupport() |
| 58 | + { |
| 59 | + $request = new Request(); |
| 60 | + |
| 61 | + $this->guardAuthenticator->expects($this->once()) |
| 62 | + ->method('supports') |
| 63 | + ->with($request) |
| 64 | + ->willReturn(false); |
| 65 | + |
| 66 | + $this->assertFalse($this->authenticator->supports($request)); |
| 67 | + } |
| 68 | + |
| 69 | + public function testAuthenticate() |
| 70 | + { |
| 71 | + $request = new Request(); |
| 72 | + |
| 73 | + $credentials = ['password' => 's3cr3t']; |
| 74 | + $this->guardAuthenticator->expects($this->once()) |
| 75 | + ->method('getCredentials') |
| 76 | + ->with($request) |
| 77 | + ->willReturn($cr
10000
edentials); |
| 78 | + |
| 79 | + $user = new User('test', null, ['ROLE_USER']); |
| 80 | + $this->guardAuthenticator->expects($this->once()) |
| 81 | + ->method('getUser') |
| 82 | + ->with($credentials, $this->userProvider) |
| 83 | + ->willReturn($user); |
| 84 | + |
| 85 | + $passport = $this->authenticator->authenticate($request); |
| 86 | + $this->assertTrue($passport->hasBadge(CustomCredentials::class)); |
| 87 | + |
| 88 | + $this->guardAuthenticator->expects($this->once()) |
| 89 | + ->method('checkCredentials') |
| 90 | + ->with($credentials, $user) |
| 91 | + ->willReturn(true); |
| 92 | + |
| 93 | + $passport->getBadge(CustomCredentials::class)->executeCustomChecker($user); |
| 94 | + } |
| 95 | + |
| 96 | + public function testAuthenticateNoUser() |
| 97 | + { |
| 98 | + $this->expectException(UsernameNotFoundException::class); |
| 99 | + |
| 100 | + $request = new Request(); |
| 101 | + |
| 102 | + $credentials = ['password' => 's3cr3t']; |
| 103 | + $this->guardAuthenticator->expects($this->once()) |
| 104 | + ->method('getCredentials') |
| 105 | + ->with($request) |
| 106 | + ->willReturn($credentials); |
| 107 | + |
| 108 | + $this->guardAuthenticator->expects($this->once()) |
| 109 | + ->method('getUser') |
| 110 | + ->with($credentials, $this->userProvider) |
| 111 | + ->willReturn(null); |
| 112 | + |
| 113 | + $this->authenticator->authenticate($request); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @dataProvider provideRememberMeData |
| 118 | + */ |
| 119 | + public function testAuthenticateRememberMe(bool $rememberMeSupported) |
| 120 | + { |
| 121 | + $request = new Request(); |
| 122 | + |
| 123 | + $credentials = ['password' => 's3cr3t']; |
| 124 | + $this->guardAuthenticator->expects($this->once()) |
| 125 | + ->method('getCredentials') <
B41A
div aria-hidden="true" class="position-absolute top-0 d-flex user-select-none DiffLineTableCellParts-module__comment-indicator--eI0hb"> |
| 126 | + ->with($request) |
| 127 | + ->willReturn($credentials); |
| 128 | + |
| 129 | + $user = new User('test', null, ['ROLE_USER']); |
| 130 | + $this->guardAuthenticator->expects($this->once()) |
| 131 | + ->method('getUser') |
| 132 | + ->with($credentials, $this->userProvider) |
| 133 | + ->willReturn($user); |
| 134 | + |
| 135 | + $this->guardAuthenticator->expects($this->once()) |
| 136 | + ->method('supportsRememberMe') |
| 137 | + ->willReturn($rememberMeSupported); |
| 138 | + |
| 139 | + $passport = $this->authenticator->authenticate($request); |
| 140 | + $this->assertEquals($rememberMeSupported, $passport->hasBadge(RememberMeBadge::class)); |
| 141 | + } |
| 142 | + |
| 143 | + public function provideRememberMeData() |
| 144 | + { |
| 145 | + yield [true]; |
| 146 | + yield [false]; |
| 147 | + } |
| 148 | + |
| 149 | + public function testCreateAuthenticatedToken() |
| 150 | + { |
F438
td> | 151 | + $user = new User('test', null, ['ROLE_USER']); |
| 152 | + |
| 153 | + $token = new PostAuthenticationGuardToken($user, 'main', ['ROLE_USER']); |
| 154 | + $this->guardAuthenticator->expects($this->once()) |
| 155 | + ->method('createAuthenticatedToken') |
| 156 | + ->with($user, 'main') |
| 157 | + ->willReturn($token); |
| 158 | + |
| 159 | + $this->assertSame($token, $this->authenticator->createAuthenticatedToken(new SelfValidatingPassport($user), 'main')); |
| 160 | + } |
| 161 | + |
| 162 | + public function testHandleSuccess() |
| 163 | + { |
| 164 | + $request = new Request(); |
| 165 | + $token = new PostAuthenticationGuardToken(new User('test', null, ['ROLE_USER']), 'main', ['ROLE_USER']); |
| 166 | + |
| 167 | + $response = new Response(); |
| 168 | + $this->guardAuthenticator->expects($this->once()) |
| 169 | + ->method('onAuthenticationSuccess') |
| 170 | + ->with($request, $token) |
| 171 | + ->willReturn($response); |
| 172 | + |
| 173 | + $this->assertSame($response, $this->authenticator->onAuthenticationSuccess($request, $token, 'main')); |
| 174 | + } |
| 175 | + |
| 176 | + public function testOnFailure() |
| 177 | + { |
| 178 | + $request = new Request(); |
| 179 | + $exception = new AuthenticationException(); |
| 180 | + |
| 181 | + $response = new Response(); |
| 182 | + $this->guardAuthenticator->expects($this->once()) |
| 183 | + ->method('onAuthenticationFailure') |
| 184 | + ->with($request, $exception
57DA
) |
| 185 | + ->willReturn($response); |
| 186 | + |
| 187 | + $this->assertSame($response, $this->authenticator->onAuthenticationFailure($request, $exception)); |
| 188 | + } |
| 189 | +} |
0 commit comments