PublicPreviewControllerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Files_Sharing\Tests\Controller;
  24. use OCA\Files_Sharing\Controller\PublicPreviewController;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\AppFramework\Http\FileDisplayResponse;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\Constants;
  30. use OCP\Files\File;
  31. use OCP\Files\Folder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\SimpleFS\ISimpleFile;
  34. use OCP\IPreview;
  35. use OCP\IRequest;
  36. use OCP\ISession;
  37. use OCP\Share\Exceptions\ShareNotFound;
  38. use OCP\Share\IManager;
  39. use OCP\Share\IShare;
  40. use PHPUnit\Framework\MockObject\MockObject;
  41. use Punic\Data;
  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. public function setUp() {
  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. }