From e651e9e489ff27d54b235398fd10d46f45ec39ae Mon Sep 17 00:00:00 2001 From: Luis Amorim Date: Sat, 14 Nov 2026 19:47:40 -0300 Subject: [PATCH 1/2] test(signing): cover missing code request methods Signed-off-by: Luis Amorim --- .../Controller/SignFileControllerTest.php | 53 ++++++++++++++++++ .../php/Unit/Service/SignFileServiceTest.php | 54 +++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/tests/php/Unit/Controller/SignFileControllerTest.php b/tests/php/Unit/Controller/SignFileControllerTest.php index 94509fe2a3..78d1f9b342 100644 --- a/tests/php/Unit/Controller/SignFileControllerTest.php +++ b/tests/php/Unit/Controller/SignFileControllerTest.php @@ -183,6 +183,59 @@ public function testRequestCodeBySignerUuidWithoutIdDocApprovalKeepsTheSignerLoo $this->assertSame(Http::STATUS_OK, $response->getStatus()); } + /** @return array, string, string}> */ + public static function missingCodeMethods(): array { + $cases = []; + foreach (['requestCodeBySignerUuid' => 'signer-uuid', 'requestCodeByFileId' => 3] as $method => $identifier) { + $cases += [ + "$method: both omitted" => [$method, $identifier, [], '', ''], + "$method: both null" => [$method, $identifier, [null, null], '', ''], + "$method: identifyMethod omitted" => [$method, $identifier, ['signMethod' => 'emailToken'], '', 'emailToken'], + "$method: identifyMethod null" => [$method, $identifier, [null, 'emailToken'], '', 'emailToken'], + "$method: signMethod omitted" => [$method, $identifier, ['email'], 'email', ''], + "$method: signMethod null" => [$method, $identifier, ['email', null], 'email', ''], + ]; + } + return $cases; + } + + /** + * A missing method reaches the service as an empty string, which the + * service rejects (covered by SignFileServiceTest); it is never replaced + * by a default method. + * + * @param array $methods + */ + #[DataProvider('missingCodeMethods')] + public function testMissingCodeMethodsStillUseServiceValidation( + string $method, + string|int $identifier, + array $methods, + string $expectedIdentifyMethod, + string $expectedSignMethod, + ): void { + $signRequest = new SignRequest(); + $signRequest->setId(5); + $signRequest->setFileId(3); + $file = new FileEntity(); + $file->setId(3); + $lookup = is_int($identifier) ? 'getByFileIdAndUserId' : 'getBySignerUuidAndUserId'; + $this->signRequestMapper->expects($this->once())->method($lookup) + ->with($identifier)->willReturn($signRequest); + $this->signFileService->expects($this->never())->method('getFileByUuid'); + $this->signFileService->method('getFile')->with(3)->willReturn($file); + $this->signingRequestValidator->expects($this->once())->method('fileCanBeSigned')->with($file); + $this->signFileService->expects($this->once())->method('requestCode') + ->with($signRequest, $expectedIdentifyMethod, $expectedSignMethod) + ->willThrowException(new LibresignException('Invalid identification method')); + + $response = $this->getController()->$method($identifier, ...$methods); + + $this->assertSame(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + $this->assertSame('Unable to send verification code.', $response->getData()['message']); + } + + public function testSignerUuidCodeRequestForSmsUsesConfiguredMethod(): void { $this->request->method('getParam')->with('idDocApproval')->willReturn('false'); $signRequest = new SignRequest(); diff --git a/tests/php/Unit/Service/SignFileServiceTest.php b/tests/php/Unit/Service/SignFileServiceTest.php index ffd2cf76de..0285e8971b 100644 --- a/tests/php/Unit/Service/SignFileServiceTest.php +++ b/tests/php/Unit/Service/SignFileServiceTest.php @@ -44,6 +44,8 @@ use OCA\Libresign\Service\File\SigningWorkflowStatusResolver; use OCA\Libresign\Service\FileStatusService; use OCA\Libresign\Service\FolderService; +use OCA\Libresign\Service\IdentifyMethod\Email as EmailIdentifyMethod; +use OCA\Libresign\Service\IdentifyMethod\IdentifyService; use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod; use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\EmailToken; use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ISignatureMethod; @@ -55,6 +57,7 @@ use OCA\Libresign\Service\Policy\Provider\CollectMetadata\CollectMetadataPolicy; use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicy; use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicyValue; +use OCA\Libresign\Service\SessionService; use OCA\Libresign\Service\SignatureRejection\SignatureRejectionPolicyService; use OCA\Libresign\Service\SignerElementsService; use OCA\Libresign\Service\SignFileService; @@ -72,6 +75,7 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Http\Client\IClientService; @@ -496,6 +500,56 @@ public function testRequestCodeDoesNotPassDestinationToToken(): void { ); } + /** @return array */ + public static function providerRequestCodeWithPartialMethods(): array { + return [ + 'identify method missing' => ['', ISignatureMethod::SIGNATURE_METHOD_EMAIL_TOKEN, 'Invalid identification method'], + 'sign method missing' => [IdentifyMethodService::IDENTIFY_EMAIL, '', 'Sending authorization code not enabled.'], + ]; + } + + /** + * The controllers forward a missing method as an empty string. The signer + * here has a real email identify method, so the sign method is rejected + * by its own list of available signature methods. + */ + #[DataProvider('providerRequestCodeWithPartialMethods')] + public function testRequestCodeWithPartialMethodsIsRejected( + string $identifyMethodName, + string $signMethodName, + string $expectedMessage, + ): void { + $signRequest = new SignRequest(); + $signRequest->setId(171); + + $identifyService = $this->createMock(IdentifyService::class); + $identifyService->method('getL10n')->willReturn($this->l10n); + $identifyMethod = new EmailIdentifyMethod( + $identifyService, + $this->createMock(IdentifyMethodMapper::class), + $this->createMock(IRootFolder::class), + $this->createMock(ITimeFactory::class), + $this->createMock(SessionService::class), + $this->createMock(FileElementMapper::class), + $this->createMock(IUserSession::class), + $this->createMock(LoggerInterface::class), + ); + $this->assertContains(ISignatureMethod::SIGNATURE_METHOD_EMAIL_TOKEN, $identifyMethod->getAvailableSignatureMethods()); + + $this->identifyMethodService + ->expects($this->once()) + ->method('getIdentifyMethodsFromSignRequestId') + ->with(171) + ->willReturn([ + IdentifyMethodService::IDENTIFY_EMAIL => [$identifyMethod], + ]); + + $this->expectException(LibresignException::class); + $this->expectExceptionMessage($expectedMessage); + + $this->getService()->requestCode($signRequest, $identifyMethodName, $signMethodName); + } + private function getService(array $methods = []): SignFileService|MockObject { if ($methods) { return $this->getMockBuilder(SignFileService::class) From 32718659afd43ea97dc9d779758f8ca749c706d9 Mon Sep 17 00:00:00 2001 From: Luis Amorim Date: Sat, 14 Nov 2026 19:56:02 -0300 Subject: [PATCH 2/2] style(signing): fix test spacing Signed-off-by: Luis Amorim --- tests/php/Unit/Controller/SignFileControllerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/php/Unit/Controller/SignFileControllerTest.php b/tests/php/Unit/Controller/SignFileControllerTest.php index 78d1f9b342..284ba346b6 100644 --- a/tests/php/Unit/Controller/SignFileControllerTest.php +++ b/tests/php/Unit/Controller/SignFileControllerTest.php @@ -235,7 +235,6 @@ public function testMissingCodeMethodsStillUseServiceValidation( $this->assertSame('Unable to send verification code.', $response->getData()['message']); } - public function testSignerUuidCodeRequestForSmsUsesConfiguredMethod(): void { $this->request->method('getParam')->with('idDocApproval')->willReturn('false'); $signRequest = new SignRequest();