PublicPreviewControllerTest.php 6.2 KB

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