1
0

ShareesAPIControllerTest.php 13 KB

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