WellKnownUrlsTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Tests;
  25. use OCA\Settings\SetupChecks\WellKnownUrls;
  26. use OCP\Http\Client\IClientService;
  27. use OCP\Http\Client\IResponse;
  28. use OCP\IConfig;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use OCP\SetupCheck\SetupResult;
  32. use PHPUnit\Framework\MockObject\MockObject;
  33. use Psr\Log\LoggerInterface;
  34. use Test\TestCase;
  35. class WellKnownUrlsTest extends TestCase {
  36. private IL10N|MockObject $l10n;
  37. private IConfig|MockObject $config;
  38. private IURLGenerator|MockObject $urlGenerator;
  39. private IClientService|MockObject $clientService;
  40. private LoggerInterface|MockObject $logger;
  41. private WellKnownUrls|MockObject $setupcheck;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. /** @var IL10N|MockObject */
  45. $this->l10n = $this->getMockBuilder(IL10N::class)
  46. ->disableOriginalConstructor()->getMock();
  47. $this->l10n->expects($this->any())
  48. ->method('t')
  49. ->willReturnCallback(function ($message, array $replace) {
  50. return vsprintf($message, $replace);
  51. });
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  54. $this->clientService = $this->createMock(IClientService::class);
  55. $this->logger = $this->createMock(LoggerInterface::class);
  56. $this->setupcheck = $this->getMockBuilder(WellKnownUrls::class)
  57. ->onlyMethods(['runRequest'])
  58. ->setConstructorArgs([
  59. $this->l10n,
  60. $this->config,
  61. $this->urlGenerator,
  62. $this->clientService,
  63. $this->logger,
  64. ])
  65. ->getMock();
  66. }
  67. /**
  68. * Test that the SetupCheck is skipped if the system config is set
  69. */
  70. public function testDisabled(): void {
  71. $this->config
  72. ->expects($this->once())
  73. ->method('getSystemValueBool')
  74. ->with('check_for_working_wellknown_setup')
  75. ->willReturn(false);
  76. $this->setupcheck
  77. ->expects($this->never())
  78. ->method('runRequest');
  79. $result = $this->setupcheck->run();
  80. $this->assertEquals(SetupResult::INFO, $result->getSeverity());
  81. $this->assertMatchesRegularExpression('/check was skipped/', $result->getDescription());
  82. }
  83. /**
  84. * Test what happens if the local server could not be reached (no response from the requests)
  85. */
  86. public function testNoResponse(): void {
  87. $this->config
  88. ->expects($this->once())
  89. ->method('getSystemValueBool')
  90. ->with('check_for_working_wellknown_setup')
  91. ->willReturn(true);
  92. $this->setupcheck
  93. ->expects($this->once())
  94. ->method('runRequest')
  95. ->will($this->generate([]));
  96. $result = $this->setupcheck->run();
  97. $this->assertEquals(SetupResult::INFO, $result->getSeverity());
  98. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  99. }
  100. /**
  101. * Test responses
  102. * @dataProvider dataTestResponses
  103. */
  104. public function testResponses($responses, string $expectedSeverity): void {
  105. $this->config
  106. ->expects($this->once())
  107. ->method('getSystemValueBool')
  108. ->with('check_for_working_wellknown_setup')
  109. ->willReturn(true);
  110. $this->setupcheck
  111. ->expects($this->atLeastOnce())
  112. ->method('runRequest')
  113. ->willReturnOnConsecutiveCalls(...$responses);
  114. $result = $this->setupcheck->run();
  115. $this->assertEquals($expectedSeverity, $result->getSeverity());
  116. }
  117. public function dataTestResponses(): array {
  118. $createResponse = function (int $statuscode, array $header = []): IResponse|MockObject {
  119. $response = $this->createMock(IResponse::class);
  120. $response->expects($this->any())
  121. ->method('getStatusCode')
  122. ->willReturn($statuscode);
  123. $response->expects($this->any())
  124. ->method('getHeader')
  125. ->willReturnCallback(fn ($name) => $header[$name] ?? '');
  126. return $response;
  127. };
  128. $wellKnownHeader = ['X-NEXTCLOUD-WELL-KNOWN' => 'yes'];
  129. return [
  130. 'expected codes' => [
  131. [
  132. $this->generate([$createResponse(200, $wellKnownHeader)]),
  133. $this->generate([$createResponse(200, $wellKnownHeader)]),
  134. $this->generate([$createResponse(207)]),
  135. $this->generate([$createResponse(207)]),
  136. ],
  137. SetupResult::SUCCESS,
  138. ],
  139. 'late response with expected codes' => [
  140. [
  141. $this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]),
  142. $this->generate([$createResponse(404), $createResponse(200, $wellKnownHeader)]),
  143. $this->generate([$createResponse(404), $createResponse(207)]),
  144. $this->generate([$createResponse(404), $createResponse(207)]),
  145. ],
  146. SetupResult::SUCCESS,
  147. ],
  148. 'working but disabled webfinger' => [
  149. [
  150. $this->generate([$createResponse(404, $wellKnownHeader)]),
  151. $this->generate([$createResponse(404, $wellKnownHeader)]),
  152. $this->generate([$createResponse(207)]),
  153. $this->generate([$createResponse(207)]),
  154. ],
  155. SetupResult::SUCCESS,
  156. ],
  157. 'unauthorized webdav but with correct configured redirect' => [
  158. [
  159. $this->generate([$createResponse(404, $wellKnownHeader)]),
  160. $this->generate([$createResponse(404, $wellKnownHeader)]),
  161. $this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com,https://example.com/remote.php/dav/'])]),
  162. $this->generate([$createResponse(401, ['X-Guzzle-Redirect-History' => 'https://example.com/remote.php/dav/'])]),
  163. ],
  164. SetupResult::SUCCESS,
  165. ],
  166. 'not configured path' => [
  167. [
  168. $this->generate([$createResponse(404)]),
  169. $this->generate([$createResponse(404)]),
  170. $this->generate([$createResponse(404)]),
  171. $this->generate([$createResponse(404)]),
  172. ],
  173. SetupResult::WARNING,
  174. ],
  175. 'Invalid webfinger' => [
  176. [
  177. $this->generate([$createResponse(404)]),
  178. $this->generate([$createResponse(404, $wellKnownHeader)]),
  179. $this->generate([$createResponse(207)]),
  180. $this->generate([$createResponse(207)]),
  181. ],
  182. SetupResult::WARNING,
  183. ],
  184. 'Invalid nodeinfo' => [
  185. [
  186. $this->generate([$createResponse(404, $wellKnownHeader)]),
  187. $this->generate([$createResponse(404)]),
  188. $this->generate([$createResponse(207)]),
  189. $this->generate([$createResponse(207)]),
  190. ],
  191. SetupResult::WARNING,
  192. ],
  193. 'Invalid caldav' => [
  194. [
  195. $this->generate([$createResponse(404, $wellKnownHeader)]),
  196. $this->generate([$createResponse(404, $wellKnownHeader)]),
  197. $this->generate([$createResponse(404)]),
  198. $this->generate([$createResponse(207)]),
  199. ],
  200. SetupResult::WARNING,
  201. ],
  202. ];
  203. }
  204. /**
  205. * Helper function creates a nicer interface for mocking Generator behavior
  206. */
  207. protected function generate(array $yield_values) {
  208. return $this->returnCallback(function () use ($yield_values) {
  209. yield from $yield_values;
  210. });
  211. }
  212. }