Skip to content

Commit

Permalink
Handle 404 and adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Oct 21, 2024
1 parent 2ce0d92 commit a749159
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/ApiPlatform/Resources/Module/BulkModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\BulkToggleModuleStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Exception\ModuleNotFoundException;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;

#[ApiResource(
Expand All @@ -40,6 +41,7 @@
],
),
],
exceptionToStatus: [ModuleNotFoundException::class => 404],
)]
class BulkModules
{
Expand Down
12 changes: 8 additions & 4 deletions src/ApiPlatform/Resources/Module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\ResetModuleCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\UpdateModuleStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Module\Exception\ModuleNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Module\Query\GetModuleInfos;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;

Expand All @@ -40,20 +42,20 @@
],
),
new CQRSUpdate(
uriTemplate: '/module/status/{technicalName}',
uriTemplate: '/module/{technicalName}/status',
CQRSCommand: UpdateModuleStatusCommand::class,
CQRSQuery: GetModuleInfos::class,
scopes: [
'module_write',
],
),
new CQRSUpdate(
new CQRSPartialUpdate(
uriTemplate: '/module/{technicalName}/reset',
CQRSCommand: ResetModuleCommand::class,
CQRSQuery: GetModuleInfos::class,
scopes: [
'module_write',
]
],
),
new PaginatedList(
uriTemplate: '/modules',
Expand All @@ -63,10 +65,12 @@
gridDataFactory: 'prestashop.core.grid.data_factory.module',
),
],
normalizationContext: ['skip_null_values' => false],
exceptionToStatus: [ModuleNotFoundException::class => 404],
)]
class Module
{
public int $moduleId;
public ?int $moduleId;

public string $technicalName;

Expand Down
72 changes: 44 additions & 28 deletions tests/Integration/ApiPlatform/ModuleEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class ModuleEndpointTest extends ApiTestCase
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
DatabaseDump::restoreTables(['module', 'module_shop']);
DatabaseDump::restoreMatchingTables('/module/');
self::createApiClient(['module_write', 'module_read']);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
DatabaseDump::restoreTables(['module', 'module_shop']);
DatabaseDump::restoreMatchingTables('/module/');
}

public function getProtectedEndpoints(): iterable
Expand All @@ -59,15 +59,43 @@ public function getProtectedEndpoints(): iterable

yield 'toggle module status' => [
'PUT',
'/module/status/{technicalName}',
'/module/{technicalName}/status',
];

yield 'reset module' => [
'PUT',
'PATCH',
'/module/{technicalName}/reset',
];
}

public function testModuleNotFound(): void
{
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
// GET on non existent module returns a 404
static::createClient()->request('GET', '/module/ps_falsemodule', [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(404);

// PUT status on non existent module returns a 404
static::createClient()->request('PUT', '/module/ps_falsemodule/status', [
'auth_bearer' => $bearerToken,
'json' => [
'enabled' => true,
],
]);
self::assertResponseStatusCodeSame(404);

// PATCH reset on non existent module returns a 404
static::createClient()->request('PATCH', '/module/ps_falsemodule/reset', [
'auth_bearer' => $bearerToken,
'json' => [
'keepData' => true,
],
]);
self::assertResponseStatusCodeSame(404);
}

public function testListModules(): array
{
$modules = $this->listItems('/modules', ['module_read']);
Expand Down Expand Up @@ -168,7 +196,7 @@ public function testUpdateModuleStatusDisable(array $module): array

// Disable specific module
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
$response = static::createClient()->request('PUT', sprintf('/module/status/%s', $module['technicalName']), [
$response = static::createClient()->request('PUT', sprintf('/module/%s/status', $module['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'enabled' => false,
Expand Down Expand Up @@ -196,17 +224,9 @@ public function testUpdateModuleStatusDisable(array $module): array
$disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]);
$this->assertEquals(1, $disabledModules['totalItems']);

return $moduleInfos;
}

/**
* @depends testUpdateModuleStatusDisable
*/
public function testUpdateModuleStatusEnable(array $module): void
{
// Enable specific module
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
$response = static::createClient()->request('PUT', sprintf('/module/status/%s', $module['technicalName']), [
$response = static::createClient()->request('PUT', sprintf('/module/%s/status', $module['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'enabled' => true,
Expand All @@ -231,6 +251,8 @@ public function testUpdateModuleStatusEnable(array $module): void
// Check number of disabled modules
$disabledModules = $this->listItems('/modules', ['module_read'], ['enabled' => false]);
$this->assertEquals(0, $disabledModules['totalItems']);

return $module;
}

/**
Expand All @@ -240,7 +262,7 @@ public function testResetModule(array $module): void
{
// Reset specific module
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
$response = static::createClient()->request('PUT', sprintf('/module/%s/reset', $module['technicalName']), [
$response = static::createClient()->request('PATCH', sprintf('/module/%s/reset', $module['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'keepData' => false,
Expand All @@ -250,6 +272,12 @@ public function testResetModule(array $module): void
$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);

// Module ID has been modified because the module was uninstalled the reinstalled
$this->assertNotEquals($module['moduleId'], $decodedResponse['moduleId']);
$moduleInfos = $this->getModuleInfos($module['technicalName']);
$this->assertNotEquals($module['moduleId'], $moduleInfos['moduleId']);
$module['moduleId'] = $decodedResponse['moduleId'];

// Check response from status update request
$expectedModuleInfos = [
'moduleId' => $module['moduleId'],
Expand All @@ -261,25 +289,13 @@ public function testResetModule(array $module): void
$this->assertEquals($expectedModuleInfos, $decodedResponse);
}

public function testResetModuleNotFound(): void
{
$module = [
'technicalName' => 'ps_notthere',
];
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
static::createClient()->request('PUT', sprintf('/module/%s/reset', $module['technicalName']), [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(404);
}

/**
* @depends testUpdateModuleStatusDisable
*/
public function restResetModuleNotActive(array $module): void
{
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
static::createClient()->request('PUT', sprintf('/module/%s/reset', $module['technicalName']), [
static::createClient()->request('PATCH', sprintf('/module/%s/reset', $module['technicalName']), [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(400);
Expand Down

0 comments on commit a749159

Please sign in to comment.