Skip to content

Commit

Permalink
Merge pull request #24 from shopware/next-39569/sync-license-host
Browse files Browse the repository at this point in the history
NEXT-39569 - Sync license host
  • Loading branch information
nam4am authored Nov 18, 2024
2 parents b7a4dee + de3d927 commit 30cee2c
Show file tree
Hide file tree
Showing 3 changed files with 76 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;
}
52 changes: 48 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,29 @@ public function testSyncWithValidLicenseKey(): void
$this->assertEmpty($response->getContent());
}

public function testSyncWithMissingLicenseKey(): void
public function testSyncWithValidCredentials(): 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(['licenseKey' => 'valid_key', 'licenseHost' => 'valid_host']));

$this->commercialLicense->expects($this->once())->method('validate')->with('valid_key');

$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 testSyncWithInValidCredentials(): void
{
$shop = new Shop('my-shop-id', 'https://shop.com', 'secret');

Expand All @@ -74,8 +118,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 +178,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 30cee2c

Please sign in to comment.