Skip to content

fix: shop is not reachable when getting 401 unauthorized response #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/EventListener/BeforeRegistrationStartsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
}
Expand Down
52 changes: 51 additions & 1 deletion tests/EventListener/BeforeRegistrationStartsListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
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;
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
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)]
Expand Down Expand Up @@ -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,
Expand All @@ -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,
]))
];
}
}
Loading