PublicPreviewControllerTest.php 5.7 KB

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