ShareInfoControllerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Tests\Controller;
  25. use OCA\Files_Sharing\Controller\ShareInfoController;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\JSONResponse;
  28. use OCP\Constants;
  29. use OCP\Files\File;
  30. use OCP\Files\Folder;
  31. use OCP\IRequest;
  32. use OCP\Share\Exceptions\ShareNotFound;
  33. use OCP\Share\IManager as ShareManager;
  34. use OCP\Share\IShare;
  35. use Test\TestCase;
  36. class ShareInfoControllerTest extends TestCase {
  37. /** @var ShareInfoController */
  38. private $controller;
  39. /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */
  40. private $shareManager;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->shareManager = $this->createMock(ShareManager::class);
  44. $this->controller = $this->getMockBuilder(ShareInfoController::class)
  45. ->setConstructorArgs([
  46. 'files_sharing',
  47. $this->createMock(IRequest::class),
  48. $this->shareManager
  49. ])
  50. ->setMethods(['addROWrapper'])
  51. ->getMock();
  52. }
  53. public function testNoShare() {
  54. $this->shareManager->method('getShareByToken')
  55. ->with('token')
  56. ->willThrowException(new ShareNotFound());
  57. $expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
  58. $expected->throttle(['token' => 'token']);
  59. $this->assertEquals($expected, $this->controller->info('token'));
  60. }
  61. public function testWrongPassword() {
  62. $share = $this->createMock(IShare::class);
  63. $share->method('getPassword')
  64. ->willReturn('sharePass');
  65. $this->shareManager->method('getShareByToken')
  66. ->with('token')
  67. ->willReturn($share);
  68. $this->shareManager->method('checkPassword')
  69. ->with($share, 'pass')
  70. ->willReturn(false);
  71. $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
  72. $expected->throttle(['token' => 'token']);
  73. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  74. }
  75. public function testNoReadPermissions() {
  76. $share = $this->createMock(IShare::class);
  77. $share->method('getPassword')
  78. ->willReturn('sharePass');
  79. $share->method('getPermissions')
  80. ->willReturn(Constants::PERMISSION_CREATE);
  81. $this->shareManager->method('getShareByToken')
  82. ->with('token')
  83. ->willReturn($share);
  84. $this->shareManager->method('checkPassword')
  85. ->with($share, 'pass')
  86. ->willReturn(true);
  87. $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
  88. $expected->throttle(['token' => 'token']);
  89. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  90. }
  91. private function prepareFile() {
  92. $file = $this->createMock(File::class);
  93. $file->method('getId')->willReturn(42);
  94. $parent = $this->createMock(Folder::class);
  95. $parent->method('getId')->willReturn(41);
  96. $file->method('getParent')->willReturn($parent);
  97. $file->method('getMTime')->willReturn(1337);
  98. $file->method('getName')->willReturn('file');
  99. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  100. $file->method('getMimeType')->willReturn('mime/type');
  101. $file->method('getSize')->willReturn(1);
  102. $file->method('getType')->willReturn('file');
  103. $file->method('getEtag')->willReturn('etag');
  104. return $file;
  105. }
  106. public function testInfoFile() {
  107. $file = $this->prepareFile();
  108. $share = $this->createMock(IShare::class);
  109. $share->method('getPassword')
  110. ->willReturn('sharePass');
  111. $share->method('getPermissions')
  112. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  113. $share->method('getNode')
  114. ->willReturn($file);
  115. $this->shareManager->method('getShareByToken')
  116. ->with('token')
  117. ->willReturn($share);
  118. $this->shareManager->method('checkPassword')
  119. ->with($share, 'pass')
  120. ->willReturn(true);
  121. $expected = new JSONResponse([
  122. 'id' => 42,
  123. 'parentId' => 41,
  124. 'mtime' => 1337 ,
  125. 'name' => 'file',
  126. 'permissions' => 1,
  127. 'mimetype' => 'mime/type',
  128. 'size' => 1,
  129. 'type' => 'file',
  130. 'etag' => 'etag',
  131. ]);
  132. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  133. }
  134. public function testInfoFileRO() {
  135. $file = $this->prepareFile();
  136. $share = $this->createMock(IShare::class);
  137. $share->method('getPassword')
  138. ->willReturn('sharePass');
  139. $share->method('getPermissions')
  140. ->willReturn(Constants::PERMISSION_READ);
  141. $share->method('getNode')
  142. ->willReturn($file);
  143. $this->shareManager->method('getShareByToken')
  144. ->with('token')
  145. ->willReturn($share);
  146. $this->shareManager->method('checkPassword')
  147. ->with($share, 'pass')
  148. ->willReturn(true);
  149. $expected = new JSONResponse([
  150. 'id' => 42,
  151. 'parentId' => 41,
  152. 'mtime' => 1337 ,
  153. 'name' => 'file',
  154. 'permissions' => 1,
  155. 'mimetype' => 'mime/type',
  156. 'size' => 1,
  157. 'type' => 'file',
  158. 'etag' => 'etag',
  159. ]);
  160. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  161. }
  162. private function prepareFolder() {
  163. $root = $this->createMock(Folder::class);
  164. $root->method('getId')->willReturn(42);
  165. $parent = $this->createMock(Folder::class);
  166. $parent->method('getId')->willReturn(41);
  167. $root->method('getParent')->willReturn($parent);
  168. $root->method('getMTime')->willReturn(1337);
  169. $root->method('getName')->willReturn('root');
  170. $root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  171. $root->method('getMimeType')->willReturn('mime/type');
  172. $root->method('getSize')->willReturn(1);
  173. $root->method('getType')->willReturn('folder');
  174. $root->method('getEtag')->willReturn('etag');
  175. //Subfolder
  176. $sub = $this->createMock(Folder::class);
  177. $sub->method('getId')->willReturn(43);
  178. $sub->method('getParent')->willReturn($root);
  179. $sub->method('getMTime')->willReturn(1338);
  180. $sub->method('getName')->willReturn('sub');
  181. $sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  182. $sub->method('getMimeType')->willReturn('mime/type');
  183. $sub->method('getSize')->willReturn(2);
  184. $sub->method('getType')->willReturn('folder');
  185. $sub->method('getEtag')->willReturn('etag2');
  186. $root->method('getDirectoryListing')->willReturn([$sub]);
  187. //Subfile
  188. $file = $this->createMock(File::class);
  189. $file->method('getId')->willReturn(88);
  190. $file->method('getParent')->willReturn($sub);
  191. $file->method('getMTime')->willReturn(1339);
  192. $file->method('getName')->willReturn('file');
  193. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
  194. $file->method('getMimeType')->willReturn('mime/type');
  195. $file->method('getSize')->willReturn(3);
  196. $file->method('getType')->willReturn('file');
  197. $file->method('getEtag')->willReturn('etag3');
  198. $sub->method('getDirectoryListing')->willReturn([$file]);
  199. return $root;
  200. }
  201. public function testInfoFolder() {
  202. $file = $this->prepareFolder();
  203. $share = $this->createMock(IShare::class);
  204. $share->method('getPassword')
  205. ->willReturn('sharePass');
  206. $share->method('getPermissions')
  207. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  208. $share->method('getNode')
  209. ->willReturn($file);
  210. $this->shareManager->method('getShareByToken')
  211. ->with('token')
  212. ->willReturn($share);
  213. $this->shareManager->method('checkPassword')
  214. ->with($share, 'pass')
  215. ->willReturn(true);
  216. $expected = new JSONResponse([
  217. 'id' => 42,
  218. 'parentId' => 41,
  219. 'mtime' => 1337,
  220. 'name' => 'root',
  221. 'permissions' => 1,
  222. 'mimetype' => 'mime/type',
  223. 'size' => 1,
  224. 'type' => 'folder',
  225. 'etag' => 'etag',
  226. 'children' => [
  227. [
  228. 'id' => 43,
  229. 'parentId' => 42,
  230. 'mtime' => 1338,
  231. 'name' => 'sub',
  232. 'permissions' => 3,
  233. 'mimetype' => 'mime/type',
  234. 'size' => 2,
  235. 'type' => 'folder',
  236. 'etag' => 'etag2',
  237. 'children' => [
  238. [
  239. 'id' => 88,
  240. 'parentId' => 43,
  241. 'mtime' => 1339,
  242. 'name' => 'file',
  243. 'permissions' => 1,
  244. 'mimetype' => 'mime/type',
  245. 'size' => 3,
  246. 'type' => 'file',
  247. 'etag' => 'etag3',
  248. ]
  249. ],
  250. ]
  251. ],
  252. ]);
  253. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  254. }
  255. }