1
0

MountPublicLinkControllerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\FederatedFileSharing\Tests\Controller;
  27. use OC\Federation\CloudIdManager;
  28. use OC\HintException;
  29. use OCA\FederatedFileSharing\AddressHandler;
  30. use OCA\FederatedFileSharing\Controller\MountPublicLinkController;
  31. use OCA\FederatedFileSharing\FederatedShareProvider;
  32. use OCP\AppFramework\Http;
  33. use OCP\Federation\ICloudIdManager;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Http\Client\IClientService;
  36. use OCP\IL10N;
  37. use OCP\IRequest;
  38. use OCP\ISession;
  39. use OCP\IUserManager;
  40. use OCP\IUserSession;
  41. use OCP\Share\IManager;
  42. use OCP\Share\IShare;
  43. class MountPublicLinkControllerTest extends \Test\TestCase {
  44. /** @var MountPublicLinkController */
  45. private $controller;
  46. /** @var \OCP\IRequest | \PHPUnit_Framework_MockObject_MockObject */
  47. private $request;
  48. /** @var FederatedShareProvider | \PHPUnit_Framework_MockObject_MockObject */
  49. private $federatedShareProvider;
  50. /** @var IManager | \PHPUnit_Framework_MockObject_MockObject */
  51. private $shareManager;
  52. /** @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */
  53. private $addressHandler;
  54. /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
  55. private $rootFolder;
  56. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
  57. private $userManager;
  58. /** @var ISession | \PHPUnit_Framework_MockObject_MockObject */
  59. private $session;
  60. /** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */
  61. private $l10n;
  62. /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
  63. private $userSession;
  64. /** @var IClientService | \PHPUnit_Framework_MockObject_MockObject */
  65. private $clientService;
  66. /** @var IShare */
  67. private $share;
  68. /** @var ICloudIdManager */
  69. private $cloudIdManager;
  70. public function setUp() {
  71. parent::setUp();
  72. $this->request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
  73. $this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
  74. ->disableOriginalConstructor()->getMock();
  75. $this->shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  76. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  77. ->disableOriginalConstructor()->getMock();
  78. $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->disableOriginalConstructor()->getMock();
  79. $this->userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
  80. $this->share = new \OC\Share20\Share($this->rootFolder, $this->userManager);
  81. $this->session = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
  82. $this->l10n = $this->getMockBuilder(IL10N::class)->disableOriginalConstructor()->getMock();
  83. $this->userSession = $this->getMockBuilder(IUserSession::class)->disableOriginalConstructor()->getMock();
  84. $this->clientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->disableOriginalConstructor()->getMock();
  85. $this->cloudIdManager = new CloudIdManager();
  86. $this->controller = new MountPublicLinkController(
  87. 'federatedfilesharing', $this->request,
  88. $this->federatedShareProvider,
  89. $this->shareManager,
  90. $this->addressHandler,
  91. $this->session,
  92. $this->l10n,
  93. $this->userSession,
  94. $this->clientService,
  95. $this->cloudIdManager
  96. );
  97. }
  98. /**
  99. * @dataProvider dataTestCreateFederatedShare
  100. *
  101. * @param string $shareWith
  102. * @param bool $outgoingSharesAllowed
  103. * @param bool $validShareWith
  104. * @param string $token
  105. * @param bool $validToken
  106. * @param bool $createSuccessful
  107. * @param string $expectedReturnData
  108. */
  109. public function testCreateFederatedShare($shareWith,
  110. $outgoingSharesAllowed,
  111. $validShareWith,
  112. $token,
  113. $validToken,
  114. $createSuccessful,
  115. $expectedReturnData
  116. ) {
  117. $this->federatedShareProvider->expects($this->any())
  118. ->method('isOutgoingServer2serverShareEnabled')
  119. ->willReturn($outgoingSharesAllowed);
  120. $this->addressHandler->expects($this->any())->method('splitUserRemote')
  121. ->with($shareWith)
  122. ->willReturnCallback(
  123. function($shareWith) use ($validShareWith, $expectedReturnData) {
  124. if ($validShareWith) {
  125. return ['user', 'server'];
  126. }
  127. throw new HintException($expectedReturnData, $expectedReturnData);
  128. }
  129. );
  130. $share = $this->share;
  131. $this->shareManager->expects($this->any())->method('getShareByToken')
  132. ->with($token)
  133. ->willReturnCallback(
  134. function ($token) use ($validToken, $share, $expectedReturnData) {
  135. if ($validToken) {
  136. return $share;
  137. }
  138. throw new HintException($expectedReturnData, $expectedReturnData);
  139. }
  140. );
  141. $this->federatedShareProvider->expects($this->any())->method('create')
  142. ->with($share)
  143. ->willReturnCallback(
  144. function (IShare $share) use ($createSuccessful, $shareWith, $expectedReturnData) {
  145. $this->assertEquals($shareWith, $share->getSharedWith());
  146. if ($createSuccessful) {
  147. return $share;
  148. }
  149. throw new HintException($expectedReturnData, $expectedReturnData);
  150. }
  151. );
  152. $result = $this->controller->createFederatedShare($shareWith, $token);
  153. $errorCase = !$validShareWith || !$validToken || !$createSuccessful || !$outgoingSharesAllowed;
  154. if ($errorCase) {
  155. $this->assertSame(Http::STATUS_BAD_REQUEST, $result->getStatus());
  156. $this->assertTrue(isset($result->getData()['message']));
  157. $this->assertSame($expectedReturnData, $result->getData()['message']);
  158. } else {
  159. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  160. $this->assertTrue(isset($result->getData()['remoteUrl']));
  161. $this->assertSame($expectedReturnData, $result->getData()['remoteUrl']);
  162. }
  163. }
  164. public function dataTestCreateFederatedShare() {
  165. return [
  166. //shareWith, outgoingSharesAllowed, validShareWith, token, validToken, createSuccessful, expectedReturnData
  167. ['user@server', true, true, 'token', true, true, 'server'],
  168. ['user@server', true, false, 'token', true, true, 'invalid federated cloud id'],
  169. ['user@server', true, false, 'token', false, true, 'invalid federated cloud id'],
  170. ['user@server', true, false, 'token', false, false, 'invalid federated cloud id'],
  171. ['user@server', true, false, 'token', true, false, 'invalid federated cloud id'],
  172. ['user@server', true, true, 'token', false, true, 'invalid token'],
  173. ['user@server', true, true, 'token', false, false, 'invalid token'],
  174. ['user@server', true, true, 'token', true, false, 'can not create share'],
  175. ['user@server', false, true, 'token', true, true, 'This server doesn\'t support outgoing federated shares'],
  176. ];
  177. }
  178. }