mirror of
https://github.com/LibreSign/libresign.git
synced 2025-12-18 05:20:45 +01:00
- Add reset() method to AppConfigOverwrite that clears overWrite and deleted arrays and returns self - Integrate reset() directly into getMockAppConfig() to ensure clean state on every call - All tests now automatically get clean AppConfig state without explicit reset calls - Prevents state pollution across test suites by resetting at the source - Simplifies test code by removing need for separate reset wrapper method Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
132 lines
4.4 KiB
PHP
132 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OCA\Libresign\Tests\Unit\Service;
|
|
|
|
use OCA\Libresign\AppInfo\Application;
|
|
use OCA\Libresign\Exception\LibresignException;
|
|
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
|
|
use OCA\Libresign\Handler\DocMdpHandler;
|
|
use OCA\Libresign\Handler\FooterHandler;
|
|
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
|
|
use OCA\Libresign\Service\CaIdentifierService;
|
|
use OCA\Libresign\Service\FolderService;
|
|
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
|
|
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\Password;
|
|
use OCP\IAppConfig;
|
|
use OCP\IL10N;
|
|
use OCP\ITempManager;
|
|
use OCP\IUserSession;
|
|
use OCP\L10N\IFactory as IL10NFactory;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
final class PasswordTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|
private IdentifyService&MockObject $identifyService;
|
|
private Pkcs12Handler&MockObject $pkcs12Handler;
|
|
private IUserSession&MockObject $userSession;
|
|
private IAppConfig $appConfig;
|
|
private FolderService&MockObject $folderService;
|
|
private CertificateEngineFactory&MockObject $certificateEngineFactory;
|
|
private IL10N $l10n;
|
|
private FooterHandler&MockObject $footerHandler;
|
|
private ITempManager $tempManager;
|
|
private LoggerInterface&MockObject $logger;
|
|
private CaIdentifierService&MockObject $caIdentifierService;
|
|
private DocMdpHandler&MockObject $docMdpHandler;
|
|
|
|
public function setUp(): void {
|
|
$this->identifyService = $this->createMock(IdentifyService::class);
|
|
$this->appConfig = $this->getMockAppConfigWithReset();
|
|
$this->folderService = $this->createMock(FolderService::class);
|
|
$this->certificateEngineFactory = $this->createMock(CertificateEngineFactory::class);
|
|
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
|
|
$this->footerHandler = $this->createMock(FooterHandler::class);
|
|
$this->tempManager = \OCP\Server::get(ITempManager::class);
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
$this->caIdentifierService = $this->createMock(CaIdentifierService::class);
|
|
$this->docMdpHandler = $this->createMock(DocMdpHandler::class);
|
|
$this->pkcs12Handler = $this->getPkcs12Instance();
|
|
}
|
|
|
|
private function getClass(): Password {
|
|
return new Password(
|
|
$this->identifyService,
|
|
$this->pkcs12Handler,
|
|
$this->userSession,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return Pkcs12Handler&MockObject
|
|
*/
|
|
private function getPkcs12Instance(array $methods = []) {
|
|
return $this->getMockBuilder(Pkcs12Handler::class)
|
|
->setConstructorArgs([
|
|
$this->folderService,
|
|
$this->appConfig,
|
|
$this->certificateEngineFactory,
|
|
$this->l10n,
|
|
$this->footerHandler,
|
|
$this->tempManager,
|
|
$this->logger,
|
|
$this->caIdentifierService,
|
|
$this->docMdpHandler,
|
|
])
|
|
->onlyMethods($methods)
|
|
->getMock();
|
|
}
|
|
|
|
#[DataProvider('providerValidateToIdentify')]
|
|
public function testValidateToIdentify(string $pfx, bool $shouldThrow): void {
|
|
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']);
|
|
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx);
|
|
|
|
$password = $this->getClass();
|
|
$password->setCodeSentByUser('senha');
|
|
|
|
if ($shouldThrow) {
|
|
$this->expectException(LibresignException::class);
|
|
$password->validateToIdentify();
|
|
} else {
|
|
$password->validateToIdentify();
|
|
$this->expectNotToPerformAssertions();
|
|
}
|
|
}
|
|
|
|
public static function providerValidateToIdentify(): array {
|
|
return [
|
|
'valid pfx' => ['mock-pfx', false],
|
|
'empty pfx' => ['', true],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('providerValidateToSignWithError')]
|
|
public function testValidateToSignWithError(bool $throwsException, string $pfx): void {
|
|
$this->pkcs12Handler = $this->getPkcs12Instance(['getPfxOfCurrentSigner']);
|
|
$this->pkcs12Handler->method('getPfxOfCurrentSigner')->willReturn($pfx);
|
|
if ($throwsException) {
|
|
$this->expectException(LibresignException::class);
|
|
} else {
|
|
$this->expectNotToPerformAssertions();
|
|
}
|
|
|
|
$password = $this->getClass();
|
|
$password->setCodeSentByUser('senha');
|
|
$password->validateToSign();
|
|
}
|
|
|
|
public static function providerValidateToSignWithError(): array {
|
|
return [
|
|
'Invalid certificate' => [true, ''],
|
|
'throws InvalidPasswordException' => [true, 'mock-pfx'],
|
|
];
|
|
}
|
|
}
|