1
0

MountPublicLinkControllerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. use PHPUnit\Framework\MockObject\MockObject;
  51. use Psr\Log\LoggerInterface;
  52. class MountPublicLinkControllerTest extends \Test\TestCase {
  53. /** @var IContactsManager|MockObject */
  54. protected $contactsManager;
  55. /** @var MountPublicLinkController */
  56. private $controller;
  57. /** @var IRequest|MockObject */
  58. private $request;
  59. /** @var FederatedShareProvider|MockObject */
  60. private $federatedShareProvider;
  61. /** @var IManager|MockObject */
  62. private $shareManager;
  63. /** @var AddressHandler|MockObject */
  64. private $addressHandler;
  65. /** @var IRootFolder|MockObject */
  66. private $rootFolder;
  67. /** @var IUserManager|MockObject */
  68. private $userManager;
  69. /** @var ISession|MockObject */
  70. private $session;
  71. /** @var IL10N|MockObject */
  72. private $l10n;
  73. /** @var IUserSession|MockObject */
  74. private $userSession;
  75. /** @var IClientService|MockObject */
  76. private $clientService;
  77. /** @var IShare */
  78. private $share;
  79. /** @var ICloudIdManager */
  80. private $cloudIdManager;
  81. protected function setUp(): void {
  82. parent::setUp();
  83. $this->request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
  84. $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  85. ->disableOriginalConstructor()->getMock();
  86. $this->shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  87. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  88. ->disableOriginalConstructor()->getMock();
  89. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock();
  90. $this->userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  91. $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  92. $this->session = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
  93. $this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
  94. $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
  95. $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
  96. $this->contactsManager = $this->createMock(IContactsManager::class);
  97. $this->cloudIdManager = new CloudIdManager(
  98. $this->contactsManager,
  99. $this->createMock(IURLGenerator::class),
  100. $this->userManager,
  101. $this->createMock(ICacheFactory::class),
  102. $this->createMock(IEventDispatcher::class)
  103. );
  104. $this->controller = new MountPublicLinkController(
  105. 'federatedfilesharing', $this->request,
  106. $this->federatedShareProvider,
  107. $this->shareManager,
  108. $this->addressHandler,
  109. $this->session,
  110. $this->l10n,
  111. $this->userSession,
  112. $this->clientService,
  113. $this->cloudIdManager,
  114. $this->createMock(LoggerInterface::class),
  115. );
  116. }
  117. /**
  118. * @dataProvider dataTestCreateFederatedShare
  119. *
  120. * @param string $shareWith
  121. * @param bool $outgoingSharesAllowed
  122. * @param bool $validShareWith
  123. * @param string $token
  124. * @param bool $validToken
  125. * @param bool $createSuccessful
  126. * @param string $expectedReturnData
  127. */
  128. public function testCreateFederatedShare($shareWith,
  129. $outgoingSharesAllowed,
  130. $validShareWith,
  131. $token,
  132. $validToken,
  133. $createSuccessful,
  134. $expectedReturnData,
  135. $permissions
  136. ) {
  137. $this->federatedShareProvider->expects($this->any())
  138. ->method('isOutgoingServer2serverShareEnabled')
  139. ->willReturn($outgoingSharesAllowed);
  140. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  141. ->with($shareWith)
  142. ->willReturnCallback(
  143. function ($shareWith) use ($validShareWith, $expectedReturnData) {
  144. if ($validShareWith) {
  145. return ['user', 'server'];
  146. }
  147. throw new HintException($expectedReturnData, $expectedReturnData);
  148. }
  149. );
  150. $share = $this->share;
  151. $share->setPermissions($permissions);
  152. $this->shareManager->expects($this->any())->method('getShareByToken')
  153. ->with($token)
  154. ->willReturnCallback(
  155. function ($token) use ($validToken, $share, $expectedReturnData) {
  156. if ($validToken) {
  157. return $share;
  158. }
  159. throw new HintException($expectedReturnData, $expectedReturnData);
  160. }
  161. );
  162. $this->federatedShareProvider->expects($this->any())->method('create')
  163. ->with($share)
  164. ->willReturnCallback(
  165. function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) {
  166. $this->assertEquals($shareWith, $share->getSharedWith());
  167. if ($createSuccessful) {
  168. return $share;
  169. }
  170. throw new HintException($expectedReturnData, $expectedReturnData);
  171. }
  172. );
  173. $result = $this->controller->createFederatedShare($shareWith, $token);
  174. $errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed;
  175. if ($errorCase) {
  176. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  177. $this->assertTrue(isset($result->getData()['message']));
  178. $this->assertSame($expectedReturnData, $result->getData()['message']);
  179. } else {
  180. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  181. $this->assertTrue(isset($result->getData()['remoteUrl']));
  182. $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']);
  183. }
  184. }
  185. public function dataTestCreateFederatedShare() {
  186. return [
  187. //shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData
  188. ['user@server', true, true, 'token', true, true, 'server', 31],
  189. ['user@server', true, true, 'token', false, false, 'server', 4],
  190. ['user@server', true, false, 'token', true, true, 'invalid federated cloud id', 31],
  191. ['user@server', true, false, 'token', false, true, 'invalid federated cloud id', 31],
  192. ['user@server', true, false, 'token', false, false, 'invalid federated cloud id', 31],
  193. ['user@server', true, false, 'token', true, false, 'invalid federated cloud id', 31],
  194. ['user@server', true, true, 'token', false, true, 'invalid token', 31],
  195. ['user@server', true, true, 'token', false, false, 'invalid token', 31],
  196. ['user@server', true, true, 'token', true, false, 'can not create share', 31],
  197. ['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares', 31],
  198. ];
  199. }
  200. }