From 7b06aa6812adf94702f0872de699bcdc41236338 Mon Sep 17 00:00:00 2001 From: Moritz Krafeld Date: Fri, 17 Jan 2025 16:43:15 +0100 Subject: [PATCH] fix: shop is not reachable when getting 401 unauthorized response --- .../BeforeRegistrationStartsListener.php | 5 ++ .../BeforeRegistrationStartsListenerTest.php | 52 ++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/EventListener/BeforeRegistrationStartsListener.php b/src/EventListener/BeforeRegistrationStartsListener.php index a7d1fb6..26d8312 100644 --- a/src/EventListener/BeforeRegistrationStartsListener.php +++ b/src/EventListener/BeforeRegistrationStartsListener.php @@ -7,6 +7,7 @@ use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent; use Shopware\AppBundle\Exception\ShopURLIsNotReachableException; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; #[AsEventListener] @@ -38,6 +39,10 @@ public function __invoke(BeforeRegistrationStartsEvent $event): void 'max_redirects' => 0, ]); } catch (\Throwable $e) { + if (!$e instanceof TransportExceptionInterface) { + return; + } + throw new ShopURLIsNotReachableException($shop->getShopUrl(), $e); } } diff --git a/tests/EventListener/BeforeRegistrationStartsListenerTest.php b/tests/EventListener/BeforeRegistrationStartsListenerTest.php index 534aa7f..d002f61 100644 --- a/tests/EventListener/BeforeRegistrationStartsListenerTest.php +++ b/tests/EventListener/BeforeRegistrationStartsListenerTest.php @@ -5,6 +5,7 @@ namespace EventListener; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; @@ -12,6 +13,11 @@ use Shopware\App\SDK\Shop\ShopInterface; use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener; use Shopware\AppBundle\Exception\ShopURLIsNotReachableException; +use Symfony\Component\HttpClient\Exception\ClientException; +use Symfony\Component\HttpClient\Exception\TransportException; +use Symfony\Component\HttpClient\Response\MockResponse; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; use Symfony\Contracts\HttpClient\HttpClientInterface; #[CoversClass(BeforeRegistrationStartsListener::class)] @@ -95,7 +101,7 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable(): 'timeout' => 10, 'max_redirects' => 0, ]) - ->willThrowException(new \Exception('Shop url is not reachable')); + ->willThrowException(new TransportException('Shop is not reachable')); $listener = new BeforeRegistrationStartsListener( $this->httpClient, @@ -109,4 +115,48 @@ public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable(): ) ); } + + #[DataProvider('unauthorizedHttpExceptionProvider')] + public function testListenerDoesNotThrowExceptionWhenTheExceptionCodeIsHTTPUnauthorized($exception): void + { + $shop = $this->createMock(ShopInterface::class); + $shop + ->expects(self::once()) + ->method('getShopUrl') + ->willReturn('https://shop-url.com'); + + $this->httpClient + ->expects(self::once()) + ->method('request') + ->with('HEAD', 'https://shop-url.com/api/_info/config', [ + 'timeout' => 10, + 'max_redirects' => 0, + ]) + ->willThrowException($exception); + + $listener = new BeforeRegistrationStartsListener( + $this->httpClient, + true + ); + + $listener->__invoke( + new BeforeRegistrationStartsEvent( + $this->createMock(RequestInterface::class), + $shop + ) + ); + } + + public static function unauthorizedHttpExceptionProvider(): \Generator + { + yield 'HttpException' => [ + new UnauthorizedHttpException('Unauthorized') + ]; + + yield 'HttpExceptionInterface' => [ + new ClientException(new MockResponse('', [ + 'http_code' => Response::HTTP_UNAUTHORIZED, + ])) + ]; + } }