-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31748 from owncloud/bugfix/exception-handling-in-…
…preview-plugin [stable10] Exceptions need to properly be handled in the preview plugin
- Loading branch information
Showing
3 changed files
with
214 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
/** | ||
* @author Thomas Müller <[email protected]> | ||
* @copyright Copyright (c) 2018, ownCloud GmbH | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
*/ | ||
|
||
|
||
namespace OCA\DAV\Tests\Unit\Files; | ||
|
||
|
||
use OCA\DAV\Connector\Sabre\Exception\FileLocked; | ||
use OCA\DAV\Files\IFileNode; | ||
use OCA\DAV\Files\PreviewPlugin; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\Encryption\Exceptions\GenericEncryptionException; | ||
use OCP\Files\ForbiddenException; | ||
use OCP\Files\IPreviewNode; | ||
use OCP\Files\StorageNotAvailableException; | ||
use OCP\IImage; | ||
use OCP\ILogger; | ||
use OCP\Lock\LockedException; | ||
use Sabre\DAV\Exception\Forbidden; | ||
use Sabre\DAV\Exception\NotFound; | ||
use Sabre\DAV\Exception\ServiceUnavailable; | ||
use Sabre\DAV\Server; | ||
use Sabre\DAV\Tree; | ||
use Sabre\HTTP\RequestInterface; | ||
use Sabre\HTTP\ResponseInterface; | ||
use Test\TestCase; | ||
|
||
class PreviewPluginTest extends TestCase { | ||
|
||
/** @var RequestInterface | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $request; | ||
/** @var IPreviewNode | \PHPUnit_Framework_MockObject_MockObject */ | ||
private $previewNode; | ||
/** @var PreviewPlugin */ | ||
private $plugin; | ||
/** @var ResponseInterface| \PHPUnit_Framework_MockObject_MockObject */ | ||
private $response; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
|
||
/** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject $timeFactory */ | ||
$timeFactory = $this->createMock(ITimeFactory::class); | ||
$timeFactory->method('getTime')->willReturn(1234567); | ||
$this->plugin = new PreviewPlugin($timeFactory); | ||
|
||
$this->previewNode = $this->createMock(IPreviewNode::class); | ||
|
||
/** @var IFileNode | \PHPUnit_Framework_MockObject_MockObject $node */ | ||
$node = $this->createMock(IFileNode::class); | ||
$node->method('getNode')->willReturn($this->previewNode); | ||
|
||
/** @var Tree | \PHPUnit_Framework_MockObject_MockObject $tree */ | ||
$tree = $this->createMock(Tree::class); | ||
$tree->method('getNodeForPath')->willReturn($node); | ||
|
||
/** @var Server | \PHPUnit_Framework_MockObject_MockObject $server */ | ||
$server = $this->createMock(Server::class); | ||
$server->tree = $tree; | ||
|
||
$this->request = $this->createMock(RequestInterface::class); | ||
/** @var ResponseInterface | \PHPUnit_Framework_MockObject_MockObject $response */ | ||
$this->response = $this->createMock(ResponseInterface::class); | ||
$this->plugin->initialize($server); | ||
} | ||
|
||
public function testPreviewParamNotSet() { | ||
$this->request | ||
->expects($this->once()) | ||
->method('getQueryParameters') | ||
->will($this->returnValue([])); | ||
$this->assertTrue($this->plugin->httpGet($this->request, $this->response)); | ||
} | ||
|
||
/** | ||
* @param $expectedExceptionClass | ||
* @param $exception | ||
* @throws Forbidden | ||
* @throws \OCA\DAV\Connector\Sabre\Exception\FileLocked | ||
* @throws \Sabre\DAV\Exception\NotAuthenticated | ||
* @throws \Sabre\DAV\Exception\NotFound | ||
* @throws \Sabre\DAV\Exception\ServiceUnavailable | ||
* @dataProvider providesExceptions | ||
*/ | ||
public function testPreviewWithExceptions($expectedExceptionClass, $exception) { | ||
|
||
$this->previewNode->method('getThumbnail')->willThrowException($exception); | ||
|
||
$this->request->method('getQueryParameters')->willReturn([ | ||
'preview' => '1' | ||
]); | ||
|
||
$this->expectException($expectedExceptionClass); | ||
$this->plugin->httpGet($this->request, $this->response); | ||
} | ||
|
||
public function providesExceptions() { | ||
return [ | ||
[Forbidden::class, new GenericEncryptionException()], | ||
[ServiceUnavailable::class, new StorageNotAvailableException()], | ||
[Forbidden::class, new ForbiddenException('', false)], | ||
[FileLocked::class, new LockedException('')] | ||
]; | ||
} | ||
|
||
public function testPreviewNoImage() { | ||
|
||
$this->previewNode->method('getThumbnail')->willReturn(null); | ||
|
||
$this->request->method('getQueryParameters')->willReturn([ | ||
'preview' => '1' | ||
]); | ||
|
||
$this->expectException(NotFound::class); | ||
$this->plugin->httpGet($this->request, $this->response); | ||
} | ||
|
||
public function testPreviewCreatesImage() { | ||
$image = $this->createMock(IImage::class); | ||
$image->method('valid')->willReturn(true); | ||
$this->previewNode->method('getThumbnail')->willReturn($image); | ||
|
||
$this->request->method('getQueryParameters')->willReturn([ | ||
'preview' => '1' | ||
]); | ||
|
||
$this->response->method('setStatus')->with(200); | ||
$this->response->method('setBody')->with(''); | ||
$this->response->expects($this->exactly(4)) | ||
->method('setHeader')->withConsecutive( | ||
['Content-Type', 'application/octet-stream'], | ||
['Content-Disposition', 'attachment'], | ||
['Cache-Control', 'max-age=86400, must-revalidate'], | ||
['Expires', gmdate("D, d M Y H:i:s", 1234567 + 86400) . " GMT"] | ||
); | ||
|
||
$this->assertFalse($this->plugin->httpGet($this->request, $this->response)); | ||
} | ||
} |