]> source.dussan.org Git - nextcloud-server.git/commitdiff
[Share 2.0] When passing empty strings don't fail
authorRoeland Jago Douma <rullzer@owncloud.com>
Wed, 27 Jan 2016 14:42:11 +0000 (15:42 +0100)
committerRoeland Jago Douma <rullzer@owncloud.com>
Wed, 27 Jan 2016 19:34:06 +0000 (20:34 +0100)
The password and expiration date can be set to empty strings when
created. This is now handled gracefully.

apps/files_sharing/api/share20ocs.php
apps/files_sharing/tests/api/share20ocstest.php

index 3048eb7cd1a2f7b43fb7283603e047786a409457..081c3b1699901b8725b52c993db54cafd305e73c 100644 (file)
@@ -272,12 +272,12 @@ class Share20OCS {
                        if ($publicUpload === 'true') {
                                // Check if public upload is allowed
                                if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
-                                       return new \OC_OCS_Result(null, 403, '"public upload disabled by the administrator');
+                                       return new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator');
                                }
 
                                // Public upload can only be set for folders
                                if ($path instanceof \OCP\Files\File) {
-                                       return new \OC_OCS_Result(null, 404, '"public upload is only possible for public shared folders');
+                                       return new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders');
                                }
 
                                $share->setPermissions(
@@ -290,12 +290,16 @@ class Share20OCS {
                        }
 
                        // Set password
-                       $share->setPassword($this->request->getParam('password', null));
+                       $password = $this->request->getParam('password', '');
+
+                       if ($password !== '') {
+                               $share->setPassword($password);
+                       }
 
                        //Expire date
-                       $expireDate = $this->request->getParam('expireDate', null);
+                       $expireDate = $this->request->getParam('expireDate', '');
 
-                       if ($expireDate !== null) {
+                       if ($expireDate !== '') {
                                try {
                                        $expireDate = $this->parseDate($expireDate);
                                        $share->setExpirationDate($expireDate);
index f38af9881558e0ddff1026049528695ed81cab66..4bbcfb5a600c5a7a1c5dabe22486ca22d604b2bd 100644 (file)
@@ -31,19 +31,19 @@ use OCP\Files\IRootFolder;
 
 class Share20OCSTest extends \Test\TestCase {
 
-       /** @var \OC\Share20\Manager */
+       /** @var \OC\Share20\Manager | \PHPUnit_Framework_MockObject_MockObject */
        private $shareManager;
 
-       /** @var IGroupManager */
+       /** @var IGroupManager | \PHPUnit_Framework_MockObject_MockObject */
        private $groupManager;
 
-       /** @var IUserManager */
+       /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
        private $userManager;
 
-       /** @var IRequest */
+       /** @var IRequest | \PHPUnit_Framework_MockObject_MockObject */
        private $request;
 
-       /** @var IRootFolder */
+       /** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
        private $rootFolder;
 
        /** @var IURLGenerator */
@@ -78,6 +78,20 @@ class Share20OCSTest extends \Test\TestCase {
                );
        }
 
+       private function mockFormatShare() {
+               return $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')
+                       ->setConstructorArgs([
+                               $this->shareManager,
+                               $this->groupManager,
+                               $this->userManager,
+                               $this->request,
+                               $this->rootFolder,
+                               $this->urlGenerator,
+                               $this->currentUser
+                       ])->setMethods(['formatShare'])
+                       ->getMock();
+       }
+
        public function testDeleteShareShareNotFound() {
                $this->shareManager
                        ->expects($this->once())
@@ -696,4 +710,226 @@ class Share20OCSTest extends \Test\TestCase {
                $this->assertEquals($expected->getMeta(), $result->getMeta());
                $this->assertEquals($expected->getData(), $result->getData());
        }
+
+       public function testCreateShareLinkNoLinksAllowed() {
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\Folder');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+
+               $expected = new \OC_OCS_Result(null, 404, 'public link sharing is disabled by the administrator');
+               $result = $this->ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
+
+       public function testCreateShareLinkNoPublicUpload() {
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                               ['publicUpload', null, 'true'],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\Folder');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+               $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
+
+               $expected = new \OC_OCS_Result(null, 403, 'public upload disabled by the administrator');
+               $result = $this->ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
+
+       public function testCreateShareLinkPublicUploadFile() {
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                               ['publicUpload', null, 'true'],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\File');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+               $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
+               $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
+
+               $expected = new \OC_OCS_Result(null, 404, 'public upload is only possible for public shared folders');
+               $result = $this->ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
+
+       public function testCreateShareLinkPublicUploadFolder() {
+               $ocs = $this->mockFormatShare();
+
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                               ['publicUpload', null, 'true'],
+                               ['expireDate', '', ''],
+                               ['password', '', ''],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\Folder');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+               $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
+               $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
+
+               $currentUser = $this->currentUser;
+
+               $this->shareManager->expects($this->once())->method('createShare')->with(
+                       $this->callback(function (IShare $share) use ($path, $currentUser) {
+                               return $share->getPath() === $path &&
+                                       $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
+                                       $share->getPermissions() === \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE &&
+                                       $share->getSharedBy() === $currentUser &&
+                                       $share->getPassword() === null &&
+                                       $share->getExpirationDate() === null;
+                       })
+               );
+
+               $expected = new \OC_OCS_Result(null);
+               $result = $ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
+
+       public function testCreateShareLinkPassword() {
+               $ocs = $this->mockFormatShare();
+
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                               ['publicUpload', null, 'false'],
+                               ['expireDate', '', ''],
+                               ['password', '', 'password'],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\Folder');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+               $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
+               $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
+
+               $currentUser = $this->currentUser;
+
+               $this->shareManager->expects($this->once())->method('createShare')->with(
+                       $this->callback(function (IShare $share) use ($path, $currentUser) {
+                               return $share->getPath() === $path &&
+                               $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
+                               $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
+                               $share->getSharedBy() === $currentUser &&
+                               $share->getPassword() === 'password' &&
+                               $share->getExpirationDate() === null;
+                       })
+               );
+
+               $expected = new \OC_OCS_Result(null);
+               $result = $ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
+
+       public function testCreateShareValidExpireDate() {
+               $ocs = $this->mockFormatShare();
+
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                               ['publicUpload', null, 'false'],
+                               ['expireDate', '', '2000-01-01'],
+                               ['password', '', ''],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\Folder');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+               $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
+               $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
+
+               $currentUser = $this->currentUser;
+
+               $this->shareManager->expects($this->once())->method('createShare')->with(
+                       $this->callback(function (IShare $share) use ($path, $currentUser) {
+                               $date = new \DateTime('2000-01-01');
+                               $date->setTime(0,0,0);
+
+                               return $share->getPath() === $path &&
+                               $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
+                               $share->getPermissions() === \OCP\Constants::PERMISSION_READ &&
+                               $share->getSharedBy() === $currentUser &&
+                               $share->getPassword() === null &&
+                               $share->getExpirationDate() == $date;
+                       })
+               );
+
+               $expected = new \OC_OCS_Result(null);
+               $result = $ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
+
+       public function testCreateShareInvalidExpireDate() {
+               $ocs = $this->mockFormatShare();
+
+               $this->request
+                       ->method('getParam')
+                       ->will($this->returnValueMap([
+                               ['path', null, 'valid-path'],
+                               ['shareType', '-1', \OCP\Share::SHARE_TYPE_LINK],
+                               ['publicUpload', null, 'false'],
+                               ['expireDate', '', 'a1b2d3'],
+                               ['password', '', ''],
+                       ]));
+
+               $path = $this->getMock('\OCP\Files\Folder');
+               $this->rootFolder->method('getUserFolder')->with($this->currentUser->getUID())->will($this->returnSelf());
+               $this->rootFolder->method('get')->with('valid-path')->willReturn($path);
+
+               $this->shareManager->method('newShare')->willReturn(\OC::$server->getShareManager()->newShare());
+               $this->shareManager->method('shareApiAllowLinks')->willReturn(true);
+               $this->shareManager->method('shareApiLinkAllowPublicUpload')->willReturn(true);
+
+               $expected = new \OC_OCS_Result(null, 404, 'Invalid Date. Format must be YYYY-MM-DD.');
+               $result = $ocs->createShare();
+
+               $this->assertEquals($expected->getMeta(), $result->getMeta());
+               $this->assertEquals($expected->getData(), $result->getData());
+       }
 }