diff --git a/apps/federatedfilesharing/lib/AddressHandler.php b/apps/federatedfilesharing/lib/AddressHandler.php index 8a33a5bcbed2..8fead1715d9c 100644 --- a/apps/federatedfilesharing/lib/AddressHandler.php +++ b/apps/federatedfilesharing/lib/AddressHandler.php @@ -151,13 +151,21 @@ public function compareAddresses($user1, $server1, $user2, $server2) { * @return string */ public function removeProtocolFromUrl($url) { - if (\strpos($url, 'https://') === 0) { - return \substr($url, \strlen('https://')); - } elseif (\strpos($url, 'http://') === 0) { - return \substr($url, \strlen('http://')); - } + // replace all characters before :// and :// itself + return \preg_replace('|^(.*?://)|', '', $url); + } - return $url; + /** + * Get a remote name without a protocol, potential file names + * and a trailing slash + * + * @param string $remote + * + * @return string + */ + public function normalizeRemote($remote) { + $fixedRemote = $this->fixRemoteURL($remote); + return $this->removeProtocolFromUrl($fixedRemote); } /** diff --git a/apps/federatedfilesharing/lib/AppInfo/Application.php b/apps/federatedfilesharing/lib/AppInfo/Application.php index 3f6033c0d78a..90b8e4cc3127 100644 --- a/apps/federatedfilesharing/lib/AppInfo/Application.php +++ b/apps/federatedfilesharing/lib/AppInfo/Application.php @@ -81,10 +81,11 @@ function ($c) use ($server) { function ($c) use ($server) { return new FedShareManager( $this->getFederatedShareProvider(), - $server->getDatabaseConnection(), + $c->query('Notifications'), $server->getUserManager(), $server->getActivityManager(), $server->getNotificationManager(), + $c->query('AddressHandler'), $server->getEventDispatcher() ); } @@ -97,10 +98,8 @@ function ($c) use ($server) { $c->query('AppName'), $c->query('Request'), $this->getFederatedShareProvider(), - $server->getDatabaseConnection(), $server->getAppManager(), $server->getUserManager(), - $c->query('Notifications'), $c->query('AddressHandler'), $c->query('FederatedShareManager') ); diff --git a/apps/federatedfilesharing/lib/Controller/RequestHandlerController.php b/apps/federatedfilesharing/lib/Controller/RequestHandlerController.php index b4607dabb342..252ad9fbceb6 100644 --- a/apps/federatedfilesharing/lib/Controller/RequestHandlerController.php +++ b/apps/federatedfilesharing/lib/Controller/RequestHandlerController.php @@ -32,12 +32,10 @@ use OCA\FederatedFileSharing\Exception\InvalidShareException; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FedShareManager; -use OCA\FederatedFileSharing\Notifications; use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\AppFramework\OCSController; use OCP\Constants; -use OCP\IDBConnection; use OCP\IRequest; use OCP\IUserManager; use OCP\Share; @@ -55,17 +53,12 @@ class RequestHandlerController extends OCSController { /** @var FederatedShareProvider */ private $federatedShareProvider; - /** @var IDBConnection */ - private $connection; - /** @var IAppManager */ private $appManager; + /** @var IUserManager */ private $userManager; - /** @var Notifications */ - private $notifications; - /** @var AddressHandler */ private $addressHandler; @@ -78,30 +71,24 @@ class RequestHandlerController extends OCSController { * @param string $appName * @param IRequest $request * @param FederatedShareProvider $federatedShareProvider - * @param IDBConnection $connection * @param IAppManager $appManager * @param IUserManager $userManager - * @param Notifications $notifications * @param AddressHandler $addressHandler * @param FedShareManager $fedShareManager */ public function __construct($appName, IRequest $request, FederatedShareProvider $federatedShareProvider, - IDBConnection $connection, IAppManager $appManager, IUserManager $userManager, - Notifications $notifications, AddressHandler $addressHandler, FedShareManager $fedShareManager ) { parent::__construct($appName, $request); $this->federatedShareProvider = $federatedShareProvider; - $this->connection = $connection; $this->appManager = $appManager; $this->userManager = $userManager; - $this->notifications = $notifications; $this->addressHandler = $addressHandler; $this->fedShareManager = $fedShareManager; } @@ -228,7 +215,6 @@ public function reShare($id) { if (!$reSharingAllowed) { return new Result(null, Http::STATUS_BAD_REQUEST); } - $share->setPermissions($share->getPermissions() & $permission); $result = $this->fedShareManager->reShare( $share, $remoteId, @@ -264,20 +250,8 @@ public function reShare($id) { public function acceptShare($id) { try { $this->assertOutgoingSharingEnabled(); - $share = $this->getValidShare($id); $this->fedShareManager->acceptShare($share); - if ($share->getShareOwner() !== $share->getSharedBy()) { - list(, $remote) = $this->addressHandler->splitUserRemote( - $share->getSharedBy() - ); - $remoteId = $this->federatedShareProvider->getRemoteId($share); - $this->notifications->sendAcceptShare( - $remote, - $remoteId, - $share->getToken() - ); - } } catch (NotSupportedException $e) { return new Result( null, @@ -303,13 +277,7 @@ public function acceptShare($id) { public function declineShare($id) { try { $this->assertOutgoingSharingEnabled(); - $share = $this->getValidShare($id); - if ($share->getShareOwner() !== $share->getSharedBy()) { - list(, $remote) = $this->addressHandler->splitUserRemote($share->getSharedBy()); - $remoteId = $this->federatedShareProvider->getRemoteId($share); - $this->notifications->sendDeclineShare($remote, $remoteId, $share->getToken()); - } $this->fedShareManager->declineShare($share); } catch (NotSupportedException $e) { return new Result( @@ -338,22 +306,8 @@ public function unshare($id) { try { $this->assertOutgoingSharingEnabled(); $token = $this->request->getParam('token', null); - $query = $this->connection->getQueryBuilder(); - $query->select('*')->from('share_external') - ->where( - $query->expr()->eq( - 'remote_id', $query->createNamedParameter($id) - ) - ) - ->andWhere( - $query->expr()->eq( - 'share_token', - $query->createNamedParameter($token) - ) - ); - $shareRow = $query->execute()->fetch(); - if ($token && $id && $shareRow !== false) { - $this->fedShareManager->unshare($shareRow); + if ($token && $id) { + $this->fedShareManager->unshare($id, $token); } } catch (NotSupportedException $e) { return new Result( diff --git a/apps/federatedfilesharing/lib/FedShareManager.php b/apps/federatedfilesharing/lib/FedShareManager.php index 9af939d7fa0a..b523dde69a4d 100644 --- a/apps/federatedfilesharing/lib/FedShareManager.php +++ b/apps/federatedfilesharing/lib/FedShareManager.php @@ -24,7 +24,6 @@ use OCA\Files_Sharing\Activity; use OCP\Activity\IManager as ActivityManager; use OCP\Files\NotFoundException; -use OCP\IDBConnection; use OCP\IUserManager; use OCP\Notification\IManager as NotificationManager; use OCP\Share\IShare; @@ -45,9 +44,9 @@ class FedShareManager { private $federatedShareProvider; /** - * @var IDBConnection + * @var Notifications */ - private $connection; + private $notifications; /** * @var IUserManager @@ -64,6 +63,11 @@ class FedShareManager { */ private $notificationManager; + /** + * @var AddressHandler + */ + private $addressHandler; + /** * @var EventDispatcherInterface */ @@ -73,24 +77,27 @@ class FedShareManager { * FedShareManager constructor. * * @param FederatedShareProvider $federatedShareProvider - * @param IDBConnection $connection + * @param Notifications $notifications * @param IUserManager $userManager * @param ActivityManager $activityManager * @param NotificationManager $notificationManager + * @param AddressHandler $addressHandler * @param EventDispatcherInterface $eventDispatcher */ public function __construct(FederatedShareProvider $federatedShareProvider, - IDBConnection $connection, + Notifications $notifications, IUserManager $userManager, ActivityManager $activityManager, NotificationManager $notificationManager, + AddressHandler $addressHandler, EventDispatcherInterface $eventDispatcher ) { $this->federatedShareProvider = $federatedShareProvider; - $this->connection = $connection; + $this->notifications = $notifications; $this->userManager = $userManager; $this->activityManager = $activityManager; $this->notificationManager = $notificationManager; + $this->addressHandler = $addressHandler; $this->eventDispatcher = $eventDispatcher; } @@ -119,35 +126,19 @@ public function createShare($shareWith, $sharedBy, $token ) { - \OC_Util::setupFS($shareWith); - $externalManager = new \OCA\Files_Sharing\External\Manager( - \OC::$server->getDatabaseConnection(), - \OC\Files\Filesystem::getMountManager(), - \OC\Files\Filesystem::getLoader(), - $this->notificationManager, - $this->eventDispatcher, - $shareWith - ); - $externalManager->addShare( - $remote, - $token, - '', - $name, - $owner, - false, - $shareWith, - $remoteId - ); - $shareId = $this->connection - ->lastInsertId('*PREFIX*share_external'); if ($ownerFederatedId === null) { - $ownerFederatedId = $owner . '@' . $this->cleanupRemote($remote); + $ownerFederatedId = $owner . '@' . $this->addressHandler->normalizeRemote($remote); } // if the owner of the share and the initiator are the same user // we also complete the federated share ID for the initiator if ($sharedByFederatedId === null && $owner === $sharedBy) { $sharedByFederatedId = $ownerFederatedId; } + + $shareId = $this->federatedShareProvider->addShare( + $remote, $token, $name, $owner, $shareWith, $remoteId + ); + $this->eventDispatcher->dispatch( '\OCA\FederatedFileSharing::remote_shareReceived', new GenericEvent( @@ -233,6 +224,7 @@ public function acceptShare(IShare $share) { $file, $link ); + $this->notifyRemote($share, [$this->notifications, 'sendAcceptShare']); } /** @@ -244,6 +236,7 @@ public function acceptShare(IShare $share) { * @throws \OCP\Files\NotFoundException */ public function declineShare(IShare $share) { + $this->notifyRemote($share, [$this->notifications, 'sendDeclineShare']); $uid = $this->getCorrectUid($share); $fileId = $share->getNode()->getId(); $this->federatedShareProvider->removeShareFromTable($share); @@ -262,36 +255,26 @@ public function declineShare(IShare $share) { /** * Unshare an item * - * @param array $shareRow + * @param int $id + * @param string $token * * @return void */ - public function unshare($shareRow) { - $remote = $this->cleanupRemote($shareRow['remote']); + public function unshare($id, $token) { + $shareRow = $this->federatedShareProvider->unshare($id, $token); + if ($shareRow === false) { + return; + } + $remote = $this->addressHandler->normalizeRemote($shareRow['remote']); $owner = $shareRow['owner'] . '@' . $remote; $mountpoint = $shareRow['mountpoint']; $user = $shareRow['user']; - $query = $this->connection->getQueryBuilder(); - $query->delete('share_external') - ->where( - $query->expr()->eq( - 'remote_id', - $query->createNamedParameter($shareRow['remote_id']) - ) - ) - ->andWhere( - $query->expr()->eq( - 'share_token', - $query->createNamedParameter($shareRow['share_token']) - ) - ); - $shareRow = $query->execute(); if ($shareRow['accepted']) { $path = \trim($mountpoint, '/'); } else { $path = \trim($shareRow['name'], '/'); } - $notification = $this->createNotification($shareRow['user']); + $notification = $this->createNotification($user); $notification->setObject('remote_share', (int) $shareRow['id']); $this->notificationManager->markProcessed($notification); $this->publishActivity( @@ -323,16 +306,25 @@ public function revoke(IShare $share) { * @return void */ public function updatePermissions(IShare $share, $permissions) { - $query = $this->connection->getQueryBuilder(); - $query->update($this->shareTable) - ->where( - $query->expr()->eq( - 'id', - $query->createNamedParameter($share->getId()) - ) - ) - ->set('permissions', $query->createNamedParameter($permissions)) - ->execute(); + $share->setPermissions($permissions); + $this->federatedShareProvider->update($share); + } + + /** + * @param IShare $share + * @param callable $callback + * + * @throws \OCP\Share\Exceptions\ShareNotFound + * @throws \OC\HintException + */ + protected function notifyRemote($share, $callback) { + if ($share->getShareOwner() !== $share->getSharedBy()) { + list(, $remote) = $this->addressHandler->splitUserRemote( + $share->getSharedBy() + ); + $remoteId = $this->federatedShareProvider->getRemoteId($share); + $callback($remote, $remoteId, $share->getToken()); + } } /** @@ -375,8 +367,8 @@ protected function publishActivity($affectedUser, */ protected function createNotification($uid) { $notification = $this->notificationManager->createNotification(); - $notification->setApp('files_sharing') - ->setUser($uid); + $notification->setApp('files_sharing'); + $notification->setUser($uid); return $notification; } @@ -432,16 +424,4 @@ protected function getCorrectUid(IShare $share) { return $share->getSharedBy(); } - - /** - * Strip a protocol from the remote URL - * - * @param string $remote - * - * @return string - */ - protected function cleanupRemote($remote) { - $remote = \substr($remote, \strpos($remote, '://') + 3); - return \rtrim($remote, '/'); - } } diff --git a/apps/federatedfilesharing/lib/FederatedShareProvider.php b/apps/federatedfilesharing/lib/FederatedShareProvider.php index c3b43f2d6f06..be5a36c30e62 100644 --- a/apps/federatedfilesharing/lib/FederatedShareProvider.php +++ b/apps/federatedfilesharing/lib/FederatedShareProvider.php @@ -74,6 +74,9 @@ class FederatedShareProvider implements IShareProvider { /** @var string */ private $externalShareTable = 'share_external'; + /** @var string */ + private $shareTable = 'share'; + /** @var IUserManager */ private $userManager; @@ -306,7 +309,7 @@ protected function getShareFromExternalShareTable(IShare $share) { */ private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) { $qb = $this->dbConnection->getQueryBuilder(); - $qb->insert('share') + $qb->insert($this->shareTable) ->setValue('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE)) ->setValue('item_type', $qb->createNamedParameter($itemType)) ->setValue('item_source', $qb->createNamedParameter($itemSource)) @@ -341,7 +344,7 @@ public function update(IShare $share) { * We allow updating the permissions of federated shares */ $qb = $this->dbConnection->getQueryBuilder(); - $qb->update('share') + $qb->update($this->shareTable) ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId()))) ->set('permissions', $qb->createNamedParameter($share->getPermissions())) ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner())) @@ -382,7 +385,7 @@ protected function sendPermissionUpdate(IShare $share) { */ protected function updateSuccessfulReShare($shareId, $token) { $query = $this->dbConnection->getQueryBuilder(); - $query->update('share') + $query->update($this->shareTable) ->where($query->expr()->eq('id', $query->createNamedParameter($shareId))) ->set('token', $query->createNamedParameter($token)) ->execute(); @@ -448,7 +451,7 @@ public function getChildren(IShare $parent) { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') - ->from('share') + ->from($this->shareTable) ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId()))) ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) ->orderBy('id'); @@ -535,7 +538,7 @@ public function removeShareFromTable(IShare $share) { */ private function removeShareFromTableById($shareId) { $qb = $this->dbConnection->getQueryBuilder(); - $qb->delete('share') + $qb->delete($this->shareTable) ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId))); $qb->execute(); @@ -566,7 +569,7 @@ public function getAllSharesBy($userId, $shareTypes, $nodeIDs, $reshares) { foreach ($nodeIdsChunks as $nodeIdsChunk) { // In federates sharing currently we have only one share_type_remote $qb->select('*') - ->from('share'); + ->from($this->shareTable); $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); @@ -616,7 +619,7 @@ public function getAllSharesBy($userId, $shareTypes, $nodeIDs, $reshares) { public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') - ->from('share'); + ->from($this->shareTable); $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); @@ -673,7 +676,7 @@ public function getShareById($id, $recipientId = null) { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') - ->from('share') + ->from($this->shareTable) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))); @@ -704,7 +707,7 @@ public function getSharesByPath(Node $path) { $qb = $this->dbConnection->getQueryBuilder(); $cursor = $qb->select('*') - ->from('share') + ->from($this->shareTable) ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId()))) ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) ->execute(); @@ -735,7 +738,7 @@ public function getSharedWith($userId, $shareType, $node, $limit, $offset) { //Get shares directly with this user $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') - ->from('share'); + ->from($this->shareTable); // Order by id $qb->orderBy('id'); @@ -775,7 +778,7 @@ public function getShareByToken($token) { $qb = $this->dbConnection->getQueryBuilder(); $cursor = $qb->select('*') - ->from('share') + ->from($this->shareTable) ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_REMOTE))) ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token))) ->execute(); @@ -807,7 +810,7 @@ private function getRawShare($id) { // Now fetch the inserted share and create a complete share object $qb = $this->dbConnection->getQueryBuilder(); $qb->select('*') - ->from('share') + ->from($this->shareTable) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); $cursor = $qb->execute(); @@ -899,7 +902,7 @@ public function userDeleted($uid, $shareType) { $qb = $this->dbConnection->getQueryBuilder(); - $qb->delete('share') + $qb->delete($this->shareTable) ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_REMOTE))) ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))) ->execute(); @@ -956,4 +959,70 @@ public function updateForRecipient(IShare $share, $recipient) { */ return $share; } + + /** + * @param int $remoteId + * @param string $shareToken + * @return mixed + */ + public function unshare($remoteId, $shareToken) { + $query = $this->dbConnection->getQueryBuilder(); + $query->select('*')->from($this->externalShareTable) + ->where( + $query->expr()->eq( + 'remote_id', $query->createNamedParameter($remoteId) + ) + ) + ->andWhere( + $query->expr()->eq( + 'share_token', + $query->createNamedParameter($shareToken) + ) + ); + $shareRow = $query->execute()->fetch(); + if ($shareRow !== false) { + $query = $this->dbConnection->getQueryBuilder(); + $query->delete($this->externalShareTable) + ->where( + $query->expr()->eq( + 'remote_id', + $query->createNamedParameter($shareRow['remote_id']) + ) + ) + ->andWhere( + $query->expr()->eq( + 'share_token', + $query->createNamedParameter($shareRow['share_token']) + ) + ); + $query->execute(); + } + return $shareRow; + } + + /** + * @param $remote + * @param $token + * @param $name + * @param $owner + * @param $shareWith + * @param $remoteId + * + * @return int + */ + public function addShare($remote, $token, $name, $owner, $shareWith, $remoteId) { + \OC_Util::setupFS($shareWith); + $externalManager = new \OCA\Files_Sharing\External\Manager( + $this->dbConnection, + \OC\Files\Filesystem::getMountManager(), + \OC\Files\Filesystem::getLoader(), + \OC::$server->getNotificationManager(), + \OC::$server->getEventDispatcher(), + $shareWith + ); + $externalManager->addShare( + $remote, $token, '', $name, $owner, false, $shareWith, $remoteId + ); + return $this->dbConnection->lastInsertId("*PREFIX*{$this->externalShareTable}"); + } } diff --git a/apps/federatedfilesharing/tests/AddressHandlerTest.php b/apps/federatedfilesharing/tests/AddressHandlerTest.php index d3dcc658dcc2..791d5763c626 100644 --- a/apps/federatedfilesharing/tests/AddressHandlerTest.php +++ b/apps/federatedfilesharing/tests/AddressHandlerTest.php @@ -193,4 +193,25 @@ public function dataTestFixRemoteUrl() { ['http://localhost/index.php/s/AShareToken', 'http://localhost'], ]; } + + /** + * @dataProvider dataTestNormalizeRemote + * + * @param string $url + * @param string $expected + */ + public function testNormalizeRemote($url, $expected) { + $result = $this->addressHandler->normalizeRemote($url); + $this->assertSame($expected, $result); + } + + public function dataTestNormalizeRemote() { + return [ + ['http://localhost', 'localhost'], + ['http://localhost/', 'localhost'], + ['http://localhost/index.php', 'localhost'], + ['http://localhost/index.php/s/AShareToken', 'localhost'], + ['http://localhost/index.php/s/AShareToken/', 'localhost'], + ]; + } } diff --git a/apps/federatedfilesharing/tests/Controller/RequestHandlerTest.php b/apps/federatedfilesharing/tests/Controller/RequestHandlerTest.php index f7bc9906a674..b6cfef444b9d 100644 --- a/apps/federatedfilesharing/tests/Controller/RequestHandlerTest.php +++ b/apps/federatedfilesharing/tests/Controller/RequestHandlerTest.php @@ -29,13 +29,11 @@ use OCA\FederatedFileSharing\AddressHandler; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FedShareManager; -use OCA\FederatedFileSharing\Notifications; use OCA\FederatedFileSharing\Controller\RequestHandlerController; use OCA\FederatedFileSharing\Tests\TestCase; use OCP\App\IAppManager; use OCP\AppFramework\Http; use OCP\Constants; -use OCP\IDBConnection; use OCP\IRequest; use OCP\IUserManager; use OCP\Share; @@ -54,11 +52,6 @@ class RequestHandlerTest extends TestCase { */ private $federatedShareProvider; - /** - * @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject - */ - private $connection; - /** * @var IAppManager | \PHPUnit_Framework_MockObject_MockObject */ @@ -74,11 +67,6 @@ class RequestHandlerTest extends TestCase { */ private $request; - /** - * @var Notifications | \PHPUnit_Framework_MockObject_MockObject - */ - private $notifications; - /** * @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */ @@ -101,16 +89,12 @@ protected function setUp() { FederatedShareProvider::class ) ->disableOriginalConstructor()->getMock(); - $this->connection = $this->getMockBuilder(IDBConnection::class) - ->disableOriginalConstructor()->getMock(); $this->appManager = $this->getMockBuilder(IAppManager::class) ->disableOriginalConstructor()->getMock(); $this->userManager = $this->getMockBuilder(IUserManager::class) ->disableOriginalConstructor()->getMock(); $this->request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor()->getMock(); - $this->notifications = $this->getMockBuilder(Notifications::class) - ->disableOriginalConstructor()->getMock(); $this->addressHandler = $this->getMockBuilder(AddressHandler::class) ->disableOriginalConstructor()->getMock(); $this->fedShareManager = $this->getMockBuilder(FedShareManager::class) @@ -119,10 +103,8 @@ protected function setUp() { 'federatedfilesharing', $this->request, $this->federatedShareProvider, - $this->connection, $this->appManager, $this->userManager, - $this->notifications, $this->addressHandler, $this->fedShareManager ); @@ -344,9 +326,6 @@ public function testAcceptSuccess() { $this->fedShareManager->expects($this->once()) ->method('acceptShare'); - $this->notifications->expects($this->once()) - ->method('sendAcceptShare'); - $response = $this->requestHandlerController->acceptShare(2); $this->assertEquals( Http::STATUS_CONTINUE, @@ -387,9 +366,6 @@ public function testDeclineSuccess() { $this->fedShareManager->expects($this->once()) ->method('declineShare'); - $this->notifications->expects($this->once()) - ->method('sendDeclineShare'); - $response = $this->requestHandlerController->declineShare(2); $this->assertEquals( Http::STATUS_CONTINUE, diff --git a/apps/federatedfilesharing/tests/FedShareManagerTest.php b/apps/federatedfilesharing/tests/FedShareManagerTest.php index ca9018698bb1..506aa2082bcc 100644 --- a/apps/federatedfilesharing/tests/FedShareManagerTest.php +++ b/apps/federatedfilesharing/tests/FedShareManagerTest.php @@ -21,14 +21,17 @@ namespace OCA\FederatedFileSharing\Tests; +use OCA\FederatedFileSharing\AddressHandler; use OCA\FederatedFileSharing\FederatedShareProvider; use OCA\FederatedFileSharing\FedShareManager; +use OCA\FederatedFileSharing\Notifications; use OCA\Files_Sharing\Activity; use OCP\Activity\IEvent; use OCP\Activity\IManager as ActivityManager; -use OCP\IDBConnection; use OCP\IUserManager; +use OCP\Notification\IAction; use OCP\Notification\IManager as NotificationManager; +use OCP\Notification\INotification; use OCP\Share\IShare; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -42,8 +45,8 @@ class FedShareManagerTest extends TestCase { /** @var FederatedShareProvider | \PHPUnit_Framework_MockObject_MockObject */ private $federatedShareProvider; - /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject */ - private $connection; + /** @var Notifications | \PHPUnit_Framework_MockObject_MockObject */ + private $notifications; /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */ private $userManager; @@ -57,6 +60,9 @@ class FedShareManagerTest extends TestCase { /** @var FedShareManager | \PHPUnit_Framework_MockObject_MockObject */ private $fedShareManager; + /** @var AddressHandler | \PHPUnit_Framework_MockObject_MockObject */ + private $addressHandler; + /** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */ private $eventDispatcher; @@ -66,15 +72,17 @@ protected function setUp() { $this->federatedShareProvider = $this->getMockBuilder( FederatedShareProvider::class )->disableOriginalConstructor()->getMock(); - - $this->connection = $this->getMockBuilder(IDBConnection::class) - ->getMock(); + $this->notifications = $this->getMockBuilder(Notifications::class) + ->disableOriginalConstructor()->getMock(); $this->userManager = $this->getMockBuilder(IUserManager::class) ->getMock(); $this->activityManager = $this->getMockBuilder(ActivityManager::class) ->getMock(); $this->notificationManager = $this->getMockBuilder(NotificationManager::class) ->getMock(); + $this->addressHandler = $this->getMockBuilder(AddressHandler::class) + ->disableOriginalConstructor()->getMock(); + $this->eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class) ->getMock(); @@ -82,10 +90,11 @@ protected function setUp() { ->setConstructorArgs( [ $this->federatedShareProvider, - $this->connection, + $this->notifications, $this->userManager, $this->activityManager, $this->notificationManager, + $this->addressHandler, $this->eventDispatcher ] ) @@ -93,6 +102,53 @@ protected function setUp() { ->getMock(); } + public function testCreateShare() { + $shareWith = 'Bob'; + $remote = 'server2'; + $remoteId = 42; + $owner = 'Alice'; + $name = 'McGee'; + $ownerFederatedId = '17'; + $sharedByFederatedId = '18'; + $sharedBy = 'Steve'; + $token = 'idk'; + + $event = $this->getMockBuilder(IEvent::class)->getMock(); + $event->method($this->anything()) + ->willReturnSelf(); + $event->expects($this->at(3)) + ->method($this->anything()) + ->with(Activity::SUBJECT_REMOTE_SHARE_RECEIVED) + ->willReturnSelf(); + + $this->activityManager->expects($this->once()) + ->method('generateEvent') + ->willReturn($event); + + $action = $this->getMockBuilder(IAction::class)->getMock(); + $action->method($this->anything())->willReturnSelf(); + $notification = $this->getMockBuilder(INotification::class)->getMock(); + $notification->method('createAction')->willReturn($action); + $notification->method($this->anything()) + ->willReturnSelf(); + + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + + $this->fedShareManager->createShare( + $shareWith, + $remote, + $remoteId, + $owner, + $name, + $ownerFederatedId, + $sharedByFederatedId, + $sharedBy, + $token + ); + } + public function testAcceptShare() { $this->fedShareManager->expects($this->once()) ->method('getFile') @@ -110,8 +166,7 @@ public function testAcceptShare() { ->willReturn($node); $event = $this->getMockBuilder(IEvent::class)->getMock(); - $event->expects($this->any()) - ->method($this->anything()) + $event->method($this->anything()) ->willReturnSelf(); $event->expects($this->at(3)) ->method($this->anything()) @@ -140,10 +195,16 @@ public function testDeclineShare() { $share->expects($this->once()) ->method('getNode') ->willReturn($node); + $share->method('getShareOwner') + ->willReturn('Alice'); + $share->method('getSharedBy') + ->willReturn('Bob'); + + $this->notifications->expects($this->once()) + ->method('sendDeclineShare'); $event = $this->getMockBuilder(IEvent::class)->getMock(); - $event->expects($this->any()) - ->method($this->anything()) + $event->method($this->anything()) ->willReturnSelf(); $event->expects($this->at(3)) ->method($this->anything()) @@ -157,6 +218,46 @@ public function testDeclineShare() { $this->fedShareManager->declineShare($share); } + public function testUnshare() { + $shareRow = [ + 'id' => 42, + 'remote' => 'peer', + 'remote_id' => 142, + 'share_token' => 'abc', + 'password' => '', + 'name' => 'McGee', + 'owner' => 'Alice', + 'user' => 'Bob', + 'mountpoint' => '/mount/', + 'accepted' => 1 + ]; + $this->federatedShareProvider + ->method('unshare') + ->willReturn($shareRow); + + $notification = $this->getMockBuilder(INotification::class)->getMock(); + $notification->method($this->anything()) + ->willReturnSelf(); + + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + + $event = $this->getMockBuilder(IEvent::class)->getMock(); + $event->method($this->anything()) + ->willReturnSelf(); + $event->expects($this->at(3)) + ->method($this->anything()) + ->with(Activity::SUBJECT_REMOTE_SHARE_UNSHARED) + ->willReturnSelf(); + + $this->activityManager->expects($this->once()) + ->method('generateEvent') + ->willReturn($event); + + $this->fedShareManager->unshare($shareRow['id'], $shareRow['share_token']); + } + public function testRevoke() { $share = $this->getMockBuilder(IShare::class) ->disableOriginalConstructor()->getMock();