ShareesAPIControllerTest.php 14 KB

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