Skip to content
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

NEXT-39569 - Sync license host #24

Merged
merged 1 commit into from
Nov 18, 2024
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
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
Loading