MountPublicLinkControllerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing\Tests\Controller;
  8. use OC\Federation\CloudIdManager;
  9. use OC\Share20\Share;
  10. use OCA\FederatedFileSharing\AddressHandler;
  11. use OCA\FederatedFileSharing\Controller\MountPublicLinkController;
  12. use OCA\FederatedFileSharing\FederatedShareProvider;
  13. use OCP\AppFramework\Http;
  14. use OCP\Contacts\IManager as IContactsManager;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\Federation\ICloudIdManager;
  17. use OCP\Files\IRootFolder;
  18. use OCP\HintException;
  19. use OCP\Http\Client\IClientService;
  20. use OCP\ICacheFactory;
  21. use OCP\IL10N;
  22. use OCP\IRequest;
  23. use OCP\ISession;
  24. use OCP\IURLGenerator;
  25. use OCP\IUserManager;
  26. use OCP\IUserSession;
  27. use OCP\Share\IManager;
  28. use OCP\Share\IShare;
  29. use PHPUnit\Framework\MockObject\MockObject;
  30. use Psr\Log\LoggerInterface;
  31. class MountPublicLinkControllerTest extends \Test\TestCase {
  32. /** @var IContactsManager|MockObject */
  33. protected $contactsManager;
  34. /** @var MountPublicLinkController */
  35. private $controller;
  36. /** @var IRequest|MockObject */
  37. private $request;
  38. /** @var FederatedShareProvider|MockObject */
  39. private $federatedShareProvider;
  40. /** @var IManager|MockObject */
  41. private $shareManager;
  42. /** @var AddressHandler|MockObject */
  43. private $addressHandler;
  44. /** @var IRootFolder|MockObject */
  45. private $rootFolder;
  46. /** @var IUserManager|MockObject */
  47. private $userManager;
  48. /** @var ISession|MockObject */
  49. private $session;
  50. /** @var IL10N|MockObject */
  51. private $l10n;
  52. /** @var IUserSession|MockObject */
  53. private $userSession;
  54. /** @var IClientService|MockObject */
  55. private $clientService;
  56. /** @var IShare */
  57. private $share;
  58. /** @var ICloudIdManager */
  59. private $cloudIdManager;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
  63. $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  64. ->disableOriginalConstructor()->getMock();
  65. $this->shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  66. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  67. ->disableOriginalConstructor()->getMock();
  68. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock();
  69. $this->userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  70. $this->share = new Share($this->rootFolder, $this->userManager);
  71. $this->session = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
  72. $this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
  73. $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
  74. $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
  75. $this->contactsManager = $this->createMock(IContactsManager::class);
  76. $this->cloudIdManager = new CloudIdManager(
  77. $this->contactsManager,
  78. $this->createMock(IURLGenerator::class),
  79. $this->userManager,
  80. $this->createMock(ICacheFactory::class),
  81. $this->createMock(IEventDispatcher::class)
  82. );
  83. $this->controller = new MountPublicLinkController(
  84. 'federatedfilesharing', $this->request,
  85. $this->federatedShareProvider,
  86. $this->shareManager,
  87. $this->addressHandler,
  88. $this->session,
  89. $this->l10n,
  90. $this->userSession,
  91. $this->clientService,
  92. $this->cloudIdManager,
  93. $this->createMock(LoggerInterface::class),
  94. );
  95. }
  96. /**
  97. * @dataProvider dataTestCreateFederatedShare
  98. *
  99. * @param string $shareWith
  100. * @param bool $outgoingSharesAllowed
  101. * @param bool $validShareWith
  102. * @param string $token
  103. * @param bool $validToken
  104. * @param bool $createSuccessful
  105. * @param string $expectedReturnData
  106. */
  107. public function testCreateFederatedShare($shareWith,
  108. $outgoingSharesAllowed,
  109. $validShareWith,
  110. $token,
  111. $validToken,
  112. $createSuccessful,
  113. $expectedReturnData,
  114. $permissions,
  115. ): void {
  116. $this->federatedShareProvider->expects($this->any())
  117. ->method('isOutgoingServer2serverShareEnabled')
  118. ->willReturn($outgoingSharesAllowed);
  119. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  120. ->with($shareWith)
  121. ->willReturnCallback(
  122. function ($shareWith) use ($validShareWith, $expectedReturnData) {
  123. if ($validShareWith) {
  124. return ['user', 'server'];
  125. }
  126. throw new HintException($expectedReturnData, $expectedReturnData);
  127. }
  128. );
  129. $share = $this->share;
  130. $share->setPermissions($permissions);
  131. $this->shareManager->expects($this->any())->method('getShareByToken')
  132. ->with($token)
  133. ->willReturnCallback(
  134. function ($token) use ($validToken, $share, $expectedReturnData) {
  135. if ($validToken) {
  136. return $share;
  137. }
  138. throw new HintException($expectedReturnData, $expectedReturnData);
  139. }
  140. );
  141. $this->federatedShareProvider->expects($this->any())->method('create')
  142. ->with($share)
  143. ->willReturnCallback(
  144. function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) {
  145. $this->assertEquals($shareWith, $share->getSharedWith());
  146. if ($createSuccessful) {
  147. return $share;
  148. }
  149. throw new HintException($expectedReturnData, $expectedReturnData);
  150. }
  151. );
  152. $result = $this->controller->createFederatedShare($shareWith, $token);
  153. $errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed;
  154. if ($errorCase) {
  155. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  156. $this->assertTrue(isset($result->getData()['message']));
  157. $this->assertSame($expectedReturnData, $result->getData()['message']);
  158. } else {
  159. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  160. $this->assertTrue(isset($result->getData()['remoteUrl']));
  161. $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']);
  162. }
  163. }
  164. public function dataTestCreateFederatedShare() {
  165. return [
  166. //shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData
  167. ['user@server', true, true, 'token', true, true, 'server', 31],
  168. ['user@server', true, true, 'token', false, false, 'server', 4],
  169. ['user@server', true, false, 'token', true, true, 'invalid federated cloud id', 31],
  170. ['user@server', true, false, 'token', false, true, 'invalid federated cloud id', 31],
  171. ['user@server', true, false, 'token', false, false, 'invalid federated cloud id', 31],
  172. ['user@server', true, false, 'token', true, false, 'invalid federated cloud id', 31],
  173. ['user@server', true, true, 'token', false, true, 'invalid token', 31],
  174. ['user@server', true, true, 'token', false, false, 'invalid token', 31],
  175. ['user@server', true, true, 'token', true, false, 'can not create share', 31],
  176. ['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares', 31],
  177. ];
  178. }
  179. }