|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Controller; |
| 10 | + |
| 11 | +use OCA\Libresign\AppInfo\Application; |
| 12 | +use OCA\Libresign\Controller\PageController; |
| 13 | +use OCA\Libresign\Db\File as FileEntity; |
| 14 | +use OCA\Libresign\Db\SignRequest as SignRequestEntity; |
| 15 | +use OCA\Libresign\Helper\ValidateHelper; |
| 16 | +use OCA\Libresign\Service\AccountService; |
| 17 | +use OCA\Libresign\Service\DocMdp\ConfigService; |
| 18 | +use OCA\Libresign\Service\File\FileListService; |
| 19 | +use OCA\Libresign\Service\FileService; |
| 20 | +use OCA\Libresign\Service\IdentifyMethodService; |
| 21 | +use OCA\Libresign\Service\RequestSignatureService; |
| 22 | +use OCA\Libresign\Service\SessionService; |
| 23 | +use OCA\Libresign\Service\SignerElementsService; |
| 24 | +use OCA\Libresign\Service\SignFileService; |
| 25 | +use OCA\Libresign\Tests\Unit\TestCase; |
| 26 | +use OCP\EventDispatcher\IEventDispatcher; |
| 27 | +use OCP\IAppConfig; |
| 28 | +use OCP\IInitialStateService; |
| 29 | +use OCP\IL10N; |
| 30 | +use OCP\IRequest; |
| 31 | +use OCP\IURLGenerator; |
| 32 | +use OCP\IUser; |
| 33 | +use OCP\IUserSession; |
| 34 | +use PHPUnit\Framework\MockObject\MockObject; |
| 35 | +use Psr\Log\LoggerInterface; |
| 36 | + |
| 37 | +final class PageControllerTest extends TestCase { |
| 38 | + private IRequest&MockObject $request; |
| 39 | + private IUserSession&MockObject $userSession; |
| 40 | + private AccountService&MockObject $accountService; |
| 41 | + private FileService&MockObject $fileService; |
| 42 | + private SignFileService&MockObject $signFileService; |
| 43 | + private SignerElementsService&MockObject $signerElementsService; |
| 44 | + private PageController $controller; |
| 45 | + |
| 46 | + public function setUp(): void { |
| 47 | + $this->request = $this->createMock(IRequest::class); |
| 48 | + $this->request->method('getServerHost')->willReturn('localhost:8080'); |
| 49 | + $this->userSession = $this->createMock(IUserSession::class); |
| 50 | + $user = $this->createMock(IUser::class); |
| 51 | + $user->method('getUID')->willReturn('requester'); |
| 52 | + $this->userSession->method('getUser')->willReturn($user); |
| 53 | + |
| 54 | + $this->accountService = $this->createMock(AccountService::class); |
| 55 | + $this->accountService->method('getConfig')->willReturn([]); |
| 56 | + $this->accountService->method('getConfigFilters')->willReturn([]); |
| 57 | + $this->accountService->method('getConfigSorting')->willReturn([]); |
| 58 | + $this->accountService->method('getCertificateEngineName')->willReturn('openssl'); |
| 59 | + |
| 60 | + $this->fileService = $this->createMock(FileService::class); |
| 61 | + $this->fileService->method('setFile')->willReturnSelf(); |
| 62 | + $this->fileService->method('setSignRequest')->willReturnSelf(); |
| 63 | + $this->fileService->method('setHost')->willReturnSelf(); |
| 64 | + $this->fileService->method('setMe')->willReturnSelf(); |
| 65 | + $this->fileService->method('setSignerIdentified')->willReturnSelf(); |
| 66 | + $this->fileService->method('setIdentifyMethodId')->willReturnSelf(); |
| 67 | + $this->fileService->method('showVisibleElements')->willReturnSelf(); |
| 68 | + $this->fileService->method('showSigners')->willReturnSelf(); |
| 69 | + $this->fileService->method('showSettings')->willReturnSelf(); |
| 70 | + $this->fileService->method('toArray')->willReturn([ |
| 71 | + 'id' => 5, |
| 72 | + 'nodeId' => 50, |
| 73 | + 'status' => 1, |
| 74 | + 'statusText' => 'Ready to sign', |
| 75 | + 'signers' => [], |
| 76 | + 'visibleElements' => [], |
| 77 | + 'settings' => [ |
| 78 | + 'needIdentificationDocuments' => false, |
| 79 | + 'identificationDocumentsWaitingApproval' => false, |
| 80 | + ], |
| 81 | + ]); |
| 82 | + |
| 83 | + $this->signFileService = $this->createMock(SignFileService::class); |
| 84 | + $this->signFileService->method('getPdfUrlsForSigning')->willReturn(['/apps/libresign/pdf/sign-uuid']); |
| 85 | + |
| 86 | + $this->signerElementsService = $this->createMock(SignerElementsService::class); |
| 87 | + $this->signerElementsService->method('getElementsFromSessionAsArray')->willReturn([]); |
| 88 | + $this->signerElementsService->method('getUserElements')->willReturn([]); |
| 89 | + |
| 90 | + $this->controller = new PageController( |
| 91 | + request: $this->request, |
| 92 | + userSession: $this->userSession, |
| 93 | + sessionService: $this->createMock(SessionService::class), |
| 94 | + initialState: new \OC\AppFramework\Services\InitialState( |
| 95 | + $this->createMock(IInitialStateService::class), |
| 96 | + Application::APP_ID, |
| 97 | + ), |
| 98 | + accountService: $this->accountService, |
| 99 | + signFileService: $this->signFileService, |
| 100 | + requestSignatureService: \OCP\Server::get(RequestSignatureService::class), |
| 101 | + signerElementsService: $this->signerElementsService, |
| 102 | + l10n: $this->createMock(IL10N::class), |
| 103 | + identifyMethodService: $this->createConfiguredMock(IdentifyMethodService::class, [ |
| 104 | + 'getIdentifyMethodsSettings' => [], |
| 105 | + ]), |
| 106 | + appConfig: \OCP\Server::get(IAppConfig::class), |
| 107 | + fileService: $this->fileService, |
| 108 | + fileListService: \OCP\Server::get(FileListService::class), |
| 109 | + fileMapper: \OCP\Server::get(\OCA\Libresign\Db\FileMapper::class), |
| 110 | + signRequestMapper: \OCP\Server::get(\OCA\Libresign\Db\SignRequestMapper::class), |
| 111 | + logger: \OCP\Server::get(LoggerInterface::class), |
| 112 | + validateHelper: $this->createMock(ValidateHelper::class), |
| 113 | + eventDispatcher: $this->createMock(IEventDispatcher::class), |
| 114 | + urlGenerator: \OCP\Server::get(IURLGenerator::class), |
| 115 | + docMdpConfigService: $this->createConfiguredMock(ConfigService::class, [ |
| 116 | + 'getConfig' => [], |
| 117 | + ]), |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + public function testIndexAllowsSelfWorkerSrcDomain(): void { |
| 122 | + $response = $this->controller->index(); |
| 123 | + |
| 124 | + self::assertStringContainsString("worker-src 'self'", $response->getContentSecurityPolicy()->buildPolicy()); |
| 125 | + } |
| 126 | + |
| 127 | + public function testPublicSignAllowsSelfWorkerSrcDomain(): void { |
| 128 | + $fileEntity = new FileEntity(); |
| 129 | + $fileEntity->setId(5); |
| 130 | + $fileEntity->setName('small_valid'); |
| 131 | + $fileEntity->setNodeId(50); |
| 132 | + $fileEntity->setNodeType('file'); |
| 133 | + |
| 134 | + $signRequestEntity = new SignRequestEntity(); |
| 135 | + $signRequestEntity->setFileId(5); |
| 136 | + $signRequestEntity->setUuid('sign-uuid'); |
| 137 | + $signRequestEntity->setDescription(''); |
| 138 | + $this->signFileService->method('getSignRequestByUuid')->willReturn($signRequestEntity); |
| 139 | + $this->signFileService->method('getFile')->willReturn($fileEntity); |
| 140 | + $this->controller->loadNextcloudFileFromSignRequestUuid('sign-uuid'); |
| 141 | + |
| 142 | + $response = $this->controller->sign('sign-uuid'); |
| 143 | + |
| 144 | + self::assertStringContainsString("worker-src 'self'", $response->getContentSecurityPolicy()->buildPolicy()); |
| 145 | + } |
| 146 | +} |
0 commit comments