|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\Settings\Tests\SetupChecks; |
| 10 | + |
| 11 | +use OCP\Http\Client\IClientService; |
| 12 | +use OCP\IConfig; |
| 13 | +use OCP\IL10N; |
| 14 | +use OCP\IURLGenerator; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Test\TestCase; |
| 18 | + |
| 19 | +class CheckServerResponseTraitTest extends TestCase { |
| 20 | + |
| 21 | + protected const BASE_URL = 'https://nextcloud.local'; |
| 22 | + |
| 23 | + private IL10N&MockObject $l10n; |
| 24 | + private IConfig&MockObject $config; |
| 25 | + private IURLGenerator&MockObject $urlGenerator; |
| 26 | + private IClientService&MockObject $clientService; |
| 27 | + private LoggerInterface&MockObject $logger; |
| 28 | + |
| 29 | + private CheckServerResponseTraitImplementation $trait; |
| 30 | + |
| 31 | + protected function setUp(): void { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->l10n = $this->createMock(IL10N::class); |
| 35 | + $this->l10n->method('t') |
| 36 | + ->willReturnArgument(0); |
| 37 | + $this->config = $this->createMock(IConfig::class); |
| 38 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
| 39 | + $this->clientService = $this->createMock(IClientService::class); |
| 40 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 41 | + |
| 42 | + $this->trait = new CheckServerResponseTraitImplementation( |
| 43 | + $this->l10n, |
| 44 | + $this->config, |
| 45 | + $this->urlGenerator, |
| 46 | + $this->clientService, |
| 47 | + $this->logger, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider dataNormalizeUrl |
| 53 | + */ |
| 54 | + public function testNormalizeUrl(string $url, string $webRoot, bool $removeWebRoot, string $expected): void { |
| 55 | + $this->assertEquals($expected, $this->trait->normalizeUrl($url, $webRoot, $removeWebRoot)); |
| 56 | + } |
| 57 | + |
| 58 | + public static function dataNormalizeUrl(): array { |
| 59 | + return [ |
| 60 | + 'valid and nothing to change' => ['http://example.com/root', '/root', false, 'http://example.com/root'], |
| 61 | + 'trailing slash' => ['http://example.com/root/', '/root', false, 'http://example.com/root'], |
| 62 | + 'remove web root' => ['http://example.com/root/', '/root', true, 'http://example.com'], |
| 63 | + 'remove web root but empty' => ['http://example.com', '', true, 'http://example.com'], |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @dataProvider dataGetTestUrls |
| 69 | + */ |
| 70 | + public function testGetTestUrls( |
| 71 | + string $url, |
| 72 | + bool $removeWebRoot, |
| 73 | + string $cliUrl, |
| 74 | + string $webRoot, |
| 75 | + array $trustedDomains, |
| 76 | + array $expected, |
| 77 | + ): void { |
| 78 | + $this->config->expects(self::atLeastOnce()) |
| 79 | + ->method('getSystemValueString') |
| 80 | + ->with('overwrite.cli.url', '') |
| 81 | + ->willReturn($cliUrl); |
| 82 | + |
| 83 | + $this->config->expects(self::atLeastOnce()) |
| 84 | + ->method('getSystemValue') |
| 85 | + ->with('trusted_domains', []) |
| 86 | + ->willReturn($trustedDomains); |
| 87 | + |
| 88 | + $this->urlGenerator->expects(self::atLeastOnce()) |
| 89 | + ->method('getWebroot') |
| 90 | + ->willReturn($webRoot); |
| 91 | + |
| 92 | + $this->urlGenerator->expects(self::atLeastOnce()) |
| 93 | + ->method('getBaseUrl') |
| 94 | + ->willReturn(self::BASE_URL . $webRoot); |
| 95 | + |
| 96 | + $result = $this->trait->getTestUrls($url, $removeWebRoot); |
| 97 | + $this->assertEquals($expected, $result); |
| 98 | + } |
| 99 | + |
| 100 | + public static function dataGetTestUrls(): array { |
| 101 | + return [ |
| 102 | + 'same cli and base URL' => [ |
| 103 | + '/apps/files/js/example.js', false, 'https://nextcloud.local', '', ['nextcloud.local'], [ |
| 104 | + // from cli url |
| 105 | + 'https://nextcloud.local/apps/files/js/example.js', |
| 106 | + // http variant from trusted domains |
| 107 | + 'http://nextcloud.local/apps/files/js/example.js', |
| 108 | + ] |
| 109 | + ], |
| 110 | + 'different cli and base URL' => [ |
| 111 | + '/apps/files/js/example.js', false, 'https://example.com', '', ['nextcloud.local'], [ |
| 112 | + // from cli url |
| 113 | + 'https://example.com/apps/files/js/example.js', |
| 114 | + // from base url |
| 115 | + 'https://nextcloud.local/apps/files/js/example.js', |
| 116 | + // http variant from trusted domains |
| 117 | + 'http://nextcloud.local/apps/files/js/example.js', |
| 118 | + ] |
| 119 | + ], |
| 120 | + 'different cli and base URL and trusted domains' => [ |
| 121 | + '/apps/files/js/example.js', false, 'https://example.com', '', ['nextcloud.local', 'example.com', '127.0.0.1'], [ |
| 122 | + // from cli url |
| 123 | + 'https://example.com/apps/files/js/example.js', |
| 124 | + // from base url |
| 125 | + 'https://nextcloud.local/apps/files/js/example.js', |
| 126 | + // http variant from trusted domains |
| 127 | + 'http://nextcloud.local/apps/files/js/example.js', |
| 128 | + 'http://example.com/apps/files/js/example.js', |
| 129 | + // trusted domains |
| 130 | + 'https://127.0.0.1/apps/files/js/example.js', |
| 131 | + 'http://127.0.0.1/apps/files/js/example.js', |
| 132 | + ] |
| 133 | + ], |
| 134 | + 'wildcard trusted domains' => [ |
| 135 | + '/apps/files/js/example.js', false, '', '', ['nextcloud.local', '*.example.com'], [ |
| 136 | + // from base url |
| 137 | + 'https://nextcloud.local/apps/files/js/example.js', |
| 138 | + // http variant from trusted domains |
| 139 | + 'http://nextcloud.local/apps/files/js/example.js', |
| 140 | + // trusted domains with wild card are skipped |
| 141 | + ] |
| 142 | + ], |
| 143 | + 'missing leading slash' => [ |
| 144 | + 'apps/files/js/example.js', false, 'https://nextcloud.local', '', ['nextcloud.local'], [ |
| 145 | + // from cli url |
| 146 | + 'https://nextcloud.local/apps/files/js/example.js', |
| 147 | + // http variant from trusted domains |
| 148 | + 'http://nextcloud.local/apps/files/js/example.js', |
| 149 | + ] |
| 150 | + ], |
| 151 | + 'keep web-root' => [ |
| 152 | + '/apps/files/js/example.js', false, 'https://example.com', '/nextcloud', ['nextcloud.local', 'example.com', '192.168.100.1'], [ |
| 153 | + // from cli url (note that the CLI url has NO web root) |
| 154 | + 'https://example.com/apps/files/js/example.js', |
| 155 | + // from base url |
| 156 | + 'https://nextcloud.local/nextcloud/apps/files/js/example.js', |
| 157 | + // http variant from trusted domains |
| 158 | + 'http://nextcloud.local/nextcloud/apps/files/js/example.js', |
| 159 | + // trusted domains with web-root |
| 160 | + 'https://example.com/nextcloud/apps/files/js/example.js', |
| 161 | + 'http://example.com/nextcloud/apps/files/js/example.js', |
| 162 | + 'https://192.168.100.1/nextcloud/apps/files/js/example.js', |
| 163 | + 'http://192.168.100.1/nextcloud/apps/files/js/example.js', |
| 164 | + ] |
| 165 | + ], |
| 166 | + // example if the URL is generated by the URL generator |
| 167 | + 'keep web-root and web root in url' => [ |
| 168 | + '/nextcloud/apps/files/js/example.js', false, 'https://example.com', '/nextcloud', ['nextcloud.local', 'example.com', '192.168.100.1'], [ |
| 169 | + // from cli url (note that the CLI url has NO web root) |
| 170 | + 'https://example.com/apps/files/js/example.js', |
| 171 | + // from base url |
| 172 | + 'https://nextcloud.local/nextcloud/apps/files/js/example.js', |
| 173 | + // http variant from trusted domains |
| 174 | + 'http://nextcloud.local/nextcloud/apps/files/js/example.js', |
| 175 | + // trusted domains with web-root |
| 176 | + 'https://example.com/nextcloud/apps/files/js/example.js', |
| 177 | + 'http://example.com/nextcloud/apps/files/js/example.js', |
| 178 | + 'https://192.168.100.1/nextcloud/apps/files/js/example.js', |
| 179 | + 'http://192.168.100.1/nextcloud/apps/files/js/example.js', |
| 180 | + ] |
| 181 | + ], |
| 182 | + 'remove web-root' => [ |
| 183 | + '/.well-known/caldav', true, 'https://example.com', '/nextcloud', ['nextcloud.local', 'example.com', '192.168.100.1'], [ |
| 184 | + // from cli url (note that the CLI url has NO web root) |
| 185 | + 'https://example.com/.well-known/caldav', |
| 186 | + // from base url |
| 187 | + 'https://nextcloud.local/.well-known/caldav', |
| 188 | + // http variant from trusted domains |
| 189 | + 'http://nextcloud.local/.well-known/caldav', |
| 190 | + 'http://example.com/.well-known/caldav', |
| 191 | + // trusted domains with web-root |
| 192 | + 'https://192.168.100.1/.well-known/caldav', |
| 193 | + 'http://192.168.100.1/.well-known/caldav', |
| 194 | + ] |
| 195 | + ], |
| 196 | + // example if the URL is generated by the URL generator |
| 197 | + 'remove web-root and web root in url' => [ |
| 198 | + '/nextcloud/.well-known/caldav', true, 'https://example.com', '/nextcloud', ['nextcloud.local', 'example.com', '192.168.100.1'], [ |
| 199 | + // from cli url (note that the CLI url has NO web root) |
| 200 | + 'https://example.com/.well-known/caldav', |
| 201 | + // from base url |
| 202 | + 'https://nextcloud.local/.well-known/caldav', |
| 203 | + // http variant from trusted domains |
| 204 | + 'http://nextcloud.local/.well-known/caldav', |
| 205 | + 'http://example.com/.well-known/caldav', |
| 206 | + // trusted domains with web-root |
| 207 | + 'https://192.168.100.1/.well-known/caldav', |
| 208 | + 'http://192.168.100.1/.well-known/caldav', |
| 209 | + ] |
| 210 | + ], |
| 211 | + ]; |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments