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.

PublicPreviewControllerTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Tests\Controller;
  26. use OCA\Files_Sharing\Controller\PublicPreviewController;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\Http\FileDisplayResponse;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\Constants;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\SimpleFS\ISimpleFile;
  36. use OCP\IPreview;
  37. use OCP\IRequest;
  38. use OCP\ISession;
  39. use OCP\Share\Exceptions\ShareNotFound;
  40. use OCP\Share\IManager;
  41. use OCP\Share\IShare;
  42. use PHPUnit\Framework\MockObject\MockObject;
  43. use Test\TestCase;
  44. class PublicPreviewControllerTest extends TestCase {
  45. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  46. private $previewManager;
  47. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  48. private $shareManager;
  49. /** @var ITimeFactory|MockObject */
  50. private $timeFactory;
  51. /** @var PublicPreviewController */
  52. private $controller;
  53. protected function setUp(): void {
  54. parent::setUp();
  55. $this->previewManager = $this->createMock(IPreview::class);
  56. $this->shareManager = $this->createMock(IManager::class);
  57. $this->timeFactory = $this->createMock(ITimeFactory::class);
  58. $this->timeFactory->method('getTime')
  59. ->willReturn(1337);
  60. $this->overwriteService(ITimeFactory::class, $this->timeFactory);
  61. $this->controller = new PublicPreviewController(
  62. 'files_sharing',
  63. $this->createMock(IRequest::class),
  64. $this->shareManager,
  65. $this->createMock(ISession::class),
  66. $this->previewManager
  67. );
  68. }
  69. public function testInvalidToken() {
  70. $res = $this->controller->getPreview('', 'file', 10, 10, '');
  71. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  72. $this->assertEquals($expected, $res);
  73. }
  74. public function testInvalidWidth() {
  75. $res = $this->controller->getPreview('token', 'file', 0);
  76. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  77. $this->assertEquals($expected, $res);
  78. }
  79. public function testInvalidHeight() {
  80. $res = $this->controller->getPreview('token', 'file', 10, 0);
  81. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  82. $this->assertEquals($expected, $res);
  83. }
  84. public function testInvalidShare() {
  85. $this->shareManager->method('getShareByToken')
  86. ->with($this->equalTo('token'))
  87. ->willThrowException(new ShareNotFound());
  88. $res = $this->controller->getPreview('token', 'file', 10, 10);
  89. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  90. $this->assertEquals($expected, $res);
  91. }
  92. public function testShareNotAccessable() {
  93. $share = $this->createMock(IShare::class);
  94. $this->shareManager->method('getShareByToken')
  95. ->with($this->equalTo('token'))
  96. ->willReturn($share);
  97. $share->method('getPermissions')
  98. ->willReturn(0);
  99. $res = $this->controller->getPreview('token', 'file', 10, 10);
  100. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  101. $this->assertEquals($expected, $res);
  102. }
  103. public function testPreviewFile() {
  104. $share = $this->createMock(IShare::class);
  105. $this->shareManager->method('getShareByToken')
  106. ->with($this->equalTo('token'))
  107. ->willReturn($share);
  108. $share->method('getPermissions')
  109. ->willReturn(Constants::PERMISSION_READ);
  110. $file = $this->createMock(File::class);
  111. $share->method('getNode')
  112. ->willReturn($file);
  113. $preview = $this->createMock(ISimpleFile::class);
  114. $preview->method('getName')->willReturn('name');
  115. $preview->method('getMTime')->willReturn(42);
  116. $this->previewManager->method('getPreview')
  117. ->with($this->equalTo($file), 10, 10, false)
  118. ->willReturn($preview);
  119. $preview->method('getMimeType')
  120. ->willReturn('myMime');
  121. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  122. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  123. $expected->cacheFor(3600 * 24);
  124. $this->assertEquals($expected, $res);
  125. }
  126. public function testPreviewFolderInvalidFile() {
  127. $share = $this->createMock(IShare::class);
  128. $this->shareManager->method('getShareByToken')
  129. ->with($this->equalTo('token'))
  130. ->willReturn($share);
  131. $share->method('getPermissions')
  132. ->willReturn(Constants::PERMISSION_READ);
  133. $folder = $this->createMock(Folder::class);
  134. $share->method('getNode')
  135. ->willReturn($folder);
  136. $folder->method('get')
  137. ->with($this->equalTo('file'))
  138. ->willThrowException(new NotFoundException());
  139. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  140. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  141. $this->assertEquals($expected, $res);
  142. }
  143. public function testPreviewFolderValidFile() {
  144. $share = $this->createMock(IShare::class);
  145. $this->shareManager->method('getShareByToken')
  146. ->with($this->equalTo('token'))
  147. ->willReturn($share);
  148. $share->method('getPermissions')
  149. ->willReturn(Constants::PERMISSION_READ);
  150. $folder = $this->createMock(Folder::class);
  151. $share->method('getNode')
  152. ->willReturn($folder);
  153. $file = $this->createMock(File::class);
  154. $folder->method('get')
  155. ->with($this->equalTo('file'))
  156. ->willReturn($file);
  157. $preview = $this->createMock(ISimpleFile::class);
  158. $preview->method('getName')->willReturn('name');
  159. $preview->method('getMTime')->willReturn(42);
  160. $this->previewManager->method('getPreview')
  161. ->with($this->equalTo($file), 10, 10, false)
  162. ->willReturn($preview);
  163. $preview->method('getMimeType')
  164. ->willReturn('myMime');
  165. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  166. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  167. $expected->cacheFor(3600 * 24);
  168. $this->assertEquals($expected, $res);
  169. }
  170. }