summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-05-20 09:14:05 +0200
committerMorris Jobke <hey@morrisjobke.de>2015-05-20 09:14:05 +0200
commit0c231f5ac65b22654ea6a41b96eaa5422119cb42 (patch)
tree1d991db9c051b3e51d2baf255b4e0800a3e380d3
parent7015dd74ce27db6e7c7cb2596e61343ac9f22902 (diff)
parent5733878d6657c6e02ba3ded9e7eab4a9ba3dafe3 (diff)
downloadnextcloud-server-0c231f5ac65b22654ea6a41b96eaa5422119cb42.tar.gz
nextcloud-server-0c231f5ac65b22654ea6a41b96eaa5422119cb42.zip
Merge pull request #15959 from owncloud/backport-15596-remote-share-feedback-urls
Backport 15596 remote share feedback urls
-rw-r--r--apps/files_sharing/ajax/external.php2
-rw-r--r--apps/files_sharing/api/server2server.php2
-rw-r--r--apps/files_sharing/lib/external/manager.php113
-rw-r--r--apps/files_sharing/lib/external/storage.php2
-rw-r--r--apps/files_sharing/tests/external/managertest.php216
-rw-r--r--lib/private/share/share.php15
-rw-r--r--tests/lib/share/share.php58
7 files changed, 367 insertions, 41 deletions
diff --git a/apps/files_sharing/ajax/external.php b/apps/files_sharing/ajax/external.php
index 30c1f38801e..153285e11ff 100644
--- a/apps/files_sharing/ajax/external.php
+++ b/apps/files_sharing/ajax/external.php
@@ -38,8 +38,6 @@ $externalManager = new \OCA\Files_Sharing\External\Manager(
\OC::$server->getUserSession()->getUser()->getUID()
);
-$name = OCP\Files::buildNotExistingFileName('/', $name);
-
// check for ssl cert
if (substr($remote, 0, 5) === 'https' and !OC_Util::getUrlContent($remote)) {
\OCP\JSON::error(array('data' => array('message' => $l->t('Invalid or untrusted SSL certificate'))));
diff --git a/apps/files_sharing/api/server2server.php b/apps/files_sharing/api/server2server.php
index f2f7561598f..89a0262481c 100644
--- a/apps/files_sharing/api/server2server.php
+++ b/apps/files_sharing/api/server2server.php
@@ -64,8 +64,6 @@ class Server2Server {
$shareWith
);
- $name = \OCP\Files::buildNotExistingFileName('/', $name);
-
try {
$externalManager->addShare($remote, $token, '', $name, $owner, false, $shareWith, $remoteId);
diff --git a/apps/files_sharing/lib/external/manager.php b/apps/files_sharing/lib/external/manager.php
index 8985aeb3fce..aef7654d382 100644
--- a/apps/files_sharing/lib/external/manager.php
+++ b/apps/files_sharing/lib/external/manager.php
@@ -9,6 +9,7 @@
namespace OCA\Files_Sharing\External;
use OC\Files\Filesystem;
+use OCP\Files;
class Manager {
const STORAGE = '\OCA\Files_Sharing\External\Storage';
@@ -29,7 +30,7 @@ class Manager {
private $mountManager;
/**
- * @var \OC\Files\Storage\StorageFactory
+ * @var \OCP\Files\Storage\IStorageFactory
*/
private $storageLoader;
@@ -41,12 +42,12 @@ class Manager {
/**
* @param \OCP\IDBConnection $connection
* @param \OC\Files\Mount\Manager $mountManager
- * @param \OC\Files\Storage\StorageFactory $storageLoader
+ * @param \OCP\Files\Storage\IStorageFactory $storageLoader
* @param \OC\HTTPHelper $httpHelper
* @param string $uid
*/
public function __construct(\OCP\IDBConnection $connection, \OC\Files\Mount\Manager $mountManager,
- \OC\Files\Storage\StorageFactory $storageLoader, \OC\HTTPHelper $httpHelper, $uid) {
+ \OCP\Files\Storage\IStorageFactory $storageLoader, \OC\HTTPHelper $httpHelper, $uid) {
$this->connection = $connection;
$this->mountManager = $mountManager;
$this->storageLoader = $storageLoader;
@@ -65,33 +66,64 @@ class Manager {
* @param boolean $accepted
* @param string $user
* @param int $remoteId
- * @return mixed
+ * @return Mount|null
*/
public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) {
$user = $user ? $user : $this->uid;
$accepted = $accepted ? 1 : 0;
+ $name = Filesystem::normalizePath('/' . $name);
+
+ if (!$accepted) {
+ // To avoid conflicts with the mount point generation later,
+ // we only use a temporary mount point name here. The real
+ // mount point name will be generated when accepting the share,
+ // using the original share item name.
+ $tmpMountPointName = '{{TemporaryMountPointName#' . $name . '}}';
+ $mountPoint = $tmpMountPointName;
+ $hash = md5($tmpMountPointName);
+ $data = [
+ 'remote' => $remote,
+ 'share_token' => $token,
+ 'password' => $password,
+ 'name' => $name,
+ 'owner' => $owner,
+ 'user' => $user,
+ 'mountpoint' => $mountPoint,
+ 'mountpoint_hash' => $hash,
+ 'accepted' => $accepted,
+ 'remote_id' => $remoteId,
+ ];
+
+ $i = 1;
+ while (!$this->connection->insertIfNotExist('*PREFIX*share_external', $data, ['user', 'mountpoint_hash'])) {
+ // The external share already exists for the user
+ $data['mountpoint'] = $tmpMountPointName . '-' . $i;
+ $data['mountpoint_hash'] = md5($data['mountpoint']);
+ $i++;
+ }
+ return null;
+ }
- $mountPoint = Filesystem::normalizePath('/' . $name);
+ $mountPoint = Files::buildNotExistingFileName('/', $name);
+ $mountPoint = Filesystem::normalizePath('/' . $mountPoint);
+ $hash = md5($mountPoint);
$query = $this->connection->prepare('
INSERT INTO `*PREFIX*share_external`
(`remote`, `share_token`, `password`, `name`, `owner`, `user`, `mountpoint`, `mountpoint_hash`, `accepted`, `remote_id`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');
- $hash = md5($mountPoint);
$query->execute(array($remote, $token, $password, $name, $owner, $user, $mountPoint, $hash, $accepted, $remoteId));
- if ($accepted) {
- $options = array(
- 'remote' => $remote,
- 'token' => $token,
- 'password' => $password,
- 'mountpoint' => $mountPoint,
- 'owner' => $owner
- );
- return $this->mountShare($options);
- }
+ $options = array(
+ 'remote' => $remote,
+ 'token' => $token,
+ 'password' => $password,
+ 'mountpoint' => $mountPoint,
+ 'owner' => $owner
+ );
+ return $this->mountShare($options);
}
private function setupMounts() {
@@ -124,7 +156,7 @@ class Manager {
*/
private function getShare($id) {
$getShare = $this->connection->prepare('
- SELECT `remote`, `share_token`
+ SELECT `remote`, `remote_id`, `share_token`, `name`
FROM `*PREFIX*share_external`
WHERE `id` = ? AND `user` = ?');
$result = $getShare->execute(array($id, $this->uid));
@@ -142,12 +174,18 @@ class Manager {
$share = $this->getShare($id);
if ($share) {
+ $mountPoint = Files::buildNotExistingFileName('/', $share['name']);
+ $mountPoint = Filesystem::normalizePath('/' . $mountPoint);
+ $hash = md5($mountPoint);
+
$acceptShare = $this->connection->prepare('
UPDATE `*PREFIX*share_external`
- SET `accepted` = ?
+ SET `accepted` = ?,
+ `mountpoint` = ?,
+ `mountpoint_hash` = ?
WHERE `id` = ? AND `user` = ?');
- $acceptShare->execute(array(1, $id, $this->uid));
- $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $id, 'accept');
+ $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid));
+ $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept');
}
}
@@ -164,7 +202,7 @@ class Manager {
$removeShare = $this->connection->prepare('
DELETE FROM `*PREFIX*share_external` WHERE `id` = ? AND `user` = ?');
$removeShare->execute(array($id, $this->uid));
- $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $id, 'decline');
+ $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'decline');
}
}
@@ -173,13 +211,13 @@ class Manager {
*
* @param string $remote
* @param string $token
- * @param int $id
+ * @param int $remoteId Share id on the remote host
* @param string $feedback
* @return boolean
*/
- private function sendFeedbackToRemote($remote, $token, $id, $feedback) {
+ private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) {
- $url = $remote . \OCP\Share::BASE_PATH_TO_SHARE_API . '/' . $id . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
+ $url = rtrim($remote, '/') . \OCP\Share::BASE_PATH_TO_SHARE_API . '/' . $remoteId . '/' . $feedback . '?format=' . \OCP\Share::RESPONSE_FORMAT;
$fields = array('token' => $token);
$result = $this->httpHelper->post($url, $fields);
@@ -315,10 +353,29 @@ class Manager {
* @return array list of open server-to-server shares
*/
public function getOpenShares() {
- $openShares = $this->connection->prepare('SELECT * FROM `*PREFIX*share_external` WHERE `accepted` = ? AND `user` = ?');
- $result = $openShares->execute(array(0, $this->uid));
+ return $this->getShares(false);
+ }
+
+ /**
+ * return a list of shares for the user
+ *
+ * @param bool|null $accepted True for accepted only,
+ * false for not accepted,
+ * null for all shares of the user
+ * @return array list of open server-to-server shares
+ */
+ private function getShares($accepted) {
+ $query = 'SELECT * FROM `*PREFIX*share_external` WHERE `user` = ?';
+ $parameters = [$this->uid];
+ if (!is_null($accepted)) {
+ $query .= ' AND `accepted` = ?';
+ $parameters[] = (int) $accepted;
+ }
+ $query .= ' ORDER BY `id` ASC';
- return $result ? $openShares->fetchAll() : array();
+ $shares = $this->connection->prepare($query);
+ $result = $shares->execute($parameters);
+ return $result ? $shares->fetchAll() : [];
}
-} \ No newline at end of file
+}
diff --git a/apps/files_sharing/lib/external/storage.php b/apps/files_sharing/lib/external/storage.php
index 449d7a7eff5..bcd93f5de3f 100644
--- a/apps/files_sharing/lib/external/storage.php
+++ b/apps/files_sharing/lib/external/storage.php
@@ -70,7 +70,7 @@ class Storage extends DAV implements ISharedStorage {
'host' => $host,
'root' => $root,
'user' => $options['token'],
- 'password' => $options['password']
+ 'password' => (string)$options['password']
));
}
diff --git a/apps/files_sharing/tests/external/managertest.php b/apps/files_sharing/tests/external/managertest.php
new file mode 100644
index 00000000000..33a6465cf82
--- /dev/null
+++ b/apps/files_sharing/tests/external/managertest.php
@@ -0,0 +1,216 @@
+<?php
+/**
+ * ownCloud
+ *
+ * @author Joas Schilling
+ * @copyright 2015 Joas Schilling <nickvergessen@owncloud.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Files_Sharing\Tests\External;
+
+use OC\Files\Storage\StorageFactory;
+use OCA\Files_Sharing\External\Manager;
+use OCA\Files_Sharing\Tests\TestCase;
+
+class ManagerTest extends TestCase {
+
+ /** @var Manager **/
+ private $manager;
+
+ /** @var \OC\Files\Mount\Manager */
+ private $mountManager;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $httpHelper;
+
+ private $uid;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->uid = $this->getUniqueID('user');
+ $this->mountManager = new \OC\Files\Mount\Manager();
+ $this->httpHelper = $httpHelper = $this->getMockBuilder('\OC\HTTPHelper')->disableOriginalConstructor()->getMock();
+ /** @var \OC\HTTPHelper $httpHelper */
+ $this->manager = new Manager(
+ \OC::$server->getDatabaseConnection(),
+ $this->mountManager,
+ new StorageFactory(),
+ $httpHelper,
+ $this->uid
+ );
+ }
+
+ public function testAddShare() {
+
+ $shareData1 = [
+ 'remote' => 'http://localhost',
+ 'token' => 'token1',
+ 'password' => '',
+ 'name' => '/SharedFolder',
+ 'owner' => 'foobar',
+ 'accepted' => false,
+ 'user' => $this->uid,
+ ];
+ $shareData2 = $shareData1;
+ $shareData2['token'] = 'token2';
+ $shareData3 = $shareData1;
+ $shareData3['token'] = 'token3';
+
+ // Add a share for "user"
+ $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
+ $openShares = $this->manager->getOpenShares();
+ $this->assertCount(1, $openShares);
+ $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertNotMount('SharedFolder');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+
+ // Add a second share for "user" with the same name
+ $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
+ $openShares = $this->manager->getOpenShares();
+ $this->assertCount(2, $openShares);
+ $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ // New share falls back to "-1" appendix, because the name is already taken
+ $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
+
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertNotMount('SharedFolder');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
+
+ $this->httpHelper->expects($this->at(0))
+ ->method('post')
+ ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything());
+
+ // Accept the first share
+ $this->manager->acceptShare($openShares[0]['id']);
+
+ // Check remaining shares - Accepted
+ $acceptedShares = \Test_Helper::invokePrivate($this->manager, 'getShares', [true]);
+ $this->assertCount(1, $acceptedShares);
+ $shareData1['accepted'] = true;
+ $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
+ // Check remaining shares - Open
+ $openShares = $this->manager->getOpenShares();
+ $this->assertCount(1, $openShares);
+ $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
+
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertMount($shareData1['name']);
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
+
+ // Add another share for "user" with the same name
+ $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
+ $openShares = $this->manager->getOpenShares();
+ $this->assertCount(2, $openShares);
+ $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
+ // New share falls back to the original name (no "-\d", because the name is not taken)
+ $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}');
+
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertMount($shareData1['name']);
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
+
+ $this->httpHelper->expects($this->at(0))
+ ->method('post')
+ ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything());
+
+ // Decline the third share
+ $this->manager->declineShare($openShares[1]['id']);
+
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertMount($shareData1['name']);
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
+
+ // Check remaining shares - Accepted
+ $acceptedShares = \Test_Helper::invokePrivate($this->manager, 'getShares', [true]);
+ $this->assertCount(1, $acceptedShares);
+ $shareData1['accepted'] = true;
+ $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
+ // Check remaining shares - Open
+ $openShares = $this->manager->getOpenShares();
+ $this->assertCount(1, $openShares);
+ $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
+
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertMount($shareData1['name']);
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
+
+ $this->httpHelper->expects($this->at(0))
+ ->method('post')
+ ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything());
+ $this->httpHelper->expects($this->at(1))
+ ->method('post')
+ ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything());
+
+ $this->manager->removeUserShares($this->uid);
+ $this->assertEmpty(\Test_Helper::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
+
+ $this->mountManager->clear();
+ \Test_Helper::invokePrivate($this->manager, 'setupMounts');
+ $this->assertNotMount($shareData1['name']);
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
+ $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
+ }
+
+ /**
+ * @param array $expected
+ * @param array $actual
+ * @param int $share
+ * @param string $mountPoint
+ */
+ protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) {
+ $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
+ $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
+ $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
+ $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
+ $this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share);
+ $this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share);
+ $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
+ }
+
+ private function assertMount($mountPoint) {
+ $mountPoint = rtrim($mountPoint, '/');
+ $mount = $this->mountManager->find($this->getFullPath($mountPoint));
+ $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
+ $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
+ $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
+ $storage = $mount->getStorage();
+ $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
+ }
+
+ private function assertNotMount($mountPoint) {
+ $mountPoint = rtrim($mountPoint, '/');
+ $mount = $this->mountManager->find($this->getFullPath($mountPoint));
+ if ($mount) {
+ $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
+ $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
+ } else {
+ $this->assertNull($mount);
+ }
+ }
+
+ private function getFullPath($path) {
+ return '/' . $this->uid . '/files' . $path;
+ }
+}
diff --git a/lib/private/share/share.php b/lib/private/share/share.php
index a548698a61e..e52704be3d8 100644
--- a/lib/private/share/share.php
+++ b/lib/private/share/share.php
@@ -2289,15 +2289,17 @@ class Share extends \OC\Share\Constants {
*
* @param string $url
* @param array $fields post parameters
- * @return bool
+ * @return array
*/
private static function tryHttpPost($url, $fields) {
$protocol = 'https://';
- $success = false;
+ $result = [
+ 'success' => false,
+ 'result' => '',
+ ];
$try = 0;
- while ($success === false && $try < 2) {
+ while ($result['success'] === false && $try < 2) {
$result = \OC::$server->getHTTPHelper()->post($protocol . $url, $fields);
- $success = $result['success'];
$try++;
$protocol = 'http://';
}
@@ -2320,7 +2322,7 @@ class Share extends \OC\Share\Constants {
list($user, $remote) = explode('@', $shareWith, 2);
if ($user && $remote) {
- $url = $remote . self::BASE_PATH_TO_SHARE_API . '?format=' . self::RESPONSE_FORMAT;
+ $url = rtrim($remote, '/') . self::BASE_PATH_TO_SHARE_API . '?format=' . self::RESPONSE_FORMAT;
$local = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
@@ -2353,8 +2355,9 @@ class Share extends \OC\Share\Constants {
* @return bool
*/
private static function sendRemoteUnshare($remote, $id, $token) {
- $url = $remote . self::BASE_PATH_TO_SHARE_API . '/' . $id . '/unshare?format=' . self::RESPONSE_FORMAT;
+ $url = rtrim($remote, '/') . self::BASE_PATH_TO_SHARE_API . '/' . $id . '/unshare?format=' . self::RESPONSE_FORMAT;
$fields = array('token' => $token, 'format' => 'json');
+ $url = self::removeProtocolFromUrl($url);
$result = self::tryHttpPost($url, $fields);
$status = json_decode($result['result'], true);
diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php
index 43539866508..8f3d927be34 100644
--- a/tests/lib/share/share.php
+++ b/tests/lib/share/share.php
@@ -105,6 +105,12 @@ class Test_Share extends \Test\TestCase {
parent::tearDown();
}
+ protected function setHttpHelper($httpHelper) {
+ \OC::$server->registerService('HTTPHelper', function () use ($httpHelper) {
+ return $httpHelper;
+ });
+ }
+
public function testShareInvalidShareType() {
$message = 'Share type foobar is not valid for test.txt';
try {
@@ -1015,10 +1021,58 @@ class Test_Share extends \Test\TestCase {
);
}
+ public function dataRemoteShareUrlCalls() {
+ return [
+ ['admin@localhost', 'localhost'],
+ ['admin@https://localhost', 'localhost'],
+ ['admin@http://localhost', 'localhost'],
+ ['admin@localhost/subFolder', 'localhost/subFolder'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataRemoteShareUrlCalls
+ *
+ * @param string $shareWith
+ * @param string $urlHost
+ */
+ public function testRemoteShareUrlCalls($shareWith, $urlHost) {
+ $oldHttpHelper = \OC::$server->query('HTTPHelper');
+ $httpHelperMock = $this->getMockBuilder('OC\HttpHelper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->setHttpHelper($httpHelperMock);
+
+ $httpHelperMock->expects($this->at(0))
+ ->method('post')
+ ->with($this->stringStartsWith('https://' . $urlHost . '/ocs/v1.php/cloud/shares'), $this->anything())
+ ->willReturn(['success' => false, 'result' => 'Exception']);
+ $httpHelperMock->expects($this->at(1))
+ ->method('post')
+ ->with($this->stringStartsWith('http://' . $urlHost . '/ocs/v1.php/cloud/shares'), $this->anything())
+ ->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
+
+ \OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith, \OCP\Constants::PERMISSION_READ);
+ $shares = \OCP\Share::getItemShared('test', 'test.txt');
+ $share = array_shift($shares);
+
+ $httpHelperMock->expects($this->at(0))
+ ->method('post')
+ ->with($this->stringStartsWith('https://' . $urlHost . '/ocs/v1.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
+ ->willReturn(['success' => false, 'result' => 'Exception']);
+ $httpHelperMock->expects($this->at(1))
+ ->method('post')
+ ->with($this->stringStartsWith('http://' . $urlHost . '/ocs/v1.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything())
+ ->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]);
+
+ \OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $shareWith);
+ $this->setHttpHelper($oldHttpHelper);
+ }
+
/**
* @dataProvider dataProviderTestGroupItems
- * @param type $ungrouped
- * @param type $grouped
+ * @param array $ungrouped
+ * @param array $grouped
*/
function testGroupItems($ungrouped, $grouped) {