Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\WebserviceKey;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\BulkDeleteWebserviceKeyCommand;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceNotFoundException;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints as Assert;

#[ApiResource(
operations: [
new CQRSUpdate(
uriTemplate: '/webservice-keys/bulk-delete',
// No output 204 code
output: false,
CQRSCommand: BulkDeleteWebserviceKeyCommand::class,
scopes: [
'webservice_key_write',
],
),
],
exceptionToStatus: [
WebserviceNotFoundException::class => Response::HTTP_NOT_FOUND,
],
)]
class BulkWebserviceKeysDelete
{
/**
* @var int[]
*/
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
#[Assert\NotBlank]
public array $webserviceKeyIds;
}
9 changes: 9 additions & 0 deletions src/ApiPlatform/Resources/WebserviceKey/WebserviceKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\AddWebserviceKeyCommand;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\DeleteWebserviceKeyCommand;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\EditWebserviceKeyCommand;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceKeyNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Webservice\Query\GetWebserviceKeyForEditing;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
Expand All @@ -48,6 +50,13 @@
'[enabled]' => '[status]',
],
),
new CQRSDelete(
uriTemplate: '/webservice-keys/{webserviceKeyId}',
requirements: ['webserviceKeyId' => '\d+'],
output: false,
CQRSCommand: DeleteWebserviceKeyCommand::class,
scopes: ['webservice_key_write']
),
new CQRSGet(
uriTemplate: '/webservice-keys/{webserviceKeyId}',
requirements: ['webserviceKeyId' => '\d+'],
Expand Down
103 changes: 87 additions & 16 deletions tests/Integration/ApiPlatform/WebserviceKeyEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,20 @@ public static function getProtectedEndpoints(): iterable
'/webservice-keys/1',
];

yield 'delete endpoint' => [
'DELETE',
'/webservice-keys/1',
];

yield 'list endpoint' => [
'GET',
'/webservice-keys',
];

yield 'bulk delete endpoint' => [
'PUT',
'/webservice-keys/bulk-delete',
];
}

public function testAddWebserviceKey(): int
Expand Down Expand Up @@ -100,10 +110,6 @@ public function testAddWebserviceKey(): int

/**
* @depends testAddWebserviceKey
*
* @param int $webserviceKeyId
*
* @return int
*/
public function testGetWebserviceKey(int $webserviceKeyId): int
{
Expand Down Expand Up @@ -144,10 +150,6 @@ public function testGetWebserviceKey(int $webserviceKeyId): int

/**
* @depends testGetWebserviceKey
*
* @param int $webserviceKeyId
*
* @return int
*/
public function testUpdateWebserviceKey(int $webserviceKeyId): int
{
Expand Down Expand Up @@ -302,10 +304,6 @@ public function testUpdateWebserviceKey(int $webserviceKeyId): int

/**
* @depends testUpdateWebserviceKey
*
* @param int $webserviceKeyId
*
* @return int
*/
public function testGetUpdatedWebserviceKey(int $webserviceKeyId): int
{
Expand Down Expand Up @@ -346,10 +344,6 @@ public function testGetUpdatedWebserviceKey(int $webserviceKeyId): int

/**
* @depends testGetUpdatedWebserviceKey
*
* @param int $webserviceKeyId
*
* @return int
*/
public function testListWebserviceKeys(int $webserviceKeyId): int
{
Expand Down Expand Up @@ -378,6 +372,83 @@ public function testListWebserviceKeys(int $webserviceKeyId): int
return $webserviceKeyId;
}

/**
* @depends testListWebserviceKeys
*/
public function testDelete(int $webserviceKeyId): void
{
$return = $this->deleteItem('/webservice-keys/' . $webserviceKeyId, ['webservice_key_write']);
// This endpoint return empty response and 204 HTTP code
$this->assertNull($return);

// Getting the item should result in a 404 now
$this->getItem('/webservice-keys/' . $webserviceKeyId, ['webservice_key_read'], Response::HTTP_NOT_FOUND);
}

/**
* @depends testListWebserviceKeys
*/
public function testBulkDelete(): void
{
$itemsCount = $this->countItems('/webservice-keys', ['webservice_key_read']);
$this->assertEquals(0, $itemsCount);

$webserviceKey1 = $this->createItem('/webservice-keys', [
'key' => 'AZERTYUIOPAZERTYUIOPAZERTYUIOPAZ',
'description' => 'Webservice Key test',
'enabled' => false,
'permissions' => [
'DELETE' => ['addresses'],
'GET' => ['addresses'],
'HEAD' => ['addresses', 'carriers'],
'PATCH' => ['addresses', 'carriers'],
'PUT' => ['addresses', 'carriers', 'carts'],
'POST' => ['addresses', 'carriers', 'carts'],
],
'shopIds' => [1],
], ['webservice_key_write']);
$this->assertArrayHasKey('webserviceKeyId', $webserviceKey1);
$webserviceKeyId1 = $webserviceKey1['webserviceKeyId'];

$webserviceKey2 = $this->createItem('/webservice-keys', [
'key' => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
'description' => 'Webservice Key test',
'enabled' => false,
'permissions' => [
'DELETE' => ['addresses'],
'GET' => ['addresses'],
'HEAD' => ['addresses', 'carriers'],
'PATCH' => ['addresses', 'carriers'],
'PUT' => ['addresses', 'carriers', 'carts'],
'POST' => ['addresses', 'carriers', 'carts'],
],
'shopIds' => [1],
], ['webservice_key_write']);
$this->assertArrayHasKey('webserviceKeyId', $webserviceKey2);
$webserviceKeyId2 = $webserviceKey2['webserviceKeyId'];

$itemsCount = $this->countItems('/webservice-keys', ['webservice_key_read']);
$this->assertEquals(2, $itemsCount);

// We remove the two webservice keys
$bulkWebserviceKeyIds = [
$webserviceKeyId1,
$webserviceKeyId2,
];

$this->updateItem('/webservice-keys/bulk-delete', [
'webserviceKeyIds' => $bulkWebserviceKeyIds,
], ['webservice_key_write'], Response::HTTP_NO_CONTENT);

// Assert the provided webservice keys have been removed
foreach ($bulkWebserviceKeyIds as $webserviceKeyId) {
$this->getItem('/webservice-keys/' . $webserviceKeyId, ['webservice_key_read'], Response::HTTP_NOT_FOUND);
}

$itemsCount = $this->countItems('/webservice-keys', ['webservice_key_read']);
$this->assertEquals(0, $itemsCount);
}

public function testCreateInvalidWebserviceKey(): void
{
// Creating with invalid data should return a response with invalid constraint messages and use an http code 422
Expand Down
Loading