|
| 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 | + |
| 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\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\Share\Exceptions\ShareNotFound; |
| 37 | +use OCP\Share\IManager; |
| 38 | +use OCP\Share\IShare; |
| 39 | +use Punic\Data; |
| 40 | +use Test\TestCase; |
| 41 | + |
| 42 | +class PublicPreviewControllerTest extends TestCase { |
| 43 | + |
| 44 | + /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */ |
| 45 | + private $previewManager; |
| 46 | + |
| 47 | + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ |
| 48 | + private $shareManager; |
| 49 | + |
| 50 | + /** @var PublicPreviewController */ |
| 51 | + private $controller; |
| 52 | + |
| 53 | + public function setUp() { |
| 54 | + parent::setUp(); |
| 55 | + |
| 56 | + $this->previewManager = $this->createMock(IPreview::class); |
| 57 | + $this->shareManager = $this->createMock(IManager::class); |
| 58 | + |
| 59 | + $this->controller = new PublicPreviewController( |
| 60 | + 'files_sharing', |
| 61 | + $this->createMock(IRequest::class), |
| 62 | + $this->shareManager, |
| 63 | + $this->previewManager |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public function testInvalidToken() { |
| 68 | + $res = $this->controller->getPreview('file', 10, 10, ''); |
| 69 | + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); |
| 70 | + |
| 71 | + $this->assertEquals($expected, $res); |
| 72 | + } |
| 73 | + |
| 74 | + public function testInvalidWidth() { |
| 75 | + $res = $this->controller->getPreview('file', 0); |
| 76 | + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); |
| 77 | + |
| 78 | + $this->assertEquals($expected, $res); |
| 79 | + } |
| 80 | + |
| 81 | + public function testInvalidHeight() { |
| 82 | + $res = $this->controller->getPreview('file', 10, 0); |
| 83 | + $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); |
| 84 | + |
| 85 | + $this->assertEquals($expected, $res); |
| 86 | + } |
| 87 | + |
| 88 | + public function testInvalidShare() { |
| 89 | + $this->shareManager->method('getShareByToken') |
| 90 | + ->with($this->equalTo('token')) |
| 91 | + ->willThrowException(new ShareNotFound()); |
| 92 | + |
| 93 | + $res = $this->controller->getPreview('file', 10, 10, 'token'); |
| 94 | + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); |
| 95 | + |
| 96 | + $this->assertEquals($expected, $res); |
| 97 | + } |
| 98 | + |
| 99 | + public function testShareNotAccessable() { |
| 100 | + $share = $this->createMock(IShare::class); |
| 101 | + $this->shareManager->method('getShareByToken') |
| 102 | + ->with($this->equalTo('token')) |
| 103 | + ->willReturn($share); |
| 104 | + |
| 105 | + $share->method('getPermissions') |
| 106 | + ->willReturn(0); |
| 107 | + |
| 108 | + $res = $this->controller->getPreview('file', 10, 10, 'token'); |
| 109 | + $expected = new DataResponse([], Http::STATUS_FORBIDDEN); |
| 110 | + |
| 111 | + $this->assertEquals($expected, $res); |
| 112 | + } |
| 113 | + |
| 114 | + public function testPreviewFile() { |
| 115 | + $share = $this->createMock(IShare::class); |
| 116 | + $this->shareManager->method('getShareByToken') |
| 117 | + ->with($this->equalTo('token')) |
| 118 | + ->willReturn($share); |
| 119 | + |
| 120 | + $share->method('getPermissions') |
| 121 | + ->willReturn(Constants::PERMISSION_READ); |
| 122 | + |
| 123 | + $file = $this->createMock(File::class); |
| 124 | + $share->method('getNode') |
| 125 | + ->willReturn($file); |
| 126 | + |
| 127 | + $preview = $this->createMock(ISimpleFile::class); |
| 128 | + $this->previewManager->method('getPreview') |
| 129 | + ->with($this->equalTo($file), 10, 10, false) |
| 130 | + ->willReturn($preview); |
| 131 | + |
| 132 | + $preview->method('getMimeType') |
| 133 | + ->willReturn('myMime'); |
| 134 | + |
| 135 | + $res = $this->controller->getPreview('file', 10, 10, 'token', true); |
| 136 | + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); |
| 137 | + $this->assertEquals($expected, $res); |
| 138 | + } |
| 139 | + |
| 140 | + public function testPreviewFolderInvalidFile() { |
| 141 | + $share = $this->createMock(IShare::class); |
| 142 | + $this->shareManager->method('getShareByToken') |
| 143 | + ->with($this->equalTo('token')) |
| 144 | + ->willReturn($share); |
| 145 | + |
| 146 | + $share->method('getPermissions') |
| 147 | + ->willReturn(Constants::PERMISSION_READ); |
| 148 | + |
| 149 | + $folder = $this->createMock(Folder::class); |
| 150 | + $share->method('getNode') |
| 151 | + ->willReturn($folder); |
| 152 | + |
| 153 | + $folder->method('get') |
| 154 | + ->with($this->equalTo('file')) |
| 155 | + ->willThrowException(new NotFoundException()); |
| 156 | + |
| 157 | + $res = $this->controller->getPreview('file', 10, 10, 'token', true); |
| 158 | + $expected = new DataResponse([], Http::STATUS_NOT_FOUND); |
| 159 | + $this->assertEquals($expected, $res); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + public function testPreviewFolderValidFile() { |
| 164 | + $share = $this->createMock(IShare::class); |
| 165 | + $this->shareManager->method('getShareByToken') |
| 166 | + ->with($this->equalTo('token')) |
| 167 | + ->willReturn($share); |
| 168 | + |
| 169 | + $share->method('getPermissions') |
| 170 | + ->willReturn(Constants::PERMISSION_READ); |
| 171 | + |
| 172 | + $folder = $this->createMock(Folder::class); |
| 173 | + $share->method('getNode') |
| 174 | + ->willReturn($folder); |
| 175 | + |
| 176 | + $file = $this->createMock(File::class); |
| 177 | + $folder->method('get') |
| 178 | + ->with($this->equalTo('file')) |
| 179 | + ->willReturn($file); |
| 180 | + |
| 181 | + $preview = $this->createMock(ISimpleFile::class); |
| 182 | + $this->previewManager->method('getPreview') |
| 183 | + ->with($this->equalTo($file), 10, 10, false) |
| 184 | + ->willReturn($preview); |
| 185 | + |
| 186 | + $preview->method('getMimeType') |
| 187 | + ->willReturn('myMime'); |
| 188 | + |
| 189 | + $res = $this->controller->getPreview('file', 10, 10, 'token', true); |
| 190 | + $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); |
| 191 | + $this->assertEquals($expected, $res); |
| 192 | + } |
| 193 | +} |
0 commit comments