ExternalStorageTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\Federation\CloudId;
  9. use OCA\Files_Sharing\External\Manager as ExternalShareManager;
  10. use OCA\Files_Sharing\External\Storage;
  11. use OCP\Http\Client\IClient;
  12. use OCP\Http\Client\IClientService;
  13. use OCP\Http\Client\IResponse;
  14. /**
  15. * Tests for the external Storage class for remote shares.
  16. *
  17. * @group DB
  18. */
  19. class ExternalStorageTest extends \Test\TestCase {
  20. public function optionsProvider() {
  21. return [
  22. [
  23. 'http://remoteserver:8080/owncloud',
  24. 'http://remoteserver:8080/owncloud/public.php/webdav/',
  25. ],
  26. // extra slash
  27. [
  28. 'http://remoteserver:8080/owncloud/',
  29. 'http://remoteserver:8080/owncloud/public.php/webdav/',
  30. ],
  31. // extra path
  32. [
  33. 'http://remoteserver:8080/myservices/owncloud/',
  34. 'http://remoteserver:8080/myservices/owncloud/public.php/webdav/',
  35. ],
  36. // root path
  37. [
  38. 'http://remoteserver:8080/',
  39. 'http://remoteserver:8080/public.php/webdav/',
  40. ],
  41. // without port
  42. [
  43. 'http://remoteserver/oc.test',
  44. 'http://remoteserver/oc.test/public.php/webdav/',
  45. ],
  46. // https
  47. [
  48. 'https://remoteserver/',
  49. 'https://remoteserver/public.php/webdav/',
  50. ],
  51. ];
  52. }
  53. private function getTestStorage($uri) {
  54. $certificateManager = \OC::$server->getCertificateManager();
  55. $httpClientService = $this->createMock(IClientService::class);
  56. $manager = $this->createMock(ExternalShareManager::class);
  57. $client = $this->createMock(IClient::class);
  58. $response = $this->createMock(IResponse::class);
  59. $client
  60. ->expects($this->any())
  61. ->method('get')
  62. ->willReturn($response);
  63. $client
  64. ->expects($this->any())
  65. ->method('post')
  66. ->willReturn($response);
  67. $httpClientService
  68. ->expects($this->any())
  69. ->method('newClient')
  70. ->willReturn($client);
  71. return new TestSharingExternalStorage(
  72. [
  73. 'cloudId' => new CloudId('testOwner@' . $uri, 'testOwner', $uri),
  74. 'remote' => $uri,
  75. 'owner' => 'testOwner',
  76. 'mountpoint' => 'remoteshare',
  77. 'token' => 'abcdef',
  78. 'password' => '',
  79. 'manager' => $manager,
  80. 'certificateManager' => $certificateManager,
  81. 'HttpClientService' => $httpClientService,
  82. ]
  83. );
  84. }
  85. /**
  86. * @dataProvider optionsProvider
  87. */
  88. public function testStorageMountOptions($inputUri, $baseUri): void {
  89. $storage = $this->getTestStorage($inputUri);
  90. $this->assertEquals($baseUri, $storage->getBaseUri());
  91. }
  92. public function testIfTestReturnsTheValue(): void {
  93. $storage = $this->getTestStorage('https://remoteserver');
  94. $result = $storage->test();
  95. $this->assertSame(true, $result);
  96. }
  97. }
  98. /**
  99. * Dummy subclass to make it possible to access private members
  100. */
  101. class TestSharingExternalStorage extends Storage {
  102. public function getBaseUri() {
  103. return $this->createBaseUri();
  104. }
  105. public function stat(string $path): array|false {
  106. if ($path === '') {
  107. return ['key' => 'value'];
  108. }
  109. return parent::stat($path);
  110. }
  111. }