ShareInfoControllerTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. public function setUp() {
  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. $this->controller->expects($this->once())
  147. ->method('addROWrapper');
  148. $expected = new JSONResponse([
  149. 'id' => 42,
  150. 'parentId' => 41,
  151. 'mtime' => 1337 ,
  152. 'name' => 'file',
  153. 'permissions' => 1,
  154. 'mimetype' => 'mime/type',
  155. 'size' => 1,
  156. 'type' => 'file',
  157. 'etag' => 'etag',
  158. ]);
  159. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  160. }
  161. private function prepareFolder() {
  162. $root = $this->createMock(Folder::class);
  163. $root->method('getId')->willReturn(42);
  164. $parent = $this->createMock(Folder::class);
  165. $parent->method('getId')->willReturn(41);
  166. $root->method('getParent')->willReturn($parent);
  167. $root->method('getMTime')->willReturn(1337);
  168. $root->method('getName')->willReturn('root');
  169. $root->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
  170. $root->method('getMimeType')->willReturn('mime/type');
  171. $root->method('getSize')->willReturn(1);
  172. $root->method('getType')->willReturn('folder');
  173. $root->method('getEtag')->willReturn('etag');
  174. //Subfolder
  175. $sub = $this->createMock(Folder::class);
  176. $sub->method('getId')->willReturn(43);
  177. $sub->method('getParent')->willReturn($root);
  178. $sub->method('getMTime')->willReturn(1338);
  179. $sub->method('getName')->willReturn('sub');
  180. $sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  181. $sub->method('getMimeType')->willReturn('mime/type');
  182. $sub->method('getSize')->willReturn(2);
  183. $sub->method('getType')->willReturn('folder');
  184. $sub->method('getEtag')->willReturn('etag2');
  185. $root->method('getDirectoryListing')->willReturn([$sub]);
  186. //Subfile
  187. $file = $this->createMock(File::class);
  188. $file->method('getId')->willReturn(88);
  189. $file->method('getParent')->willReturn($sub);
  190. $file->method('getMTime')->willReturn(1339);
  191. $file->method('getName')->willReturn('file');
  192. $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE);
  193. $file->method('getMimeType')->willReturn('mime/type');
  194. $file->method('getSize')->willReturn(3);
  195. $file->method('getType')->willReturn('file');
  196. $file->method('getEtag')->willReturn('etag3');
  197. $sub->method('getDirectoryListing')->willReturn([$file]);
  198. return $root;
  199. }
  200. public function testInfoFolder() {
  201. $file = $this->prepareFolder();
  202. $share = $this->createMock(IShare::class);
  203. $share->method('getPassword')
  204. ->willReturn('sharePass');
  205. $share->method('getPermissions')
  206. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE);
  207. $share->method('getNode')
  208. ->willReturn($file);
  209. $this->shareManager->method('getShareByToken')
  210. ->with('token')
  211. ->willReturn($share);
  212. $this->shareManager->method('checkPassword')
  213. ->with($share, 'pass')
  214. ->willReturn(true);
  215. $expected = new JSONResponse([
  216. 'id' => 42,
  217. 'parentId' => 41,
  218. 'mtime' => 1337,
  219. 'name' => 'root',
  220. 'permissions' => 1,
  221. 'mimetype' => 'mime/type',
  222. 'size' => 1,
  223. 'type' => 'folder',
  224. 'etag' => 'etag',
  225. 'children' => [
  226. [
  227. 'id' => 43,
  228. 'parentId' => 42,
  229. 'mtime' => 1338,
  230. 'name' => 'sub',
  231. 'permissions' => 3,
  232. 'mimetype' => 'mime/type',
  233. 'size' => 2,
  234. 'type' => 'folder',
  235. 'etag' => 'etag2',
  236. 'children' => [
  237. [
  238. 'id' => 88,
  239. 'parentId' => 43,
  240. 'mtime' => 1339,
  241. 'name' => 'file',
  242. 'permissions' => 9,
  243. 'mimetype' => 'mime/type',
  244. 'size' => 3,
  245. 'type' => 'file',
  246. 'etag' => 'etag3',
  247. ]
  248. ],
  249. ]
  250. ],
  251. ]);
  252. $this->assertEquals($expected, $this->controller->info('token', 'pass'));
  253. }
  254. }