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