1
0

ShareInfoControllerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Tests\Controller;
  7. use OCA\Files_Sharing\Controller\ShareInfoController;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\JSONResponse;
  10. use OCP\Constants;
  11. use OCP\Files\File;
  12. use OCP\Files\Folder;
  13. use OCP\IRequest;
  14. use OCP\Share\Exceptions\ShareNotFound;
  15. use OCP\Share\IManager as ShareManager;
  16. use OCP\Share\IShare;
  17. use Test\TestCase;
  18. class ShareInfoControllerTest extends TestCase {
  19. /** @var ShareInfoController */
  20. private $controller;
  21. /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */
  22. private $shareManager;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->shareManager = $this->createMock(ShareManager::class);
  26. $this->controller = $this->getMockBuilder(ShareInfoController::class)
  27. ->setConstructorArgs([
  28. 'files_sharing',
  29. $this->createMock(IRequest::class),
  30. $this->shareManager
  31. ])
  32. ->setMethods(['addROWrapper'])
  33. ->getMock();
  34. }
  35. public function testNoShare(): void {
  36. $this->shareManager->method('getShareByToken')
  37. ->with('token')
  38. ->willThrowException(new ShareNotFound());
  39. $expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
  40. $expected->throttle(['token' => 'token']);
  41. $this->assertEquals($expected, $this->controller->info('token'));
  42. }
  43. public function testWrongPassword(): void {
  44. $share = $this->createMock(IShare::class);
  45. $share->method('getPassword')
  46. ->willReturn('sharePass');
  47. $this->shareManager->method('getShareByToken')
  48. ->with('token')
  49. ->willReturn($share);
  50. $this->shareManager->method('checkPassword')
  51. ->with($share, 'pass')
  52. ->willReturn(false);
  53. $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
  54. $expected->throttle(['token' => 'token']);
  55. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  56. }
  57. public function testNoReadPermissions(): void {
  58. $share = $this->createMock(IShare::class);
  59. $share->method('getPassword')
  60. ->willReturn('sharePass');
  61. $share->method('getPermissions')
  62. ->willReturn(Constants::PERMISSION_CREATE);
  63. $this->shareManager->method('getShareByToken')
  64. ->with('token')
  65. ->willReturn($share);
  66. $this->shareManager->method('checkPassword')
  67. ->with($share, 'pass')
  68. ->willReturn(true);
  69. $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
  70. $expected->throttle(['token' => 'token']);
  71. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  72. }
  73. private function prepareFile() {
  74. $file = $this->createMock(File::class);
  75. $file->method('getId')->willReturn(42);
  76. $parent = $this->createMock(Folder::class);
  77. $parent->method('getId')->willReturn(41);
  78. $file->method('getParent')->willReturn($parent);
  79. $file->method('getMTime')->willReturn(1337);
  80. $file->method('getName')->willReturn('file');
  81. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  82. $file->method('getMimeType')->willReturn('mime/type');
  83. $file->method('getSize')->willReturn(1);
  84. $file->method('getType')->willReturn('file');
  85. $file->method('getEtag')->willReturn('etag');
  86. return $file;
  87. }
  88. public function testInfoFile(): void {
  89. $file = $this->prepareFile();
  90. $share = $this->createMock(IShare::class);
  91. $share->method('getPassword')
  92. ->willReturn('sharePass');
  93. $share->method('getPermissions')
  94. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  95. $share->method('getNode')
  96. ->willReturn($file);
  97. $this->shareManager->method('getShareByToken')
  98. ->with('token')
  99. ->willReturn($share);
  100. $this->shareManager->method('checkPassword')
  101. ->with($share, 'pass')
  102. ->willReturn(true);
  103. $expected = new JSONResponse([
  104. 'id' => 42,
  105. 'parentId' => 41,
  106. 'mtime' => 1337 ,
  107. 'name' => 'file',
  108. 'permissions' => 1,
  109. 'mimetype' => 'mime/type',
  110. 'size' => 1,
  111. 'type' => 'file',
  112. 'etag' => 'etag',
  113. ]);
  114. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  115. }
  116. public function testInfoFileRO(): void {
  117. $file = $this->prepareFile();
  118. $share = $this->createMock(IShare::class);
  119. $share->method('getPassword')
  120. ->willReturn('sharePass');
  121. $share->method('getPermissions')
  122. ->willReturn(Constants::PERMISSION_READ);
  123. $share->method('getNode')
  124. ->willReturn($file);
  125. $this->shareManager->method('getShareByToken')
  126. ->with('token')
  127. ->willReturn($share);
  128. $this->shareManager->method('checkPassword')
  129. ->with($share, 'pass')
  130. ->willReturn(true);
  131. $expected = new JSONResponse([
  132. 'id' => 42,
  133. 'parentId' => 41,
  134. 'mtime' => 1337 ,
  135. 'name' => 'file',
  136. 'permissions' => 1,
  137. 'mimetype' => 'mime/type',
  138. 'size' => 1,
  139. 'type' => 'file',
  140. 'etag' => 'etag',
  141. ]);
  142. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  143. }
  144. private function prepareFolder() {
  145. $root = $this->createMock(Folder::class);
  146. $root->method('getId')->willReturn(42);
  147. $parent = $this->createMock(Folder::class);
  148. $parent->method('getId')->willReturn(41);
  149. $root->method('getParent')->willReturn($parent);
  150. $root->method('getMTime')->willReturn(1337);
  151. $root->method('getName')->willReturn('root');
  152. $root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  153. $root->method('getMimeType')->willReturn('mime/type');
  154. $root->method('getSize')->willReturn(1);
  155. $root->method('getType')->willReturn('folder');
  156. $root->method('getEtag')->willReturn('etag');
  157. //Subfolder
  158. $sub = $this->createMock(Folder::class);
  159. $sub->method('getId')->willReturn(43);
  160. $sub->method('getParent')->willReturn($root);
  161. $sub->method('getMTime')->willReturn(1338);
  162. $sub->method('getName')->willReturn('sub');
  163. $sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  164. $sub->method('getMimeType')->willReturn('mime/type');
  165. $sub->method('getSize')->willReturn(2);
  166. $sub->method('getType')->willReturn('folder');
  167. $sub->method('getEtag')->willReturn('etag2');
  168. $root->method('getDirectoryListing')->willReturn([$sub]);
  169. //Subfile
  170. $file = $this->createMock(File::class);
  171. $file->method('getId')->willReturn(88);
  172. $file->method('getParent')->willReturn($sub);
  173. $file->method('getMTime')->willReturn(1339);
  174. $file->method('getName')->willReturn('file');
  175. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
  176. $file->method('getMimeType')->willReturn('mime/type');
  177. $file->method('getSize')->willReturn(3);
  178. $file->method('getType')->willReturn('file');
  179. $file->method('getEtag')->willReturn('etag3');
  180. $sub->method('getDirectoryListing')->willReturn([$file]);
  181. return $root;
  182. }
  183. public function testInfoFolder(): void {
  184. $file = $this->prepareFolder();
  185. $share = $this->createMock(IShare::class);
  186. $share->method('getPassword')
  187. ->willReturn('sharePass');
  188. $share->method('getPermissions')
  189. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  190. $share->method('getNode')
  191. ->willReturn($file);
  192. $this->shareManager->method('getShareByToken')
  193. ->with('token')
  194. ->willReturn($share);
  195. $this->shareManager->method('checkPassword')
  196. ->with($share, 'pass')
  197. ->willReturn(true);
  198. $expected = new JSONResponse([
  199. 'id' => 42,
  200. 'parentId' => 41,
  201. 'mtime' => 1337,
  202. 'name' => 'root',
  203. 'permissions' => 1,
  204. 'mimetype' => 'mime/type',
  205. 'size' => 1,
  206. 'type' => 'folder',
  207. 'etag' => 'etag',
  208. 'children' => [
  209. [
  210. 'id' => 43,
  211. 'parentId' => 42,
  212. 'mtime' => 1338,
  213. 'name' => 'sub',
  214. 'permissions' => 3,
  215. 'mimetype' => 'mime/type',
  216. 'size' => 2,
  217. 'type' => 'folder',
  218. 'etag' => 'etag2',
  219. 'children' => [
  220. [
  221. 'id' => 88,
  222. 'parentId' => 43,
  223. 'mtime' => 1339,
  224. 'name' => 'file',
  225. 'permissions' => 1,
  226. 'mimetype' => 'mime/type',
  227. 'size' => 3,
  228. 'type' => 'file',
  229. 'etag' => 'etag3',
  230. ]
  231. ],
  232. ]
  233. ],
  234. ]);
  235. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  236. }
  237. }