RequestHandlerControllerTest.php 8.1 KB

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