CheckServerResponseTraitTest.php 8.0 KB

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