You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShareInfoControllerTest.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $this->assertEquals($expected, $this->controller->info('token'));
  59. }
  60. public function testWrongPassword() {
  61. $share = $this->createMock(IShare::class);
  62. $share->method('getPassword')
  63. ->willReturn('sharePass');
  64. $this->shareManager->method('getShareByToken')
  65. ->with('token')
  66. ->willReturn($share);
  67. $this->shareManager->method('checkPassword')
  68. ->with($share, 'pass')
  69. ->willReturn(false);
  70. $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
  71. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  72. }
  73. public function testNoReadPermissions() {
  74. $share = $this->createMock(IShare::class);
  75. $share->method('getPassword')
  76. ->willReturn('sharePass');
  77. $share->method('getPermissions')
  78. ->willReturn(Constants::PERMISSION_CREATE);
  79. $this->shareManager->method('getShareByToken')
  80. ->with('token')
  81. ->willReturn($share);
  82. $this->shareManager->method('checkPassword')
  83. ->with($share, 'pass')
  84. ->willReturn(true);
  85. $expected = new JSONResponse([], Http::STATUS_FORBIDDEN);
  86. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  87. }
  88. private function prepareFile() {
  89. $file = $this->createMock(File::class);
  90. $file->method('getId')->willReturn(42);
  91. $parent = $this->createMock(Folder::class);
  92. $parent->method('getId')->willReturn(41);
  93. $file->method('getParent')->willReturn($parent);
  94. $file->method('getMTime')->willReturn(1337);
  95. $file->method('getName')->willReturn('file');
  96. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  97. $file->method('getMimeType')->willReturn('mime/type');
  98. $file->method('getSize')->willReturn(1);
  99. $file->method('getType')->willReturn('file');
  100. $file->method('getEtag')->willReturn('etag');
  101. return $file;
  102. }
  103. public function testInfoFile() {
  104. $file = $this->prepareFile();
  105. $share = $this->createMock(IShare::class);
  106. $share->method('getPassword')
  107. ->willReturn('sharePass');
  108. $share->method('getPermissions')
  109. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  110. $share->method('getNode')
  111. ->willReturn($file);
  112. $this->shareManager->method('getShareByToken')
  113. ->with('token')
  114. ->willReturn($share);
  115. $this->shareManager->method('checkPassword')
  116. ->with($share, 'pass')
  117. ->willReturn(true);
  118. $expected = new JSONResponse([
  119. 'id' => 42,
  120. 'parentId' => 41,
  121. 'mtime' => 1337 ,
  122. 'name' => 'file',
  123. 'permissions' => 1,
  124. 'mimetype' => 'mime/type',
  125. 'size' => 1,
  126. 'type' => 'file',
  127. 'etag' => 'etag',
  128. ]);
  129. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  130. }
  131. public function testInfoFileRO() {
  132. $file = $this->prepareFile();
  133. $share = $this->createMock(IShare::class);
  134. $share->method('getPassword')
  135. ->willReturn('sharePass');
  136. $share->method('getPermissions')
  137. ->willReturn(Constants::PERMISSION_READ);
  138. $share->method('getNode')
  139. ->willReturn($file);
  140. $this->shareManager->method('getShareByToken')
  141. ->with('token')
  142. ->willReturn($share);
  143. $this->shareManager->method('checkPassword')
  144. ->with($share, 'pass')
  145. ->willReturn(true);
  146. $expected = new JSONResponse([
  147. 'id' => 42,
  148. 'parentId' => 41,
  149. 'mtime' => 1337 ,
  150. 'name' => 'file',
  151. 'permissions' => 1,
  152. 'mimetype' => 'mime/type',
  153. 'size' => 1,
  154. 'type' => 'file',
  155. 'etag' => 'etag',
  156. ]);
  157. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  158. }
  159. private function prepareFolder() {
  160. $root = $this->createMock(Folder::class);
  161. $root->method('getId')->willReturn(42);
  162. $parent = $this->createMock(Folder::class);
  163. $parent->method('getId')->willReturn(41);
  164. $root->method('getParent')->willReturn($parent);
  165. $root->method('getMTime')->willReturn(1337);
  166. $root->method('getName')->willReturn('root');
  167. $root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  168. $root->method('getMimeType')->willReturn('mime/type');
  169. $root->method('getSize')->willReturn(1);
  170. $root->method('getType')->willReturn('folder');
  171. $root->method('getEtag')->willReturn('etag');
  172. //Subfolder
  173. $sub = $this->createMock(Folder::class);
  174. $sub->method('getId')->willReturn(43);
  175. $sub->method('getParent')->willReturn($root);
  176. $sub->method('getMTime')->willReturn(1338);
  177. $sub->method('getName')->willReturn('sub');
  178. $sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  179. $sub->method('getMimeType')->willReturn('mime/type');
  180. $sub->method('getSize')->willReturn(2);
  181. $sub->method('getType')->willReturn('folder');
  182. $sub->method('getEtag')->willReturn('etag2');
  183. $root->method('getDirectoryListing')->willReturn([$sub]);
  184. //Subfile
  185. $file = $this->createMock(File::class);
  186. $file->method('getId')->willReturn(88);
  187. $file->method('getParent')->willReturn($sub);
  188. $file->method('getMTime')->willReturn(1339);
  189. $file->method('getName')->willReturn('file');
  190. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
  191. $file->method('getMimeType')->willReturn('mime/type');
  192. $file->method('getSize')->willReturn(3);
  193. $file->method('getType')->willReturn('file');
  194. $file->method('getEtag')->willReturn('etag3');
  195. $sub->method('getDirectoryListing')->willReturn([$file]);
  196. return $root;
  197. }
  198. public function testInfoFolder() {
  199. $file = $this->prepareFolder();
  200. $share = $this->createMock(IShare::class);
  201. $share->method('getPassword')
  202. ->willReturn('sharePass');
  203. $share->method('getPermissions')
  204. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  205. $share->method('getNode')
  206. ->willReturn($file);
  207. $this->shareManager->method('getShareByToken')
  208. ->with('token')
  209. ->willReturn($share);
  210. $this->shareManager->method('checkPassword')
  211. ->with($share, 'pass')
  212. ->willReturn(true);
  213. $expected = new JSONResponse([
  214. 'id' => 42,
  215. 'parentId' => 41,
  216. 'mtime' => 1337,
  217. 'name' => 'root',
  218. 'permissions' => 1,
  219. 'mimetype' => 'mime/type',
  220. 'size' => 1,
  221. 'type' => 'folder',
  222. 'etag' => 'etag',
  223. 'children' => [
  224. [
  225. 'id' => 43,
  226. 'parentId' => 42,
  227. 'mtime' => 1338,
  228. 'name' => 'sub',
  229. 'permissions' => 3,
  230. 'mimetype' => 'mime/type',
  231. 'size' => 2,
  232. 'type' => 'folder',
  233. 'etag' => 'etag2',
  234. 'children' => [
  235. [
  236. 'id' => 88,
  237. 'parentId' => 43,
  238. 'mtime' => 1339,
  239. 'name' => 'file',
  240. 'permissions' => 1,
  241. 'mimetype' => 'mime/type',
  242. 'size' => 3,
  243. 'type' => 'file',
  244. 'etag' => 'etag3',
  245. ]
  246. ],
  247. ]
  248. ],
  249. ]);
  250. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  251. }
  252. }