summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2017-04-15 13:06:24 -0500
committerGitHub <noreply@github.com>2017-04-15 13:06:24 -0500
commit10290eb0060eb2194be2dd4fc8b707aa7eef5c0a (patch)
treeb71665687f9c19f5b6f8e9f34ffbd5a8a2d4f5e8 /tests
parentdafa9c740abee38bb54e9df05947bcf09857380a (diff)
parentcab41118f67e6a4743bac2cfcfa69a4bee28f2a3 (diff)
downloadnextcloud-server-10290eb0060eb2194be2dd4fc8b707aa7eef5c0a.tar.gz
nextcloud-server-10290eb0060eb2194be2dd4fc8b707aa7eef5c0a.zip
Merge pull request #2834 from nextcloud/accesListToShareManager
Access list to share manager
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Share20/DefaultShareProviderTest.php187
-rw-r--r--tests/lib/Share20/ManagerTest.php122
-rw-r--r--tests/lib/Share20/ShareHelperTest.php231
3 files changed, 532 insertions, 8 deletions
diff --git a/tests/lib/Share20/DefaultShareProviderTest.php b/tests/lib/Share20/DefaultShareProviderTest.php
index fce5668440d..0fa8aa3d0c6 100644
--- a/tests/lib/Share20/DefaultShareProviderTest.php
+++ b/tests/lib/Share20/DefaultShareProviderTest.php
@@ -1545,9 +1545,9 @@ class DefaultShareProviderTest extends \Test\TestCase {
$this->assertEquals(1, $stmt);
$id = $qb->getLastInsertId();
- $user1 = $this->getMock('\OCP\IUser');
+ $user1 = $this->createMock(IUser::class);
$user1->method('getUID')->willReturn('user1');
- $user2 = $this->getMock('\OCP\IUser');
+ $user2 = $this->createMock(IUser::class);
$user2->method('getUID')->willReturn('user2');
$this->userManager->method('get')->will($this->returnValueMap([
['user1', $user1],
@@ -1556,7 +1556,7 @@ class DefaultShareProviderTest extends \Test\TestCase {
$this->groupManager->method('get')->with('group')->willReturn(null);
- $file = $this->getMock('\OCP\Files\File');
+ $file = $this->createMock(File::class);
$file->method('getId')->willReturn(1);
$this->rootFolder->method('getUserFolder')->with('user1')->will($this->returnSelf());
@@ -2434,4 +2434,185 @@ class DefaultShareProviderTest extends \Test\TestCase {
$u3->delete();
$g1->delete();
}
+
+ public function testGetAccessListNoCurrentAccessRequired() {
+ $userManager = \OC::$server->getUserManager();
+ $groupManager = \OC::$server->getGroupManager();
+ $rootFolder = \OC::$server->getRootFolder();
+
+ $provider = new DefaultShareProvider(
+ $this->dbConn,
+ $userManager,
+ $groupManager,
+ $rootFolder
+ );
+
+ $u1 = $userManager->createUser('testShare1', 'test');
+ $u2 = $userManager->createUser('testShare2', 'test');
+ $u3 = $userManager->createUser('testShare3', 'test');
+ $u4 = $userManager->createUser('testShare4', 'test');
+ $u5 = $userManager->createUser('testShare5', 'test');
+
+ $g1 = $groupManager->createGroup('group1');
+ $g1->addUser($u3);
+ $g1->addUser($u4);
+
+ $u1Folder = $rootFolder->getUserFolder($u1->getUID());
+ $folder1 = $u1Folder->newFolder('foo');
+ $folder2 = $folder1->newFolder('baz');
+ $file1 = $folder2->newFile('bar');
+
+ $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
+ $this->assertCount(0, $result['users']);
+ $this->assertFalse($result['public']);
+
+ $shareManager = \OC::$server->getShareManager();
+ $share1 = $shareManager->newShare();
+ $share1->setNode($folder1)
+ ->setSharedBy($u1->getUID())
+ ->setSharedWith($u2->getUID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_USER)
+ ->setPermissions(\OCP\Constants::PERMISSION_ALL);
+ $share1 = $this->provider->create($share1);
+
+ $share2 = $shareManager->newShare();
+ $share2->setNode($folder2)
+ ->setSharedBy($u2->getUID())
+ ->setSharedWith($g1->getGID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
+ ->setPermissions(\OCP\Constants::PERMISSION_ALL);
+ $share2 = $this->provider->create($share2);
+
+ $shareManager->deleteFromSelf($share2, $u4->getUID());
+
+ $share3 = $shareManager->newShare();
+ $share3->setNode($file1)
+ ->setSharedBy($u3->getUID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
+ ->setPermissions(\OCP\Constants::PERMISSION_READ);
+ $share3 = $this->provider->create($share3);
+
+ $share4 = $shareManager->newShare();
+ $share4->setNode($file1)
+ ->setSharedBy($u3->getUID())
+ ->setSharedWith($u5->getUID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_USER)
+ ->setPermissions(\OCP\Constants::PERMISSION_READ);
+ $share4 = $this->provider->create($share4);
+
+ $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
+
+ $this->assertCount(4, $result['users']);
+ $this->assertContains('testShare2', $result['users']);
+ $this->assertContains('testShare3', $result['users']);
+ $this->assertContains('testShare4', $result['users']);
+ $this->assertContains('testShare5', $result['users']);
+ $this->assertTrue($result['public']);
+
+ $provider->delete($share1);
+ $provider->delete($share2);
+ $provider->delete($share3);
+ $provider->delete($share4);
+
+ $u1->delete();
+ $u2->delete();
+ $u3->delete();
+ $u4->delete();
+ $u5->delete();
+ $g1->delete();
+ }
+
+ public function testGetAccessListCurrentAccessRequired() {
+ $userManager = \OC::$server->getUserManager();
+ $groupManager = \OC::$server->getGroupManager();
+ $rootFolder = \OC::$server->getRootFolder();
+
+ $provider = new DefaultShareProvider(
+ $this->dbConn,
+ $userManager,
+ $groupManager,
+ $rootFolder
+ );
+
+ $u1 = $userManager->createUser('testShare1', 'test');
+ $u2 = $userManager->createUser('testShare2', 'test');
+ $u3 = $userManager->createUser('testShare3', 'test');
+ $u4 = $userManager->createUser('testShare4', 'test');
+ $u5 = $userManager->createUser('testShare5', 'test');
+
+ $g1 = $groupManager->createGroup('group1');
+ $g1->addUser($u3);
+ $g1->addUser($u4);
+
+ $u1Folder = $rootFolder->getUserFolder($u1->getUID());
+ $folder1 = $u1Folder->newFolder('foo');
+ $folder2 = $folder1->newFolder('baz');
+ $file1 = $folder2->newFile('bar');
+
+ $result = $provider->getAccessList([$folder1, $folder2, $file1], false);
+ $this->assertCount(0, $result['users']);
+ $this->assertFalse($result['public']);
+
+ $shareManager = \OC::$server->getShareManager();
+ $share1 = $shareManager->newShare();
+ $share1->setNode($folder1)
+ ->setSharedBy($u1->getUID())
+ ->setSharedWith($u2->getUID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_USER)
+ ->setPermissions(\OCP\Constants::PERMISSION_ALL);
+ $share1 = $this->provider->create($share1);
+
+ $share2 = $shareManager->newShare();
+ $share2->setNode($folder2)
+ ->setSharedBy($u2->getUID())
+ ->setSharedWith($g1->getGID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_GROUP)
+ ->setPermissions(\OCP\Constants::PERMISSION_ALL);
+ $share2 = $this->provider->create($share2);
+
+ $shareManager->deleteFromSelf($share2, $u4->getUID());
+
+ $share3 = $shareManager->newShare();
+ $share3->setNode($file1)
+ ->setSharedBy($u3->getUID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_LINK)
+ ->setPermissions(\OCP\Constants::PERMISSION_READ);
+ $share3 = $this->provider->create($share3);
+
+ $share4 = $shareManager->newShare();
+ $share4->setNode($file1)
+ ->setSharedBy($u3->getUID())
+ ->setSharedWith($u5->getUID())
+ ->setShareOwner($u1->getUID())
+ ->setShareType(\OCP\Share::SHARE_TYPE_USER)
+ ->setPermissions(\OCP\Constants::PERMISSION_READ);
+ $share4 = $this->provider->create($share4);
+
+ $result = $provider->getAccessList([$folder1, $folder2, $file1], true);
+
+ $this->assertCount(3, $result['users']);
+ $this->assertArrayHasKey('testShare2', $result['users']);
+ $this->assertArrayHasKey('testShare3', $result['users']);
+ $this->assertArrayHasKey('testShare5', $result['users']);
+ $this->assertTrue($result['public']);
+
+ $provider->delete($share1);
+ $provider->delete($share2);
+ $provider->delete($share3);
+ $provider->delete($share4);
+
+ $u1->delete();
+ $u2->delete();
+ $u3->delete();
+ $u4->delete();
+ $u5->delete();
+ $g1->delete();
+ }
}
diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php
index 5436b188350..42308a9d6a6 100644
--- a/tests/lib/Share20/ManagerTest.php
+++ b/tests/lib/Share20/ManagerTest.php
@@ -1022,12 +1022,12 @@ class ManagerTest extends \Test\TestCase {
public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup() {
$share = $this->manager->newShare();
- $sharedWith = $this->getMock('\OCP\IUser');
+ $sharedWith = $this->createMock(IUser::class);
$sharedWith->method('getUID')->willReturn('sharedWith');
$this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
- $path = $this->getMock('\OCP\Files\Node');
+ $path = $this->createMock(Node::class);
$share->setSharedWith('sharedWith')
->setNode($path)
@@ -1136,7 +1136,7 @@ class ManagerTest extends \Test\TestCase {
public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup() {
$share = $this->manager->newShare();
- $user = $this->getMock('\OCP\IUser');
+ $user = $this->createMock(IUser::class);
$share->setSharedBy('user')->setSharedWith('group');
$this->groupManager->method('get')->with('group')->willReturn(null);
@@ -2611,7 +2611,7 @@ class ManagerTest extends \Test\TestCase {
$share->setShareType(\OCP\Share::SHARE_TYPE_GROUP);
$share->setSharedWith('shareWith');
- $recipient = $this->getMock('\OCP\IUser');
+ $recipient = $this->createMock(IUser::class);
$this->groupManager->method('get')->with('shareWith')->willReturn(null);
$this->userManager->method('get')->with('recipient')->willReturn($recipient);
@@ -2639,7 +2639,6 @@ class ManagerTest extends \Test\TestCase {
$this->manager->moveShare($share, 'recipient');
}
-
/**
* @dataProvider dataTestShareProviderExists
*/
@@ -2737,6 +2736,119 @@ class ManagerTest extends \Test\TestCase {
$this->assertSame($expects, $result);
}
+
+ public function testGetAccessList() {
+ $factory = new DummyFactory2($this->createMock(IServerContainer::class));
+
+ $manager = new Manager(
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l,
+ $factory,
+ $this->userManager,
+ $this->rootFolder,
+ $this->eventDispatcher
+ );
+
+ $factory->setProvider($this->defaultProvider);
+ $extraProvider = $this->createMock(IShareProvider::class);
+ $factory->setSecondProvider($extraProvider);
+
+ $owner = $this->createMock(IUser::class);
+ $owner->expects($this->once())
+ ->method('getUID')
+ ->willReturn('owner');
+
+ $node = $this->createMock(Node::class);
+ $node->expects($this->once())
+ ->method('getOwner')
+ ->willReturn($owner);
+ $node->expects($this->once())
+ ->method('getId')
+ ->willReturn(42);
+
+ $userFolder = $this->createMock(Folder::class);
+ $file = $this->createMock(File::class);
+ $folder = $this->createMock(Folder::class);
+
+ $file->method('getParent')
+ ->willReturn($folder);
+ $file->method('getPath')
+ ->willReturn('/owner/files/folder/file');
+ $file->method('getId')
+ ->willReturn(23);
+ $folder->method('getParent')
+ ->willReturn($userFolder);
+ $folder->method('getPath')
+ ->willReturn('/owner/files/folder');
+ $userFolder->method('getById')
+ ->with($this->equalTo(42))
+ ->willReturn([$file]);
+ $userFolder->method('getPath')
+ ->willReturn('/owner/files');
+
+ $this->userManager->method('userExists')
+ ->with($this->equalTo('owner'))
+ ->willReturn(true);
+
+ $this->defaultProvider->method('getAccessList')
+ ->with(
+ $this->equalTo([$file, $folder]),
+ true
+ )
+ ->willReturn([
+ 'users' => [
+ 'user1' => [],
+ 'user2' => [],
+ 'user3' => [],
+ ],
+ 'public' => true,
+ ]);
+
+ $extraProvider->method('getAccessList')
+ ->with(
+ $this->equalTo([$file, $folder]),
+ true
+ )
+ ->willReturn([
+ 'users' => [
+ 'user3' => [],
+ 'user4' => [],
+ 'user5' => [],
+ ],
+ 'remote' => [
+ 'remote1',
+ ],
+ ]);
+
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->equalTo('owner'))
+ ->willReturn($userFolder);
+
+ $expected = [
+ 'users' => [
+ 'owner' => [
+ 'node_id' => 23,
+ 'node_path' => '/folder/file'
+ ]
+ , 'user1' => [], 'user2' => [], 'user3' => [], 'user4' => [], 'user5' => []],
+ 'remote' => [
+ 'remote1',
+ ],
+ 'public' => true,
+ ];
+
+ $result = $manager->getAccessList($node, true, true);
+
+ $this->assertSame($expected['public'], $result['public']);
+ $this->assertSame($expected['remote'], $result['remote']);
+ $this->assertSame(array_values($expected['users']), array_values($result['users']));
+
+ }
}
class DummyFactory implements IProviderFactory {
diff --git a/tests/lib/Share20/ShareHelperTest.php b/tests/lib/Share20/ShareHelperTest.php
new file mode 100644
index 00000000000..69609f9f724
--- /dev/null
+++ b/tests/lib/Share20/ShareHelperTest.php
@@ -0,0 +1,231 @@
+<?php
+/**
+ * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace Test\Share20;
+
+use OC\Share20\ShareHelper;
+use OCP\Files\Node;
+use OCP\Files\NotFoundException;
+use OCP\Share\IManager;
+use Test\TestCase;
+
+class ShareHelperTest extends TestCase {
+
+ /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
+ private $manager;
+
+ /** @var ShareHelper */
+ private $helper;
+
+ public function setUp() {
+ parent::setUp();
+
+ $this->manager = $this->createMock(IManager::class);
+
+ $this->helper = new ShareHelper($this->manager);
+ }
+
+ public function dataGetPathsForAccessList() {
+ return [
+ [[], [], false, [], [], false, [
+ 'users' => [],
+ 'remotes' => [],
+ ]],
+ [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, [], [], false, [
+ 'users' => ['user1' => 'foo', 'user2' => 'bar'],
+ 'remotes' => [],
+ ]],
+ [[], [], false, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
+ 'users' => [],
+ 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
+ ]],
+ [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
+ 'users' => ['user1' => 'foo', 'user2' => 'bar'],
+ 'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
+ ]],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetPathsForAccessList
+ */
+ public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) {
+ $this->manager->expects($this->once())
+ ->method('getAccessList')
+ ->willReturn([
+ 'users' => $userList,
+ 'remote' => $remoteList,
+ ]);
+
+ /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
+ $node = $this->createMock(Node::class);
+ /** @var ShareHelper|\PHPUnit_Framework_MockObject_MockObject $helper */
+ $helper = $this->getMockBuilder(ShareHelper::class)
+ ->setConstructorArgs([$this->manager])
+ ->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
+ ->getMock();
+
+ $helper->expects($resolveUsers ? $this->once() : $this->never())
+ ->method('getPathsForUsers')
+ ->with($node, $userList)
+ ->willReturn($userMap);
+
+ $helper->expects($resolveRemotes ? $this->once() : $this->never())
+ ->method('getPathsForRemotes')
+ ->with($node, $remoteList)
+ ->willReturn($remoteMap);
+
+ $this->assertSame($expected, $helper->getPathsForAccessList($node));
+ }
+
+ public function dataGetPathsForUsers() {
+ return [
+ [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
+ [
+ [
+ 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
+ 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
+ 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
+ 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
+ ],
+ [16 => 'SixTeen', 23 => 'TwentyThree', 42 => 'FortyTwo'],
+ [
+ 'test1' => '/foo/TwentyThree/FortyTwo',
+ 'test2' => '/bar/FortyTwo',
+ 'test3' => '/cat',
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetPathsForUsers
+ *
+ * @param array $users
+ * @param array $nodes
+ * @param array $expected
+ */
+ public function testGetPathsForUsers(array $users, array $nodes, array $expected) {
+ $lastNode = null;
+ foreach ($nodes as $nodeId => $nodeName) {
+ /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
+ $node = $this->createMock(Node::class);
+ $node->expects($this->any())
+ ->method('getId')
+ ->willReturn($nodeId);
+ $node->expects($this->any())
+ ->method('getName')
+ ->willReturn($nodeName);
+ if ($lastNode === null) {
+ $node->expects($this->any())
+ ->method('getParent')
+ ->willThrowException(new NotFoundException());
+ } else {
+ $node->expects($this->any())
+ ->method('getParent')
+ ->willReturn($lastNode);
+ }
+ $lastNode = $node;
+ }
+
+ $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForUsers', [$lastNode, $users]));
+ }
+
+ public function dataGetPathsForRemotes() {
+ return [
+ [[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
+ [
+ [
+ 'test1' => ['node_id' => 16, 'token' => 't1'],
+ 'test2' => ['node_id' => 23, 'token' => 't2'],
+ 'test3' => ['node_id' => 42, 'token' => 't3'],
+ 'test4' => ['node_id' => 48, 'token' => 't4'],
+ ],
+ [
+ 16 => '/admin/files/SixTeen',
+ 23 => '/admin/files/SixTeen/TwentyThree',
+ 42 => '/admin/files/SixTeen/TwentyThree/FortyTwo',
+ ],
+ [
+ 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
+ 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
+ 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetPathsForRemotes
+ *
+ * @param array $remotes
+ * @param array $nodes
+ * @param array $expected
+ */
+ public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected) {
+ $lastNode = null;
+ foreach ($nodes as $nodeId => $nodePath) {
+ /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
+ $node = $this->createMock(Node::class);
+ $node->expects($this->any())
+ ->method('getId')
+ ->willReturn($nodeId);
+ $node->expects($this->any())
+ ->method('getPath')
+ ->willReturn($nodePath);
+ if ($lastNode === null) {
+ $node->expects($this->any())
+ ->method('getParent')
+ ->willThrowException(new NotFoundException());
+ } else {
+ $node->expects($this->any())
+ ->method('getParent')
+ ->willReturn($lastNode);
+ }
+ $lastNode = $node;
+ }
+
+ $this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForRemotes', [$lastNode, $remotes]));
+ }
+
+ public function dataGetMountedPath() {
+ return [
+ ['/admin/files/foobar', '/foobar'],
+ ['/admin/files/foo/bar', '/foo/bar'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetMountedPath
+ * @param string $path
+ * @param string $expected
+ */
+ public function testGetMountedPath($path, $expected) {
+ /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
+ $node = $this->createMock(Node::class);
+ $node->expects($this->once())
+ ->method('getPath')
+ ->willReturn($path);
+
+ $this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
+ }
+}