|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Tests\Richdocuments; |
| 11 | + |
| 12 | +use OCA\Federation\TrustedServers; |
| 13 | +use OCA\Richdocuments\AppConfig; |
| 14 | +use OCA\Richdocuments\Service\FederationService; |
| 15 | +use OCA\Richdocuments\TokenManager; |
| 16 | +use OCP\Http\Client\IClientService; |
| 17 | +use OCP\ICacheFactory; |
| 18 | +use OCP\IRequest; |
| 19 | +use OCP\IURLGenerator; |
| 20 | +use OCP\Security\ITrustedDomainHelper; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +class FederationServiceTest extends TestCase { |
| 25 | + public const NEXTCLOUD_ADDRESS = 'https://nextcloud.local'; |
| 26 | + public const COLLABORA_ADDRESS = 'https://collabora.local'; |
| 27 | + |
| 28 | + public function setUp(): void { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
| 32 | + $this->clientService = $this->createMock(IClientService::class); |
| 33 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 34 | + $this->tokenManager = $this->createMock(TokenManager::class); |
| 35 | + $this->appConfig = $this->createStub(AppConfig::class); |
| 36 | + $this->request = $this->createMock(IRequest::class); |
| 37 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
| 38 | + $this->trustedDomainHelper = $this->createStub(ITrustedDomainHelper::class); |
| 39 | + |
| 40 | + $this->federationService = new FederationService( |
| 41 | + $this->cacheFactory, |
| 42 | + $this->clientService, |
| 43 | + $this->logger, |
| 44 | + $this->tokenManager, |
| 45 | + $this->appConfig, |
| 46 | + $this->request, |
| 47 | + $this->urlGenerator, |
| 48 | + $this->trustedDomainHelper |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @test |
| 54 | + * @testdox returns own instance's Collabora URL |
| 55 | + */ |
| 56 | + public function getRemoteCollaboraURLFromOwnInstance(): void { |
| 57 | + // Ensure that trusted domains can be used for federated editing |
| 58 | + $this->appConfig->method('isTrustedDomainAllowedForFederation') |
| 59 | + ->willReturn(true); |
| 60 | + $this->appConfig->method('getCollaboraUrlInternal') |
| 61 | + ->willReturn(self::COLLABORA_ADDRESS); |
| 62 | + |
| 63 | + $this->trustedDomainHelper->method('isTrustedUrl') |
| 64 | + ->with(self::NEXTCLOUD_ADDRESS) |
| 65 | + ->willReturn(true); |
| 66 | + |
| 67 | + // Create a stub TrustedServers class which always tells us |
| 68 | + // the server is trusted |
| 69 | + $trustedServers = $this->createStub(TrustedServers::class); |
| 70 | + $trustedServers->method('isTrustedServer') |
| 71 | + ->with('nextcloud.local') |
| 72 | + ->willReturn(true); |
| 73 | + |
| 74 | + // Do some reflection property manipulation to set the TrustedServers object |
| 75 | + // It would be nice if the TrustedServers were passed into FederationService |
| 76 | + // instead of being set manually in the constructor to make testing easier |
| 77 | + $reflection = new \ReflectionClass($this->federationService); |
| 78 | + $reflectionProperty = $reflection->getProperty('trustedServers'); |
| 79 | + $reflectionProperty->setAccessible(true); |
| 80 | + $reflectionProperty->setValue($this->federationService, $trustedServers); |
| 81 | + |
| 82 | + $this->assertEquals(self::COLLABORA_ADDRESS, $this->federationService->getRemoteCollaboraURL(self::NEXTCLOUD_ADDRESS)); |
| 83 | + } |
| 84 | +} |
0 commit comments