From da35e08d12ad9a9893de63b00be4cf1f0db3e205 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Thu, 5 May 2022 20:16:45 -0300 Subject: [PATCH 1/2] Improve test coverate to PdfParser --- lib/Command/Configure/Cfssl.php | 2 +- .../Version2040Date20211027183759.php | 42 +-------- lib/Service/PdfParserService.php | 9 ++ lib/Service/SignFileService.php | 1 - tests/Unit/Service/PdfParseServiceTest.php | 90 +++++++++++++++++++ 5 files changed, 103 insertions(+), 41 deletions(-) create mode 100644 tests/Unit/Service/PdfParseServiceTest.php diff --git a/lib/Command/Configure/Cfssl.php b/lib/Command/Configure/Cfssl.php index 0f9a3f356..e0f2926e0 100644 --- a/lib/Command/Configure/Cfssl.php +++ b/lib/Command/Configure/Cfssl.php @@ -108,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $configPath = $this->getFullPath() . DIRECTORY_SEPARATOR . 'cfssl_config' . DIRECTORY_SEPARATOR; $cfsslUri = 'http://127.0.0.1:8888/api/v1/cfssl/'; } else { - $output->writeln('CFSSL binary not found!'); + $output->writeln('CFSSL binary not found! run libresign:istall --cfssl first.'); if (!$configPath = $input->getOption('config-path')) { throw new InvalidArgumentException('Invalid config path'); } diff --git a/lib/Migration/Version2040Date20211027183759.php b/lib/Migration/Version2040Date20211027183759.php index 18991b688..e30658c62 100644 --- a/lib/Migration/Version2040Date20211027183759.php +++ b/lib/Migration/Version2040Date20211027183759.php @@ -10,14 +10,13 @@ use OCP\DB\ISchemaWrapper; use OCP\Files\File; use OCP\Files\IRootFolder; -use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; -use Symfony\Component\Console\Input\StringInput; -use Symfony\Component\Console\Output\NullOutput; class Version2040Date20211027183759 extends SimpleMigrationStep { + /** @var IDBConnection */ + private $connection; /** @var IRootFolder*/ private $root; /** @var PdfParserService */ @@ -28,10 +27,6 @@ public function __construct(IDBConnection $connection, IRootFolder $root, PdfParserService $PdfParserService) { $this->connection = $connection; - $this->install = $install; - $this->config = $config; - $this->systemConfig = $systemConfig; - $this->rootFolder = $rootfolder; $this->root = $root; $this->PdfParserService = $PdfParserService; } @@ -40,7 +35,7 @@ public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $query = $this->connection->getQueryBuilder(); $query->select('id', 'node_id', 'user_id') ->from('libresign_file', 'f'); - $this->rows = $query->execute()->fetchAll(); + $this->rows = $query->executeQuery()->fetchAll(); } /** @@ -65,7 +60,6 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt } public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void { - $cli = $this->getLibesignCli(); foreach ($this->rows as $row) { $userFolder = $this->root->getUserFolder($row['user_id']); /** @var File[] */ @@ -83,34 +77,4 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array } } } - - private function getMetadataFromCli(string $cli, string $filePath): array { - $fullPath = $this->getDataDir() . $filePath; - $json = shell_exec($cli . ' info ' . $fullPath); - $array = json_decode($json, true); - $output = [ - 'p' => count($array['pages']), - 'extension' => 'pdf', - ]; - foreach ($array['pages'] as $page) { - $output['d'][] = [ - 'w' => $page['width'], - 'h' => $page['height'], - ]; - } - return $output; - } - - private function getDataDir(): string { - return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/'); - } - - private function getLibesignCli(): string { - $path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path'); - if (!file_exists($path)) { - $this->install->run(new StringInput('--cli'), new NullOutput()); - $path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path'); - } - return $path; - } } diff --git a/lib/Service/PdfParserService.php b/lib/Service/PdfParserService.php index 1eaa389ce..1262c6df3 100644 --- a/lib/Service/PdfParserService.php +++ b/lib/Service/PdfParserService.php @@ -4,6 +4,7 @@ use OC\SystemConfig; use OCA\Libresign\AppInfo\Application; +use OCA\Libresign\Exception\LibresignException; use OCP\IConfig; class PdfParserService { @@ -31,10 +32,18 @@ private function getDataDir(): string { return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/'); } + /** + * @param string $filePath + * @return array + * @throws LibresignException + */ public function getMetadata(string $filePath): array { $fullPath = $this->getDataDir() . $filePath; $json = shell_exec($this->cliPath . ' info ' . $fullPath); $array = json_decode($json, true); + if (!is_array($array)) { + throw new LibresignException('Impossible get metadata from this file. Check if you installed correctly the libresign-cli.'); + } $output = [ 'p' => count($array['pages']), ]; diff --git a/lib/Service/SignFileService.php b/lib/Service/SignFileService.php index 1008c2c40..6cf95efa5 100644 --- a/lib/Service/SignFileService.php +++ b/lib/Service/SignFileService.php @@ -15,7 +15,6 @@ use OCA\Libresign\Exception\LibresignException; use OCA\Libresign\Handler\Pkcs7Handler; use OCA\Libresign\Handler\Pkcs12Handler; -use OCA\Libresign\Handler\TCPDILibresign; use OCA\Libresign\Helper\JSActions; use OCA\Libresign\Helper\ValidateHelper; use OCP\Accounts\IAccountManager; diff --git a/tests/Unit/Service/PdfParseServiceTest.php b/tests/Unit/Service/PdfParseServiceTest.php new file mode 100644 index 000000000..2f42a0a69 --- /dev/null +++ b/tests/Unit/Service/PdfParseServiceTest.php @@ -0,0 +1,90 @@ +config = $this->createMock(IConfig::class); + $this->systemConfig = $this->createMock(SystemConfig::class); + $this->installService = $this->createMock(InstallService::class); + } + + private function getService(): PdfParserService { + return new PdfParserService( + $this->config, + $this->systemConfig, + $this->installService + ); + } + + public function testGetMetadataWithFail(): void { + $this->expectException(LibresignException::class); + $this->config + ->method('getAppValue') + ->willReturnCallback(function ($appid, $key, $default) { + switch ($key) { + case 'libresign_cli_path': return '/fake_path/'; + } + }); + $this->systemConfig + ->method('getValue') + ->willReturnCallback(function ($key, $default) { + switch ($key) { + case 'datadirectory': return $default; + } + }); + $path = '/fake'; + $this->getService()->getMetadata($path); + } + + /** + * @dataProvider providerGetMetadataWIthSuccess + */ + public function testGetMetadataWIthSuccess(string $path, array $expected): void { + $this->installService = \OC::$server->get(InstallService::class); + $this->systemConfig = \OC::$server->get(SystemConfig::class); + $this->config = \OC::$server->get(IConfig::class); + $actual = $this->getService()->getMetadata($path); + $this->assertEquals($expected, $actual); + } + + public function providerGetMetadataWIthSuccess(): array { + return [ + [ + '/../apps/libresign/tests/fixtures/small_valid.pdf', + [ + 'p' => 1, + 'd' => [ + ['w' => 595.276, 'h' => 841.89], + ], + ] + ], + [ + '/../apps/libresign/tests/fixtures/small_valid-signed.pdf', + [ + 'p' => 1, + 'd' => [ + ['w' => 595.276, 'h' => 841.89], + ], + ] + ], + ]; + } +} From 336035e2b7b2664bbed25b74ad5eacc7b677d61f Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Sat, 9 Jul 2022 09:25:46 -0300 Subject: [PATCH 2/2] update dependencies --- composer.json | 2 +- composer.lock | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index c9491bbae..05016eb12 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "mikey179/vfsstream": "^1.6", "donatj/mock-webserver": "^2.4", "vimeo/psalm": "^4.18", - "christophwurst/nextcloud": "dev-master" + "christophwurst/nextcloud": "^24.0" }, "scripts": { "lint": "find . -name \\*.php -not -path './vendor/*' -not -path './build/*' -print0 | xargs -0 -n1 php -l", diff --git a/composer.lock b/composer.lock index 9f497cc70..ae821665a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "11f426a4c218c314983bfa473cd72f38", + "content-hash": "29c49eb52b7ef817859877b3933d397b", "packages": [ { "name": "bacon/bacon-qr-code", @@ -1035,29 +1035,28 @@ }, { "name": "christophwurst/nextcloud", - "version": "dev-master", + "version": "v24.0.1", "source": { "type": "git", "url": "https://github.com/ChristophWurst/nextcloud_composer.git", - "reference": "1a1fee726782ce1dc3db59f0d262c0679488f95b" + "reference": "f032acdff1502a7323f95a6524d163290f43b446" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/1a1fee726782ce1dc3db59f0d262c0679488f95b", - "reference": "1a1fee726782ce1dc3db59f0d262c0679488f95b", + "url": "https://api.github.com/repos/ChristophWurst/nextcloud_composer/zipball/f032acdff1502a7323f95a6524d163290f43b446", + "reference": "f032acdff1502a7323f95a6524d163290f43b446", "shasum": "" }, "require": { "php": "^7.4 || ~8.0 || ~8.1", - "psr/container": "^1.0", + "psr/container": "^1.1.1", "psr/event-dispatcher": "^1.0", "psr/log": "^1.1" }, - "default-branch": true, "type": "library", "extra": { "branch-alias": { - "dev-master": "25.0.0-dev" + "dev-master": "24.0.0-dev" } }, "notification-url": "https://packagist.org/downloads/", @@ -1073,9 +1072,9 @@ "description": "Composer package containing Nextcloud's public API (classes, interfaces)", "support": { "issues": "https://github.com/ChristophWurst/nextcloud_composer/issues", - "source": "https://github.com/ChristophWurst/nextcloud_composer/tree/master" + "source": "https://github.com/ChristophWurst/nextcloud_composer/tree/v24.0.1" }, - "time": "2022-05-05T01:56:01+00:00" + "time": "2022-06-02T14:16:47+00:00" }, { "name": "composer/package-versions-deprecated", @@ -6007,12 +6006,11 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { - "iio/libmergepdf": 20, - "christophwurst/nextcloud": 20 + "iio/libmergepdf": 20 }, "prefer-stable": false, "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" }