ShareesAPIControllerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\Files_Sharing\Tests\Controller;
  32. use OCA\Files_Sharing\Controller\ShareesAPIController;
  33. use OCA\Files_Sharing\Tests\TestCase;
  34. use OCP\AppFramework\Http;
  35. use OCP\AppFramework\OCS\OCSBadRequestException;
  36. use OCP\Collaboration\Collaborators\ISearch;
  37. use OCP\IConfig;
  38. use OCP\IRequest;
  39. use OCP\IURLGenerator;
  40. use OCP\Share\IShare;
  41. use OCP\Share\IManager;
  42. /**
  43. * Class ShareesTest
  44. *
  45. * @group DB
  46. *
  47. * @package OCA\Files_Sharing\Tests\API
  48. */
  49. class ShareesAPIControllerTest extends TestCase {
  50. /** @var ShareesAPIController */
  51. protected $sharees;
  52. /** @var string */
  53. protected $uid;
  54. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  55. protected $request;
  56. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  57. protected $shareManager;
  58. /** @var ISearch|\PHPUnit\Framework\MockObject\MockObject */
  59. protected $collaboratorSearch;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->uid = 'test123';
  63. $this->request = $this->createMock(IRequest::class);
  64. $this->shareManager = $this->createMock(IManager::class);
  65. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $configMock */
  66. $configMock = $this->createMock(IConfig::class);
  67. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject $urlGeneratorMock */
  68. $urlGeneratorMock = $this->createMock(IURLGenerator::class);
  69. $this->collaboratorSearch = $this->createMock(ISearch::class);
  70. $this->sharees = new ShareesAPIController(
  71. $this->uid,
  72. 'files_sharing',
  73. $this->request,
  74. $configMock,
  75. $urlGeneratorMock,
  76. $this->shareManager,
  77. $this->collaboratorSearch
  78. );
  79. }
  80. public function dataSearch() {
  81. $noRemote = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_EMAIL];
  82. $allTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_EMAIL];
  83. return [
  84. [[], '', 'yes', true, true, true, $noRemote, false, true, true],
  85. // Test itemType
  86. [[
  87. 'search' => '',
  88. ], '', 'yes', true, true, true, $noRemote, false, true, true],
  89. [[
  90. 'search' => 'foobar',
  91. ], '', 'yes', true, true, true, $noRemote, false, true, true],
  92. [[
  93. 'search' => 0,
  94. ], '', 'yes', true, true, true, $noRemote, false, true, true],
  95. // Test itemType
  96. [[
  97. 'itemType' => '',
  98. ], '', 'yes', true, true, true, $noRemote, false, true, true],
  99. [[
  100. 'itemType' => 'folder',
  101. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  102. [[
  103. 'itemType' => 0,
  104. ], '', 'yes', true, true , true, $noRemote, false, true, true],
  105. // Test shareType
  106. [[
  107. 'itemType' => 'call',
  108. ], '', 'yes', true, true, true, $noRemote, false, true, true],
  109. [[
  110. 'itemType' => 'folder',
  111. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  112. [[
  113. 'itemType' => 'folder',
  114. 'shareType' => 0,
  115. ], '', 'yes', true, true, false, [0], false, true, true],
  116. [[
  117. 'itemType' => 'folder',
  118. 'shareType' => '0',
  119. ], '', 'yes', true, true, false, [0], false, true, true],
  120. [[
  121. 'itemType' => 'folder',
  122. 'shareType' => 1,
  123. ], '', 'yes', true, true, false, [1], false, true, true],
  124. [[
  125. 'itemType' => 'folder',
  126. 'shareType' => 12,
  127. ], '', 'yes', true, true, false, [], false, true, true],
  128. [[
  129. 'itemType' => 'folder',
  130. 'shareType' => 'foobar',
  131. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  132. [[
  133. 'itemType' => 'folder',
  134. 'shareType' => [0, 1, 2],
  135. ], '', 'yes', false, false, false, [0, 1], false, true, true],
  136. [[
  137. 'itemType' => 'folder',
  138. 'shareType' => [0, 1],
  139. ], '', 'yes', false, false, false, [0, 1], false, true, true],
  140. [[
  141. 'itemType' => 'folder',
  142. 'shareType' => $allTypes,
  143. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  144. [[
  145. 'itemType' => 'folder',
  146. 'shareType' => $allTypes,
  147. ], '', 'yes', false, false, false, [0, 1], false, true, true],
  148. [[
  149. 'itemType' => 'folder',
  150. 'shareType' => $allTypes,
  151. ], '', 'yes', true, false, false, [0, 6], false, true, false],
  152. [[
  153. 'itemType' => 'folder',
  154. 'shareType' => $allTypes,
  155. ], '', 'yes', false, false, true, [0, 4], false, true, false],
  156. [[
  157. 'itemType' => 'folder',
  158. 'shareType' => $allTypes,
  159. ], '', 'yes', true, true, false, [0, 6, 9], false, true, false],
  160. // Test pagination
  161. [[
  162. 'itemType' => 'folder',
  163. 'page' => 1,
  164. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  165. [[
  166. 'itemType' => 'folder',
  167. 'page' => 10,
  168. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  169. // Test perPage
  170. [[
  171. 'itemType' => 'folder',
  172. 'perPage' => 1,
  173. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  174. [[
  175. 'itemType' => 'folder',
  176. 'perPage' => 10,
  177. ], '', 'yes', true, true, true, $allTypes, false, true, true],
  178. // Test $shareWithGroupOnly setting
  179. [[
  180. 'itemType' => 'folder',
  181. ], 'no', 'yes', true, true, true, $allTypes, false, true, true],
  182. [[
  183. 'itemType' => 'folder',
  184. ], 'yes', 'yes', true, true, true, $allTypes, true, true, true],
  185. // Test $shareeEnumeration setting
  186. [[
  187. 'itemType' => 'folder',
  188. ], 'no', 'yes', true, true, true, $allTypes, false, true, true],
  189. [[
  190. 'itemType' => 'folder',
  191. ], 'no', 'no', true, true, true, $allTypes, false, false, true],
  192. ];
  193. }
  194. /**
  195. * @dataProvider dataSearch
  196. *
  197. * @param array $getData
  198. * @param string $apiSetting
  199. * @param string $enumSetting
  200. * @param bool $remoteSharingEnabled
  201. * @param bool $isRemoteGroupSharingEnabled
  202. * @param bool $emailSharingEnabled
  203. * @param array $shareTypes
  204. * @param bool $shareWithGroupOnly
  205. * @param bool $shareeEnumeration
  206. * @param bool $allowGroupSharing
  207. * @throws OCSBadRequestException
  208. */
  209. public function testSearch($getData, $apiSetting, $enumSetting, $remoteSharingEnabled, $isRemoteGroupSharingEnabled, $emailSharingEnabled, $shareTypes, $shareWithGroupOnly, $shareeEnumeration, $allowGroupSharing) {
  210. $search = isset($getData['search']) ? $getData['search'] : '';
  211. $itemType = isset($getData['itemType']) ? $getData['itemType'] : 'irrelevant';
  212. $page = isset($getData['page']) ? $getData['page'] : 1;
  213. $perPage = isset($getData['perPage']) ? $getData['perPage'] : 200;
  214. $shareType = isset($getData['shareType']) ? $getData['shareType'] : null;
  215. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */
  216. $config = $this->createMock(IConfig::class);
  217. $config->expects($this->exactly(1))
  218. ->method('getAppValue')
  219. ->with($this->anything(), $this->anything(), $this->anything())
  220. ->willReturnMap([
  221. ['files_sharing', 'lookupServerEnabled', 'yes', 'yes'],
  222. ]);
  223. $this->shareManager->expects($itemType === 'file' || $itemType === 'folder' ? $this->once() : $this->never())
  224. ->method('allowGroupSharing')
  225. ->willReturn($allowGroupSharing);
  226. /** @var string */
  227. $uid = 'test123';
  228. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  229. $request = $this->createMock(IRequest::class);
  230. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject $urlGenerator */
  231. $urlGenerator = $this->createMock(IURLGenerator::class);
  232. /** @var \PHPUnit\Framework\MockObject\MockObject|\OCA\Files_Sharing\Controller\ShareesAPIController $sharees */
  233. $sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController')
  234. ->setConstructorArgs([
  235. $uid,
  236. 'files_sharing',
  237. $request,
  238. $config,
  239. $urlGenerator,
  240. $this->shareManager,
  241. $this->collaboratorSearch
  242. ])
  243. ->setMethods(['isRemoteSharingAllowed', 'shareProviderExists', 'isRemoteGroupSharingAllowed'])
  244. ->getMock();
  245. $expectedShareTypes = $shareTypes;
  246. sort($expectedShareTypes);
  247. $this->collaboratorSearch->expects($this->once())
  248. ->method('search')
  249. ->with($search, $expectedShareTypes, $this->anything(), $perPage, $perPage * ($page -1))
  250. ->willReturn([[], false]);
  251. $sharees->expects($this->any())
  252. ->method('isRemoteSharingAllowed')
  253. ->with($itemType)
  254. ->willReturn($remoteSharingEnabled);
  255. $sharees->expects($this->any())
  256. ->method('isRemoteGroupSharingAllowed')
  257. ->with($itemType)
  258. ->willReturn($isRemoteGroupSharingEnabled);
  259. $this->shareManager->expects($this->any())
  260. ->method('shareProviderExists')
  261. ->willReturnCallback(function ($shareType) use ($emailSharingEnabled) {
  262. if ($shareType === IShare::TYPE_EMAIL) {
  263. return $emailSharingEnabled;
  264. } else {
  265. return false;
  266. }
  267. });
  268. $this->assertInstanceOf(Http\DataResponse::class, $sharees->search($search, $itemType, $page, $perPage, $shareType));
  269. }
  270. public function dataSearchInvalid() {
  271. return [
  272. // Test invalid pagination
  273. [[
  274. 'page' => 0,
  275. ], 'Invalid page'],
  276. [[
  277. 'page' => '0',
  278. ], 'Invalid page'],
  279. [[
  280. 'page' => -1,
  281. ], 'Invalid page'],
  282. // Test invalid perPage
  283. [[
  284. 'perPage' => 0,
  285. ], 'Invalid perPage argument'],
  286. [[
  287. 'perPage' => '0',
  288. ], 'Invalid perPage argument'],
  289. [[
  290. 'perPage' => -1,
  291. ], 'Invalid perPage argument'],
  292. ];
  293. }
  294. /**
  295. * @dataProvider dataSearchInvalid
  296. *
  297. * @param array $getData
  298. * @param string $message
  299. */
  300. public function testSearchInvalid($getData, $message) {
  301. $page = isset($getData['page']) ? $getData['page'] : 1;
  302. $perPage = isset($getData['perPage']) ? $getData['perPage'] : 200;
  303. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $config */
  304. $config = $this->createMock(IConfig::class);
  305. $config->expects($this->never())
  306. ->method('getAppValue');
  307. /** @var string */
  308. $uid = 'test123';
  309. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  310. $request = $this->createMock(IRequest::class);
  311. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject $urlGenerator */
  312. $urlGenerator = $this->createMock(IURLGenerator::class);
  313. /** @var \PHPUnit\Framework\MockObject\MockObject|\OCA\Files_Sharing\Controller\ShareesAPIController $sharees */
  314. $sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController')
  315. ->setConstructorArgs([
  316. $uid,
  317. 'files_sharing',
  318. $request,
  319. $config,
  320. $urlGenerator,
  321. $this->shareManager,
  322. $this->collaboratorSearch
  323. ])
  324. ->setMethods(['isRemoteSharingAllowed'])
  325. ->getMock();
  326. $sharees->expects($this->never())
  327. ->method('isRemoteSharingAllowed');
  328. $this->collaboratorSearch->expects($this->never())
  329. ->method('search');
  330. try {
  331. $sharees->search('', null, $page, $perPage, null);
  332. $this->fail();
  333. } catch (OCSBadRequestException $e) {
  334. $this->assertEquals($message, $e->getMessage());
  335. }
  336. }
  337. public function dataIsRemoteSharingAllowed() {
  338. return [
  339. ['file', true],
  340. ['folder', true],
  341. ['', false],
  342. ['contacts', false],
  343. ];
  344. }
  345. /**
  346. * @dataProvider dataIsRemoteSharingAllowed
  347. *
  348. * @param string $itemType
  349. * @param bool $expected
  350. */
  351. public function testIsRemoteSharingAllowed($itemType, $expected) {
  352. $this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType]));
  353. }
  354. public function testSearchNoItemType() {
  355. $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
  356. $this->expectExceptionMessage('Missing itemType');
  357. $this->sharees->search('', null, 1, 10, [], false);
  358. }
  359. public function dataGetPaginationLink() {
  360. return [
  361. [1, '/ocs/v1.php', ['perPage' => 2], '<?perPage=2&page=2>; rel="next"'],
  362. [10, '/ocs/v2.php', ['perPage' => 2], '<?perPage=2&page=11>; rel="next"'],
  363. ];
  364. }
  365. /**
  366. * @dataProvider dataGetPaginationLink
  367. *
  368. * @param int $page
  369. * @param string $scriptName
  370. * @param array $params
  371. * @param array $expected
  372. */
  373. public function testGetPaginationLink($page, $scriptName, $params, $expected) {
  374. $this->request->expects($this->once())
  375. ->method('getScriptName')
  376. ->willReturn($scriptName);
  377. $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'getPaginationLink', [$page, $params]));
  378. }
  379. public function dataIsV2() {
  380. return [
  381. ['/ocs/v1.php', false],
  382. ['/ocs/v2.php', true],
  383. ];
  384. }
  385. /**
  386. * @dataProvider dataIsV2
  387. *
  388. * @param string $scriptName
  389. * @param bool $expected
  390. */
  391. public function testIsV2($scriptName, $expected) {
  392. $this->request->expects($this->once())
  393. ->method('getScriptName')
  394. ->willReturn($scriptName);
  395. $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'isV2'));
  396. }
  397. }