1
0

PublicPreviewControllerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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\PublicPreviewController;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\DataResponse;
  10. use OCP\AppFramework\Http\FileDisplayResponse;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\Constants;
  13. use OCP\Files\File;
  14. use OCP\Files\Folder;
  15. use OCP\Files\NotFoundException;
  16. use OCP\Files\SimpleFS\ISimpleFile;
  17. use OCP\IPreview;
  18. use OCP\IRequest;
  19. use OCP\ISession;
  20. use OCP\Share\Exceptions\ShareNotFound;
  21. use OCP\Share\IAttributes;
  22. use OCP\Share\IManager;
  23. use OCP\Share\IShare;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. use Test\TestCase;
  26. class PublicPreviewControllerTest extends TestCase {
  27. private IPreview&MockObject $previewManager;
  28. private IManager&MockObject $shareManager;
  29. private ITimeFactory&MockObject $timeFactory;
  30. private IRequest&MockObject $request;
  31. private PublicPreviewController $controller;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->previewManager = $this->createMock(IPreview::class);
  35. $this->shareManager = $this->createMock(IManager::class);
  36. $this->timeFactory = $this->createMock(ITimeFactory::class);
  37. $this->request = $this->createMock(IRequest::class);
  38. $this->timeFactory->method('getTime')
  39. ->willReturn(1337);
  40. $this->overwriteService(ITimeFactory::class, $this->timeFactory);
  41. $this->controller = new PublicPreviewController(
  42. 'files_sharing',
  43. $this->request,
  44. $this->shareManager,
  45. $this->createMock(ISession::class),
  46. $this->previewManager
  47. );
  48. }
  49. public function testInvalidToken(): void {
  50. $res = $this->controller->getPreview('', 'file', 10, 10, '');
  51. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  52. $this->assertEquals($expected, $res);
  53. }
  54. public function testInvalidWidth(): void {
  55. $res = $this->controller->getPreview('token', 'file', 0);
  56. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  57. $this->assertEquals($expected, $res);
  58. }
  59. public function testInvalidHeight(): void {
  60. $res = $this->controller->getPreview('token', 'file', 10, 0);
  61. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  62. $this->assertEquals($expected, $res);
  63. }
  64. public function testInvalidShare(): void {
  65. $this->shareManager->method('getShareByToken')
  66. ->with($this->equalTo('token'))
  67. ->willThrowException(new ShareNotFound());
  68. $res = $this->controller->getPreview('token', 'file', 10, 10);
  69. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  70. $this->assertEquals($expected, $res);
  71. }
  72. public function testShareNotAccessable(): void {
  73. $share = $this->createMock(IShare::class);
  74. $this->shareManager->method('getShareByToken')
  75. ->with($this->equalTo('token'))
  76. ->willReturn($share);
  77. $share->method('getPermissions')
  78. ->willReturn(0);
  79. $res = $this->controller->getPreview('token', 'file', 10, 10);
  80. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  81. $this->assertEquals($expected, $res);
  82. }
  83. public function testShareNoDownload() {
  84. $share = $this->createMock(IShare::class);
  85. $this->shareManager->method('getShareByToken')
  86. ->with($this->equalTo('token'))
  87. ->willReturn($share);
  88. $share->method('getPermissions')
  89. ->willReturn(Constants::PERMISSION_READ);
  90. $attributes = $this->createMock(IAttributes::class);
  91. $attributes->method('getAttribute')
  92. ->with('permissions', 'download')
  93. ->willReturn(false);
  94. $share->method('getAttributes')
  95. ->willReturn($attributes);
  96. $res = $this->controller->getPreview('token', 'file', 10, 10);
  97. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  98. $this->assertEquals($expected, $res);
  99. }
  100. public function testShareNoDownloadButPreviewHeader() {
  101. $share = $this->createMock(IShare::class);
  102. $this->shareManager->method('getShareByToken')
  103. ->with($this->equalTo('token'))
  104. ->willReturn($share);
  105. $share->method('getPermissions')
  106. ->willReturn(Constants::PERMISSION_READ);
  107. $attributes = $this->createMock(IAttributes::class);
  108. $attributes->method('getAttribute')
  109. ->with('permissions', 'download')
  110. ->willReturn(false);
  111. $share->method('getAttributes')
  112. ->willReturn($attributes);
  113. $this->request->method('getHeader')
  114. ->with('X-NC-Preview')
  115. ->willReturn('true');
  116. $file = $this->createMock(File::class);
  117. $share->method('getNode')
  118. ->willReturn($file);
  119. $preview = $this->createMock(ISimpleFile::class);
  120. $preview->method('getName')->willReturn('name');
  121. $preview->method('getMTime')->willReturn(42);
  122. $this->previewManager->method('getPreview')
  123. ->with($this->equalTo($file), 10, 10, false)
  124. ->willReturn($preview);
  125. $preview->method('getMimeType')
  126. ->willReturn('myMime');
  127. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  128. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  129. $expected->cacheFor(15 * 60);
  130. $this->assertEquals($expected, $res);
  131. }
  132. public function testShareWithAttributes() {
  133. $share = $this->createMock(IShare::class);
  134. $this->shareManager->method('getShareByToken')
  135. ->with($this->equalTo('token'))
  136. ->willReturn($share);
  137. $share->method('getPermissions')
  138. ->willReturn(Constants::PERMISSION_READ);
  139. $attributes = $this->createMock(IAttributes::class);
  140. $attributes->method('getAttribute')
  141. ->with('permissions', 'download')
  142. ->willReturn(true);
  143. $share->method('getAttributes')
  144. ->willReturn($attributes);
  145. $this->request->method('getHeader')
  146. ->with('X-NC-Preview')
  147. ->willReturn('true');
  148. $file = $this->createMock(File::class);
  149. $share->method('getNode')
  150. ->willReturn($file);
  151. $preview = $this->createMock(ISimpleFile::class);
  152. $preview->method('getName')->willReturn('name');
  153. $preview->method('getMTime')->willReturn(42);
  154. $this->previewManager->method('getPreview')
  155. ->with($this->equalTo($file), 10, 10, false)
  156. ->willReturn($preview);
  157. $preview->method('getMimeType')
  158. ->willReturn('myMime');
  159. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  160. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  161. $expected->cacheFor(3600 * 24);
  162. $this->assertEquals($expected, $res);
  163. }
  164. public function testPreviewFile() {
  165. $share = $this->createMock(IShare::class);
  166. $this->shareManager->method('getShareByToken')
  167. ->with($this->equalTo('token'))
  168. ->willReturn($share);
  169. $share->method('getPermissions')
  170. ->willReturn(Constants::PERMISSION_READ);
  171. $file = $this->createMock(File::class);
  172. $share->method('getNode')
  173. ->willReturn($file);
  174. $preview = $this->createMock(ISimpleFile::class);
  175. $preview->method('getName')->willReturn('name');
  176. $preview->method('getMTime')->willReturn(42);
  177. $this->previewManager->method('getPreview')
  178. ->with($this->equalTo($file), 10, 10, false)
  179. ->willReturn($preview);
  180. $preview->method('getMimeType')
  181. ->willReturn('myMime');
  182. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  183. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  184. $expected->cacheFor(3600 * 24);
  185. $this->assertEquals($expected, $res);
  186. }
  187. public function testPreviewFolderInvalidFile(): void {
  188. $share = $this->createMock(IShare::class);
  189. $this->shareManager->method('getShareByToken')
  190. ->with($this->equalTo('token'))
  191. ->willReturn($share);
  192. $share->method('getPermissions')
  193. ->willReturn(Constants::PERMISSION_READ);
  194. $folder = $this->createMock(Folder::class);
  195. $share->method('getNode')
  196. ->willReturn($folder);
  197. $folder->method('get')
  198. ->with($this->equalTo('file'))
  199. ->willThrowException(new NotFoundException());
  200. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  201. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  202. $this->assertEquals($expected, $res);
  203. }
  204. public function testPreviewFolderValidFile(): void {
  205. $share = $this->createMock(IShare::class);
  206. $this->shareManager->method('getShareByToken')
  207. ->with($this->equalTo('token'))
  208. ->willReturn($share);
  209. $share->method('getPermissions')
  210. ->willReturn(Constants::PERMISSION_READ);
  211. $folder = $this->createMock(Folder::class);
  212. $share->method('getNode')
  213. ->willReturn($folder);
  214. $file = $this->createMock(File::class);
  215. $folder->method('get')
  216. ->with($this->equalTo('file'))
  217. ->willReturn($file);
  218. $preview = $this->createMock(ISimpleFile::class);
  219. $preview->method('getName')->willReturn('name');
  220. $preview->method('getMTime')->willReturn(42);
  221. $this->previewManager->method('getPreview')
  222. ->with($this->equalTo($file), 10, 10, false)
  223. ->willReturn($preview);
  224. $preview->method('getMimeType')
  225. ->willReturn('myMime');
  226. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  227. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  228. $expected->cacheFor(3600 * 24);
  229. $this->assertEquals($expected, $res);
  230. }
  231. }