MountPublicLinkControllerTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Björn Schießle <bjoern@schiessle.org>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\Controller;
  30. use OC\Federation\CloudIdManager;
  31. use OCA\FederatedFileSharing\AddressHandler;
  32. use OCA\FederatedFileSharing\Controller\MountPublicLinkController;
  33. use OCA\FederatedFileSharing\FederatedShareProvider;
  34. use OCP\AppFramework\Http;
  35. use OCP\Contacts\IManager as IContactsManager;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\Federation\ICloudIdManager;
  38. use OCP\Files\IRootFolder;
  39. use OCP\HintException;
  40. use OCP\Http\Client\IClientService;
  41. use OCP\ICacheFactory;
  42. use OCP\IL10N;
  43. use OCP\IRequest;
  44. use OCP\ISession;
  45. use OCP\IURLGenerator;
  46. use OCP\IUserManager;
  47. use OCP\IUserSession;
  48. use OCP\Share\IManager;
  49. use OCP\Share\IShare;
  50. class MountPublicLinkControllerTest extends \Test\TestCase {
  51. /** @var IContactsManager|\PHPUnit\Framework\MockObject\MockObject */
  52. protected $contactsManager;
  53. /** @var MountPublicLinkController */
  54. private $controller;
  55. /** @var \OCP\IRequest | \PHPUnit\Framework\MockObject\MockObject */
  56. private $request;
  57. /** @var FederatedShareProvider | \PHPUnit\Framework\MockObject\MockObject */
  58. private $federatedShareProvider;
  59. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  60. private $shareManager;
  61. /** @var AddressHandler | \PHPUnit\Framework\MockObject\MockObject */
  62. private $addressHandler;
  63. /** @var IRootFolder | \PHPUnit\Framework\MockObject\MockObject */
  64. private $rootFolder;
  65. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
  66. private $userManager;
  67. /** @var ISession | \PHPUnit\Framework\MockObject\MockObject */
  68. private $session;
  69. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  70. private $l10n;
  71. /** @var IUserSession | \PHPUnit\Framework\MockObject\MockObject */
  72. private $userSession;
  73. /** @var IClientService | \PHPUnit\Framework\MockObject\MockObject */
  74. private $clientService;
  75. /** @var IShare */
  76. private $share;
  77. /** @var ICloudIdManager */
  78. private $cloudIdManager;
  79. protected function setUp(): void {
  80. parent::setUp();
  81. $this->request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
  82. $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  83. ->disableOriginalConstructor()->getMock();
  84. $this->shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  85. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  86. ->disableOriginalConstructor()->getMock();
  87. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock();
  88. $this->userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  89. $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  90. $this->session = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
  91. $this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
  92. $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
  93. $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
  94. $this->contactsManager = $this->createMock(IContactsManager::class);
  95. $this->cloudIdManager = new CloudIdManager(
  96. $this->contactsManager,
  97. $this->createMock(IURLGenerator::class),
  98. $this->userManager,
  99. $this->createMock(ICacheFactory::class),
  100. $this->createMock(IEventDispatcher::class)
  101. );
  102. $this->controller = new MountPublicLinkController(
  103. 'federatedfilesharing', $this->request,
  104. $this->federatedShareProvider,
  105. $this->shareManager,
  106. $this->addressHandler,
  107. $this->session,
  108. $this->l10n,
  109. $this->userSession,
  110. $this->clientService,
  111. $this->cloudIdManager
  112. );
  113. }
  114. /**
  115. * @dataProvider dataTestCreateFederatedShare
  116. *
  117. * @param string $shareWith
  118. * @param bool $outgoingSharesAllowed
  119. * @param bool $validShareWith
  120. * @param string $token
  121. * @param bool $validToken
  122. * @param bool $createSuccessful
  123. * @param string $expectedReturnData
  124. */
  125. public function testCreateFederatedShare($shareWith,
  126. $outgoingSharesAllowed,
  127. $validShareWith,
  128. $token,
  129. $validToken,
  130. $createSuccessful,
  131. $expectedReturnData,
  132. $permissions
  133. ) {
  134. $this->federatedShareProvider->expects($this->any())
  135. ->method('isOutgoingServer2serverShareEnabled')
  136. ->willReturn($outgoingSharesAllowed);
  137. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  138. ->with($shareWith)
  139. ->willReturnCallback(
  140. function ($shareWith) use ($validShareWith, $expectedReturnData) {
  141. if ($validShareWith) {
  142. return ['user', 'server'];
  143. }
  144. throw new HintException($expectedReturnData, $expectedReturnData);
  145. }
  146. );
  147. $share = $this->share;
  148. $share->setPermissions($permissions);
  149. $this->shareManager->expects($this->any())->method('getShareByToken')
  150. ->with($token)
  151. ->willReturnCallback(
  152. function ($token) use ($validToken, $share, $expectedReturnData) {
  153. if ($validToken) {
  154. return $share;
  155. }
  156. throw new HintException($expectedReturnData, $expectedReturnData);
  157. }
  158. );
  159. $this->federatedShareProvider->expects($this->any())->method('create')
  160. ->with($share)
  161. ->willReturnCallback(
  162. function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) {
  163. $this->assertEquals($shareWith, $share->getSharedWith());
  164. if ($createSuccessful) {
  165. return $share;
  166. }
  167. throw new HintException($expectedReturnData, $expectedReturnData);
  168. }
  169. );
  170. $result = $this->controller->createFederatedShare($shareWith, $token);
  171. $errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed;
  172. if ($errorCase) {
  173. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  174. $this->assertTrue(isset($result->getData()['message']));
  175. $this->assertSame($expectedReturnData, $result->getData()['message']);
  176. } else {
  177. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  178. $this->assertTrue(isset($result->getData()['remoteUrl']));
  179. $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']);
  180. }
  181. }
  182. public function dataTestCreateFederatedShare() {
  183. return [
  184. //shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData
  185. ['user@server', true, true, 'token', true, true, 'server', 31],
  186. ['user@server', true, true, 'token', false, false, 'server', 4],
  187. ['user@server', true, false, 'token', true, true, 'invalid federated cloud id', 31],
  188. ['user@server', true, false, 'token', false, true, 'invalid federated cloud id', 31],
  189. ['user@server', true, false, 'token', false, false, 'invalid federated cloud id', 31],
  190. ['user@server', true, false, 'token', true, false, 'invalid federated cloud id', 31],
  191. ['user@server', true, true, 'token', false, true, 'invalid token', 31],
  192. ['user@server', true, true, 'token', false, false, 'invalid token', 31],
  193. ['user@server', true, true, 'token', true, false, 'can not create share', 31],
  194. ['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares', 31],
  195. ];
  196. }
  197. }