|
9 | 9 | * SPDX-License-Identifier: AGPL-3.0-or-later |
10 | 10 | */ |
11 | 11 |
|
12 | | -use bovigo\vfs\vfsStream; |
13 | 12 | use DateTime; |
14 | 13 | use OC\User\NoUserException; |
15 | 14 | use OCA\Libresign\BackgroundJob\SignSingleFileJob; |
@@ -1568,7 +1567,7 @@ public function testSetVisibleElements( |
1568 | 1567 | throw new NotFoundException(); |
1569 | 1568 | }); |
1570 | 1569 |
|
1571 | | - vfsStream::setup('home'); |
| 1570 | + \bovigo\vfs\vfsStream::setup('home'); |
1572 | 1571 | $this->tempManager->method('getTemporaryFile') |
1573 | 1572 | ->willReturnCallback(function ($postFix) { |
1574 | 1573 | preg_match('/.*(\d+).*/', $postFix, $matches); |
@@ -2664,6 +2663,150 @@ public static function cleanupUnsignedSignedFileProvider(): array { |
2664 | 2663 | ]; |
2665 | 2664 | } |
2666 | 2665 |
|
| 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 | + |
2667 | 2810 | #[DataProvider('cleanupUnsignedSignedFileProvider')] |
2668 | 2811 | public function testCleanupUnsignedSignedFile(bool $hasFile, ?\Exception $deleteException): void { |
2669 | 2812 | $service = $this->getService(); |
|
0 commit comments