RequestHandlerControllerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\Federation\ICloudFederationFactory;
  32. use OCP\Federation\ICloudFederationProvider;
  33. use OCP\Federation\ICloudFederationProviderManager;
  34. use OCP\Federation\ICloudFederationShare;
  35. use OCP\Federation\ICloudIdManager;
  36. use OCP\EventDispatcher\IEventDispatcher;
  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. // simulate a post request
  132. $_POST['remote'] = 'localhost';
  133. $_POST['token'] = 'token';
  134. $_POST['name'] = 'name';
  135. $_POST['owner'] = $this->owner;
  136. $_POST['sharedBy'] = $this->user1;
  137. $_POST['shareWith'] = $this->user2;
  138. $_POST['remoteId'] = 1;
  139. $_POST['sharedByFederatedId'] = $this->user1CloudId;
  140. $_POST['ownerFederatedId'] = $this->ownerCloudId;
  141. $this->cloudFederationFactory->expects($this->once())->method('getCloudFederationShare')
  142. ->with(
  143. $this->user2,
  144. 'name',
  145. '',
  146. 1,
  147. $this->ownerCloudId,
  148. $this->owner,
  149. $this->user1CloudId,
  150. $this->user1,
  151. 'token',
  152. 'user',
  153. 'file'
  154. )->willReturn($this->cloudFederationShare);
  155. /** @var ICloudFederationProvider|\PHPUnit\Framework\MockObject\MockObject $provider */
  156. $this->cloudFederationProviderManager->expects($this->once())
  157. ->method('getCloudFederationProvider')
  158. ->with('file')
  159. ->willReturn($this->cloudFederationProvider);
  160. $this->cloudFederationProvider->expects($this->once())->method('shareReceived')
  161. ->with($this->cloudFederationShare);
  162. $result = $this->requestHandler->createShare();
  163. $this->assertInstanceOf(DataResponse::class, $result);
  164. }
  165. public function testDeclineShare() {
  166. $id = 42;
  167. $_POST['token'] = 'token';
  168. $notification = [
  169. 'sharedSecret' => 'token',
  170. 'message' => 'Recipient declined the share'
  171. ];
  172. $this->cloudFederationProviderManager->expects($this->once())
  173. ->method('getCloudFederationProvider')
  174. ->with('file')
  175. ->willReturn($this->cloudFederationProvider);
  176. $this->cloudFederationProvider->expects($this->once())
  177. ->method('notificationReceived')
  178. ->with('SHARE_DECLINED', $id, $notification);
  179. $result = $this->requestHandler->declineShare($id);
  180. $this->assertInstanceOf(DataResponse::class, $result);
  181. }
  182. public function testAcceptShare() {
  183. $id = 42;
  184. $_POST['token'] = 'token';
  185. $notification = [
  186. 'sharedSecret' => 'token',
  187. 'message' => 'Recipient accept the share'
  188. ];
  189. $this->cloudFederationProviderManager->expects($this->once())
  190. ->method('getCloudFederationProvider')
  191. ->with('file')
  192. ->willReturn($this->cloudFederationProvider);
  193. $this->cloudFederationProvider->expects($this->once())
  194. ->method('notificationReceived')
  195. ->with('SHARE_ACCEPTED', $id, $notification);
  196. $result = $this->requestHandler->acceptShare($id);
  197. $this->assertInstanceOf(DataResponse::class, $result);
  198. }
  199. }