PublicPreviewControllerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\IManager;
  22. use OCP\Share\IShare;
  23. use PHPUnit\Framework\MockObject\MockObject;
  24. use Test\TestCase;
  25. class PublicPreviewControllerTest extends TestCase {
  26. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  27. private $previewManager;
  28. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  29. private $shareManager;
  30. /** @var ITimeFactory|MockObject */
  31. private $timeFactory;
  32. /** @var PublicPreviewController */
  33. private $controller;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->previewManager = $this->createMock(IPreview::class);
  37. $this->shareManager = $this->createMock(IManager::class);
  38. $this->timeFactory = $this->createMock(ITimeFactory::class);
  39. $this->timeFactory->method('getTime')
  40. ->willReturn(1337);
  41. $this->overwriteService(ITimeFactory::class, $this->timeFactory);
  42. $this->controller = new PublicPreviewController(
  43. 'files_sharing',
  44. $this->createMock(IRequest::class),
  45. $this->shareManager,
  46. $this->createMock(ISession::class),
  47. $this->previewManager
  48. );
  49. }
  50. public function testInvalidToken() {
  51. $res = $this->controller->getPreview('', 'file', 10, 10, '');
  52. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  53. $this->assertEquals($expected, $res);
  54. }
  55. public function testInvalidWidth() {
  56. $res = $this->controller->getPreview('token', 'file', 0);
  57. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  58. $this->assertEquals($expected, $res);
  59. }
  60. public function testInvalidHeight() {
  61. $res = $this->controller->getPreview('token', 'file', 10, 0);
  62. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  63. $this->assertEquals($expected, $res);
  64. }
  65. public function testInvalidShare() {
  66. $this->shareManager->method('getShareByToken')
  67. ->with($this->equalTo('token'))
  68. ->willThrowException(new ShareNotFound());
  69. $res = $this->controller->getPreview('token', 'file', 10, 10);
  70. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  71. $this->assertEquals($expected, $res);
  72. }
  73. public function testShareNotAccessable() {
  74. $share = $this->createMock(IShare::class);
  75. $this->shareManager->method('getShareByToken')
  76. ->with($this->equalTo('token'))
  77. ->willReturn($share);
  78. $share->method('getPermissions')
  79. ->willReturn(0);
  80. $res = $this->controller->getPreview('token', 'file', 10, 10);
  81. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  82. $this->assertEquals($expected, $res);
  83. }
  84. public function testPreviewFile() {
  85. $share = $this->createMock(IShare::class);
  86. $this->shareManager->method('getShareByToken')
  87. ->with($this->equalTo('token'))
  88. ->willReturn($share);
  89. $share->method('getPermissions')
  90. ->willReturn(Constants::PERMISSION_READ);
  91. $file = $this->createMock(File::class);
  92. $share->method('getNode')
  93. ->willReturn($file);
  94. $preview = $this->createMock(ISimpleFile::class);
  95. $preview->method('getName')->willReturn('name');
  96. $preview->method('getMTime')->willReturn(42);
  97. $this->previewManager->method('getPreview')
  98. ->with($this->equalTo($file), 10, 10, false)
  99. ->willReturn($preview);
  100. $preview->method('getMimeType')
  101. ->willReturn('myMime');
  102. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  103. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  104. $expected->cacheFor(3600 * 24);
  105. $this->assertEquals($expected, $res);
  106. }
  107. public function testPreviewFolderInvalidFile() {
  108. $share = $this->createMock(IShare::class);
  109. $this->shareManager->method('getShareByToken')
  110. ->with($this->equalTo('token'))
  111. ->willReturn($share);
  112. $share->method('getPermissions')
  113. ->willReturn(Constants::PERMISSION_READ);
  114. $folder = $this->createMock(Folder::class);
  115. $share->method('getNode')
  116. ->willReturn($folder);
  117. $folder->method('get')
  118. ->with($this->equalTo('file'))
  119. ->willThrowException(new NotFoundException());
  120. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  121. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  122. $this->assertEquals($expected, $res);
  123. }
  124. public function testPreviewFolderValidFile() {
  125. $share = $this->createMock(IShare::class);
  126. $this->shareManager->method('getShareByToken')
  127. ->with($this->equalTo('token'))
  128. ->willReturn($share);
  129. $share->method('getPermissions')
  130. ->willReturn(Constants::PERMISSION_READ);
  131. $folder = $this->createMock(Folder::class);
  132. $share->method('getNode')
  133. ->willReturn($folder);
  134. $file = $this->createMock(File::class);
  135. $folder->method('get')
  136. ->with($this->equalTo('file'))
  137. ->willReturn($file);
  138. $preview = $this->createMock(ISimpleFile::class);
  139. $preview->method('getName')->willReturn('name');
  140. $preview->method('getMTime')->willReturn(42);
  141. $this->previewManager->method('getPreview')
  142. ->with($this->equalTo($file), 10, 10, false)
  143. ->willReturn($preview);
  144. $preview->method('getMimeType')
  145. ->willReturn('myMime');
  146. $res = $this->controller->getPreview('token', 'file', 10, 10, true);
  147. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']);
  148. $expected->cacheFor(3600 * 24);
  149. $this->assertEquals($expected, $res);
  150. }
  151. }