Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/php/Unit/Controller/SignFileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,58 @@ public function testRequestCodeBySignerUuidWithoutIdDocApprovalKeepsTheSignerLoo
$this->assertSame(Http::STATUS_OK, $response->getStatus());
}

/** @return array<string, array{string, string|int, array<int|string, null|string>, 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<int|string, null|string> $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();
Expand Down
54 changes: 54 additions & 0 deletions tests/php/Unit/Service/SignFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -496,6 +500,56 @@ public function testRequestCodeDoesNotPassDestinationToToken(): void {
);
}

/** @return array<string, array{string, string, string}> */
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)
Expand Down
Loading