Skip to content

Commit

Permalink
Restored PUT on hook-status
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosomb committed Dec 19, 2024
1 parent bf29f91 commit c6142e6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ApiPlatform/Resources/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Hook\Command\UpdateHookStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Hook\Exception\HookNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHook;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
use PrestaShopBundle\ApiPlatform\Provider\QueryListProvider;

#[ApiResource(
operations: [
new CQRSUpdate(
uriTemplate: '/hook-status',
CQRSCommand: UpdateHookStatusCommand::class,
scopes: ['hook_write']
),
new CQRSGet(
uriTemplate: '/hook/{id}',
requirements: ['id' => '\d+'],
Expand Down
93 changes: 93 additions & 0 deletions tests/Integration/ApiPlatform/GetHookStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

declare(strict_types=1);

namespace PsApiResourcesTest\Integration\ApiPlatform;

use Tests\Resources\DatabaseDump;

class GetHookStatusTest extends ApiTestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
DatabaseDump::restoreTables(['hook']);
self::createApiClient(['hook_write', 'hook_read']);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
DatabaseDump::restoreTables(['hook']);
}

public function getProtectedEndpoints(): iterable
{
yield 'put endpoint' => [
'PUT',
'/hook-status',
];
}

public function testDisableHook(): void
{
$hook = new \Hook();
$hook->name = 'disableHook';
$hook->active = true;
$hook->add();

$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);
static::createClient()->request('PUT', '/hook-status', [
'auth_bearer' => $bearerToken,
'json' => ['id' => (int) $hook->id, 'active' => false],
]);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/hook/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, false);
self::assertResponseStatusCodeSame(200);
}

public function testEnableHook(): void
{
$hook = new \Hook();
$hook->name = 'enableHook';
$hook->active = false;
$hook->add();

$bearerToken = $this->getBearerToken([
'hook_read',
'hook_write',
]);
static::createClient()->request('PUT', '/hook-status', [
'auth_bearer' => $bearerToken,
'json' => ['id' => (int) $hook->id, 'active' => true],
]);
self::assertResponseStatusCodeSame(200);

$response = static::createClient()->request('GET', '/hook/' . (int) $hook->id, ['auth_bearer' => $bearerToken]);
self::assertEquals(json_decode($response->getContent())->active, true);
self::assertResponseStatusCodeSame(200);
}
}

0 comments on commit c6142e6

Please sign in to comment.