Skip to content

Commit c57dcc7

Browse files
Progi1984Progi1984
authored andcommitted
Added endpoints for domain "WebserviceKey" (Delete & BulkDelete)
1 parent d55f78c commit c57dcc7

File tree

3 files changed

+151
-16
lines changed

3 files changed

+151
-16
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to [email protected] so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <[email protected]>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\WebserviceKey;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\BulkDeleteWebserviceKeyCommand;
26+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceNotFoundException;
27+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
28+
use Symfony\Component\HttpFoundation\Response;
29+
use Symfony\Component\Validator\Constraints as Assert;
30+
31+
#[ApiResource(
32+
operations: [
33+
new CQRSUpdate(
34+
uriTemplate: '/webservice-keys/bulk-delete',
35+
// No output 204 code
36+
output: false,
37+
CQRSCommand: BulkDeleteWebserviceKeyCommand::class,
38+
scopes: [
39+
'webservice_key_write',
40+
],
41+
),
42+
],
43+
exceptionToStatus: [
44+
WebserviceNotFoundException::class => Response::HTTP_NOT_FOUND,
45+
],
46+
)]
47+
class BulkWebserviceKeysDelete
48+
{
49+
/**
50+
* @var int[]
51+
*/
52+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
53+
#[Assert\NotBlank]
54+
public array $webserviceKeyIds;
55+
}

src/ApiPlatform/Resources/WebserviceKey/WebserviceKey.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
use ApiPlatform\Metadata\ApiProperty;
2626
use ApiPlatform\Metadata\ApiResource;
2727
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\AddWebserviceKeyCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\DeleteWebserviceKeyCommand;
2829
use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\EditWebserviceKeyCommand;
2930
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceConstraintException;
3031
use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceKeyNotFoundException;
3132
use PrestaShop\PrestaShop\Core\Domain\Webservice\Query\GetWebserviceKeyForEditing;
3233
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate;
34+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
3335
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
3436
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
3537
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
@@ -48,6 +50,13 @@
4850
'[enabled]' => '[status]',
4951
],
5052
),
53+
new CQRSDelete(
54+
uriTemplate: '/webservice-keys/{webserviceKeyId}',
55+
requirements: ['webserviceKeyId' => '\d+'],
56+
output: false,
57+
CQRSCommand: DeleteWebserviceKeyCommand::class,
58+
scopes: ['webservice_key_write']
59+
),
5160
new CQRSGet(
5261
uriTemplate: '/webservice-keys/{webserviceKeyId}',
5362
requirements: ['webserviceKeyId' => '\d+'],

tests/Integration/ApiPlatform/WebserviceKeyEndpointTest.php

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,20 @@ public static function getProtectedEndpoints(): iterable
5959
'/webservice-keys/1',
6060
];
6161

62+
yield 'delete endpoint' => [
63+
'DELETE',
64+
'/webservice-keys/1',
65+
];
66+
6267
yield 'list endpoint' => [
6368
'GET',
6469
'/webservice-keys',
6570
];
71+
72+
yield 'bulk delete endpoint' => [
73+
'PUT',
74+
'/webservice-keys/bulk-delete',
75+
];
6676
}
6777

6878
public function testAddWebserviceKey(): int
@@ -100,10 +110,6 @@ public function testAddWebserviceKey(): int
100110

101111
/**
102112
* @depends testAddWebserviceKey
103-
*
104-
* @param int $webserviceKeyId
105-
*
106-
* @return int
107113
*/
108114
public function testGetWebserviceKey(int $webserviceKeyId): int
109115
{
@@ -144,10 +150,6 @@ public function testGetWebserviceKey(int $webserviceKeyId): int
144150

145151
/**
146152
* @depends testGetWebserviceKey
147-
*
148-
* @param int $webserviceKeyId
149-
*
150-
* @return int
151153
*/
152154
public function testUpdateWebserviceKey(int $webserviceKeyId): int
153155
{
@@ -302,10 +304,6 @@ public function testUpdateWebserviceKey(int $webserviceKeyId): int
302304

303305
/**
304306
* @depends testUpdateWebserviceKey
305-
*
306-
* @param int $webserviceKeyId
307-
*
308-
* @return int
309307
*/
310308
public function testGetUpdatedWebserviceKey(int $webserviceKeyId): int
311309
{
@@ -346,10 +344,6 @@ public function testGetUpdatedWebserviceKey(int $webserviceKeyId): int
346344

347345
/**
348346
* @depends testGetUpdatedWebserviceKey
349-
*
350-
* @param int $webserviceKeyId
351-
*
352-
* @return int
353347
*/
354348
public function testListWebserviceKeys(int $webserviceKeyId): int
355349
{
@@ -378,6 +372,83 @@ public function testListWebserviceKeys(int $webserviceKeyId): int
378372
return $webserviceKeyId;
379373
}
380374

375+
/**
376+
* @depends testListWebserviceKeys
377+
*/
378+
public function testDelete(int $webserviceKeyId): void
379+
{
380+
$return = $this->deleteItem('/webservice-keys/' . $webserviceKeyId, ['webservice_key_write']);
381+
// This endpoint return empty response and 204 HTTP code
382+
$this->assertNull($return);
383+
384+
// Getting the item should result in a 404 now
385+
$this->getItem('/webservice-keys/' . $webserviceKeyId, ['webservice_key_read'], Response::HTTP_NOT_FOUND);
386+
}
387+
388+
/**
389+
* @depends testListWebserviceKeys
390+
*/
391+
public function testBulkDelete(): void
392+
{
393+
$itemsCount = $this->countItems('/webservice-keys', ['webservice_key_read']);
394+
$this->assertEquals(0, $itemsCount);
395+
396+
$webserviceKey1 = $this->createItem('/webservice-keys', [
397+
'key' => 'AZERTYUIOPAZERTYUIOPAZERTYUIOPAZ',
398+
'description' => 'Webservice Key test',
399+
'enabled' => false,
400+
'permissions' => [
401+
'DELETE' => ['addresses'],
402+
'GET' => ['addresses'],
403+
'HEAD' => ['addresses', 'carriers'],
404+
'PATCH' => ['addresses', 'carriers'],
405+
'PUT' => ['addresses', 'carriers', 'carts'],
406+
'POST' => ['addresses', 'carriers', 'carts'],
407+
],
408+
'shopIds' => [1],
409+
], ['webservice_key_write']);
410+
$this->assertArrayHasKey('webserviceKeyId', $webserviceKey1);
411+
$webserviceKeyId1 = $webserviceKey1['webserviceKeyId'];
412+
413+
$webserviceKey2 = $this->createItem('/webservice-keys', [
414+
'key' => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
415+
'description' => 'Webservice Key test',
416+
'enabled' => false,
417+
'permissions' => [
418+
'DELETE' => ['addresses'],
419+
'GET' => ['addresses'],
420+
'HEAD' => ['addresses', 'carriers'],
421+
'PATCH' => ['addresses', 'carriers'],
422+
'PUT' => ['addresses', 'carriers', 'carts'],
423+
'POST' => ['addresses', 'carriers', 'carts'],
424+
],
425+
'shopIds' => [1],
426+
], ['webservice_key_write']);
427+
$this->assertArrayHasKey('webserviceKeyId', $webserviceKey2);
428+
$webserviceKeyId2 = $webserviceKey2['webserviceKeyId'];
429+
430+
$itemsCount = $this->countItems('/webservice-keys', ['webservice_key_read']);
431+
$this->assertEquals(2, $itemsCount);
432+
433+
// We remove the two webservice keys
434+
$bulkWebserviceKeyIds = [
435+
$webserviceKeyId1,
436+
$webserviceKeyId2,
437+
];
438+
439+
$this->updateItem('/webservice-keys/bulk-delete', [
440+
'webserviceKeyIds' => $bulkWebserviceKeyIds,
441+
], ['webservice_key_write'], Response::HTTP_NO_CONTENT);
442+
443+
// Assert the provided webservice keys have been removed
444+
foreach ($bulkWebserviceKeyIds as $webserviceKeyId) {
445+
$this->getItem('/webservice-keys/' . $webserviceKeyId, ['webservice_key_read'], Response::HTTP_NOT_FOUND);
446+
}
447+
448+
$itemsCount = $this->countItems('/webservice-keys', ['webservice_key_read']);
449+
$this->assertEquals(0, $itemsCount);
450+
}
451+
381452
public function testCreateInvalidWebserviceKey(): void
382453
{
383454
// Creating with invalid data should return a response with invalid constraint messages and use an http code 422

0 commit comments

Comments
 (0)