1
0

WellKnownUrlsTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 OCA\Settings\Tests;
  8. use OCA\Settings\SetupChecks\WellKnownUrls;
  9. use OCP\Http\Client\IClientService;
  10. use OCP\Http\Client\IResponse;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\SetupCheck\SetupResult;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. class WellKnownUrlsTest extends TestCase {
  19. private IL10N|MockObject $l10n;
  20. private IConfig|MockObject $config;
  21. private IURLGenerator|MockObject $urlGenerator;
  22. private IClientService|MockObject $clientService;
  23. private LoggerInterface|MockObject $logger;
  24. private WellKnownUrls|MockObject $setupcheck;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. /** @var IL10N|MockObject */
  28. $this->l10n = $this->getMockBuilder(IL10N::class)
  29. ->disableOriginalConstructor()->getMock();
  30. $this->l10n->expects($this->any())
  31. ->method('t')
  32. ->willReturnCallback(function ($message, array $replace) {
  33. return vsprintf($message, $replace);
  34. });
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  37. $this->clientService = $this->createMock(IClientService::class);
  38. $this->logger = $this->createMock(LoggerInterface::class);
  39. $this->setupcheck = $this->getMockBuilder(WellKnownUrls::class)
  40. ->onlyMethods(['runRequest'])
  41. ->setConstructorArgs([
  42. $this->l10n,
  43. $this->config,
  44. $this->urlGenerator,
  45. $this->clientService,
  46. $this->logger,
  47. ])
  48. ->getMock();
  49. }
  50. /**
  51. * Test that the SetupCheck is skipped if the system config is set
  52. */
  53. public function testDisabled(): void {
  54. $this->config
  55. ->expects($this->once())
  56. ->method('getSystemValueBool')
  57. ->with('check_for_working_wellknown_setup')
  58. ->willReturn(false);
  59. $this->setupcheck
  60. ->expects($this->never())
  61. ->method('runRequest');
  62. $result = $this->setupcheck->run();
  63. $this->assertEquals(SetupResult::INFO, $result->getSeverity());
  64. $this->assertMatchesRegularExpression('/check was skipped/', $result->getDescription());
  65. }
  66. /**
  67. * Test what happens if the local server could not be reached (no response from the requests)
  68. */
  69. public function testNoResponse(): void {
  70. $this->config
  71. ->expects($this->once())
  72. ->method('getSystemValueBool')
  73. ->with('check_for_working_wellknown_setup')
  74. ->willReturn(true);
  75. $this->setupcheck
  76. ->expects($this->once())
  77. ->method('runRequest')
  78. ->will($this->generate([]));
  79. $result = $this->setupcheck->run();
  80. $this->assertEquals(SetupResult::INFO, $result->getSeverity());
  81. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  82. }
  83. /**
  84. * Test responses
  85. * @dataProvider dataTestResponses
  86. */
  87. public function testResponses($responses, string $expectedSeverity): void {
  88. $this->config
  89. ->expects($this->once())
  90. ->method('getSystemValueBool')
  91. ->with('check_for_working_wellknown_setup')
  92. ->willReturn(true);
  93. $this->setupcheck
  94. ->expects($this->atLeastOnce())
  95. ->method('runRequest')
  96. ->willReturnOnConsecutiveCalls(...$responses);
  97. $result = $this->setupcheck->run();
  98. $this->assertEquals($expectedSeverity, $result->getSeverity());
  99. }
  100. public function dataTestResponses(): array {
  101. $createResponse = function (int $statuscode, array $header = []): IResponse|MockObject {
  102. $response = $this->createMock(IResponse::class);
  103. $response->expects($this->any())
  104. ->method('getStatusCode')
  105. ->willReturn($statuscode);
  106. $response->expects($this->any())
  107. ->method('getHeader')
  108. ->willReturnCallback(fn ($name) => $header[$name] ?? '');
  109. return $response;
  110. };
  111. $wellKnownHeader = ['X-NEXTCLOUD-WELL-KNOWN' => 'yes'];
  112. return [
  113. 'expected codes' => [
  114. [
  115. $this->generate([$createResponse(200, $wellKnownHeader)]),
  116. $this->generate([$createResponse(200, $wellKnownHeader)]),
  117. $this->generate([$createResponse(207)]),
  118. $this->generate([$createResponse(207)]),
  119. ],
  120. SetupResult::SUCCESS,
  121. ],
  122. 'late response with expected codes' => [
  123. [
  124. $this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]),
  125. $this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]),
  126. $this->generate([$createResponse(404), $createResponse(207)]),
  127. $this->generate([$createResponse(404), $createResponse(207)]),
  128. ],
  129. SetupResult::SUCCESS,
  130. ],
  131. 'working but disabled webfinger' => [
  132. [
  133. $this->generate([$createResponse(404, $wellKnownHeader)]),
  134. $this->generate([$createResponse(404, $wellKnownHeader)]),
  135. $this->generate([$createResponse(207)]),
  136. $this->generate([$createResponse(207)]),
  137. ],
  138. SetupResult::SUCCESS,
  139. ],
  140. 'unauthorized webdav but with correct configured redirect' => [
  141. [
  142. $this->generate([$createResponse(404, $wellKnownHeader)]),
  143. $this->generate([$createResponse(404, $wellKnownHeader)]),
  144. $this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com,https://example.com/remote.php/dav/'])]),
  145. $this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com/remote.php/dav/'])]),
  146. ],
  147. SetupResult::SUCCESS,
  148. ],
  149. 'not configured path' => [
  150. [
  151. $this->generate([$createResponse(404)]),
  152. $this->generate([$createResponse(404)]),
  153. $this->generate([$createResponse(404)]),
  154. $this->generate([$createResponse(404)]),
  155. ],
  156. SetupResult::WARNING,
  157. ],
  158. 'Invalid webfinger' => [
  159. [
  160. $this->generate([$createResponse(404)]),
  161. $this->generate([$createResponse(404, $wellKnownHeader)]),
  162. $this->generate([$createResponse(207)]),
  163. $this->generate([$createResponse(207)]),
  164. ],
  165. SetupResult::WARNING,
  166. ],
  167. 'Invalid nodeinfo' => [
  168. [
  169. $this->generate([$createResponse(404, $wellKnownHeader)]),
  170. $this->generate([$createResponse(404)]),
  171. $this->generate([$createResponse(207)]),
  172. $this->generate([$createResponse(207)]),
  173. ],
  174. SetupResult::WARNING,
  175. ],
  176. 'Invalid caldav' => [
  177. [
  178. $this->generate([$createResponse(404, $wellKnownHeader)]),
  179. $this->generate([$createResponse(404, $wellKnownHeader)]),
  180. $this->generate([$createResponse(404)]),
  181. $this->generate([$createResponse(207)]),
  182. ],
  183. SetupResult::WARNING,
  184. ],
  185. ];
  186. }
  187. /**
  188. * Helper function creates a nicer interface for mocking Generator behavior
  189. */
  190. protected function generate(array $yield_values) {
  191. return $this->returnCallback(function () use ($yield_values) {
  192. yield from $yield_values;
  193. });
  194. }
  195. }