Skip to content

Commit

Permalink
NEXT-39569 - Sync license host
Browse files Browse the repository at this point in the history
fix

fix
  • Loading branch information
nam4am committed Nov 15, 2024
1 parent 8c26b3e commit 2bb76a4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
56 changes: 25 additions & 31 deletions src/Controller/LicenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,41 @@ public function sync(ShopInterface $shop, Request $request): Response
{
$payload = $request->getPayload()->all();

$licenseKey = $payload['licenseKey'] ?? null;

if (!$licenseKey) {
$data = [
'errors' => [
'type' => 'missing_license_key',
'detail' => 'No license key provided',
],
];

return new JsonResponse($data, Response::HTTP_BAD_REQUEST);
if (!isset($payload['licenseKey']) && !isset($payload['licenseHost'])) {
return $this->createErrorResponse('missing_license_credentials', 'No licenseKey and licenseHost provided', Response::HTTP_BAD_REQUEST);
}

try {
$this->commercialLicense->validate($licenseKey);
} catch (LicenseException $e) {
$data = [
'errors' => [
'type' => 'license_validation_failed',
'detail' => $e->getMessage(),
],
];
$licenseKey = $payload['licenseKey'] ?? '';
$licenseHost = $payload['licenseHost'] ?? '';

return new JsonResponse($data, Response::HTTP_FORBIDDEN);
if ($licenseKey !== '') {
try {
$this->commercialLicense->validate($licenseKey);
$shop->commercialLicenseKey = $licenseKey;
} catch (LicenseException $e) {
return $this->createErrorResponse('license_validation_failed', $e->getMessage(), Response::HTTP_FORBIDDEN);
}
}

$shop->commercialLicenseKey = $licenseKey;
$shop->commercialLicenseHost = $licenseHost;

try {
$this->shopRepository->updateShop($shop);
} catch (\Throwable) {
$data = [
'errors' => [
'type' => 'shop_update_license_failed',
'detail' => 'Failed to sync commercial license to shop',
],
];

return new JsonResponse($data, Response::HTTP_INTERNAL_SERVER_ERROR);
} catch (\Throwable $e) {
return $this->createErrorResponse('shop_update_license_failed', $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}

return new Response(null, Response::HTTP_NO_CONTENT);
}

private function createErrorResponse(string $type, string $detail, int $statusCode): JsonResponse
{
$data = [
'errors' => [
'type' => $type,
'detail' => $detail,
],
];
return new JsonResponse($data, $statusCode);
}
}
3 changes: 3 additions & 0 deletions src/Entity/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ class Shop extends AbstractShop

#[Column(type: 'string', nullable: true)]
public ?string $commercialLicenseKey = null;

#[Column(type: 'string', nullable: true)]
public ?string $commercialLicenseHost = null;
}
30 changes: 26 additions & 4 deletions tests/Controller/LicenseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ protected function setUp(): void
$this->commercialLicense = $this->createMock(CommercialLicense::class);
}

public function testSyncWithValidLicenseHost(): void
{
$shop = new Shop('my-shop-id', 'https://shop.com', 'secret');

/** @var ShopRepositoryInterface<Shop> $shopRepository */
$shopRepository = new MockShopRepository();
$shopRepository->createShop($shop);

$request = $this->createMock(Request::class);
$request->method('getPayload')->willReturn(new InputBag(['licenseHost' => 'valid_host']));

$this->commercialLicense->expects($this->never())->method('validate');

$licenseController = new LicenseController($shopRepository, $this->commercialLicense);

$response = $licenseController->sync($shop, $request);

$this->assertInstanceOf(Response::class, $response);
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
$this->assertEmpty($response->getContent());
}

public function testSyncWithValidLicenseKey(): void
{
$shop = new Shop('my-shop-id', 'https://shop.com', 'secret');
Expand All @@ -50,7 +72,7 @@ public function testSyncWithValidLicenseKey(): void
$this->assertEmpty($response->getContent());
}

public function testSyncWithMissingLicenseKey(): void
public function testSyncWithMissingLicenseKeyAndHost(): void
{
$shop = new Shop('my-shop-id', 'https://shop.com', 'secret');

Expand All @@ -74,8 +96,8 @@ public function testSyncWithMissingLicenseKey(): void
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
$this->assertEquals([
'errors' => [
'type' => 'missing_license_key',
'detail' => 'No license key provided',
'type' => 'missing_license_credentials',
'detail' => 'No licenseKey and licenseHost provided',
],
], $decodedContent);
}
Expand Down Expand Up @@ -134,7 +156,7 @@ public function testSyncWithShopUpdateFailure(): void
$this->assertEquals([
'errors' => [
'type' => 'shop_update_license_failed',
'detail' => 'Failed to sync commercial license to shop',
'detail' => 'Shop with id "my-shop-id-1" not found',
],
], $decodedContent);
}
Expand Down

0 comments on commit 2bb76a4

Please sign in to comment.