Skip to content

Commit d2dfa2b

Browse files
committed
More tests
* PreviewController test * PublicPreview test * Versions Preview test * Trash Preview test Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 66a37fe commit d2dfa2b

8 files changed

Lines changed: 783 additions & 45 deletions

File tree

apps/files_sharing/lib/Controller/PublicPreviewController.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323
namespace OCA\Files_Sharing\Controller;
2424

25-
use OC\PreviewManager;
2625
use OCP\AppFramework\Controller;
2726
use OCP\AppFramework\Http;
2827
use OCP\AppFramework\Http\DataResponse;
2928
use OCP\AppFramework\Http\FileDisplayResponse;
3029
use OCP\Constants;
3130
use OCP\Files\Folder;
3231
use OCP\Files\NotFoundException;
32+
use OCP\IPreview;
3333
use OCP\IRequest;
3434
use OCP\Share\Exceptions\ShareNotFound;
3535
use OCP\Share\IManager as ShareManager;
@@ -39,13 +39,13 @@ class PublicPreviewController extends Controller {
3939
/** @var ShareManager */
4040
private $shareManager;
4141

42-
/** @var PreviewManager */
42+
/** @var IPreview */
4343
private $previewManager;
4444

4545
public function __construct($appName,
4646
IRequest $request,
4747
ShareManager $shareManger,
48-
PreviewManager $previewManager) {
48+
IPreview $previewManager) {
4949
parent::__construct($appName, $request);
5050

5151
$this->shareManager = $shareManger;
@@ -71,11 +71,7 @@ public function getPreview(
7171
$a = false
7272
) {
7373

74-
if ($t === '') {
75-
return new DataResponse([], Http::STATUS_BAD_REQUEST);
76-
}
77-
78-
if ($x === 0 || $y === 0) {
74+
if ($t === '' || $x === 0 || $y === 0) {
7975
return new DataResponse([], Http::STATUS_BAD_REQUEST);
8076
}
8177

@@ -89,20 +85,18 @@ public function getPreview(
8985
return new DataResponse([], Http::STATUS_FORBIDDEN);
9086
}
9187

92-
$node = $share->getNode();
93-
94-
if ($node instanceof Folder) {
95-
try {
88+
try {
89+
$node = $share->getNode();
90+
if ($node instanceof Folder) {
9691
$file = $node->get($file);
97-
} catch (NotFoundException $e) {
98-
return new DataResponse([], Http::STATUS_NOT_FOUND);
92+
} else {
93+
$file = $node;
9994
}
100-
} else {
101-
$file = $node;
102-
}
103-
104-
$f = $this->previewManager->getPreview($file, $x, $y, !$a);
10595

106-
return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
96+
$f = $this->previewManager->getPreview($file, $x, $y, !$a);
97+
return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
98+
} catch (NotFoundException $e) {
99+
return new DataResponse([], Http::STATUS_NOT_FOUND);
100+
}
107101
}
108102
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}

apps/files_trashbin/lib/Controller/PreviewController.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ public function getPreview(
114114
return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
115115
} catch (NotFoundException $e) {
116116
return new DataResponse([], Http::STATUS_NOT_FOUND);
117-
} catch (\OC\PreviewNotAvailableException $e) {
118-
return new DataResponse([], Http::STATUS_NOT_FOUND);
119-
}catch(\Exception $e) {
120-
return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
121117
}
122-
123118
}
124119
}

0 commit comments

Comments
 (0)