-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPasswordTest.php
More file actions
132 lines (118 loc) · 4.38 KB
/
PasswordTest.php
File metadata and controls
132 lines (118 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?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'],
];
}
}