RequestHandlerControllerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\FederatedFileSharing\Tests;
  8. use OCA\FederatedFileSharing\AddressHandler;
  9. use OCA\FederatedFileSharing\Controller\RequestHandlerController;
  10. use OCA\FederatedFileSharing\FederatedShareProvider;
  11. use OCA\FederatedFileSharing\Notifications;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Federation\ICloudFederationFactory;
  15. use OCP\Federation\ICloudFederationProvider;
  16. use OCP\Federation\ICloudFederationProviderManager;
  17. use OCP\Federation\ICloudFederationShare;
  18. use OCP\Federation\ICloudIdManager;
  19. use OCP\IDBConnection;
  20. use OCP\IRequest;
  21. use OCP\IUserManager;
  22. use OCP\Share;
  23. use OCP\Share\IShare;
  24. use Psr\Log\LoggerInterface;
  25. /**
  26. * Class RequestHandlerTest
  27. *
  28. * @package OCA\FederatedFileSharing\Tests
  29. * @group DB
  30. */
  31. class RequestHandlerControllerTest extends \Test\TestCase {
  32. private $owner = 'owner';
  33. private $user1 = 'user1';
  34. private $user2 = 'user2';
  35. private $ownerCloudId = 'owner@server0.org';
  36. private $user1CloudId = 'user1@server1.org';
  37. private $user2CloudId = 'user2@server2.org';
  38. /** @var RequestHandlerController */
  39. private $requestHandler;
  40. /** @var FederatedShareProvider|\PHPUnit\Framework\MockObject\MockObject */
  41. private $federatedShareProvider;
  42. /** @var Notifications|\PHPUnit\Framework\MockObject\MockObject */
  43. private $notifications;
  44. /** @var AddressHandler|\PHPUnit\Framework\MockObject\MockObject */
  45. private $addressHandler;
  46. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  47. private $userManager;
  48. /** @var IShare|\PHPUnit\Framework\MockObject\MockObject */
  49. private $share;
  50. /** @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject */
  51. private $cloudIdManager;
  52. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  53. private $logger;
  54. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  55. private $request;
  56. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  57. private $connection;
  58. /** @var Share\IManager|\PHPUnit\Framework\MockObject\MockObject */
  59. private $shareManager;
  60. /** @var ICloudFederationFactory|\PHPUnit\Framework\MockObject\MockObject */
  61. private $cloudFederationFactory;
  62. /** @var ICloudFederationProviderManager|\PHPUnit\Framework\MockObject\MockObject */
  63. private $cloudFederationProviderManager;
  64. /** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject */
  65. private $cloudFederationProvider;
  66. /** @var ICloudFederationShare|\PHPUnit\Framework\MockObject\MockObject */
  67. private $cloudFederationShare;
  68. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  69. private $eventDispatcher;
  70. protected function setUp(): void {
  71. $this->share = $this->getMockBuilder(IShare::class)->getMock();
  72. $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  73. ->disableOriginalConstructor()->getMock();
  74. $this->federatedShareProvider->expects($this->any())
  75. ->method('isOutgoingServer2serverShareEnabled')->willReturn(true);
  76. $this->federatedShareProvider->expects($this->any())
  77. ->method('isIncomingServer2serverShareEnabled')->willReturn(true);
  78. $this->federatedShareProvider->expects($this->any())->method('getShareById')
  79. ->willReturn($this->share);
  80. $this->notifications = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  81. ->disableOriginalConstructor()->getMock();
  82. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  83. ->disableOriginalConstructor()->getMock();
  84. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  85. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  86. $this->request = $this->createMock(IRequest::class);
  87. $this->connection = $this->createMock(IDBConnection::class);
  88. $this->shareManager = $this->createMock(Share\IManager::class);
  89. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  90. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  91. $this->cloudFederationProvider = $this->createMock(ICloudFederationProvider::class);
  92. $this->cloudFederationShare = $this->createMock(ICloudFederationShare::class);
  93. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  94. $this->eventDispatcher->expects($this->any())->method('dispatchTyped');
  95. $this->logger = $this->createMock(LoggerInterface::class);
  96. $this->requestHandler = new RequestHandlerController(
  97. 'federatedfilesharing',
  98. $this->request,
  99. $this->federatedShareProvider,
  100. $this->connection,
  101. $this->shareManager,
  102. $this->notifications,
  103. $this->addressHandler,
  104. $this->userManager,
  105. $this->cloudIdManager,
  106. $this->logger,
  107. $this->cloudFederationFactory,
  108. $this->cloudFederationProviderManager,
  109. $this->eventDispatcher
  110. );
  111. }
  112. public function testCreateShare(): void {
  113. $this->cloudFederationFactory->expects($this->once())->method('getCloudFederationShare')
  114. ->with(
  115. $this->user2,
  116. 'name',
  117. '',
  118. 1,
  119. $this->ownerCloudId,
  120. $this->owner,
  121. $this->user1CloudId,
  122. $this->user1,
  123. 'token',
  124. 'user',
  125. 'file'
  126. )->willReturn($this->cloudFederationShare);
  127. /** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject $provider */
  128. $this->cloudFederationProviderManager->expects($this->once())
  129. ->method('getCloudFederationProvider')
  130. ->with('file')
  131. ->willReturn($this->cloudFederationProvider);
  132. $this->cloudFederationProvider->expects($this->once())->method('shareReceived')
  133. ->with($this->cloudFederationShare);
  134. $result = $this->requestHandler->createShare('localhost', 'token', 'name', $this->owner, $this->user1, $this->user2, 1, $this->user1CloudId, $this->ownerCloudId);
  135. $this->assertInstanceOf(DataResponse::class, $result);
  136. }
  137. public function testDeclineShare(): void {
  138. $id = 42;
  139. $notification = [
  140. 'sharedSecret' => 'token',
  141. 'message' => 'Recipient declined the share'
  142. ];
  143. $this->cloudFederationProviderManager->expects($this->once())
  144. ->method('getCloudFederationProvider')
  145. ->with('file')
  146. ->willReturn($this->cloudFederationProvider);
  147. $this->cloudFederationProvider->expects($this->once())
  148. ->method('notificationReceived')
  149. ->with('SHARE_DECLINED', $id, $notification);
  150. $result = $this->requestHandler->declineShare($id, 'token');
  151. $this->assertInstanceOf(DataResponse::class, $result);
  152. }
  153. public function testAcceptShare(): void {
  154. $id = 42;
  155. $notification = [
  156. 'sharedSecret' => 'token',
  157. 'message' => 'Recipient accept the share'
  158. ];
  159. $this->cloudFederationProviderManager->expects($this->once())
  160. ->method('getCloudFederationProvider')
  161. ->with('file')
  162. ->willReturn($this->cloudFederationProvider);
  163. $this->cloudFederationProvider->expects($this->once())
  164. ->method('notificationReceived')
  165. ->with('SHARE_ACCEPTED', $id, $notification);
  166. $result = $this->requestHandler->acceptShare($id, 'token');
  167. $this->assertInstanceOf(DataResponse::class, $result);
  168. }
  169. }