Skip to content

Commit

Permalink
Update module endpoints tests to check the new feature from the list …
Browse files Browse the repository at this point in the history
…and the workflow to upload install and uninstall a module
  • Loading branch information
jolelievre committed Nov 5, 2024
1 parent d294398 commit 0a898bd
Showing 1 changed file with 101 additions and 90 deletions.
191 changes: 101 additions & 90 deletions tests/Integration/ApiPlatform/ModuleEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use Module;
use Tests\Resources\DatabaseDump;
use Tests\Resources\ResourceResetter;

class ModuleEndpointTest extends ApiTestCase
{
Expand All @@ -38,6 +39,7 @@ public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
DatabaseDump::restoreMatchingTables('/module/');
(new ResourceResetter())->resetTestModules();
}

public function getProtectedEndpoints(): iterable
Expand Down Expand Up @@ -68,7 +70,7 @@ public function getProtectedEndpoints(): iterable
];

yield 'upload module' => [
'POST',
'PUT',
'/module/{technicalName}/upload',
];

Expand Down Expand Up @@ -121,10 +123,11 @@ public function testListModules(): array
$apiModule = $modules['items'][0];
$this->assertEquals('ps_apiresources', $apiModule['technicalName']);
$this->assertTrue($apiModule['enabled']);
$this->assertTrue(version_compare($apiModule['version'], '0.1.0', '>='));
$this->assertTrue(version_compare($apiModule['moduleVersion'], '0.1.0', '>='));
$this->assertTrue(version_compare($apiModule['installedVersion'], '0.1.0', '>='));
$this->assertGreaterThan(0, $apiModule['moduleId']);

return ['moduleId' => $apiModule['moduleId'], 'technicalName' => $apiModule['technicalName'], 'version' => $apiModule['version']];
return $apiModule;
}

/**
Expand All @@ -137,7 +140,8 @@ public function testGetModuleInfos(array $module): array
[
'moduleId' => $module['moduleId'],
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'moduleVersion' => $module['moduleVersion'],
'installedVersion' => $module['installedVersion'],
'enabled' => true,
'installed' => true,
],
Expand Down Expand Up @@ -225,7 +229,8 @@ public function testUpdateModuleStatusDisable(array $module): array
$expectedModuleInfos = [
'moduleId' => $module['moduleId'],
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'moduleVersion' => $module['moduleVersion'],
'installedVersion' => $module['installedVersion'],
'enabled' => false,
'installed' => true,
];
Expand Down Expand Up @@ -297,7 +302,8 @@ public function testResetModule(array $module): void
$expectedModuleInfos = [
'moduleId' => $module['moduleId'],
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'moduleVersion' => $module['moduleVersion'],
'installedVersion' => $module['installedVersion'],
'enabled' => true,
'installed' => true,
];
Expand All @@ -307,7 +313,7 @@ public function testResetModule(array $module): void
/**
* @depends testUpdateModuleStatusDisable
*/
public function restResetModuleNotActive(array $module): void
public function testResetModuleNotActive(array $module): void
{
$bearerToken = $this->getBearerToken(['module_read', 'module_write']);
static::createClient()->request('PATCH', sprintf('/module/%s/reset', $module['technicalName']), [
Expand All @@ -316,139 +322,135 @@ public function restResetModuleNotActive(array $module): void
self::assertResponseStatusCodeSame(400);
}

public function testUninstallModule()
/**
* @depends testResetModuleNotActive
*/
public function testUploadModuleFromUrl(): void
{
$module = [
'technicalName' => 'bankwire',
'deleteFile' => false,
$this->assertModuleNotFound('dashactivity');
$expectedModule = [
'moduleId' => null,
'technicalName' => 'dashactivity',
'moduleVersion' => '2.1.0',
// Module is simply uploaded not installed
'installedVersion' => null,
'enabled' => false,
'installed' => false,
];

// uninstall specific module deleteFile true
$bearerToken = $this->getBearerToken(['module_write']);
static::createClient()->request('PUT', sprintf('/module/%s/uninstall', $module['technicalName']), [
$response = static::createClient()->request('PUT', sprintf('/module/%s/upload', $expectedModule['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'deleteFile' => $module['deleteFile'],
'source' => 'https://github.com/PrestaShop/dashactivity/releases/download/v2.1.0/dashactivity.zip',
],
]);

self::assertResponseStatusCodeSame(204);
}

public function testBulkUninstallModule()
{
$modules = ['ps_featuredproducts', 'ps_emailsubscription'];

// uninstall specific module deleteFile true
$bearerToken = $this->getBearerToken(['module_write']);
static::createClient()->request('PUT', sprintf('/modules/uninstall'), [
'auth_bearer' => $bearerToken,
'json' => [
'modules' => $modules,
'deleteFile' => true,
],
]);
self::assertResponseStatusCodeSame(200);
$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);
// Check response from status update request
$this->assertEquals($expectedModule, $decodedResponse);

self::assertResponseStatusCodeSame(204);
// Check result from GET API
$this->assertEquals($expectedModule, $this->getModuleInfos($expectedModule['technicalName']));
}

/**
* @depends testUninstallModule
* @depends testUploadModuleFromUrl
*/
public function testInstallModuleExistInFolder(): void
public function testInstallModule(): void
{
$module = [
'technicalName' => 'bankwire',
'version' => '2.0.0',
$expectedModule = [
'technicalName' => 'dashactivity',
'moduleVersion' => '2.1.0',
'installedVersion' => '2.1.0',
'enabled' => true,
'installed' => true,
];

$bearerToken = $this->getBearerToken(['module_write']);
$response = static::createClient()->request('POST', sprintf('/module/%s/install', $module['technicalName']), [
$response = static::createClient()->request('PUT', sprintf('/module/%s/install', $expectedModule['technicalName']), [
'auth_bearer' => $bearerToken,
// We must define a JSON body even if it is empty, we need to search how to make this optional
'json' => [
],
]);

self::assertResponseStatusCodeSame(200);
$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);
// The ID is dynamic so we fetch it after creation
$this->assertArrayHasKey('moduleId', $decodedResponse);
$expectedModule['moduleId'] = $decodedResponse['moduleId'];

// Check response from status update request
$expectedModuleInfos = [
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'enabled' => true,
'installed' => true,
];
// Check response from install request
$this->assertEquals($expectedModule, $decodedResponse);

// Check response from status update request
$this->assertEquals($expectedModuleInfos, $decodedResponse);
// Check result from GET API
$this->assertEquals($expectedModule, $this->getModuleInfos($expectedModule['technicalName']));
}

public function testInstallModuleFromZip(): void
/**
* @depends testInstallModule
*/
public function testUninstallModule()
{
$module = [
'technicalName' => 'test_install_cqrs_command',
'source' => _PS_MODULE_DIR_ . 'test_install_cqrs_command.zip',
'version' => '1.0.0',
$expectedModule = [
'moduleId' => null,
'technicalName' => 'dashactivity',
'moduleVersion' => '2.1.0',
'installedVersion' => null,
'enabled' => false,
'installed' => false,
];

// Uninstall specific module deleteFile true
$bearerToken = $this->getBearerToken(['module_write']);
$response = static::createClient()->request('POST', sprintf('/module/%s/install', $module['technicalName']), [
static::createClient()->request('PUT', sprintf('/module/%s/uninstall', $expectedModule['technicalName']), [
'auth_bearer' => $bearerToken,
'json' => [
'source' => $module['source'],
// We keep files, so we can check the module status afterward (deleted module would return a 404)
'deleteFile' => false,
],
]);
self::assertResponseStatusCodeSame(204);

self::assertResponseStatusCodeSame(201);
$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);

// Check response from status update request
$expectedModuleInfos = [
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'enabled' => true,
'installed' => true,
];

// Check response from status update request
$this->assertEquals($expectedModuleInfos, $decodedResponse);
// Check result from GET API
$this->assertEquals($expectedModule, $this->getModuleInfos($expectedModule['technicalName']));
}

/**
* @depends testBulkUpdateStatus
* @depends testUninstallModule
*/
public function testInstallModuleFromUrl(): void
public function testBulkUninstallModule()
{
$module = [
'technicalName' => 'ps_featuredproducts',
'source' => 'https://github.com/PrestaShop/ps_featuredproducts/releases/download/v2.1.5/ps_featuredproducts.zip',
'version' => '2.1.5',
];
$modules = ['bankwire', 'ps_emailsubscription'];
foreach ($modules as $module) {
$moduleInfos = $this->getModuleInfos($module);
$this->assertGreaterThan(0, $moduleInfos['moduleId']);
$this->assertTrue($moduleInfos['enabled']);
$this->assertTrue($moduleInfos['installed']);
$this->assertTrue(version_compare($moduleInfos['moduleVersion'], '0.1.0', '>='));
$this->assertTrue(version_compare($moduleInfos['installedVersion'], '0.1.0', '>='));
}

// uninstall specific module deleteFile true
$bearerToken = $this->getBearerToken(['module_write']);
$response = static::createClient()->request('POST', sprintf('/module/%s/install', $module['technicalName']), [
static::createClient()->request('PUT', sprintf('/modules/uninstall'), [
'auth_bearer' => $bearerToken,
'json' => [
'source' => $module['source'],
'modules' => $modules,
// Force removal of the files
'deleteFile' => true,
],
]);
self::assertResponseStatusCodeSame(204);

self::assertResponseStatusCodeSame(200);
$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);

// Check response from status update request
$expectedModuleInfos = [
'technicalName' => $module['technicalName'],
'version' => $module['version'],
'enabled' => true,
'installed' => true,
];

// Check response from status update request
$this->assertEquals($expectedModuleInfos, $decodedResponse);
// Module files have been removed, so they don't exist at all anymore, thus requesting their info results in a 404
foreach ($modules as $module) {
$this->assertModuleNotFound($module);
}
}

private function getModuleInfos(string $technicalName): array
Expand All @@ -464,4 +466,13 @@ private function getModuleInfos(string $technicalName): array

return $decodedResponse;
}

private function assertModuleNotFound(string $technicalName): void
{
$bearerToken = $this->getBearerToken(['module_read']);
static::createClient()->request('GET', '/module/' . $technicalName, [
'auth_bearer' => $bearerToken,
]);
self::assertResponseStatusCodeSame(404);
}
}

0 comments on commit 0a898bd

Please sign in to comment.