ExternalStorageTest.php 2.9 KB

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