From 8a539ec0f6dc1650902a9340197d62688164d462 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 24 Jul 2017 16:48:17 +0200 Subject: [PATCH 1/3] Move shareinfo over to proper controller Signed-off-by: Roeland Jago Douma --- apps/files_sharing/appinfo/routes.php | 8 +- .../lib/Controller/ShareInfoController.php | 130 ++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 apps/files_sharing/lib/Controller/ShareInfoController.php diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 7a5f5650fc..104c4167e8 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -48,6 +48,12 @@ $application->registerRoutes($this, [ 'url' => '/ajax/publicpreview.php', 'verb' => 'GET', ], + + [ + 'name' => 'ShareInfo#info', + 'url' => '/shareinfo', + 'verb' => 'POST', + ], ], 'ocs' => [ /* @@ -123,5 +129,5 @@ $application->registerRoutes($this, [ ]); /** @var $this \OCP\Route\IRouter */ -$this->create('sharing_external_shareinfo', '/shareinfo') +$this->create('sharing_external_shareinfo', '/shareinfo3') ->actionInclude('files_sharing/ajax/shareinfo.php'); diff --git a/apps/files_sharing/lib/Controller/ShareInfoController.php b/apps/files_sharing/lib/Controller/ShareInfoController.php new file mode 100644 index 0000000000..d926721f06 --- /dev/null +++ b/apps/files_sharing/lib/Controller/ShareInfoController.php @@ -0,0 +1,130 @@ +shareManager = $shareManager; + $this->logger = $logger; + } + + /** + * @PublicPage + * @NoCSRFRequired + * + * @param string $t + * @param null $password + * @param null $dir + * @return JSONResponse + */ + public function info($t, $password = null, $dir = null) { + $this->logger->error('HERE!'); + try { + $share = $this->shareManager->getShareByToken($t); + } catch (ShareNotFound $e) { + return new JSONResponse([], Http::STATUS_NOT_FOUND); + } + + if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) { + return new JSONResponse([], Http::STATUS_FORBIDDEN); + } + + if (!($share->getPermissions() & Constants::PERMISSION_READ)) { + return new JSONResponse([], Http::STATUS_FORBIDDEN); + } + + // TODO FIX!!! + $isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE); + if (!$isWritable) { + // FIXME: should not add storage wrappers outside of preSetup, need to find a better way + $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); + \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { + return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); + }); + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); + } + + $node = $share->getNode(); + + if ($dir !== null && $node instanceof Folder) { + try { + $node = $node->get($dir); + } catch (NotFoundException $e) { + + } + } + + $result = [ + 'data' => $this->parseNode($node), + 'status' => 'success' + ]; + + return new JSONResponse($result); + } + + private function parseNode(Node $node) { + if ($node instanceof File) { + return $this->parseFile($node); + } + return $this->parseFolder($node); + } + + private function parseFile(File $file) { + return $this->format($file); + } + + private function parseFolder(Folder $folder) { + $data = $this->format($folder); + + $data['children'] = []; + + $nodes = $folder->getDirectoryListing(); + foreach ($nodes as $node) { + $data['children'][] = $this->parseNode($node); + } + + return $data; + } + + private function format(Node $node) { + $entry = []; + + $entry['id'] = $node->getId(); + $entry['parentId'] = $node->getParent()->getId(); + $entry['mtime'] = $node->getMTime(); + + $entry['name'] = $node->getName(); + $entry['permissions'] = $node->getPermissions(); + $entry['mimetype'] = $node->getMimetype(); + $entry['size'] = $node->getSize(); + $entry['type'] = $node->getType(); + $entry['etag'] = $node->getEtag(); + + return $entry; + } +} From c9d2e31d527190660c51a75741747178f029091c Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 25 Jul 2017 22:25:23 +0200 Subject: [PATCH 2/3] Remove old code + add Middleware * Add proper middleware for shareinfo * Remove old shareinfo routes Signed-off-by: Roeland Jago Douma --- apps/files_sharing/ajax/shareinfo.php | 111 ------------------ apps/files_sharing/appinfo/routes.php | 9 +- .../files_sharing/lib/AppInfo/Application.php | 8 ++ .../lib/Controller/ShareInfoController.php | 9 +- .../lib/Middleware/ShareInfoMiddleware.php | 84 +++++++++++++ 5 files changed, 96 insertions(+), 125 deletions(-) delete mode 100644 apps/files_sharing/ajax/shareinfo.php create mode 100644 apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php diff --git a/apps/files_sharing/ajax/shareinfo.php b/apps/files_sharing/ajax/shareinfo.php deleted file mode 100644 index a32b0a0732..0000000000 --- a/apps/files_sharing/ajax/shareinfo.php +++ /dev/null @@ -1,111 +0,0 @@ - - * @author Björn Schießle - * @author Lukas Reschke - * @author Morris Jobke - * @author Robin Appelman - * @author Roeland Jago Douma - * @author Stefan Weil - * @author Vincent Petry - * - * @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 - * - */ - -OCP\JSON::checkAppEnabled('files_sharing'); - -if (!isset($_GET['t'])) { - \OC_Response::setStatus(400); //400 Bad Request - exit; -} - -$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); -$federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); - -if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false) { - \OC_Response::setStatus(404); // 404 not found - exit; -} - -$token = $_GET['t']; - -$password = null; -if (isset($_POST['password'])) { - $password = $_POST['password']; -} - -$relativePath = null; -if (isset($_GET['dir'])) { - $relativePath = $_GET['dir']; -} - -$data = \OCA\Files_Sharing\Helper::setupFromToken($token, $relativePath, $password); - -/** @var \OCP\Share\IShare $share */ -$share = $data['share']; -// Load the files -$path = $data['realPath']; - -$isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE); -if (!$isWritable) { - // FIXME: should not add storage wrappers outside of preSetup, need to find a better way - $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); - \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { - return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); - }); - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); -} - -$rootInfo = \OC\Files\Filesystem::getFileInfo($path); -$rootView = new \OC\Files\View(''); - -if($rootInfo === false || !($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { - OCP\JSON::error(array('data' => 'Share is not readable.')); - exit(); -} - -/** - * @param \OCP\Files\FileInfo $dir - * @param \OC\Files\View $view - * @return array - */ -function getChildInfo($dir, $view, $sharePermissions) { - $children = $view->getDirectoryContent($dir->getPath()); - $result = array(); - foreach ($children as $child) { - $formatted = \OCA\Files\Helper::formatFileInfo($child); - if ($child->getType() === 'dir') { - $formatted['children'] = getChildInfo($child, $view, $sharePermissions); - } - $formatted['mtime'] = $formatted['mtime'] / 1000; - $formatted['permissions'] = $sharePermissions & (int)$formatted['permissions']; - $result[] = $formatted; - } - return $result; -} - -$result = \OCA\Files\Helper::formatFileInfo($rootInfo); -$result['mtime'] = $result['mtime'] / 1000; -$result['permissions'] = (int)$result['permissions'] & $share->getPermissions(); - - -if ($rootInfo->getType() === 'dir') { - $result['children'] = getChildInfo($rootInfo, $rootView, $share->getPermissions()); -} - -OCP\JSON::success(array('data' => $result)); diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php index 104c4167e8..310b1c46eb 100644 --- a/apps/files_sharing/appinfo/routes.php +++ b/apps/files_sharing/appinfo/routes.php @@ -26,8 +26,7 @@ * */ -$application = new \OCA\Files_Sharing\AppInfo\Application(); -$application->registerRoutes($this, [ +return [ 'resources' => [ 'ExternalShares' => ['url' => '/api/externalShares'], ], @@ -126,8 +125,4 @@ $application->registerRoutes($this, [ 'verb' => 'DELETE', ], ], -]); - -/** @var $this \OCP\Route\IRouter */ -$this->create('sharing_external_shareinfo', '/shareinfo3') - ->actionInclude('files_sharing/ajax/shareinfo.php'); +]; diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index db2175c344..3e95c738df 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -28,6 +28,7 @@ namespace OCA\Files_Sharing\AppInfo; use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware; +use OCA\Files_Sharing\Middleware\ShareInfoMiddleware; use OCA\Files_Sharing\MountProvider; use OCP\AppFramework\App; use OC\AppFramework\Utility\SimpleContainer; @@ -124,9 +125,16 @@ class Application extends App { ); }); + $container->registerService(ShareInfoMiddleware::class, function () use ($server) { + return new ShareInfoMiddleware( + $server->getShareManager() + ); + }); + // Execute middlewares $container->registerMiddleWare('SharingCheckMiddleware'); $container->registerMiddleWare('OCSShareAPIMiddleware'); + $container->registerMiddleWare(ShareInfoMiddleware::class); $container->registerService('MountProvider', function (IContainer $c) { /** @var \OCP\IServerContainer $server */ diff --git a/apps/files_sharing/lib/Controller/ShareInfoController.php b/apps/files_sharing/lib/Controller/ShareInfoController.php index d926721f06..696b064ac0 100644 --- a/apps/files_sharing/lib/Controller/ShareInfoController.php +++ b/apps/files_sharing/lib/Controller/ShareInfoController.php @@ -41,9 +41,9 @@ class ShareInfoController extends ApiController { * @param null $password * @param null $dir * @return JSONResponse + * @throws ShareNotFound */ public function info($t, $password = null, $dir = null) { - $this->logger->error('HERE!'); try { $share = $this->shareManager->getShareByToken($t); } catch (ShareNotFound $e) { @@ -79,12 +79,7 @@ class ShareInfoController extends ApiController { } } - $result = [ - 'data' => $this->parseNode($node), - 'status' => 'success' - ]; - - return new JSONResponse($result); + return new JSONResponse($this->parseNode($node)); } private function parseNode(Node $node) { diff --git a/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php b/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php new file mode 100644 index 0000000000..a069ecacc2 --- /dev/null +++ b/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php @@ -0,0 +1,84 @@ +shareManager = $shareManager; + } + + /** + * @param Controller $controller + * @param string $methodName + * @throws S2SException + */ + public function beforeController(Controller $controller, $methodName) { + if (!($controller instanceof ShareInfoController)) { + return; + } + + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { + throw new S2SException(); + } + } + + /** + * @param Controller $controller + * @param string $methodName + * @param \Exception $exception + * @throws \Exception + * @return Response + */ + public function afterException(Controller $controller, $methodName, \Exception $exception) { + if (!($controller instanceof ShareInfoController)) { + throw $exception; + } + + if ($exception instanceof S2SException) { + return new JSONResponse([], Http::STATUS_NOT_FOUND); + } + } + + /** + * @param Controller $controller + * @param string $methodName + * @param Response $response + * @return Response + */ + public function afterController(Controller $controller, $methodName, Response $response) { + if (!($controller instanceof ShareInfoController)) { + return $response; + } + + if (!($response instanceof JSONResponse)) { + return $response; + } + + $data = $response->getData(); + $status = 'error'; + + if ($response->getStatus() === Http::STATUS_OK) { + $status = 'success'; + } + + $response->setData([ + 'data' => $data, + 'status' => $status, + ]); + + return $response; + } +} From 685f27221289a2b33918048b62427a44573511bb Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 10 Aug 2017 16:25:58 +0200 Subject: [PATCH 3/3] Add unit tests Signed-off-by: Roeland Jago Douma --- .../lib/Controller/ShareInfoController.php | 31 +- .../lib/Middleware/ShareInfoMiddleware.php | 8 +- .../Controller/ShareInfoControllerTest.php | 278 ++++++++++++++++++ .../Middleware/ShareInfoMiddlewareTest.php | 136 +++++++++ 4 files changed, 437 insertions(+), 16 deletions(-) create mode 100644 apps/files_sharing/tests/Controller/ShareInfoControllerTest.php create mode 100644 apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php diff --git a/apps/files_sharing/lib/Controller/ShareInfoController.php b/apps/files_sharing/lib/Controller/ShareInfoController.php index 696b064ac0..ccf7b7093a 100644 --- a/apps/files_sharing/lib/Controller/ShareInfoController.php +++ b/apps/files_sharing/lib/Controller/ShareInfoController.php @@ -20,17 +20,19 @@ class ShareInfoController extends ApiController { /** @var IManager */ private $shareManager; - /** @var ILogger */ - private $logger; - + /** + * ShareInfoController constructor. + * + * @param string $appName + * @param IRequest $request + * @param IManager $shareManager + */ public function __construct($appName, IRequest $request, - IManager $shareManager, - ILogger $logger) { + IManager $shareManager) { parent::__construct($appName, $request); $this->shareManager = $shareManager; - $this->logger = $logger; } /** @@ -58,15 +60,9 @@ class ShareInfoController extends ApiController { return new JSONResponse([], Http::STATUS_FORBIDDEN); } - // TODO FIX!!! $isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE); if (!$isWritable) { - // FIXME: should not add storage wrappers outside of preSetup, need to find a better way - $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); - \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { - return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); - }); - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); + $this->addROWrapper(); } $node = $share->getNode(); @@ -122,4 +118,13 @@ class ShareInfoController extends ApiController { return $entry; } + + protected function addROWrapper() { + // FIXME: should not add storage wrappers outside of preSetup, need to find a better way + $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); + \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { + return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); + }); + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); + } } diff --git a/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php b/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php index a069ecacc2..56e9b48f57 100644 --- a/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php +++ b/apps/files_sharing/lib/Middleware/ShareInfoMiddleware.php @@ -25,7 +25,7 @@ class ShareInfoMiddleware extends Middleware { * @param string $methodName * @throws S2SException */ - public function beforeController(Controller $controller, $methodName) { + public function beforeController($controller, $methodName) { if (!($controller instanceof ShareInfoController)) { return; } @@ -42,7 +42,7 @@ class ShareInfoMiddleware extends Middleware { * @throws \Exception * @return Response */ - public function afterException(Controller $controller, $methodName, \Exception $exception) { + public function afterException($controller, $methodName, \Exception $exception) { if (!($controller instanceof ShareInfoController)) { throw $exception; } @@ -50,6 +50,8 @@ class ShareInfoMiddleware extends Middleware { if ($exception instanceof S2SException) { return new JSONResponse([], Http::STATUS_NOT_FOUND); } + + throw $exception; } /** @@ -58,7 +60,7 @@ class ShareInfoMiddleware extends Middleware { * @param Response $response * @return Response */ - public function afterController(Controller $controller, $methodName, Response $response) { + public function afterController($controller, $methodName, Response $response) { if (!($controller instanceof ShareInfoController)) { return $response; } diff --git a/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php b/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php new file mode 100644 index 0000000000..497b7f7d5a --- /dev/null +++ b/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php @@ -0,0 +1,278 @@ +shareManager = $this->createMock(ShareManager::class); + + $this->controller = $this->getMockBuilder(ShareInfoController::class) + ->setConstructorArgs([ + 'files_sharing', + $this->createMock(IRequest::class), + $this->shareManager + ]) + ->setMethods(['addROWrapper']) + ->getMock(); + } + + public function testNoShare() { + $this->shareManager->method('getShareByToken') + ->with('token') + ->willThrowException(new ShareNotFound()); + + $expected = new JSONResponse([], Http::STATUS_NOT_FOUND); + $this->assertEquals($expected, $this->controller->info('token')); + } + + public function testWrongPassword() { + $share = $this->createMock(IShare::class); + $share->method('getPassword') + ->willReturn('sharePass'); + + $this->shareManager->method('getShareByToken') + ->with('token') + ->willReturn($share); + $this->shareManager->method('checkPassword') + ->with($share, 'pass') + ->willReturn(false); + + $expected = new JSONResponse([], Http::STATUS_FORBIDDEN); + $this->assertEquals($expected, $this->controller->info('token', 'pass')); + } + + public function testNoReadPermissions() { + $share = $this->createMock(IShare::class); + $share->method('getPassword') + ->willReturn('sharePass'); + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_CREATE); + + $this->shareManager->method('getShareByToken') + ->with('token') + ->willReturn($share); + $this->shareManager->method('checkPassword') + ->with($share, 'pass') + ->willReturn(true); + + $expected = new JSONResponse([], Http::STATUS_FORBIDDEN); + $this->assertEquals($expected, $this->controller->info('token', 'pass')); + } + + private function prepareFile() { + $file = $this->createMock(File::class); + + $file->method('getId')->willReturn(42); + + $parent = $this->createMock(Folder::class); + $parent->method('getId')->willReturn(41); + $file->method('getParent')->willReturn($parent); + + $file->method('getMTime')->willReturn(1337); + $file->method('getName')->willReturn('file'); + $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ); + $file->method('getMimeType')->willReturn('mime/type'); + $file->method('getSize')->willReturn(1); + $file->method('getType')->willReturn('file'); + $file->method('getEtag')->willReturn('etag'); + + return $file; + } + + public function testInfoFile() { + $file = $this->prepareFile(); + + $share = $this->createMock(IShare::class); + $share->method('getPassword') + ->willReturn('sharePass'); + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE); + $share->method('getNode') + ->willReturn($file); + + $this->shareManager->method('getShareByToken') + ->with('token') + ->willReturn($share); + $this->shareManager->method('checkPassword') + ->with($share, 'pass') + ->willReturn(true); + + $expected = new JSONResponse([ + 'id' => 42, + 'parentId' => 41, + 'mtime' => 1337 , + 'name' => 'file', + 'permissions' => 1, + 'mimetype' => 'mime/type', + 'size' => 1, + 'type' => 'file', + 'etag' => 'etag', + ]); + $this->assertEquals($expected, $this->controller->info('token', 'pass')); + } + + public function testInfoFileRO() { + $file = $this->prepareFile(); + + $share = $this->createMock(IShare::class); + $share->method('getPassword') + ->willReturn('sharePass'); + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); + $share->method('getNode') + ->willReturn($file); + + $this->shareManager->method('getShareByToken') + ->with('token') + ->willReturn($share); + $this->shareManager->method('checkPassword') + ->with($share, 'pass') + ->willReturn(true); + + $this->controller->expects($this->once()) + ->method('addROWrapper'); + + $expected = new JSONResponse([ + 'id' => 42, + 'parentId' => 41, + 'mtime' => 1337 , + 'name' => 'file', + 'permissions' => 1, + 'mimetype' => 'mime/type', + 'size' => 1, + 'type' => 'file', + 'etag' => 'etag', + ]); + $this->assertEquals($expected, $this->controller->info('token', 'pass')); + } + + private function prepareFolder() { + $root = $this->createMock(Folder::class); + + $root->method('getId')->willReturn(42); + + $parent = $this->createMock(Folder::class); + $parent->method('getId')->willReturn(41); + $root->method('getParent')->willReturn($parent); + + $root->method('getMTime')->willReturn(1337); + $root->method('getName')->willReturn('root'); + $root->method('getPermissions')->willReturn(Constants::PERMISSION_READ); + $root->method('getMimeType')->willReturn('mime/type'); + $root->method('getSize')->willReturn(1); + $root->method('getType')->willReturn('folder'); + $root->method('getEtag')->willReturn('etag'); + + + //Subfolder + $sub = $this->createMock(Folder::class); + + $sub->method('getId')->willReturn(43); + $sub->method('getParent')->willReturn($root); + $sub->method('getMTime')->willReturn(1338); + $sub->method('getName')->willReturn('sub'); + $sub->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE); + $sub->method('getMimeType')->willReturn('mime/type'); + $sub->method('getSize')->willReturn(2); + $sub->method('getType')->willReturn('folder'); + $sub->method('getEtag')->willReturn('etag2'); + + $root->method('getDirectoryListing')->willReturn([$sub]); + + //Subfile + $file = $this->createMock(File::class); + $file->method('getId')->willReturn(88); + $file->method('getParent')->willReturn($sub); + $file->method('getMTime')->willReturn(1339); + $file->method('getName')->willReturn('file'); + $file->method('getPermissions')->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_DELETE); + $file->method('getMimeType')->willReturn('mime/type'); + $file->method('getSize')->willReturn(3); + $file->method('getType')->willReturn('file'); + $file->method('getEtag')->willReturn('etag3'); + + $sub->method('getDirectoryListing')->willReturn([$file]); + + return $root; + } + + public function testInfoFolder() { + $file = $this->prepareFolder(); + + $share = $this->createMock(IShare::class); + $share->method('getPassword') + ->willReturn('sharePass'); + $share->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE); + $share->method('getNode') + ->willReturn($file); + + $this->shareManager->method('getShareByToken') + ->with('token') + ->willReturn($share); + $this->shareManager->method('checkPassword') + ->with($share, 'pass') + ->willReturn(true); + + $expected = new JSONResponse([ + 'id' => 42, + 'parentId' => 41, + 'mtime' => 1337, + 'name' => 'root', + 'permissions' => 1, + 'mimetype' => 'mime/type', + 'size' => 1, + 'type' => 'folder', + 'etag' => 'etag', + 'children' => [ + [ + 'id' => 43, + 'parentId' => 42, + 'mtime' => 1338, + 'name' => 'sub', + 'permissions' => 3, + 'mimetype' => 'mime/type', + 'size' => 2, + 'type' => 'folder', + 'etag' => 'etag2', + 'children' => [ + [ + 'id' => 88, + 'parentId' => 43, + 'mtime' => 1339, + 'name' => 'file', + 'permissions' => 9, + 'mimetype' => 'mime/type', + 'size' => 3, + 'type' => 'file', + 'etag' => 'etag3', + ] + ], + ] + ], + ]); + $this->assertEquals($expected, $this->controller->info('token', 'pass')); + } +} diff --git a/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php b/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php new file mode 100644 index 0000000000..7f81bcbaa5 --- /dev/null +++ b/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php @@ -0,0 +1,136 @@ +shareManager = $this->createMock(ShareManager::class); + $this->middleware = new ShareInfoMiddleware($this->shareManager); + } + + public function testBeforeControllerNoShareInfo() { + $this->shareManager->expects($this->never()) + ->method($this->anything()); + + $this->middleware->beforeController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo'); + } + + public function testBeforeControllerShareInfoNoS2s() { + $this->shareManager->expects($this->once()) + ->method('outgoingServer2ServerSharesAllowed') + ->willReturn(false); + + $this->expectException(S2SException::class); + $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo'); + } + + public function testBeforeControllerShareInfo() { + $this->shareManager->expects($this->once()) + ->method('outgoingServer2ServerSharesAllowed') + ->willReturn(true); + + $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo'); + } + + public function testAfterExceptionNoShareInfo() { + $exeption = new \Exception(); + + try { + $this->middleware->afterException($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $exeption); + $this->fail(); + } catch (\Exception $e) { + $this->assertSame($exeption, $e); + } + } + + + public function testAfterExceptionNoS2S() { + $exeption = new \Exception(); + + try { + $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', $exeption); + $this->fail(); + } catch (\Exception $e) { + $this->assertSame($exeption, $e); + } + } + + public function testAfterExceptionS2S() { + $expected = new JSONResponse([], Http::STATUS_NOT_FOUND); + + $this->assertEquals( + $expected, + $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', new S2SException()) + ); + } + + public function testAfterControllerNoShareInfo() { + $response = $this->createMock(Http\Response::class); + + $this->assertEquals( + $response, + $this->middleware->afterController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $response) + ); + } + + public function testAfterControllerNoJSON() { + $response = $this->createMock(Http\Response::class); + + $this->assertEquals( + $response, + $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response) + ); + } + + public function testAfterControllerJSONok() { + $data = ['foo' => 'bar']; + $response = new JSONResponse($data); + + $expected = new JSONResponse([ + 'data' => $data, + 'status' => 'success', + ]); + + $this->assertEquals( + $expected, + $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response) + ); + } + + public function testAfterControllerJSONerror() { + $data = ['foo' => 'bar']; + $response = new JSONResponse($data, Http::STATUS_FORBIDDEN); + + $expected = new JSONResponse([ + 'data' => $data, + 'status' => 'error', + ], Http::STATUS_FORBIDDEN); + + $this->assertEquals( + $expected, + $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response) + ); + } +} + +class ShareInfoMiddlewareTestController extends Controller { + +}