Skip to content

Commit

Permalink
!fixup minor fixes and unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
  • Loading branch information
georgehrke committed Aug 15, 2019
1 parent 9684e9c commit e47e293
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 37 deletions.
10 changes: 8 additions & 2 deletions apps/dav/lib/CalDAV/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function getACL() {
];
$acl[] = [
'privilege' => '{DAV:}write-properties',
'principal' => $this->getOwner() . '/calendar-proxy-read',
'principal' => $this->getOwner() . '/calendar-proxy-write',
'protected' => true,
];
}
Expand Down Expand Up @@ -213,7 +213,13 @@ public function getACL() {
}

$acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
$allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public'];
$allowedPrincipals = [
$this->getOwner(),
$this->getOwner(). '/calendar-proxy-read',
$this->getOwner(). '/calendar-proxy-write',
parent::getOwner(),
'principals/system/public'
];
return array_filter($acl, function($rule) use ($allowedPrincipals) {
return \in_array($rule['principal'], $allowedPrincipals, true);
});
Expand Down
5 changes: 5 additions & 0 deletions apps/dav/lib/Traits/PrincipalProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public function setGroupMemberSet($principal, array $members) {
throw new Exception('Setting members of the group is not supported yet');
}

$masterPrincipalArray = $this->getPrincipalByPath($principalUri);
if (!$masterPrincipalArray) {
throw new Exception('Principal not found');
}

$permission = ProxyMapper::PERMISSION_READ;
if ($target === 'calendar-proxy-write') {
$permission |= ProxyMapper::PERMISSION_WRITE;
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,8 +84,8 @@ public function setUp() {
$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();
Expand Down
37 changes: 30 additions & 7 deletions apps/dav/tests/unit/CalDAV/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,44 @@ public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet, $uri = 'def
'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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -64,6 +69,7 @@ public function setUp() {
$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() {
Expand Down Expand Up @@ -152,21 +158,113 @@ public function testGetPrincipalByPathWrongPrefix() {
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setUp() {
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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setUp() {
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';
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/tests/unit/CardDAV/CardDavBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,8 +132,8 @@ public function setUp() {
$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();
Expand Down
Loading

0 comments on commit e47e293

Please sign in to comment.