|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Bundle\User\Security\Authentication; |
| 10 | + |
| 11 | +use Ibexa\Bundle\User\Security\Authentication\DefaultAuthenticationFailureHandler; |
| 12 | +use Ibexa\Contracts\Core\Repository\Exceptions\PasswordInUnsupportedFormatException; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Session\Session; |
| 17 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 18 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 19 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 20 | +use Symfony\Component\Security\Http\HttpUtils; |
| 21 | + |
| 22 | +final class DefaultAuthenticationFailureHandlerTest extends TestCase |
| 23 | +{ |
| 24 | + private DefaultAuthenticationFailureHandler $handler; |
| 25 | + |
| 26 | + private MockObject&HttpUtils $httpUtils; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->httpUtils = $this->createMock(HttpUtils::class); |
| 31 | + $this->handler = new DefaultAuthenticationFailureHandler( |
| 32 | + $this->createMock(HttpKernelInterface::class), |
| 33 | + $this->httpUtils |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public function testHandlePasswordInUnsupportedFormatException(): void |
| 38 | + { |
| 39 | + $request = $this->getRequest(); |
| 40 | + $exception = new PasswordInUnsupportedFormatException(); |
| 41 | + $expectedUrl = '/forgot-password'; |
| 42 | + |
| 43 | + $this->httpUtils |
| 44 | + ->expects(self::once()) |
| 45 | + ->method('generateUri') |
| 46 | + ->with($request, 'ibexa.user.forgot_password.migration') |
| 47 | + ->willReturn($expectedUrl); |
| 48 | + |
| 49 | + $this->handler->onAuthenticationFailure($request, $exception); |
| 50 | + } |
| 51 | + |
| 52 | + public function testOnAuthenticationFailureAltersBadCredentialsExceptionMessage(): void |
| 53 | + { |
| 54 | + $session = $this->getSession(); |
| 55 | + $session |
| 56 | + ->expects(self::once()) |
| 57 | + ->method('set') |
| 58 | + ->with( |
| 59 | + '_security.last_error', |
| 60 | + self::callback(static function (AuthenticationException $exception): bool { |
| 61 | + self::assertInstanceOf(BadCredentialsException::class, $exception); |
| 62 | + self::assertSame('Bad credentials.', $exception->getMessage()); |
| 63 | + |
| 64 | + return true; |
| 65 | + }) |
| 66 | + ); |
| 67 | + |
| 68 | + $request = $this->getRequest($session); |
| 69 | + $originalException = new BadCredentialsException('Original message'); |
| 70 | + |
| 71 | + $this->httpUtils |
| 72 | + ->expects(self::once()) |
| 73 | + ->method('createRedirectResponse') |
| 74 | + ->with($request); |
| 75 | + |
| 76 | + $this->handler->onAuthenticationFailure($request, $originalException); |
| 77 | + } |
| 78 | + |
| 79 | + private function getRequest(?Session $session = null): Request |
| 80 | + { |
| 81 | + $request = new Request(); |
| 82 | + $request->setSession($session ?? $this->getSession()); |
| 83 | + |
| 84 | + return $request; |
| 85 | + } |
| 86 | + |
| 87 | + public function getSession(): MockObject&Session |
| 88 | + { |
| 89 | + $session = $this->createMock(Session::class); |
| 90 | + $session->expects(self::any())->method('isStarted')->willReturn(true); |
| 91 | + |
| 92 | + return $session; |
| 93 | + } |
| 94 | +} |
0 commit comments