1
0

RequestHandlerControllerTest.php 6.9 KB

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