diff options
author | Joas Schilling <coding@schilljs.com> | 2021-11-09 14:41:09 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-11-09 14:42:57 +0100 |
commit | 3b91e4cc48d92430959698602fedd222d70c1c07 (patch) | |
tree | 726bc5e36cc184119cc357a04e74bff849b617eb /tests | |
parent | fa036b2001e0505006b6f9fe24d3fc56af937b06 (diff) | |
download | nextcloud-server-3b91e4cc48d92430959698602fedd222d70c1c07.tar.gz nextcloud-server-3b91e4cc48d92430959698602fedd222d70c1c07.zip |
Add unit test for share enumeration method
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Share20/ManagerTest.php | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 5c4d71b34c7..f1a53200f5f 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -54,6 +54,7 @@ use OCP\Security\IHasher; use OCP\Security\ISecureRandom; use OCP\Share\Exceptions\AlreadySharedException; use OCP\Share\Exceptions\ShareNotFound; +use OCP\Share\IManager; use OCP\Share\IProviderFactory; use OCP\Share\IShare; use OCP\Share\IShareProvider; @@ -171,7 +172,7 @@ class ManagerTest extends \Test\TestCase { * @return MockBuilder */ private function createManagerMock() { - return $this->getMockBuilder('\OC\Share20\Manager') + return $this->getMockBuilder(Manager::class) ->setConstructorArgs([ $this->logger, $this->config, @@ -4521,6 +4522,83 @@ class ManagerTest extends \Test\TestCase { $this->assertSame($expects, $result); } + + public function dataCurrentUserCanEnumerateTargetUser(): array { + return [ + 'Full match guest' => [true, true, false, false, false, false, false, true], + 'Full match user' => [false, true, false, false, false, false, false, true], + 'Enumeration off guest' => [true, false, false, false, false, false, false, false], + 'Enumeration off user' => [false, false, false, false, false, false, false, false], + 'Enumeration guest' => [true, false, true, false, false, false, false, true], + 'Enumeration user' => [false, false, true, false, false, false, false, true], + + // Restricted enumerations guests never works + 'Guest phone' => [true, false, true, true, false, false, false, false], + 'Guest group' => [true, false, true, false, true, false, false, false], + 'Guest both' => [true, false, true, true, true, false, false, false], + + // Restricted enumerations users + 'User phone but not known' => [false, false, true, true, false, false, false, false], + 'User phone known' => [false, false, true, true, false, true, false, true], + 'User group but no match' => [false, false, true, false, true, false, false, false], + 'User group with match' => [false, false, true, false, true, false, true, true], + ]; + } + + /** + * @dataProvider dataCurrentUserCanEnumerateTargetUser + * @param bool $expected + */ + public function testCurrentUserCanEnumerateTargetUser(bool $currentUserIsGuest, bool $allowEnumerationFullMatch, bool $allowEnumeration, bool $limitEnumerationToPhone, bool $limitEnumerationToGroups, bool $isKnownToUser, bool $haveCommonGroup, bool $expected): void { + /** @var IManager|MockObject $manager */ + $manager = $this->createManagerMock() + ->setMethods([ + 'allowEnumerationFullMatch', + 'allowEnumeration', + 'limitEnumerationToPhone', + 'limitEnumerationToGroups', + ]) + ->getMock(); + + $manager->method('allowEnumerationFullMatch') + ->willReturn($allowEnumerationFullMatch); + $manager->method('allowEnumeration') + ->willReturn($allowEnumeration); + $manager->method('limitEnumerationToPhone') + ->willReturn($limitEnumerationToPhone); + $manager->method('limitEnumerationToGroups') + ->willReturn($limitEnumerationToGroups); + + $this->knownUserService->method('isKnownToUser') + ->with('current', 'target') + ->willReturn($isKnownToUser); + + $currentUser = null; + if (!$currentUserIsGuest) { + $currentUser = $this->createMock(IUser::class); + $currentUser->method('getUID') + ->willReturn('current'); + } + $targetUser = $this->createMock(IUser::class); + $targetUser->method('getUID') + ->willReturn('target'); + + if ($haveCommonGroup) { + $this->groupManager->method('getUserGroupIds') + ->willReturnMap([ + [$targetUser, ['gid1', 'gid2']], + [$currentUser, ['gid2', 'gid3']], + ]); + } else { + $this->groupManager->method('getUserGroupIds') + ->willReturnMap([ + [$targetUser, ['gid1', 'gid2']], + [$currentUser, ['gid3', 'gid4']], + ]); + } + + $this->assertSame($expected, $manager->currentUserCanEnumerateTargetUser($currentUser, $targetUser)); + } } class DummyFactory implements IProviderFactory { |