RequestHandlerControllerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\FederatedFileSharing\Tests;
  29. use OCA\FederatedFileSharing\Controller\RequestHandlerController;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\EventDispatcher\IEventDispatcher;
  32. use OCP\Federation\ICloudFederationFactory;
  33. use OCP\Federation\ICloudFederationProvider;
  34. use OCP\Federation\ICloudFederationProviderManager;
  35. use OCP\Federation\ICloudFederationShare;
  36. use OCP\Federation\ICloudIdManager;
  37. use OCP\IDBConnection;
  38. use OCP\IRequest;
  39. use OCP\IUserManager;
  40. use OCP\Share;
  41. use OCP\Share\IShare;
  42. use Psr\Log\LoggerInterface;
  43. /**
  44. * Class RequestHandlerTest
  45. *
  46. * @package OCA\FederatedFileSharing\Tests
  47. * @group DB
  48. */
  49. class RequestHandlerControllerTest extends \Test\TestCase {
  50. private $owner = 'owner';
  51. private $user1 = 'user1';
  52. private $user2 = 'user2';
  53. private $ownerCloudId = 'owner@server0.org';
  54. private $user1CloudId = 'user1@server1.org';
  55. private $user2CloudId = 'user2@server2.org';
  56. /** @var RequestHandlerController */
  57. private $requestHandler;
  58. /** @var \OCA\FederatedFileSharing\FederatedShareProvider|\PHPUnit\Framework\MockObject\MockObject */
  59. private $federatedShareProvider;
  60. /** @var \OCA\FederatedFileSharing\Notifications|\PHPUnit\Framework\MockObject\MockObject */
  61. private $notifications;
  62. /** @var \OCA\FederatedFileSharing\AddressHandler|\PHPUnit\Framework\MockObject\MockObject */
  63. private $addressHandler;
  64. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  65. private $userManager;
  66. /** @var IShare|\PHPUnit\Framework\MockObject\MockObject */
  67. private $share;
  68. /** @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject */
  69. private $cloudIdManager;
  70. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  71. private $logger;
  72. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  73. private $request;
  74. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  75. private $connection;
  76. /** @var Share\IManager|\PHPUnit\Framework\MockObject\MockObject */
  77. private $shareManager;
  78. /** @var ICloudFederationFactory|\PHPUnit\Framework\MockObject\MockObject */
  79. private $cloudFederationFactory;
  80. /** @var ICloudFederationProviderManager|\PHPUnit\Framework\MockObject\MockObject */
  81. private $cloudFederationProviderManager;
  82. /** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject */
  83. private $cloudFederationProvider;
  84. /** @var ICloudFederationShare|\PHPUnit\Framework\MockObject\MockObject */
  85. private $cloudFederationShare;
  86. /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
  87. private $eventDispatcher;
  88. protected function setUp(): void {
  89. $this->share = $this->getMockBuilder(IShare::class)->getMock();
  90. $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  91. ->disableOriginalConstructor()->getMock();
  92. $this->federatedShareProvider->expects($this->any())
  93. ->method('isOutgoingServer2serverShareEnabled')->willReturn(true);
  94. $this->federatedShareProvider->expects($this->any())
  95. ->method('isIncomingServer2serverShareEnabled')->willReturn(true);
  96. $this->federatedShareProvider->expects($this->any())->method('getShareById')
  97. ->willReturn($this->share);
  98. $this->notifications = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  99. ->disableOriginalConstructor()->getMock();
  100. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  101. ->disableOriginalConstructor()->getMock();
  102. $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
  103. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  104. $this->request = $this->createMock(IRequest::class);
  105. $this->connection = $this->createMock(IDBConnection::class);
  106. $this->shareManager = $this->createMock(Share\IManager::class);
  107. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  108. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  109. $this->cloudFederationProvider = $this->createMock(ICloudFederationProvider::class);
  110. $this->cloudFederationShare = $this->createMock(ICloudFederationShare::class);
  111. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  112. $this->eventDispatcher->expects($this->any())->method('dispatchTyped');
  113. $this->logger = $this->createMock(LoggerInterface::class);
  114. $this->requestHandler = new RequestHandlerController(
  115. 'federatedfilesharing',
  116. $this->request,
  117. $this->federatedShareProvider,
  118. $this->connection,
  119. $this->shareManager,
  120. $this->notifications,
  121. $this->addressHandler,
  122. $this->userManager,
  123. $this->cloudIdManager,
  124. $this->logger,
  125. $this->cloudFederationFactory,
  126. $this->cloudFederationProviderManager,
  127. $this->eventDispatcher
  128. );
  129. }
  130. public function testCreateShare() {
  131. $this->cloudFederationFactory->expects($this->once())->method('getCloudFederationShare')
  132. ->with(
  133. $this->user2,
  134. 'name',
  135. '',
  136. 1,
  137. $this->ownerCloudId,
  138. $this->owner,
  139. $this->user1CloudId,
  140. $this->user1,
  141. 'token',
  142. 'user',
  143. 'file'
  144. )->willReturn($this->cloudFederationShare);
  145. /** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject $provider */
  146. $this->cloudFederationProviderManager->expects($this->once())
  147. ->method('getCloudFederationProvider')
  148. ->with('file')
  149. ->willReturn($this->cloudFederationProvider);
  150. $this->cloudFederationProvider->expects($this->once())->method('shareReceived')
  151. ->with($this->cloudFederationShare);
  152. $result = $this->requestHandler->createShare('localhost', 'token', 'name', $this->owner, $this->user1, $this->user2, 1, $this->user1CloudId, $this->ownerCloudId);
  153. $this->assertInstanceOf(DataResponse::class, $result);
  154. }
  155. public function testDeclineShare() {
  156. $id = 42;
  157. $notification = [
  158. 'sharedSecret' => 'token',
  159. 'message' => 'Recipient declined the share'
  160. ];
  161. $this->cloudFederationProviderManager->expects($this->once())
  162. ->method('getCloudFederationProvider')
  163. ->with('file')
  164. ->willReturn($this->cloudFederationProvider);
  165. $this->cloudFederationProvider->expects($this->once())
  166. ->method('notificationReceived')
  167. ->with('SHARE_DECLINED', $id, $notification);
  168. $result = $this->requestHandler->declineShare($id, 'token');
  169. $this->assertInstanceOf(DataResponse::class, $result);
  170. }
  171. public function testAcceptShare() {
  172. $id = 42;
  173. $notification = [
  174. 'sharedSecret' => 'token',
  175. 'message' => 'Recipient accept the share'
  176. ];
  177. $this->cloudFederationProviderManager->expects($this->once())
  178. ->method('getCloudFederationProvider')
  179. ->with('file')
  180. ->willReturn($this->cloudFederationProvider);
  181. $this->cloudFederationProvider->expects($this->once())
  182. ->method('notificationReceived')
  183. ->with('SHARE_ACCEPTED', $id, $notification);
  184. $result = $this->requestHandler->acceptShare($id, 'token');
  185. $this->assertInstanceOf(DataResponse::class, $result);
  186. }
  187. }