0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-03-15 00:43:23 +00:00

Add unit test for share enumeration method

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2021-11-09 14:41:09 +01:00
parent fa036b2001
commit 3b91e4cc48
No known key found for this signature in database
GPG key ID: 7076EA9751AACDDA

View file

@ -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 {