123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
- declare(strict_types=1);
- /**
- * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\DAV\Tests\unit\DAV\Sharing;
- use OCA\DAV\CalDAV\Sharing\Backend as CalendarSharingBackend;
- use OCA\DAV\CalDAV\Sharing\Service;
- use OCA\DAV\CardDAV\Sharing\Backend as ContactsSharingBackend;
- use OCA\DAV\Connector\Sabre\Principal;
- use OCA\DAV\DAV\Sharing\Backend;
- use OCA\DAV\DAV\Sharing\IShareable;
- use OCP\ICache;
- use OCP\ICacheFactory;
- use OCP\IDBConnection;
- use OCP\IGroupManager;
- use OCP\IUserManager;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- class BackendTest extends TestCase {
- private IDBConnection|MockObject $db;
- private IUserManager|MockObject $userManager;
- private IGroupManager|MockObject $groupManager;
- private MockObject|Principal $principalBackend;
- private MockObject|ICache $shareCache;
- private LoggerInterface|MockObject $logger;
- private MockObject|ICacheFactory $cacheFactory;
- private Service|MockObject $calendarService;
- private CalendarSharingBackend $backend;
- protected function setUp(): void {
- parent::setUp();
- $this->db = $this->createMock(IDBConnection::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->principalBackend = $this->createMock(Principal::class);
- $this->cacheFactory = $this->createMock(ICacheFactory::class);
- $this->shareCache = $this->createMock(ICache::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->calendarService = $this->createMock(Service::class);
- $this->cacheFactory->expects(self::any())
- ->method('createInMemory')
- ->willReturn($this->shareCache);
- $this->backend = new CalendarSharingBackend(
- $this->userManager,
- $this->groupManager,
- $this->principalBackend,
- $this->cacheFactory,
- $this->calendarService,
- $this->logger,
- );
- }
- public function testUpdateShareCalendarBob(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $add = [
- [
- 'href' => 'principal:principals/users/bob',
- 'readOnly' => true,
- ]
- ];
- $principal = 'principals/users/bob';
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn($principal);
- $this->userManager->expects(self::once())
- ->method('userExists')
- ->willReturn(true);
- $this->groupManager->expects(self::never())
- ->method('groupExists');
- $this->calendarService->expects(self::once())
- ->method('shareWith')
- ->with($shareable->getResourceId(), $principal, Backend::ACCESS_READ);
- $this->backend->updateShares($shareable, $add, []);
- }
- public function testUpdateShareCalendarGroup(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $add = [
- [
- 'href' => 'principal:principals/groups/bob',
- 'readOnly' => true,
- ]
- ];
- $principal = 'principals/groups/bob';
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn($principal);
- $this->userManager->expects(self::never())
- ->method('userExists');
- $this->groupManager->expects(self::once())
- ->method('groupExists')
- ->willReturn(true);
- $this->calendarService->expects(self::once())
- ->method('shareWith')
- ->with($shareable->getResourceId(), $principal, Backend::ACCESS_READ);
- $this->backend->updateShares($shareable, $add, []);
- }
- public function testUpdateShareContactsBob(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $add = [
- [
- 'href' => 'principal:principals/users/bob',
- 'readOnly' => true,
- ]
- ];
- $principal = 'principals/users/bob';
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn($principal);
- $this->userManager->expects(self::once())
- ->method('userExists')
- ->willReturn(true);
- $this->groupManager->expects(self::never())
- ->method('groupExists');
- $this->calendarService->expects(self::once())
- ->method('shareWith')
- ->with($shareable->getResourceId(), $principal, Backend::ACCESS_READ);
- $this->backend->updateShares($shareable, $add, []);
- }
- public function testUpdateShareContactsGroup(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $add = [
- [
- 'href' => 'principal:principals/groups/bob',
- 'readOnly' => true,
- ]
- ];
- $principal = 'principals/groups/bob';
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn($principal);
- $this->userManager->expects(self::never())
- ->method('userExists');
- $this->groupManager->expects(self::once())
- ->method('groupExists')
- ->willReturn(true);
- $this->calendarService->expects(self::once())
- ->method('shareWith')
- ->with($shareable->getResourceId(), $principal, Backend::ACCESS_READ);
- $this->backend->updateShares($shareable, $add, []);
- }
- public function testUpdateShareCircle(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $add = [
- [
- 'href' => 'principal:principals/circles/bob',
- 'readOnly' => true,
- ]
- ];
- $principal = 'principals/groups/bob';
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn($principal);
- $this->userManager->expects(self::never())
- ->method('userExists');
- $this->groupManager->expects(self::once())
- ->method('groupExists')
- ->willReturn(true);
- $this->calendarService->expects(self::once())
- ->method('shareWith')
- ->with($shareable->getResourceId(), $principal, Backend::ACCESS_READ);
- $this->backend->updateShares($shareable, $add, []);
- }
- public function testUnshareBob(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $remove = [
- [
- 'href' => 'principal:principals/users/bob',
- 'readOnly' => true,
- ]
- ];
- $principal = 'principals/users/bob';
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn($principal);
- $this->calendarService->expects(self::once())
- ->method('deleteShare')
- ->with($shareable->getResourceId(), $principal);
- $this->calendarService->expects(self::once())
- ->method('hasGroupShare')
- ->willReturn(false);
- $this->calendarService->expects(self::never())
- ->method('unshare');
- $this->backend->updateShares($shareable, [], $remove);
- }
- public function testUnshareWithBobGroup(): void {
- $shareable = $this->createConfiguredMock(IShareable::class, [
- 'getOwner' => 'principals/users/alice',
- 'getResourceId' => 42,
- ]);
- $remove = [
- [
- 'href' => 'principal:principals/users/bob',
- 'readOnly' => true,
- ]
- ];
- $oldShares = [
- [
- 'href' => 'principal:principals/groups/bob',
- 'commonName' => 'bob',
- 'status' => 1,
- 'readOnly' => true,
- '{http://owncloud.org/ns}principal' => 'principals/groups/bob',
- '{http://owncloud.org/ns}group-share' => true,
- ]
- ];
- $this->shareCache->expects(self::once())
- ->method('clear');
- $this->principalBackend->expects(self::once())
- ->method('findByUri')
- ->willReturn('principals/users/bob');
- $this->calendarService->expects(self::once())
- ->method('deleteShare')
- ->with($shareable->getResourceId(), 'principals/users/bob');
- $this->calendarService->expects(self::once())
- ->method('hasGroupShare')
- ->with($oldShares)
- ->willReturn(true);
- $this->calendarService->expects(self::once())
- ->method('unshare')
- ->with($shareable->getResourceId(), 'principals/users/bob');
- $this->backend->updateShares($shareable, [], $remove, $oldShares);
- }
- public function testGetShares(): void {
- $resourceId = 42;
- $principal = 'principals/groups/bob';
- $rows = [
- [
- 'principaluri' => $principal,
- 'access' => Backend::ACCESS_READ,
- ]
- ];
- $expected = [
- [
- 'href' => 'principal:principals/groups/bob',
- 'commonName' => 'bob',
- 'status' => 1,
- 'readOnly' => true,
- '{http://owncloud.org/ns}principal' => $principal,
- '{http://owncloud.org/ns}group-share' => true,
- ]
- ];
- $this->shareCache->expects(self::once())
- ->method('get')
- ->with((string)$resourceId)
- ->willReturn(null);
- $this->calendarService->expects(self::once())
- ->method('getShares')
- ->with($resourceId)
- ->willReturn($rows);
- $this->principalBackend->expects(self::once())
- ->method('getPrincipalByPath')
- ->with($principal)
- ->willReturn(['uri' => $principal, '{DAV:}displayname' => 'bob']);
- $this->shareCache->expects(self::once())
- ->method('set')
- ->with((string)$resourceId, $expected);
- $result = $this->backend->getShares($resourceId);
- $this->assertEquals($expected, $result);
- }
- public function testGetSharesAddressbooks(): void {
- $service = $this->createMock(\OCA\DAV\CardDAV\Sharing\Service::class);
- $backend = new ContactsSharingBackend(
- $this->userManager,
- $this->groupManager,
- $this->principalBackend,
- $this->cacheFactory,
- $service,
- $this->logger);
- $resourceId = 42;
- $principal = 'principals/groups/bob';
- $rows = [
- [
- 'principaluri' => $principal,
- 'access' => Backend::ACCESS_READ,
- ]
- ];
- $expected = [
- [
- 'href' => 'principal:principals/groups/bob',
- 'commonName' => 'bob',
- 'status' => 1,
- 'readOnly' => true,
- '{http://owncloud.org/ns}principal' => $principal,
- '{http://owncloud.org/ns}group-share' => true,
- ]
- ];
- $this->shareCache->expects(self::once())
- ->method('get')
- ->with((string)$resourceId)
- ->willReturn(null);
- $service->expects(self::once())
- ->method('getShares')
- ->with($resourceId)
- ->willReturn($rows);
- $this->principalBackend->expects(self::once())
- ->method('getPrincipalByPath')
- ->with($principal)
- ->willReturn(['uri' => $principal, '{DAV:}displayname' => 'bob']);
- $this->shareCache->expects(self::once())
- ->method('set')
- ->with((string)$resourceId, $expected);
- $result = $backend->getShares($resourceId);
- $this->assertEquals($expected, $result);
- }
- public function testPreloadShares(): void {
- $resourceIds = [42, 99];
- $rows = [
- [
- 'resourceid' => 42,
- 'principaluri' => 'principals/groups/bob',
- 'access' => Backend::ACCESS_READ,
- ],
- [
- 'resourceid' => 99,
- 'principaluri' => 'principals/users/carlos',
- 'access' => Backend::ACCESS_READ_WRITE,
- ]
- ];
- $principalResults = [
- ['uri' => 'principals/groups/bob', '{DAV:}displayname' => 'bob'],
- ['uri' => 'principals/users/carlos', '{DAV:}displayname' => 'carlos'],
- ];
- $this->shareCache->expects(self::exactly(2))
- ->method('get')
- ->willReturn(null);
- $this->calendarService->expects(self::once())
- ->method('getSharesForIds')
- ->with($resourceIds)
- ->willReturn($rows);
- $this->principalBackend->expects(self::exactly(2))
- ->method('getPrincipalByPath')
- ->willReturnCallback(function (string $principal) use ($principalResults) {
- switch ($principal) {
- case 'principals/groups/bob':
- return $principalResults[0];
- default:
- return $principalResults[1];
- }
- });
- $this->shareCache->expects(self::exactly(2))
- ->method('set');
- $this->backend->preloadShares($resourceIds);
- }
- }
|