ShareesAPIControllerTest.php 13 KB

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