ShareesAPIControllerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests\Controller;
  8. use OCA\Files_Sharing\Controller\ShareesAPIController;
  9. use OCA\Files_Sharing\Tests\TestCase;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\OCS\OCSBadRequestException;
  12. use OCP\Collaboration\Collaborators\ISearch;
  13. use OCP\IConfig;
  14. use OCP\IRequest;
  15. use OCP\IURLGenerator;
  16. use OCP\Share\IManager;
  17. use OCP\Share\IShare;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. /**
  20. * Class ShareesTest
  21. *
  22. * @group DB
  23. *
  24. * @package OCA\Files_Sharing\Tests\API
  25. */
  26. class ShareesAPIControllerTest extends TestCase {
  27. /** @var ShareesAPIController */
  28. protected $sharees;
  29. /** @var string */
  30. protected $uid;
  31. /** @var IRequest|MockObject */
  32. protected $request;
  33. /** @var IManager|MockObject */
  34. protected $shareManager;
  35. /** @var ISearch|MockObject */
  36. protected $collaboratorSearch;
  37. /** @var IConfig|MockObject */
  38. protected $config;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->uid = 'test123';
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->shareManager = $this->createMock(IManager::class);
  44. $this->config = $this->createMock(IConfig::class);
  45. /** @var IURLGenerator|MockObject $urlGeneratorMock */
  46. $urlGeneratorMock = $this->createMock(IURLGenerator::class);
  47. $this->collaboratorSearch = $this->createMock(ISearch::class);
  48. $this->sharees = new ShareesAPIController(
  49. 'files_sharing',
  50. $this->request,
  51. $this->uid,
  52. $this->config,
  53. $urlGeneratorMock,
  54. $this->shareManager,
  55. $this->collaboratorSearch
  56. );
  57. }
  58. public function dataSearch(): array {
  59. $noRemote = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_EMAIL];
  60. $allTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_EMAIL];
  61. return [
  62. [[], '', 'yes', false, true, true, true, $noRemote, false, true, true],
  63. // Test itemType
  64. [[
  65. 'search' => '',
  66. ], '', 'yes', false, true, true, true, $noRemote, false, true, true],
  67. [[
  68. 'search' => 'foobar',
  69. ], '', 'yes', false, true, true, true, $noRemote, false, true, true],
  70. [[
  71. 'search' => 0,
  72. ], '', 'yes', false, true, true, true, $noRemote, false, true, true],
  73. // Test itemType
  74. [[
  75. 'itemType' => '',
  76. ], '', 'yes', false, true, true, true, $noRemote, false, true, true],
  77. [[
  78. 'itemType' => 'folder',
  79. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  80. [[
  81. 'itemType' => 0,
  82. ], '', 'yes', false, true, true , true, $noRemote, false, true, true],
  83. // Test shareType
  84. [[
  85. 'itemType' => 'call',
  86. ], '', 'yes', false, true, true, true, $noRemote, false, true, true],
  87. [[
  88. 'itemType' => 'call',
  89. ], '', 'yes', false, true, true, true, [0, 4], false, true, false],
  90. [[
  91. 'itemType' => 'folder',
  92. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  93. [[
  94. 'itemType' => 'folder',
  95. 'shareType' => 0,
  96. ], '', 'yes', false, true, true, false, [0], false, true, true],
  97. [[
  98. 'itemType' => 'folder',
  99. 'shareType' => '0',
  100. ], '', 'yes', false, true, true, false, [0], false, true, true],
  101. [[
  102. 'itemType' => 'folder',
  103. 'shareType' => 1,
  104. ], '', 'yes', false, true, true, false, [1], false, true, true],
  105. [[
  106. 'itemType' => 'folder',
  107. 'shareType' => 12,
  108. ], '', 'yes', false, true, true, false, [], false, true, true],
  109. [[
  110. 'itemType' => 'folder',
  111. 'shareType' => 'foobar',
  112. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  113. [[
  114. 'itemType' => 'folder',
  115. 'shareType' => [0, 1, 2],
  116. ], '', 'yes', false, false, false, false, [0, 1], false, true, true],
  117. [[
  118. 'itemType' => 'folder',
  119. 'shareType' => [0, 1],
  120. ], '', 'yes', false, false, false, false, [0, 1], false, true, true],
  121. [[
  122. 'itemType' => 'folder',
  123. 'shareType' => $allTypes,
  124. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  125. [[
  126. 'itemType' => 'folder',
  127. 'shareType' => $allTypes,
  128. ], '', 'yes', false, false, false, false, [0, 1], false, true, true],
  129. [[
  130. 'itemType' => 'folder',
  131. 'shareType' => $allTypes,
  132. ], '', 'yes', false, true, false, false, [0, 6], false, true, false],
  133. [[
  134. 'itemType' => 'folder',
  135. 'shareType' => $allTypes,
  136. ], '', 'yes', false, false, false, true, [0, 4], false, true, false],
  137. [[
  138. 'itemType' => 'folder',
  139. 'shareType' => $allTypes,
  140. ], '', 'yes', false, true, true, false, [0, 6, 9], false, true, false],
  141. // Test pagination
  142. [[
  143. 'itemType' => 'folder',
  144. 'page' => 1,
  145. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  146. [[
  147. 'itemType' => 'folder',
  148. 'page' => 10,
  149. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  150. // Test perPage
  151. [[
  152. 'itemType' => 'folder',
  153. 'perPage' => 1,
  154. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  155. [[
  156. 'itemType' => 'folder',
  157. 'perPage' => 10,
  158. ], '', 'yes', false, true, true, true, $allTypes, false, true, true],
  159. // Test $shareWithGroupOnly setting
  160. [[
  161. 'itemType' => 'folder',
  162. ], 'no', 'yes', false, true, true, true, $allTypes, false, true, true],
  163. [[
  164. 'itemType' => 'folder',
  165. ], 'yes', 'yes', false, true, true, true, $allTypes, true, true, true],
  166. // Test $shareeEnumeration setting
  167. [[
  168. 'itemType' => 'folder',
  169. ], 'no', 'yes', false, true, true, true, $allTypes, false, true, true],
  170. [[
  171. 'itemType' => 'folder',
  172. ], 'no', 'no', false, true, true, true, $allTypes, false, false, true],
  173. ];
  174. }
  175. /**
  176. * @dataProvider dataSearch
  177. *
  178. * @param array $getData
  179. * @param string $apiSetting
  180. * @param string $enumSetting
  181. * @param bool $remoteSharingEnabled
  182. * @param bool $isRemoteGroupSharingEnabled
  183. * @param bool $emailSharingEnabled
  184. * @param array $shareTypes
  185. * @param bool $shareWithGroupOnly
  186. * @param bool $shareeEnumeration
  187. * @param bool $allowGroupSharing
  188. * @throws OCSBadRequestException
  189. */
  190. public function testSearch(
  191. array $getData,
  192. string $apiSetting,
  193. string $enumSetting,
  194. bool $sharingDisabledForUser,
  195. bool $remoteSharingEnabled,
  196. bool $isRemoteGroupSharingEnabled,
  197. bool $emailSharingEnabled,
  198. array $shareTypes,
  199. bool $shareWithGroupOnly,
  200. bool $shareeEnumeration,
  201. bool $allowGroupSharing,
  202. ): void {
  203. $search = $getData['search'] ?? '';
  204. $itemType = $getData['itemType'] ?? 'irrelevant';
  205. $page = $getData['page'] ?? 1;
  206. $perPage = $getData['perPage'] ?? 200;
  207. $shareType = $getData['shareType'] ?? null;
  208. /** @var IConfig|MockObject $config */
  209. $config = $this->createMock(IConfig::class);
  210. $config->expects($this->exactly(1))
  211. ->method('getAppValue')
  212. ->with($this->anything(), $this->anything(), $this->anything())
  213. ->willReturnMap([
  214. ['files_sharing', 'lookupServerEnabled', 'yes', 'yes'],
  215. ]);
  216. $this->shareManager->expects($this->once())
  217. ->method('allowGroupSharing')
  218. ->willReturn($allowGroupSharing);
  219. /** @var string */
  220. $uid = 'test123';
  221. /** @var IRequest|MockObject $request */
  222. $request = $this->createMock(IRequest::class);
  223. /** @var IURLGenerator|MockObject $urlGenerator */
  224. $urlGenerator = $this->createMock(IURLGenerator::class);
  225. /** @var MockObject|ShareesAPIController $sharees */
  226. $sharees = $this->getMockBuilder(ShareesAPIController::class)
  227. ->setConstructorArgs([
  228. 'files_sharing',
  229. $request,
  230. $uid,
  231. $config,
  232. $urlGenerator,
  233. $this->shareManager,
  234. $this->collaboratorSearch
  235. ])
  236. ->onlyMethods(['isRemoteSharingAllowed', 'isRemoteGroupSharingAllowed'])
  237. ->getMock();
  238. $expectedShareTypes = $shareTypes;
  239. sort($expectedShareTypes);
  240. $this->collaboratorSearch->expects($this->once())
  241. ->method('search')
  242. ->with($search, $expectedShareTypes, $this->anything(), $perPage, $perPage * ($page - 1))
  243. ->willReturn([[], false]);
  244. $sharees->expects($this->any())
  245. ->method('isRemoteSharingAllowed')
  246. ->with($itemType)
  247. ->willReturn($remoteSharingEnabled);
  248. $sharees->expects($this->any())
  249. ->method('isRemoteGroupSharingAllowed')
  250. ->with($itemType)
  251. ->willReturn($isRemoteGroupSharingEnabled);
  252. $this->shareManager->expects($this->any())
  253. ->method('sharingDisabledForUser')
  254. ->with($uid)
  255. ->willReturn($sharingDisabledForUser);
  256. $this->shareManager->expects($this->any())
  257. ->method('shareProviderExists')
  258. ->willReturnCallback(function ($shareType) use ($emailSharingEnabled) {
  259. if ($shareType === IShare::TYPE_EMAIL) {
  260. return $emailSharingEnabled;
  261. } else {
  262. return false;
  263. }
  264. });
  265. $this->assertInstanceOf(DataResponse::class, $sharees->search($search, $itemType, $page, $perPage, $shareType));
  266. }
  267. public function dataSearchInvalid(): array {
  268. return [
  269. // Test invalid pagination
  270. [[
  271. 'page' => 0,
  272. ], 'Invalid page'],
  273. [[
  274. 'page' => '0',
  275. ], 'Invalid page'],
  276. [[
  277. 'page' => -1,
  278. ], 'Invalid page'],
  279. // Test invalid perPage
  280. [[
  281. 'perPage' => 0,
  282. ], 'Invalid perPage argument'],
  283. [[
  284. 'perPage' => '0',
  285. ], 'Invalid perPage argument'],
  286. [[
  287. 'perPage' => -1,
  288. ], 'Invalid perPage argument'],
  289. ];
  290. }
  291. /**
  292. * @dataProvider dataSearchInvalid
  293. *
  294. * @param array $getData
  295. * @param string $message
  296. */
  297. public function testSearchInvalid($getData, $message): void {
  298. $page = $getData['page'] ?? 1;
  299. $perPage = $getData['perPage'] ?? 200;
  300. /** @var IConfig|MockObject $config */
  301. $config = $this->createMock(IConfig::class);
  302. $config->expects($this->never())
  303. ->method('getAppValue');
  304. /** @var string */
  305. $uid = 'test123';
  306. /** @var IRequest|MockObject $request */
  307. $request = $this->createMock(IRequest::class);
  308. /** @var IURLGenerator|MockObject $urlGenerator */
  309. $urlGenerator = $this->createMock(IURLGenerator::class);
  310. /** @var MockObject|ShareesAPIController $sharees */
  311. $sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController')
  312. ->setConstructorArgs([
  313. 'files_sharing',
  314. $request,
  315. $uid,
  316. $config,
  317. $urlGenerator,
  318. $this->shareManager,
  319. $this->collaboratorSearch
  320. ])
  321. ->onlyMethods(['isRemoteSharingAllowed'])
  322. ->getMock();
  323. $sharees->expects($this->never())
  324. ->method('isRemoteSharingAllowed');
  325. $this->collaboratorSearch->expects($this->never())
  326. ->method('search');
  327. try {
  328. $sharees->search('', null, $page, $perPage, null);
  329. $this->fail();
  330. } catch (OCSBadRequestException $e) {
  331. $this->assertEquals($message, $e->getMessage());
  332. }
  333. }
  334. public function dataIsRemoteSharingAllowed() {
  335. return [
  336. ['file', true],
  337. ['folder', true],
  338. ['', false],
  339. ['contacts', false],
  340. ];
  341. }
  342. /**
  343. * @dataProvider dataIsRemoteSharingAllowed
  344. *
  345. * @param string $itemType
  346. * @param bool $expected
  347. */
  348. public function testIsRemoteSharingAllowed($itemType, $expected): void {
  349. $this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType]));
  350. }
  351. public function testSearchSharingDisabled(): void {
  352. $this->shareManager->expects($this->once())
  353. ->method('sharingDisabledForUser')
  354. ->with($this->uid)
  355. ->willReturn(true);
  356. $this->config->expects($this->once())
  357. ->method('getSystemValueInt')
  358. ->with('sharing.minSearchStringLength', 0)
  359. ->willReturn(0);
  360. $this->shareManager->expects($this->never())
  361. ->method('allowGroupSharing');
  362. $this->assertInstanceOf(DataResponse::class, $this->sharees->search('', null, 1, 10, [], false));
  363. }
  364. public function testSearchNoItemType(): void {
  365. $this->expectException(OCSBadRequestException::class);
  366. $this->expectExceptionMessage('Missing itemType');
  367. $this->sharees->search('', null, 1, 10, [], false);
  368. }
  369. public function dataGetPaginationLink() {
  370. return [
  371. [1, '/ocs/v1.php', ['perPage' => 2], '<?perPage=2&page=2>; rel="next"'],
  372. [10, '/ocs/v2.php', ['perPage' => 2], '<?perPage=2&page=11>; rel="next"'],
  373. ];
  374. }
  375. /**
  376. * @dataProvider dataGetPaginationLink
  377. *
  378. * @param int $page
  379. * @param string $scriptName
  380. * @param array $params
  381. * @param array $expected
  382. */
  383. public function testGetPaginationLink($page, $scriptName, $params, $expected): void {
  384. $this->request->expects($this->once())
  385. ->method('getScriptName')
  386. ->willReturn($scriptName);
  387. $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'getPaginationLink', [$page, $params]));
  388. }
  389. public function dataIsV2() {
  390. return [
  391. ['/ocs/v1.php', false],
  392. ['/ocs/v2.php', true],
  393. ];
  394. }
  395. /**
  396. * @dataProvider dataIsV2
  397. *
  398. * @param string $scriptName
  399. * @param bool $expected
  400. */
  401. public function testIsV2($scriptName, $expected): void {
  402. $this->request->expects($this->once())
  403. ->method('getScriptName')
  404. ->willReturn($scriptName);
  405. $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'isV2'));
  406. }
  407. }