Skip to content

Commit 90b51c9

Browse files
committed
test: cover owner-scoped signed file writes
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 32d2203 commit 90b51c9

1 file changed

Lines changed: 145 additions & 2 deletions

File tree

tests/php/Unit/Service/SignFileServiceTest.php

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* SPDX-License-Identifier: AGPL-3.0-or-later
1010
*/
1111

12-
use bovigo\vfs\vfsStream;
1312
use DateTime;
1413
use OC\User\NoUserException;
1514
use OCA\Libresign\BackgroundJob\SignSingleFileJob;
@@ -1568,7 +1567,7 @@ public function testSetVisibleElements(
15681567
throw new NotFoundException();
15691568
});
15701569

1571-
vfsStream::setup('home');
1570+
\bovigo\vfs\vfsStream::setup('home');
15721571
$this->tempManager->method('getTemporaryFile')
15731572
->willReturnCallback(function ($postFix) {
15741573
preg_match('/.*(\d+).*/', $postFix, $matches);
@@ -2664,6 +2663,150 @@ public static function cleanupUnsignedSignedFileProvider(): array {
26642663
];
26652664
}
26662665

2666+
public function testCreateSignedFileRunsAsOwnerAndRestoresSession(): void {
2667+
$service = $this->getService();
2668+
2669+
$currentUser = $this->createMock(\OCP\IUser::class);
2670+
$currentUser->method('getUID')->willReturn('signer1');
2671+
2672+
$owner = $this->createMock(\OCP\IUser::class);
2673+
$owner->method('getUID')->willReturn('admin');
2674+
2675+
$originalFile = $this->createMock(\OCP\Files\File::class);
2676+
$originalFile->method('getExtension')->willReturn('pdf');
2677+
$originalFile->method('getPath')->willReturn('/admin/files/LibreSign/Document.pdf');
2678+
$originalFile->method('getOwner')->willReturn($owner);
2679+
$originalFile->method('getParentId')->willReturn(101);
2680+
2681+
$libreSignFile = new File();
2682+
$libreSignFile->setId(61);
2683+
$service->setLibreSignFile($libreSignFile);
2684+
2685+
$createdFile = $this->createMock(\OCP\Files\File::class);
2686+
$parentFolder = $this->createMock(\OCP\Files\Folder::class);
2687+
$parentFolder->expects($this->once())
2688+
->method('newFile')
2689+
->with('Document.signed_61.pdf', 'signed-content')
2690+
->willReturn($createdFile);
2691+
2692+
$userFolder = $this->createMock(\OCP\Files\Folder::class);
2693+
$userFolder->expects($this->once())
2694+
->method('getFirstNodeById')
2695+
->with(101)
2696+
->willReturn($parentFolder);
2697+
2698+
$this->root->expects($this->once())
2699+
->method('getUserFolder')
2700+
->with('admin')
2701+
->willReturn($userFolder);
2702+
2703+
$this->userSession->expects($this->once())
2704+
->method('getUser')
2705+
->willReturn($currentUser);
2706+
$this->userSession->expects($this->exactly(2))
2707+
->method('setVolatileActiveUser')
2708+
->with($this->callback(static function ($user) use ($owner, $currentUser): bool {
2709+
static $calls = 0;
2710+
$calls++;
2711+
2712+
return match ($calls) {
2713+
1 => $user === $owner,
2714+
2 => $user === $currentUser,
2715+
default => false,
2716+
};
2717+
}));
2718+
2719+
$result = self::invokePrivate($service, 'createSignedFile', [$originalFile, 'signed-content']);
2720+
2721+
$this->assertSame($createdFile, $result);
2722+
}
2723+
2724+
public function testSignSequentiallyRunsEngineSignAsOwnerAndRestoresSession(): void {
2725+
$service = $this->getService([
2726+
'validateDocMdpAllowsSignatures',
2727+
'getFileToSign',
2728+
'getEngine',
2729+
'computeHash',
2730+
'updateSignRequest',
2731+
'updateLibreSignFile',
2732+
'dispatchSignedEvent',
2733+
]);
2734+
2735+
$currentUser = $this->createMock(\OCP\IUser::class);
2736+
$currentUser->method('getUID')->willReturn('signer1');
2737+
2738+
$owner = $this->createMock(\OCP\IUser::class);
2739+
$owner->method('getUID')->willReturn('admin');
2740+
2741+
$fileToSign = $this->createMock(\OCP\Files\File::class);
2742+
$fileToSign->method('getOwner')->willReturn($owner);
2743+
2744+
$signedFile = $this->createMock(\OCP\Files\File::class);
2745+
$signedFile->method('getId')->willReturn(77);
2746+
2747+
$engine = $this->createMock(SignEngineHandler::class);
2748+
$engine->expects($this->once())
2749+
->method('sign')
2750+
->willReturn($signedFile);
2751+
$engine->expects($this->once())
2752+
->method('getLastSignedDate')
2753+
->willReturn(new DateTime('2026-04-02T03:11:54+00:00'));
2754+
2755+
$libreSignFile = new File();
2756+
$libreSignFile->setId(61);
2757+
$libreSignFile->setStatus(FileStatus::ABLE_TO_SIGN->value);
2758+
2759+
$signRequest = new SignRequest();
2760+
$signRequest->setId(62);
2761+
2762+
$service->setLibreSignFile($libreSignFile);
2763+
$service->setSignRequest($signRequest);
2764+
2765+
$service->expects($this->once())
2766+
->method('validateDocMdpAllowsSignatures');
2767+
$service->expects($this->once())
2768+
->method('getFileToSign')
2769+
->willReturn($fileToSign);
2770+
$service->expects($this->exactly(2))
2771+
->method('getEngine')
2772+
->willReturn($engine);
2773+
$service->expects($this->once())
2774+
->method('computeHash')
2775+
->with($signedFile)
2776+
->willReturn('hash');
2777+
$service->expects($this->once())
2778+
->method('updateSignRequest')
2779+
->with('hash');
2780+
$service->expects($this->once())
2781+
->method('updateLibreSignFile')
2782+
->with($libreSignFile, 77, 'hash');
2783+
$service->expects($this->once())
2784+
->method('dispatchSignedEvent');
2785+
2786+
$this->userSession->expects($this->once())
2787+
->method('getUser')
2788+
->willReturn($currentUser);
2789+
$this->userSession->expects($this->exactly(2))
2790+
->method('setVolatileActiveUser')
2791+
->with($this->callback(static function ($user) use ($owner, $currentUser): bool {
2792+
static $calls = 0;
2793+
$calls++;
2794+
2795+
return match ($calls) {
2796+
1 => $user === $owner,
2797+
2 => $user === $currentUser,
2798+
default => false,
2799+
};
2800+
}));
2801+
2802+
$result = self::invokePrivate($service, 'signSequentially', [[[
2803+
'file' => $libreSignFile,
2804+
'signRequest' => $signRequest,
2805+
]]]);
2806+
2807+
$this->assertInstanceOf(DateTime::class, $result);
2808+
}
2809+
26672810
#[DataProvider('cleanupUnsignedSignedFileProvider')]
26682811
public function testCleanupUnsignedSignedFile(bool $hasFile, ?\Exception $deleteException): void {
26692812
$service = $this->getService();

0 commit comments

Comments
 (0)