diff options
author | Georg Ehrke <developer@georgehrke.com> | 2019-08-14 13:38:11 +0200 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2019-08-15 15:41:28 +0200 |
commit | 63d584afb5727737fe73a0ca2ecf720022b33922 (patch) | |
tree | c3fc0a335c2f3766b61257270cdb167b9a7aa788 /apps/dav/tests | |
parent | 3d86537dc922083427a283e84d726d416f9ec95c (diff) | |
download | nextcloud-server-63d584afb5727737fe73a0ca2ecf720022b33922.tar.gz nextcloud-server-63d584afb5727737fe73a0ca2ecf720022b33922.zip |
use principaluri instead of userid, allowing to add delegates for rooms and things
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
!fixup add owner_id and proxy_id as db index, since we use it for querying
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
!fixup don't add ACL for each individual proxy, just use calendar-proxy groups
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
!fixup allow delegation of resources / rooms
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
!fixup fix addIndex call in migration
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
!fixup fix remaining constructor calls of Principal
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
!fixup minor fixes and unit tests
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Diffstat (limited to 'apps/dav/tests')
7 files changed, 280 insertions, 35 deletions
diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php index 795a04e2cbf..9f9a7c01337 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php @@ -26,6 +26,7 @@ namespace OCA\DAV\Tests\unit\CalDAV; use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCA\DAV\Connector\Sabre\Principal; use OCP\App\IAppManager; use OCP\IConfig; @@ -83,8 +84,8 @@ abstract class AbstractCalDavBackend extends TestCase { $this->groupManager, $this->createMock(ShareManager::class), $this->createMock(IUserSession::class), - $this->createMock(IConfig::class), $this->createMock(IAppManager::class), + $this->createMock(ProxyMapper::class), ]) ->setMethods(['getPrincipalByPath', 'getGroupMembership']) ->getMock(); diff --git a/apps/dav/tests/unit/CalDAV/CalendarTest.php b/apps/dav/tests/unit/CalDAV/CalendarTest.php index f467d46bf60..7ce43f40916 100644 --- a/apps/dav/tests/unit/CalDAV/CalendarTest.php +++ b/apps/dav/tests/unit/CalDAV/CalendarTest.php @@ -213,21 +213,44 @@ class CalendarTest extends TestCase { 'principal' => $hasOwnerSet ? 'user1' : 'user2', 'protected' => true ], [ - 'privilege' => '{DAV:}write', - 'principal' => $hasOwnerSet ? 'user1' : 'user2', - 'protected' => true + 'privilege' => '{DAV:}read', + 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-write', + 'protected' => true, + ], [ + 'privilege' => '{DAV:}read', + 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-read', + 'protected' => true, ]]; if ($uri === BirthdayService::BIRTHDAY_CALENDAR_URI) { - $expectedAcl = [[ - 'privilege' => '{DAV:}read', + $expectedAcl[] = [ + 'privilege' => '{DAV:}write-properties', 'principal' => $hasOwnerSet ? 'user1' : 'user2', 'protected' => true - ], [ + ]; + $expectedAcl[] = [ 'privilege' => '{DAV:}write-properties', + 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-write', + 'protected' => true + ]; + } else { + $expectedAcl[] = [ + 'privilege' => '{DAV:}write', 'principal' => $hasOwnerSet ? 'user1' : 'user2', 'protected' => true - ]]; + ]; + $expectedAcl[] = [ + 'privilege' => '{DAV:}write', + 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-write', + 'protected' => true + ]; } + + $expectedAcl[] = [ + 'privilege' => '{DAV:}write-properties', + 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-read', + 'protected' => true + ]; + if ($hasOwnerSet) { $expectedAcl[] = [ 'privilege' => '{DAV:}read', diff --git a/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php b/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php index f4019d86e2b..c6e16e2c484 100644 --- a/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/ResourceBooking/AbstractPrincipalBackendTest.php @@ -21,6 +21,8 @@ */ namespace OCA\DAV\Tests\unit\CalDAV\ResourceBooking; +use OCA\DAV\CalDAV\Proxy\Proxy; +use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IGroupManager; use OCP\ILogger; @@ -43,6 +45,9 @@ abstract class AbstractPrincipalBackendTest extends TestCase { /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ protected $logger; + /** @var ProxyMapper|\PHPUnit_Framework_MockObject_MockObject */ + protected $proxyMapper; + /** @var string */ protected $mainDbTable; @@ -64,6 +69,7 @@ abstract class AbstractPrincipalBackendTest extends TestCase { $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->logger = $this->createMock(ILogger::class); + $this->proxyMapper = $this->createMock(ProxyMapper::class); } protected function tearDown() { @@ -152,21 +158,113 @@ abstract class AbstractPrincipalBackendTest extends TestCase { } public function testGetGroupMemberSet() { - $actual = $this->principalBackend->getGroupMemberSet($this->principalPrefix . '/foo-bar'); + $actual = $this->principalBackend->getGroupMemberSet($this->principalPrefix . '/backend1-res1'); $this->assertEquals([], $actual); } + public function testGetGroupMemberSetProxyRead() { + $proxy1 = new Proxy(); + $proxy1->setProxyId('proxyId1'); + $proxy1->setPermissions(1); + + $proxy2 = new Proxy(); + $proxy2->setProxyId('proxyId2'); + $proxy2->setPermissions(3); + + $proxy3 = new Proxy(); + $proxy3->setProxyId('proxyId3'); + $proxy3->setPermissions(3); + + $this->proxyMapper->expects($this->once()) + ->method('getProxiesOf') + ->with($this->principalPrefix . '/backend1-res1') + ->willReturn([$proxy1, $proxy2, $proxy3]); + + $actual = $this->principalBackend->getGroupMemberSet($this->principalPrefix . '/backend1-res1/calendar-proxy-read'); + $this->assertEquals(['proxyId1'], $actual); + } + + public function testGetGroupMemberSetProxyWrite() { + $proxy1 = new Proxy(); + $proxy1->setProxyId('proxyId1'); + $proxy1->setPermissions(1); + + $proxy2 = new Proxy(); + $proxy2->setProxyId('proxyId2'); + $proxy2->setPermissions(3); + + $proxy3 = new Proxy(); + $proxy3->setProxyId('proxyId3'); + $proxy3->setPermissions(3); + + $this->proxyMapper->expects($this->once()) + ->method('getProxiesOf') + ->with($this->principalPrefix . '/backend1-res1') + ->willReturn([$proxy1, $proxy2, $proxy3]); + + $actual = $this->principalBackend->getGroupMemberSet($this->principalPrefix . '/backend1-res1/calendar-proxy-write'); + $this->assertEquals(['proxyId2', 'proxyId3'], $actual); + } + public function testGetGroupMembership() { - $actual = $this->principalBackend->getGroupMembership($this->principalPrefix . '/foo-bar'); - $this->assertEquals([], $actual); + $proxy1 = new Proxy(); + $proxy1->setOwnerId('proxyId1'); + $proxy1->setPermissions(1); + + $proxy2 = new Proxy(); + $proxy2->setOwnerId('proxyId2'); + $proxy2->setPermissions(3); + + $this->proxyMapper->expects($this->once()) + ->method('getProxiesFor') + ->with($this->principalPrefix . '/backend1-res1') + ->willReturn([$proxy1, $proxy2]); + + $actual = $this->principalBackend->getGroupMembership($this->principalPrefix . '/backend1-res1'); + + $this->assertEquals(['proxyId1/calendar-proxy-read', 'proxyId2/calendar-proxy-write'], $actual); } - /** - * @expectedException \Sabre\DAV\Exception - * @expectedExceptionMessage Setting members of the group is not supported yet - */ public function testSetGroupMemberSet() { - $this->principalBackend->setGroupMemberSet($this->principalPrefix . '/foo-bar', ['foo', 'bar']); + $this->proxyMapper->expects($this->at(0)) + ->method('getProxiesOf') + ->with($this->principalPrefix . '/backend1-res1') + ->willReturn([]); + + $this->proxyMapper->expects($this->at(1)) + ->method('insert') + ->with($this->callback(function($proxy) { + /** @var Proxy $proxy */ + if ($proxy->getOwnerId() !== $this->principalPrefix . '/backend1-res1') { + return false; + } + if ($proxy->getProxyId() !== $this->principalPrefix . '/backend1-res2') { + return false; + } + if ($proxy->getPermissions() !== 3) { + return false; + } + + return true; + })); + $this->proxyMapper->expects($this->at(2)) + ->method('insert') + ->with($this->callback(function($proxy) { + /** @var Proxy $proxy */ + if ($proxy->getOwnerId() !== $this->principalPrefix . '/backend1-res1') { + return false; + } + if ($proxy->getProxyId() !== $this->principalPrefix . '/backend2-res3') { + return false; + } + if ($proxy->getPermissions() !== 3) { + return false; + } + + return true; + })); + + $this->principalBackend->setGroupMemberSet($this->principalPrefix . '/backend1-res1/calendar-proxy-write', [$this->principalPrefix . '/backend1-res2', $this->principalPrefix . '/backend2-res3']); } public function testUpdatePrincipal() { diff --git a/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php b/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php index 3787e4df951..461246dd51d 100644 --- a/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/ResourceBooking/ResourcePrincipalBackendTest.php @@ -28,7 +28,7 @@ Class ResourcePrincipalBackendTest extends AbstractPrincipalBackendTest { parent::setUp(); $this->principalBackend = new ResourcePrincipalBackend(self::$realDatabase, - $this->userSession, $this->groupManager, $this->logger); + $this->userSession, $this->groupManager, $this->logger, $this->proxyMapper); $this->mainDbTable = 'calendar_resources'; $this->metadataDbTable = 'calendar_resources_md'; diff --git a/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php b/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php index 9b259c30adf..6b691400cd1 100644 --- a/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/ResourceBooking/RoomPrincipalBackendTest.php @@ -28,7 +28,7 @@ Class RoomPrincipalBackendTest extends AbstractPrincipalBackendTest { parent::setUp(); $this->principalBackend = new RoomPrincipalBackend(self::$realDatabase, - $this->userSession, $this->groupManager, $this->logger); + $this->userSession, $this->groupManager, $this->logger, $this->proxyMapper); $this->mainDbTable = 'calendar_rooms'; $this->metadataDbTable = 'calendar_rooms_md'; diff --git a/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php b/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php index 531f50e96c2..aa18ef63a5c 100644 --- a/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php +++ b/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php @@ -32,6 +32,7 @@ namespace OCA\DAV\Tests\unit\CardDAV; use InvalidArgumentException; +use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCA\DAV\CardDAV\AddressBook; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\Connector\Sabre\Principal; @@ -131,8 +132,8 @@ class CardDavBackendTest extends TestCase { $this->groupManager, $this->createMock(ShareManager::class), $this->createMock(IUserSession::class), - $this->createMock(IConfig::class), $this->createMock(IAppManager::class), + $this->createMock(ProxyMapper::class), ]) ->setMethods(['getPrincipalByPath', 'getGroupMembership']) ->getMock(); diff --git a/apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php b/apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php index 225189e7d01..2a5c122ce9e 100644 --- a/apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/PrincipalTest.php @@ -29,6 +29,8 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre; use OC\User\User; +use OCA\DAV\CalDAV\Proxy\Proxy; +use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCP\App\IAppManager; use OCP\IConfig; use OCP\IGroup; @@ -57,27 +59,27 @@ class PrincipalTest extends TestCase { /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */ private $userSession; - /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ - private $config; - /** @var IAppManager | \PHPUnit_Framework_MockObject_MockObject */ private $appManager; + /** @var ProxyMapper | \PHPUnit_Framework_MockObject_MockObject */ + private $proxyMapper; + public function setUp() { $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->shareManager = $this->createMock(IManager::class); $this->userSession = $this->createMock(IUserSession::class); - $this->config = $this->createMock(IConfig::class); $this->appManager = $this->createMock(IAppManager::class); + $this->proxyMapper = $this->createMock(ProxyMapper::class); $this->connector = new \OCA\DAV\Connector\Sabre\Principal( $this->userManager, $this->groupManager, $this->shareManager, $this->userSession, - $this->config, - $this->appManager + $this->appManager, + $this->proxyMapper ); parent::setUp(); } @@ -203,6 +205,25 @@ class PrincipalTest extends TestCase { } public function testGetGroupMemberSet() { + $response = $this->connector->getGroupMemberSet('principals/users/foo'); + $this->assertSame([], $response); + } + + /** + * @expectedException \Sabre\DAV\Exception + * @expectedExceptionMessage Principal not found + */ + public function testGetGroupMemberSetEmpty() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue(null)); + + $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read'); + } + + public function testGetGroupMemberSetProxyRead() { $fooUser = $this->createMock(User::class); $fooUser ->expects($this->exactly(1)) @@ -214,22 +235,56 @@ class PrincipalTest extends TestCase { ->with('foo') ->will($this->returnValue($fooUser)); - $response = $this->connector->getGroupMemberSet('principals/users/foo'); - $this->assertSame(['principals/users/foo'], $response); + $proxy1 = new Proxy(); + $proxy1->setProxyId('proxyId1'); + $proxy1->setPermissions(1); + + $proxy2 = new Proxy(); + $proxy2->setProxyId('proxyId2'); + $proxy2->setPermissions(3); + + $proxy3 = new Proxy(); + $proxy3->setProxyId('proxyId3'); + $proxy3->setPermissions(3); + + $this->proxyMapper->expects($this->once()) + ->method('getProxiesOf') + ->with('principals/users/foo') + ->willReturn([$proxy1, $proxy2, $proxy3]); + + $this->assertEquals(['proxyId1'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read')); } - /** - * @expectedException \Sabre\DAV\Exception - * @expectedExceptionMessage Principal not found - */ - public function testGetGroupMemberSetEmpty() { + public function testGetGroupMemberSetProxyWrite() { + $fooUser = $this->createMock(User::class); + $fooUser + ->expects($this->exactly(1)) + ->method('getUID') + ->will($this->returnValue('foo')); $this->userManager ->expects($this->once()) ->method('get') ->with('foo') - ->will($this->returnValue(null)); + ->will($this->returnValue($fooUser)); + + $proxy1 = new Proxy(); + $proxy1->setProxyId('proxyId1'); + $proxy1->setPermissions(1); + + $proxy2 = new Proxy(); + $proxy2->setProxyId('proxyId2'); + $proxy2->setPermissions(3); + + $proxy3 = new Proxy(); + $proxy3->setProxyId('proxyId3'); + $proxy3->setPermissions(3); - $this->connector->getGroupMemberSet('principals/users/foo'); + $this->proxyMapper->expects($this->once()) + ->method('getProxiesOf') + ->with('principals/users/foo') + ->willReturn([$proxy1, $proxy2, $proxy3]); + + $this->assertEquals(['proxyId2', 'proxyId3'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-write')); } public function testGetGroupMembership() { @@ -243,7 +298,7 @@ class PrincipalTest extends TestCase { ->method('getGID') ->willReturn('foo/bar'); $this->userManager - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('get') ->with('foo') ->willReturn($fooUser); @@ -256,9 +311,24 @@ class PrincipalTest extends TestCase { $group2, ]); + $proxy1 = new Proxy(); + $proxy1->setOwnerId('proxyId1'); + $proxy1->setPermissions(1); + + $proxy2 = new Proxy(); + $proxy2->setOwnerId('proxyId2'); + $proxy2->setPermissions(3); + + $this->proxyMapper->expects($this->once()) + ->method('getProxiesFor') + ->with('principals/users/foo') + ->willReturn([$proxy1, $proxy2]); + $expectedResponse = [ 'principals/groups/group1', 'principals/groups/foo%2Fbar', + 'proxyId1/calendar-proxy-read', + 'proxyId2/calendar-proxy-write', ]; $response = $this->connector->getGroupMembership('principals/users/foo'); $this->assertSame($expectedResponse, $response); @@ -286,6 +356,58 @@ class PrincipalTest extends TestCase { $this->connector->setGroupMemberSet('principals/users/foo', ['foo']); } + public function testSetGroupMembershipProxy() { + $fooUser = $this->createMock(User::class); + $fooUser + ->expects($this->exactly(1)) + ->method('getUID') + ->will($this->returnValue('foo')); + $barUser = $this->createMock(User::class); + $barUser + ->expects($this->exactly(1)) + ->method('getUID') + ->will($this->returnValue('bar')); + $this->userManager + ->expects($this->at(0)) + ->method('get') + ->with('foo') + ->will($this->returnValue($fooUser)); + $this->userManager + ->expects($this->at(1)) + ->method('get') + ->with('bar') + ->will($this->returnValue($barUser)); + + $this->proxyMapper->expects($this->at(0)) + ->method('getProxiesOf') + ->with('principals/users/foo') + ->willReturn([]); + + $this->proxyMapper->expects($this->at(1)) + ->method('insert') + ->with($this->callback(function($proxy) { + /** @var Proxy $proxy */ + if ($proxy->getOwnerId() !== 'principals/users/foo') { + return false; + } + if ($proxy->getProxyId() !== 'principals/users/bar') { + return false; + } + if ($proxy->getPermissions() !== 3) { + return false; + } + + return true; + })); + + $this->connector->setGroupMemberSet('principals/users/foo/calendar-proxy-write', ['principals/users/bar']); + } + + + + + + public function testUpdatePrincipal() { $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch(array()))); } |