From 0dca4b7427432de4bff19da6135d1b7d5c5c9742 Mon Sep 17 00:00:00 2001 From: Jonathan LELIEVRE Date: Tue, 5 Nov 2024 17:19:17 +0100 Subject: [PATCH] Update module endpoints tests to check the new feature from the list and the workflow to upload install and uninstall a module --- .../ApiPlatform/ModuleEndpointTest.php | 191 +++++++++--------- 1 file changed, 101 insertions(+), 90 deletions(-) diff --git a/tests/Integration/ApiPlatform/ModuleEndpointTest.php b/tests/Integration/ApiPlatform/ModuleEndpointTest.php index 0b56cf0..705c93e 100644 --- a/tests/Integration/ApiPlatform/ModuleEndpointTest.php +++ b/tests/Integration/ApiPlatform/ModuleEndpointTest.php @@ -24,6 +24,7 @@ use Module; use Tests\Resources\DatabaseDump; +use Tests\Resources\ResourceResetter; class ModuleEndpointTest extends ApiTestCase { @@ -38,6 +39,7 @@ public static function tearDownAfterClass(): void { parent::tearDownAfterClass(); DatabaseDump::restoreMatchingTables('/module/'); + (new ResourceResetter())->resetTestModules(); } public function getProtectedEndpoints(): iterable @@ -68,7 +70,7 @@ public function getProtectedEndpoints(): iterable ]; yield 'upload module' => [ - 'POST', + 'PUT', '/module/{technicalName}/upload', ]; @@ -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; } /** @@ -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, ], @@ -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, ]; @@ -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, ]; @@ -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']), [ @@ -316,55 +322,57 @@ 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' => [ ], ]); @@ -372,83 +380,77 @@ public function testInstallModuleExistInFolder(): void 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 @@ -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); + } }