MountPublicLinkControllerTest.php 7.0 KB

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