|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Service; |
| 10 | + |
| 11 | +use OCA\Libresign\AppInfo\Application; |
| 12 | +use OCA\Libresign\Exception\LibresignException; |
| 13 | +use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; |
| 14 | +use OCA\Libresign\Handler\FooterHandler; |
| 15 | +use OCA\Libresign\Handler\SignEngine\Pkcs12Handler; |
| 16 | +use OCA\Libresign\Service\FolderService; |
| 17 | +use OCA\Libresign\Service\IdentifyMethod\IdentifyService; |
| 18 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\Password; |
| 19 | +use OCP\IAppConfig; |
| 20 | +use OCP\IL10N; |
| 21 | +use OCP\ITempManager; |
| 22 | +use OCP\IUserSession; |
| 23 | +use OCP\L10N\IFactory as IL10NFactory; |
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | + |
| 27 | +final class PasswordTest extends \OCA\Libresign\Tests\Unit\TestCase { |
| 28 | + private IdentifyService&MockObject $identifyService; |
| 29 | + private Pkcs12Handler&MockObject $pkcs12Handler; |
| 30 | + private IUserSession&MockObject $userSession; |
| 31 | + private IAppConfig $appConfig; |
| 32 | + private FolderService&MockObject $folderService; |
| 33 | + private CertificateEngineFactory&MockObject $certificateEngineFactory; |
| 34 | + private IL10N $l10n; |
| 35 | + private FooterHandler&MockObject $footerHandler; |
| 36 | + private ITempManager $tempManager; |
| 37 | + |
| 38 | + public function setUp(): void { |
| 39 | + $this->identifyService = $this->createMock(IdentifyService::class); |
| 40 | + $this->appConfig = $this->getMockAppConfig(); |
| 41 | + $this->folderService = $this->createMock(FolderService::class); |
| 42 | + $this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class); |
| 43 | + $this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID); |
| 44 | + $this->footerHandler = $this->createMock(FooterHandler::class); |
| 45 | + $this->tempManager = \OCP\Server::get(ITempManager::class); |
| 46 | + $this->userSession = $this->createMock(IUserSession::class); |
| 47 | + $this->pkcs12Handler = $this->getPkcs12Instance(); |
| 48 | + } |
| 49 | + |
| 50 | + private function getClass(): Password { |
| 51 | + return new Password( |
| 52 | + $this->identifyService, |
| 53 | + $this->pkcs12Handler, |
| 54 | + $this->userSession, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return Pkcs12Handler&MockObject |
| 60 | + */ |
| 61 | + private function getPkcs12Instance(array $methods = []) { |
| 62 | + return $this->getMockBuilder(Pkcs12Handler::class) |
| 63 | + ->setConstructorArgs([ |
| 64 | + $this->folderService, |
| 65 | + $this->appConfig, |
| 66 | + $this->certificateEngineFactory, |
| 67 | + $this->l10n, |
| 68 | + $this->footerHandler, |
| 69 | + $this->tempManager, |
| 70 | + ]) |
| 71 | + ->onlyMethods($methods) |
| 72 | + ->getMock(); |
| 73 | + } |
| 74 | + |
| 75 | + #[DataProvider('providerValidateToIdentify')] |
| 76 | + public function testValidateToIdentify(string $pfx, bool $shouldThrow): void { |
| 77 | + $this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']); |
| 78 | + $this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx); |
| 79 | + |
| 80 | + $password = $this->getClass(); |
| 81 | + $password->setCodeSentByUser('senha'); |
| 82 | + |
| 83 | + if ($shouldThrow) { |
| 84 | + $this->expectException(LibresignException::class); |
| 85 | + $password->validateToIdentify(); |
| 86 | + } else { |
| 87 | + $password->validateToIdentify(); |
| 88 | + $this->expectNotToPerformAssertions(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static function providerValidateToIdentify(): array { |
| 93 | + return [ |
| 94 | + 'valid pfx' => ['mock-pfx', false], |
| 95 | + 'empty pfx' => ['', true], |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + #[DataProvider('providerValidateToSignWithError')] |
| 100 | + public function testValidateToSignWithError(bool $throwsException, string $pfx): void { |
| 101 | + $this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']); |
| 102 | + $this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx); |
| 103 | + if ($throwsException) { |
| 104 | + $this->expectException(LibresignException::class); |
| 105 | + } else { |
| 106 | + $this->expectNotToPerformAssertions(); |
| 107 | + } |
| 108 | + |
| 109 | + $password = $this->getClass(); |
| 110 | + $password->setCodeSentByUser('senha'); |
| 111 | + $password->validateToSign(); |
| 112 | + } |
| 113 | + |
| 114 | + public static function providerValidateToSignWithError(): array { |
| 115 | + return [ |
| 116 | + 'Invalid certificate' => [true, ''], |
| 117 | + 'throws InvalidPasswordException' => [true, 'mock-pfx'], |
| 118 | + ]; |
| 119 | + } |
| 120 | +} |
0 commit comments