Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2016-08-29 17:50:01 +0300
committerJoas Schilling <coding@schilljs.com>2016-10-18 17:22:20 +0300
commit656e3f7a24d5ce64f10a1c8032f9b76fee1c9908 (patch)
treef51887328477f0db2869e7b52fc77e77f49e9968 /tests/Core
parent6375790263c5fadc72e48c9486b06b28d9e805cb (diff)
Check the mimetype before reading the content and catch exception
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests/Core')
-rw-r--r--tests/Core/Controller/AvatarControllerTest.php48
1 files changed, 46 insertions, 2 deletions
diff --git a/tests/Core/Controller/AvatarControllerTest.php b/tests/Core/Controller/AvatarControllerTest.php
index af75c4bb751..b87f73366fa 100644
--- a/tests/Core/Controller/AvatarControllerTest.php
+++ b/tests/Core/Controller/AvatarControllerTest.php
@@ -38,6 +38,7 @@ use OCP\Files\Cache\ICache;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
use OCP\IAvatar;
use OCP\IAvatarManager;
use OCP\IL10N;
@@ -334,7 +335,12 @@ class AvatarControllerTest extends \Test\TestCase {
//Mock node API call
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
- $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
+ $file->expects($this->once())
+ ->method('getContent')
+ ->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
+ $file->expects($this->once())
+ ->method('getMimeType')
+ ->willReturn('image/jpeg');
$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
$userFolder->method('get')->willReturn($file);
@@ -365,6 +371,39 @@ class AvatarControllerTest extends \Test\TestCase {
$this->assertEquals(['data' => ['message' => 'Please select a file.']], $response->getData());
}
+ public function testPostAvatarInvalidType() {
+ $file = $this->getMockBuilder('OCP\Files\File')
+ ->disableOriginalConstructor()->getMock();
+ $file->expects($this->never())
+ ->method('getContent');
+ $file->expects($this->exactly(2))
+ ->method('getMimeType')
+ ->willReturn('text/plain');
+ $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
+ $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
+ $userFolder->method('get')->willReturn($file);
+
+ $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file is not an image.']], Http::STATUS_BAD_REQUEST);
+ $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
+ }
+
+ public function testPostAvatarNotPermittedException() {
+ $file = $this->getMockBuilder('OCP\Files\File')
+ ->disableOriginalConstructor()->getMock();
+ $file->expects($this->once())
+ ->method('getContent')
+ ->willThrowException(new NotPermittedException());
+ $file->expects($this->once())
+ ->method('getMimeType')
+ ->willReturn('image/jpeg');
+ $userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
+ $this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
+ $userFolder->method('get')->willReturn($file);
+
+ $expectedResponse = new Http\JSONResponse(['data' => ['message' => 'The selected file cannot be read.']], Http::STATUS_BAD_REQUEST);
+ $this->assertEquals($expectedResponse, $this->avatarController->postAvatar('avatar.jpg'));
+ }
+
/**
* Test what happens if the upload of the avatar fails
*/
@@ -374,7 +413,12 @@ class AvatarControllerTest extends \Test\TestCase {
->will($this->throwException(new \Exception("foo")));
$file = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()->getMock();
- $file->method('getContent')->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
+ $file->expects($this->once())
+ ->method('getContent')
+ ->willReturn(file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
+ $file->expects($this->once())
+ ->method('getMimeType')
+ ->willReturn('image/jpeg');
$userFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock();
$this->rootFolder->method('getUserFolder')->with('userid')->willReturn($userFolder);
$userFolder->method('get')->willReturn($file);