From f09c46d166f11149a5e778a6d69877a6b871b7a3 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 12 Jul 2016 16:35:27 +0200 Subject: Fix some tests --- apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php | 4 ++-- apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php index e2d63868af0..07a55fef63b 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php @@ -471,7 +471,7 @@ class FilesPluginTest extends TestCase { $node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Node') ->disableOriginalConstructor() ->getMock(); - $node->expects($this->once()) + $node->expects($this->at(0)) ->method('getFileInfo') ->willReturn($fileInfoFolderATestTXT); @@ -545,7 +545,7 @@ class FilesPluginTest extends TestCase { ->getMock(); $request - ->expects($this->once()) + ->expects($this->at(1)) ->method('getPath') ->will($this->returnValue('test/somefile.xml')); diff --git a/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php b/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php index 3a5566e8f70..71bdfb3b2bc 100644 --- a/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php +++ b/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php @@ -45,7 +45,12 @@ class SystemPrincipalBackendTest extends TestCase { [[[ 'uri' => 'principals/system/system', '{DAV:}displayname' => 'system', - ]], 'principals/system'], + ], + [ + 'uri' => 'principals/system/public', + '{DAV:}displayname' => 'public', + ] + ], 'principals/system'], ]; } -- cgit v1.2.3 From 77216e7ca97e763ab12d30c471d0a73b44faeeba Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 20 Jul 2016 14:08:45 +0200 Subject: a few tests --- apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 18 ++++++ .../unit/CalDAV/Publishing/PublishingTest.php | 71 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 1a5673161de..a597b476336 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -334,6 +334,24 @@ EOD; $this->assertEquals($event, $changes['added'][0]); } + public function testPublications() { + $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []); + + $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0]; + + $l10n = $this->getMockBuilder('\OCP\IL10N') + ->disableOriginalConstructor()->getMock(); + + $calendar = new Calendar($this->backend, $calendarInfo, $l10n); + $this->backend->setPublishStatus(true, $calendar); + $this->assertEquals(true, $this->backend->getPublishStatus($calendar)); + + $publicCalendars = $this->backend->getPublicCalendars(); + $this->assertEquals(1, count($publicCalendars)); + $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']); + + } + public function testSubscriptions() { $id = $this->backend->createSubscription(self::UNIT_TEST_USER, 'Subscription', [ '{http://calendarserver.org/ns/}source' => new Href('test-source'), diff --git a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php new file mode 100644 index 00000000000..00e4f049307 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php @@ -0,0 +1,71 @@ +getMockBuilder('OCA\DAV\DAV\PublicAuth')->disableOriginalConstructor()->getMock(); + $authBackend->method('isDavAuthenticated')->willReturn(true); + + /** @var IRequest $request */ + $request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(); + $this->plugin = new PublishPlugin($authBackend, $request); + + $root = new SimpleCollection('calendars'); + $this->server = new Server($root); + /** @var SimpleCollection $node */ + $this->book = $this->getMockBuilder('OCA\DAV\CalDAV\Calendar')-> + disableOriginalConstructor()-> + getMock(); + $this->book->method('getName')->willReturn('cal1'); + $root->addChild($this->book); + $this->plugin->initialize($this->server); + } + + public function testPublishing() { + + $this->book->expects($this->once())->method('setPublishStatus')->with(true); + + // setup request + $request = new Request(); + $request->addHeader('Content-Type', 'application/xml'); + $request->setUrl('cal1'); + $request->setBody(''); + $response = new Response(); + $this->plugin->httpPost($request, $response); + } + + public function testUnPublishing() { + + $this->book->expects($this->once())->method('setPublishStatus')->with(true); + + // setup request + $request = new Request(); + $request->addHeader('Content-Type', 'application/xml'); + $request->setUrl('cal1'); + $request->setBody(''); + $response = new Response(); + $this->plugin->httpPost($request, $response); + } +} -- cgit v1.2.3 From de5e21269462957894e62b677d389862bb647d09 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 20 Jul 2016 14:26:24 +0200 Subject: fix plugin test --- .../tests/unit/CalDAV/Publishing/PublishingTest.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php index 00e4f049307..43345a34889 100644 --- a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php +++ b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php @@ -6,6 +6,8 @@ use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\Publishing\PublishPlugin; use OCA\DAV\Connector\Sabre\Auth; use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\IConfig; use Sabre\DAV\Server; use Sabre\DAV\SimpleCollection; use Sabre\HTTP\Request; @@ -14,12 +16,16 @@ use Test\TestCase; class PluginTest extends TestCase { - /** @var Plugin */ + /** @var PublishPlugin */ private $plugin; /** @var Server */ private $server; /** @var Calendar | \PHPUnit_Framework_MockObject_MockObject */ private $book; + /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ + private $config; + /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */ + private $urlGenerator; public function setUp() { parent::setUp(); @@ -28,9 +34,15 @@ class PluginTest extends TestCase { $authBackend = $this->getMockBuilder('OCA\DAV\DAV\PublicAuth')->disableOriginalConstructor()->getMock(); $authBackend->method('isDavAuthenticated')->willReturn(true); + $this->config = $this->getMock('\OCP\IConfig'); + $this->config->expects($this->any())->method('getSystemValue') + ->with($this->equalTo('secret')) + ->willReturn('mysecret'); + + $this->urlGenerator = $this->getMock('OCP\IURLGenerator'); + /** @var IRequest $request */ - $request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(); - $this->plugin = new PublishPlugin($authBackend, $request); + $this->plugin = new PublishPlugin($this->config, $this->urlGenerator); $root = new SimpleCollection('calendars'); $this->server = new Server($root); -- cgit v1.2.3 From ebf23778f5a71b421021165627738e6e7ed5216f Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 20 Jul 2016 16:08:21 +0200 Subject: fix unpublishing test --- apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php index 43345a34889..b3ff36fc4ed 100644 --- a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php +++ b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php @@ -70,7 +70,7 @@ class PluginTest extends TestCase { public function testUnPublishing() { - $this->book->expects($this->once())->method('setPublishStatus')->with(true); + $this->book->expects($this->once())->method('setPublishStatus')->with(false); // setup request $request = new Request(); -- cgit v1.2.3 From 3921385ed3994c26674ec31606a41899fa8648ee Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 31 Jul 2016 20:18:35 +0200 Subject: fix things (indentation, tests, comments, backend custom implementation --- apps/dav/appinfo/v1/caldav.php | 3 +- apps/dav/lib/AppInfo/Application.php | 83 ++++++++- apps/dav/lib/CalDAV/CalDavBackend.php | 49 ++--- apps/dav/lib/CalDAV/PublicCalendarRoot.php | 20 ++- apps/dav/lib/CalDAV/Publishing/PublishPlugin.php | 199 ++++++++++----------- apps/dav/lib/CalDAV/Publishing/Xml/Publisher.php | 99 +++++----- apps/dav/lib/Command/CreateCalendar.php | 4 +- apps/dav/lib/Connector/Sabre/FilesPlugin.php | 4 - apps/dav/lib/RootCollection.php | 2 +- .../unit/CalDAV/AbstractCalDavBackendTest.php | 3 +- .../unit/CalDAV/Publishing/PublishingTest.php | 17 +- .../tests/unit/Connector/Sabre/FilesPluginTest.php | 4 +- 12 files changed, 281 insertions(+), 206 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/appinfo/v1/caldav.php b/apps/dav/appinfo/v1/caldav.php index 13b4d3119ca..d9606f20b72 100644 --- a/apps/dav/appinfo/v1/caldav.php +++ b/apps/dav/appinfo/v1/caldav.php @@ -46,7 +46,8 @@ $principalBackend = new Principal( 'principals/' ); $db = \OC::$server->getDatabaseConnection(); -$calDavBackend = new CalDavBackend($db, $principalBackend, \OC::$server->getUserManager()); +$config = \OC::$server->getConfig(); +$calDavBackend = new CalDavBackend($db, $principalBackend, \OC::$server->getUserManager(), $config); $debugging = \OC::$server->getConfig()->getSystemValue('debug', false); diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index dc3ce7e8bb2..8bc43da5649 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -37,8 +37,87 @@ class Application extends App { /** * Application constructor. */ - public function __construct() { - parent::__construct('dav'); + public function __construct (array $urlParams=array()) { + parent::__construct('dav', $urlParams); + + $container = $this->getContainer(); + $container->registerService('ContactsManager', function($c) { + /** @var IAppContainer $c */ + return new ContactsManager( + $c->query('CardDavBackend') + ); + }); + + $container->registerService('HookManager', function($c) { + /** @var IAppContainer $c */ + return new HookManager( + $c->getServer()->getUserManager(), + $c->query('SyncService'), + $c->query('CalDavBackend'), + $c->query('CardDavBackend') + ); + }); + + $container->registerService('SyncService', function($c) { + /** @var IAppContainer $c */ + return new SyncService( + $c->query('CardDavBackend'), + $c->getServer()->getUserManager(), + $c->getServer()->getLogger() + ); + }); + + $container->registerService('CardDavBackend', function($c) { + /** @var IAppContainer $c */ + $db = $c->getServer()->getDatabaseConnection(); + $dispatcher = $c->getServer()->getEventDispatcher(); + $principal = new Principal( + $c->getServer()->getUserManager(), + $c->getServer()->getGroupManager() + ); + return new CardDavBackend($db, $principal, $c->getServer()->getUserManager(), $dispatcher); + }); + + $container->registerService('CalDavBackend', function($c) { + /** @var IAppContainer $c */ + $db = $c->getServer()->getDatabaseConnection(); + $config = $c->getServer()->getConfig(); + $principal = new Principal( + $c->getServer()->getUserManager(), + $c->getServer()->getGroupManager() + ); + return new CalDavBackend($db, $principal, $c->getServer()->getUserManager(), $config); + }); + + $container->registerService('BirthdayService', function($c) { + /** @var IAppContainer $c */ + $g = new GroupPrincipalBackend( + $c->getServer()->getGroupManager() + ); + return new BirthdayService( + $c->query('CalDavBackend'), + $c->query('CardDavBackend'), + $g + ); + }); + + $container->registerService('OCA\DAV\Migration\Classification', function ($c) { + /** @var IAppContainer $c */ + return new Classification( + $c->query('CalDavBackend'), + $c->getServer()->getUserManager() + ); + }); + + $container->registerService('OCA\DAV\Migration\GenerateBirthdays', function ($c) { + /** @var IAppContainer $c */ + /** @var BirthdayService $b */ + $b = $c->query('BirthdayService'); + return new GenerateBirthdays( + $b, + $c->getServer()->getUserManager() + ); + }); } /** diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 7075ccc460d..cdab20f541e 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -29,6 +29,7 @@ use OCA\DAV\DAV\Sharing\IShareable; use OCP\DB\QueryBuilder\IQueryBuilder; use OCA\DAV\Connector\Sabre\Principal; use OCA\DAV\DAV\Sharing\Backend; +use OCP\IConfig; use OCP\IDBConnection; use OCP\IUser; use OCP\IUserManager; @@ -119,7 +120,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription /** @var IUserManager */ private $userManager; - /** @var \OCP\IConfig */ + /** @var IConfig */ private $config; /** @@ -128,14 +129,14 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription * @param IDBConnection $db * @param Principal $principalBackend * @param IUserManager $userManager + * @param IConfig $config */ - public function __construct(IDBConnection $db, Principal $principalBackend, IUserManager $userManager) { + public function __construct(IDBConnection $db, Principal $principalBackend, IUserManager $userManager, IConfig $config) { $this->db = $db; $this->principalBackend = $principalBackend; $this->userManager = $userManager; $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); - // TODO: inject - $this->config = \OC::$server->getConfig(); + $this->config = $config; } /** @@ -1533,26 +1534,26 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription } /** - * @param boolean $value - * @param \OCA\DAV\CalDAV\Calendar $calendar - */ - public function setPublishStatus($value, $calendar) { - $query = $this->db->getQueryBuilder(); - if ($value) { - $query->insert('dav_shares') - ->values([ - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), - 'type' => $query->createNamedParameter('calendar'), - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()) - ]); - } else { - $query->delete('dav_shares') - ->Where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); - } - $query->execute(); - } + * @param boolean $value + * @param \OCA\DAV\CalDAV\Calendar $calendar + */ + public function setPublishStatus($value, $calendar) { + $query = $this->db->getQueryBuilder(); + if ($value) { + $query->insert('dav_shares') + ->values([ + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), + 'type' => $query->createNamedParameter('calendar'), + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()) + ]); + } else { + $query->delete('dav_shares') + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); + } + $query->execute(); + } /** * @param \OCA\DAV\CalDAV\Calendar $calendar diff --git a/apps/dav/lib/CalDAV/PublicCalendarRoot.php b/apps/dav/lib/CalDAV/PublicCalendarRoot.php index f1ff23ac7e2..797074f7e56 100644 --- a/apps/dav/lib/CalDAV/PublicCalendarRoot.php +++ b/apps/dav/lib/CalDAV/PublicCalendarRoot.php @@ -21,14 +21,19 @@ namespace OCA\DAV\CalDAV; use Sabre\DAV\Collection; +use Sabre\DAV\Exception\NotFound; class PublicCalendarRoot extends Collection { /** @var CalDavBackend */ protected $caldavBackend; + /** @var \OCP\IL10N */ + protected $l10n; + function __construct(CalDavBackend $caldavBackend) { $this->caldavBackend = $caldavBackend; + $this->l10n = \OC::$server->getL10N('dav'); } /** @@ -38,21 +43,28 @@ class PublicCalendarRoot extends Collection { return 'public-calendars'; } + /** + * @inheritdoc + */ function getChild($name) { - // TODO: for performance reason this needs to have a custom implementation - return parent::getChild($name); + foreach ($this->caldavBackend->getPublicCalendars() as $calendar) { + if ($calendar['uri'] === $name) { + // TODO: maybe implement a new class PublicCalendar ??? + return new Calendar($this->caldavBackend, $calendar, $this->l10n); + } + } + throw new NotFound('Node with name \'' . $name . '\' could not be found'); } /** * @inheritdoc */ function getChildren() { - $l10n = \OC::$server->getL10N('dav'); $calendars = $this->caldavBackend->getPublicCalendars(); $children = []; foreach ($calendars as $calendar) { // TODO: maybe implement a new class PublicCalendar ??? - $children[] = new Calendar($this->caldavBackend, $calendar, $l10n); + $children[] = new Calendar($this->caldavBackend, $calendar, $this->l10n); } return $children; diff --git a/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php b/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php index 4e82d166fc0..e0bcfb7ef6a 100644 --- a/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php +++ b/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php @@ -16,9 +16,9 @@ use OCP\IConfig; class PublishPlugin extends ServerPlugin { - const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; + const NS_CALENDARSERVER = 'http://calendarserver.org/ns/'; - /** + /** * Reference to SabreDAV server object. * * @var \Sabre\DAV\Server @@ -28,29 +28,30 @@ class PublishPlugin extends ServerPlugin /** * Config instance to get instance secret. * - * @var OCP\IConfig + * @var IConfig */ protected $config; /** * URL Generator for absolute URLs. * - * @var OCP\IURLGenerator + * @var IURLGenerator */ protected $urlGenerator; - /** - * PublishPlugin constructor. - * - * @param IURLGenerator $urlGenerator - */ + /** + * PublishPlugin constructor. + * + * @param IConfig $config + * @param IURLGenerator $urlGenerator + */ public function __construct(IConfig $config, IURLGenerator $urlGenerator) { $this->config = $config; $this->urlGenerator = $urlGenerator; } - /** + /** * This method should return a list of server-features. * * This is for example 'versioning' and is added to the DAV: header @@ -58,7 +59,7 @@ class PublishPlugin extends ServerPlugin * * @return string[] */ - public function getFeatures() + public function getFeatures() { return ['oc-calendar-publishing']; // May have to be changed to be detected } @@ -92,10 +93,9 @@ class PublishPlugin extends ServerPlugin $this->server->on('method:POST', [$this, 'httpPost']); $this->server->on('propFind', [$this, 'propFind']); -// $this->server->on('method:OPTIONS', [$this, 'httpOptions'], 5); } - public function propFind(PropFind $propFind, INode $node) + public function propFind(PropFind $propFind, INode $node) { if ($node instanceof Calendar) { $token = md5($this->config->getSystemValue('secret', '').$node->getResourceId()); @@ -104,130 +104,113 @@ class PublishPlugin extends ServerPlugin $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node, $publishUrl) { if ($node->getPublishStatus()) { - return new Publisher($publishUrl, true); // We return the publish-url only if the calendar is published. + // We return the publish-url only if the calendar is published. + return new Publisher($publishUrl, true); } }); $propFind->handle('{'.self::NS_CALENDARSERVER.'}pre-publish-url', function () use ($node, $publishUrl) { - return new Publisher($publishUrl, false); // The pre-publish-url is always returned + // The pre-publish-url is always returned + return new Publisher($publishUrl, false); }); } } - /** - * We intercept this to handle POST requests on calendars. - * - * @param RequestInterface $request - * @param ResponseInterface $response - * - * @return null|bool - */ - public function httpPost(RequestInterface $request, ResponseInterface $response) - { - $path = $request->getPath(); + /** + * We intercept this to handle POST requests on calendars. + * + * @param RequestInterface $request + * @param ResponseInterface $response + * + * @return null|bool + */ + public function httpPost(RequestInterface $request, ResponseInterface $response) + { + $path = $request->getPath(); - // Only handling xml - $contentType = $request->getHeader('Content-Type'); - if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { - return; - } + // Only handling xml + $contentType = $request->getHeader('Content-Type'); + if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) { + return; + } - // Making sure the node exists - try { - $node = $this->server->tree->getNodeForPath($path); - } catch (NotFound $e) { - return; - } + // Making sure the node exists + try { + $node = $this->server->tree->getNodeForPath($path); + } catch (NotFound $e) { + return; + } - $requestBody = $request->getBodyAsString(); + $requestBody = $request->getBodyAsString(); - // If this request handler could not deal with this POST request, it - // will return 'null' and other plugins get a chance to handle the - // request. - // - // However, we already requested the full body. This is a problem, - // because a body can only be read once. This is why we preemptively - // re-populated the request body with the existing data. - $request->setBody($requestBody); + // If this request handler could not deal with this POST request, it + // will return 'null' and other plugins get a chance to handle the + // request. + // + // However, we already requested the full body. This is a problem, + // because a body can only be read once. This is why we preemptively + // re-populated the request body with the existing data. + $request->setBody($requestBody); - $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); + $this->server->xml->parse($requestBody, $request->getUrl(), $documentType); - switch ($documentType) { + switch ($documentType) { - case '{'.self::NS_CALENDARSERVER.'}publish-calendar' : + case '{'.self::NS_CALENDARSERVER.'}publish-calendar' : - // We can only deal with IShareableCalendar objects - if (!$node instanceof Calendar) { - return; - } - $this->server->transactionType = 'post-publish-calendar'; + // We can only deal with IShareableCalendar objects + if (!$node instanceof Calendar) { + return; + } + $this->server->transactionType = 'post-publish-calendar'; - // Getting ACL info - $acl = $this->server->getPlugin('acl'); + // Getting ACL info + $acl = $this->server->getPlugin('acl'); - // If there's no ACL support, we allow everything - if ($acl) { - $acl->checkPrivileges($path, '{DAV:}write'); - } + // If there's no ACL support, we allow everything + if ($acl) { + $acl->checkPrivileges($path, '{DAV:}write'); + } - $node->setPublishStatus(true); + $node->setPublishStatus(true); - // iCloud sends back the 202, so we will too. - $response->setStatus(202); + // iCloud sends back the 202, so we will too. + $response->setStatus(202); - // Adding this because sending a response body may cause issues, - // and I wanted some type of indicator the response was handled. - $response->setHeader('X-Sabre-Status', 'everything-went-well'); + // Adding this because sending a response body may cause issues, + // and I wanted some type of indicator the response was handled. + $response->setHeader('X-Sabre-Status', 'everything-went-well'); - // Breaking the event chain - return false; + // Breaking the event chain + return false; - case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' : + case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar' : - // We can only deal with IShareableCalendar objects - if (!$node instanceof Calendar) { - return; - } - $this->server->transactionType = 'post-unpublish-calendar'; + // We can only deal with IShareableCalendar objects + if (!$node instanceof Calendar) { + return; + } + $this->server->transactionType = 'post-unpublish-calendar'; - // Getting ACL info - $acl = $this->server->getPlugin('acl'); + // Getting ACL info + $acl = $this->server->getPlugin('acl'); - // If there's no ACL support, we allow everything - if ($acl) { - $acl->checkPrivileges($path, '{DAV:}write'); - } + // If there's no ACL support, we allow everything + if ($acl) { + $acl->checkPrivileges($path, '{DAV:}write'); + } - $node->setPublishStatus(false); + $node->setPublishStatus(false); - $response->setStatus(200); + $response->setStatus(200); - // Adding this because sending a response body may cause issues, - // and I wanted some type of indicator the response was handled. - $response->setHeader('X-Sabre-Status', 'everything-went-well'); + // Adding this because sending a response body may cause issues, + // and I wanted some type of indicator the response was handled. + $response->setHeader('X-Sabre-Status', 'everything-went-well'); - // Breaking the event chain - return false; + // Breaking the event chain + return false; - } - } - - public function httpOptions(RequestInterface $request, ResponseInterface $response) { - if ($request->getPath() == 'public-calendars') { - $methods = $this->server->getAllowedMethods($request->getPath()); - - $response->setHeader('Allow', strtoupper(implode(', ', $methods))); - $features = ['1', '3', 'extended-mkcol']; - - $response->setHeader('DAV', implode(', ', $features)); - $response->setHeader('MS-Author-Via', 'DAV'); - $response->setHeader('Accept-Ranges', 'bytes'); - $response->setHeader('Content-Length', '0'); - $response->setStatus(200); - - // Sending back false will interupt the event chain and tell the server - // we've handled this method. - return false; - } - } + } + } } diff --git a/apps/dav/lib/CalDAV/Publishing/Xml/Publisher.php b/apps/dav/lib/CalDAV/Publishing/Xml/Publisher.php index 45a7fe0257b..375954ffaf3 100644 --- a/apps/dav/lib/CalDAV/Publishing/Xml/Publisher.php +++ b/apps/dav/lib/CalDAV/Publishing/Xml/Publisher.php @@ -8,57 +8,58 @@ use Sabre\Xml\XmlSerializable; class Publisher implements XmlSerializable { - /** - * @var string $publishUrl - */ - protected $publishUrl; + /** + * @var string $publishUrl + */ + protected $publishUrl; - /** - * @var boolean $isPublished - */ - protected $isPublished; + /** + * @var boolean $isPublished + */ + protected $isPublished; - /** - * @param string $publishUrl - * @param boolean $isPublished - */ - function __construct($publishUrl, $isPublished) { - $this->publishUrl = $publishUrl; - $this->isPublished = $isPublished; - } + /** + * @param string $publishUrl + * @param boolean $isPublished + */ + function __construct($publishUrl, $isPublished) { + $this->publishUrl = $publishUrl; + $this->isPublished = $isPublished; + } - /** - * @return string - */ - function getValue() { - return $this->publishUrl; - } + /** + * @return string + */ + function getValue() { + return $this->publishUrl; + } - /** - * The xmlSerialize metod is called during xml writing. - * - * Use the $writer argument to write its own xml serialization. - * - * An important note: do _not_ create a parent element. Any element - * implementing XmlSerializble should only ever write what's considered - * its 'inner xml'. - * - * The parent of the current element is responsible for writing a - * containing element. - * - * This allows serializers to be re-used for different element names. - * - * If you are opening new elements, you must also close them again. - * - * @param Writer $writer - * @return void - */ - function xmlSerialize(Writer $writer) { - if (!$this->isPublished) { - $writer->write($this->publishUrl); // for pre-publish-url - } else { - $writer->writeElement('{DAV:}href', $this->publishUrl); // for publish-url - } - - } + /** + * The xmlSerialize metod is called during xml writing. + * + * Use the $writer argument to write its own xml serialization. + * + * An important note: do _not_ create a parent element. Any element + * implementing XmlSerializble should only ever write what's considered + * its 'inner xml'. + * + * The parent of the current element is responsible for writing a + * containing element. + * + * This allows serializers to be re-used for different element names. + * + * If you are opening new elements, you must also close them again. + * + * @param Writer $writer + * @return void + */ + function xmlSerialize(Writer $writer) { + if (!$this->isPublished) { + // for pre-publish-url + $writer->write($this->publishUrl); + } else { + // for publish-url + $writer->writeElement('{DAV:}href', $this->publishUrl); + } + } } diff --git a/apps/dav/lib/Command/CreateCalendar.php b/apps/dav/lib/Command/CreateCalendar.php index 0bc6398250e..54cb06db666 100644 --- a/apps/dav/lib/Command/CreateCalendar.php +++ b/apps/dav/lib/Command/CreateCalendar.php @@ -44,6 +44,7 @@ class CreateCalendar extends Command { /** * @param IUserManager $userManager + * @param IGroupManager $groupManager * @param IDBConnection $dbConnection */ function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection) { @@ -74,9 +75,10 @@ class CreateCalendar extends Command { $this->userManager, $this->groupManager ); + $config = \OC::$server->getConfig(); $name = $input->getArgument('name'); - $caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager); + $caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $config); $caldav->createCalendar("principals/users/$user", $name, []); } } diff --git a/apps/dav/lib/Connector/Sabre/FilesPlugin.php b/apps/dav/lib/Connector/Sabre/FilesPlugin.php index 7da49e13130..dd5f958ed4c 100644 --- a/apps/dav/lib/Connector/Sabre/FilesPlugin.php +++ b/apps/dav/lib/Connector/Sabre/FilesPlugin.php @@ -244,10 +244,6 @@ class FilesPlugin extends ServerPlugin { * @param ResponseInterface $response */ function httpGet(RequestInterface $request, ResponseInterface $response) { - // Exclude published calendars - // TODO : Find a better way to do this - if (explode('/', $request->getPath())[0] === 'public-calendars') return; - // Only handle valid files $node = $this->tree->getNodeForPath($request->getPath()); if (!($node instanceof IFile)) return; diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php index f7b9c7079a0..f99d5850212 100644 --- a/apps/dav/lib/RootCollection.php +++ b/apps/dav/lib/RootCollection.php @@ -60,7 +60,7 @@ class RootCollection extends SimpleCollection { $systemPrincipals->disableListing = $disableListing; $filesCollection = new Files\RootCollection($userPrincipalBackend, 'principals/users'); $filesCollection->disableListing = $disableListing; - $caldavBackend = new CalDavBackend($db, $userPrincipalBackend, \OC::$server->getUserManager()); + $caldavBackend = new CalDavBackend($db, $userPrincipalBackend, \OC::$server->getUserManager(), $config); $calendarRoot = new CalendarRoot($userPrincipalBackend, $caldavBackend, 'principals/users'); $calendarRoot->disableListing = $disableListing; $publicCalendarRoot = new PublicCalendarRoot($caldavBackend); diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php index 0e2e1b0ee51..ebdd4d9f940 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php @@ -75,7 +75,8 @@ abstract class AbstractCalDavBackendTest extends TestCase { ->willReturn([self::UNIT_TEST_GROUP]); $db = \OC::$server->getDatabaseConnection(); - $this->backend = new CalDavBackend($db, $this->principal, $this->userManager); + $config = \OC::$server->getConfig(); + $this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $config); $this->tearDown(); } diff --git a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php index b3ff36fc4ed..69de507dac5 100644 --- a/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php +++ b/apps/dav/tests/unit/CalDAV/Publishing/PublishingTest.php @@ -4,7 +4,6 @@ namespace OCA\DAV\Tests\unit\CalDAV\Publishing; use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\Publishing\PublishPlugin; -use OCA\DAV\Connector\Sabre\Auth; use OCP\IRequest; use OCP\IURLGenerator; use OCP\IConfig; @@ -30,16 +29,16 @@ class PluginTest extends TestCase { public function setUp() { parent::setUp(); - /** @var Auth | \PHPUnit_Framework_MockObject_MockObject $authBackend */ - $authBackend = $this->getMockBuilder('OCA\DAV\DAV\PublicAuth')->disableOriginalConstructor()->getMock(); - $authBackend->method('isDavAuthenticated')->willReturn(true); - - $this->config = $this->getMock('\OCP\IConfig'); + $this->config = $this->getMockBuilder('\OCP\IConfig')-> + disableOriginalConstructor()-> + getMock(); $this->config->expects($this->any())->method('getSystemValue') ->with($this->equalTo('secret')) ->willReturn('mysecret'); - $this->urlGenerator = $this->getMock('OCP\IURLGenerator'); + $this->urlGenerator = $this->getMockBuilder('OCP\IURLGenerator')-> + disableOriginalConstructor()-> + getMock(); /** @var IRequest $request */ $this->plugin = new PublishPlugin($this->config, $this->urlGenerator); @@ -48,8 +47,8 @@ class PluginTest extends TestCase { $this->server = new Server($root); /** @var SimpleCollection $node */ $this->book = $this->getMockBuilder('OCA\DAV\CalDAV\Calendar')-> - disableOriginalConstructor()-> - getMock(); + disableOriginalConstructor()-> + getMock(); $this->book->method('getName')->willReturn('cal1'); $root->addChild($this->book); $this->plugin->initialize($this->server); diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php index 07a55fef63b..e2d63868af0 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php @@ -471,7 +471,7 @@ class FilesPluginTest extends TestCase { $node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Node') ->disableOriginalConstructor() ->getMock(); - $node->expects($this->at(0)) + $node->expects($this->once()) ->method('getFileInfo') ->willReturn($fileInfoFolderATestTXT); @@ -545,7 +545,7 @@ class FilesPluginTest extends TestCase { ->getMock(); $request - ->expects($this->at(1)) + ->expects($this->once()) ->method('getPath') ->will($this->returnValue('test/somefile.xml')); -- cgit v1.2.3 From 9af2a9ff4d6984145aed3d3fe1312eee56fa8b74 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 1 Aug 2016 17:16:30 +0200 Subject: test serializer --- .../tests/unit/CalDAV/Publishing/PublisherTest.php | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 apps/dav/tests/unit/CalDAV/Publishing/PublisherTest.php (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/Publishing/PublisherTest.php b/apps/dav/tests/unit/CalDAV/Publishing/PublisherTest.php new file mode 100644 index 00000000000..b6b1e4381b5 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Publishing/PublisherTest.php @@ -0,0 +1,56 @@ +write([ + '{' . self::NS_CALENDARSERVER . '}publish-url' => $publish, + ]); + + $this->assertEquals('urltopublish', $publish->getValue()); + + $this->assertXmlStringEqualsXmlString( + ' + + urltopublish + ', $xml); + } + + public function testSerializeNotPublished() { + $publish = new Publisher('urltopublish', false); + + $xml = $this->write([ + '{' . self::NS_CALENDARSERVER . '}pre-publish-url' => $publish, + ]); + + $this->assertEquals('urltopublish', $publish->getValue()); + + $this->assertXmlStringEqualsXmlString( + ' + urltopublish', $xml); + } + + + protected $elementMap = []; + protected $namespaceMap = ['DAV:' => 'd']; + protected $contextUri = '/'; + + private function write($input) { + $writer = new Writer(); + $writer->contextUri = $this->contextUri; + $writer->namespaceMap = $this->namespaceMap; + $writer->openMemory(); + $writer->setIndent(true); + $writer->write($input); + return $writer->outputMemory(); + } +} -- cgit v1.2.3 From f0421e1f75179a42e76973a4b11d8474ee571d08 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 14 Aug 2016 19:08:01 +0200 Subject: add missing tests --- apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php | 6 +++++- apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php index ebdd4d9f940..ca5000f3f44 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php @@ -28,6 +28,7 @@ use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\Calendar; use OCA\DAV\Connector\Sabre\Principal; use OCP\IL10N; +use OCP\IConfig; use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet; use Sabre\DAV\PropPatch; use Sabre\DAV\Xml\Property\Href; @@ -51,6 +52,9 @@ abstract class AbstractCalDavBackendTest extends TestCase { /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ protected $userManager; + + /** var OCP\IConfig */ + protected $config; const UNIT_TEST_USER = 'principals/users/caldav-unit-test'; const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1'; @@ -75,7 +79,7 @@ abstract class AbstractCalDavBackendTest extends TestCase { ->willReturn([self::UNIT_TEST_GROUP]); $db = \OC::$server->getDatabaseConnection(); - $config = \OC::$server->getConfig(); + $this->config = \OC::$server->getConfig(); $this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $config); $this->tearDown(); diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index a597b476336..0c07ed7c292 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -343,13 +343,24 @@ EOD; ->disableOriginalConstructor()->getMock(); $calendar = new Calendar($this->backend, $calendarInfo, $l10n); - $this->backend->setPublishStatus(true, $calendar); - $this->assertEquals(true, $this->backend->getPublishStatus($calendar)); + $calendar->setPublishStatus(true); + $this->assertEquals(true, $calendar->getPublishStatus()); $publicCalendars = $this->backend->getPublicCalendars(); $this->assertEquals(1, count($publicCalendars)); $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']); + $publicCalendarURI = md5($this->config->getSystemValue('secret', '') . $calendar->getResourceId()); + $publicCalendar = $this->backend->getPublicCalendar($publicCalendarURI); + $this->assertEquals(true, $publicCalendar['{http://owncloud.org/ns}public']); + + $calendar->setPublishStatus(false); + $this->assertEquals(false, $calendar->getPublishStatus()); + + $publicCalendarURI = md5($this->config->getSystemValue('secret', '') . $calendar->getResourceId()); + $this->setExpectedException('Sabre\DAV\Exception\NotFound'); + $publicCalendar = $this->backend->getPublicCalendar($publicCalendarURI); + } public function testSubscriptions() { -- cgit v1.2.3 From ff67cbc6af144c92d4b1e69d6c606c1fdbf177d4 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 18 Aug 2016 17:04:04 +0200 Subject: Add test for PublicCalendarRoot --- .../tests/unit/CalDAV/PublicCalendarRootTest.php | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php new file mode 100644 index 00000000000..9ce558ad34b --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php @@ -0,0 +1,103 @@ +getDatabaseConnection(); + $this->principal = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal') + ->disableOriginalConstructor() + ->getMock(); + $this->config = \OC::$server->getConfig(); + + $this->backend = new CalDavBackend($db, $this->principal, $this->config); + + $this->publicCalendarRoot = new PublicCalendarRoot($this->backend); + + $this->l10n = $this->getMockBuilder('\OCP\IL10N') + ->disableOriginalConstructor()->getMock(); + } + + public function testGetName() { + $name = $this->publicCalendarRoot->getName(); + $this->assertEquals('public-calendars', $name); + } + + public function testGetChild() { + + $calendar = $this->createPublicCalendar(); + + $publicCalendarURI = md5($this->config->getSystemValue('secret', '') . $calendar->getResourceId()); + + $calendarResult = $this->publicCalendarRoot->getChild($publicCalendarURI); + $this->assertEquals($calendar, $calendarResult); + } + + public function testGetChildren() { + + $publicCalendars = $this->backend->getPublicCalendars(); + + $calendarResults = $this->publicCalendarRoot->getChildren(); + + $this->assertEquals(1, count($calendarResults)); + $this->assertEquals(new Calendar($this->backend, $publicCalendars[0], $this->l10n), $calendarResults[0]); + + } + + /** + * @return Calendar + */ + protected function createPublicCalendar() { + $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []); + + $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0]; + + $calendarInfo['uri'] = md5($this->config->getSystemValue('secret', '') . $calendarInfo['id']); + list(, $name) = Uri\split($calendarInfo['principaluri']); + $calendarInfo['{DAV:}displayname'] = $calendarInfo['{DAV:}displayname'] . ' (' . $name . ')'; + $calendarInfo['{http://owncloud.org/ns}owner-principal'] = $calendarInfo['principaluri']; + $calendarInfo['{http://owncloud.org/ns}read-only'] = false; + $calendarInfo['{http://owncloud.org/ns}public'] = true; + + $calendar = new Calendar($this->backend, $calendarInfo, $this->l10n); + $calendar->setPublishStatus(true); + + return $calendar; + } + + +} -- cgit v1.2.3 From 2fff203c598e8f71ee0aea4265efe7f952864d06 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Tue, 30 Aug 2016 19:10:50 +0200 Subject: Add missing constructor argument --- apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php index ca5000f3f44..589c00c377a 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php @@ -79,8 +79,8 @@ abstract class AbstractCalDavBackendTest extends TestCase { ->willReturn([self::UNIT_TEST_GROUP]); $db = \OC::$server->getDatabaseConnection(); - $this->config = \OC::$server->getConfig(); - $this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $config); + $this->config = \OC::$server->getConfig(); + $this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $this->config); $this->tearDown(); } -- cgit v1.2.3 From 4659e3ab599f18069765cb2414a8ace8bdf30ca8 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Tue, 30 Aug 2016 19:21:32 +0200 Subject: Add new constructor args --- apps/dav/lib/CalDAV/CalDavBackend.php | 7 +++++-- apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 5f903406168..0cdfcd69571 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -130,9 +130,12 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription * @param IDBConnection $db * @param Principal $principalBackend * @param IUserManager $userManager - * @param IConfig $config + * @param IConfig $config */ - public function __construct(IDBConnection $db, Principal $principalBackend, IUserManager $userManager, IConfig $config) { + public function __construct(IDBConnection $db, + Principal $principalBackend, + IUserManager $userManager, + IConfig $config) { $this->db = $db; $this->principalBackend = $principalBackend; $this->userManager = $userManager; diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php index 9ce558ad34b..85aad24d368 100644 --- a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php @@ -3,10 +3,12 @@ namespace OCA\DAV\Tests\unit\CalDAV; use OCA\DAV\CalDAV\Calendar; +use OCA\DAV\Connector\Sabre\Principal; use OCP\IL10N; use OCP\IConfig; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\PublicCalendarRoot; +use OCP\IUserManager; use Test\TestCase; use Sabre\Uri; @@ -29,12 +31,13 @@ class PublicCalendarRootTest extends TestCase { /** @var IL10N */ private $l10n; - + /** @var IUserManager */ + private $userManager; + /** @var Principal */ + private $principal; /** var IConfig */ protected $config; - private $principal; - public function setUp() { parent::setUp(); @@ -43,8 +46,14 @@ class PublicCalendarRootTest extends TestCase { ->disableOriginalConstructor() ->getMock(); $this->config = \OC::$server->getConfig(); + $this->userManager = $this->getMockBuilder('\OCP\IUserManager')->getMock(); - $this->backend = new CalDavBackend($db, $this->principal, $this->config); + $this->backend = new CalDavBackend( + $db, + $this->principal, + $this->userManager, + $this->config + ); $this->publicCalendarRoot = new PublicCalendarRoot($this->backend); @@ -99,5 +108,4 @@ class PublicCalendarRootTest extends TestCase { return $calendar; } - } -- cgit v1.2.3 From d884370844c7f807b10aa09e63cb814927011572 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Sat, 3 Sep 2016 10:52:05 +0200 Subject: Use true random string as uri for public calendars - as a result we can no longer return the pre-publish-url --- apps/dav/appinfo/v1/caldav.php | 4 +- apps/dav/lib/AppInfo/Application.php | 5 ++- apps/dav/lib/CalDAV/CalDavBackend.php | 26 +++++++++---- apps/dav/lib/CalDAV/Calendar.php | 5 ++- apps/dav/lib/CalDAV/Publishing/PublishPlugin.php | 14 ++----- apps/dav/lib/Command/CreateCalendar.php | 3 +- apps/dav/lib/DAV/PublicAuth.php | 4 -- apps/dav/lib/RootCollection.php | 6 ++- .../unit/CalDAV/AbstractCalDavBackendTest.php | 8 +++- apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 2 +- .../tests/unit/CalDAV/PublicCalendarRootTest.php | 43 ++++++++++++++-------- 11 files changed, 73 insertions(+), 47 deletions(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/appinfo/v1/caldav.php b/apps/dav/appinfo/v1/caldav.php index d9606f20b72..d18e93dd7a9 100644 --- a/apps/dav/appinfo/v1/caldav.php +++ b/apps/dav/appinfo/v1/caldav.php @@ -47,7 +47,9 @@ $principalBackend = new Principal( ); $db = \OC::$server->getDatabaseConnection(); $config = \OC::$server->getConfig(); -$calDavBackend = new CalDavBackend($db, $principalBackend, \OC::$server->getUserManager(), $config); +$userManager = \OC::$server->getUserManager(); +$random = \OC::$server->getSecureRandom(); +$calDavBackend = new CalDavBackend($db, $principalBackend, $userManager, $config, $random); $debugging = \OC::$server->getConfig()->getSystemValue('debug', false); diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 8bc43da5649..69a5e336bbf 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -81,12 +81,15 @@ class Application extends App { $container->registerService('CalDavBackend', function($c) { /** @var IAppContainer $c */ $db = $c->getServer()->getDatabaseConnection(); + $userManager = $c->getServer()->getUserManager(); $config = $c->getServer()->getConfig(); + $random = $c->getServer()->getSecureRandom(); + $principal = new Principal( $c->getServer()->getUserManager(), $c->getServer()->getGroupManager() ); - return new CalDavBackend($db, $principal, $c->getServer()->getUserManager(), $config); + return new CalDavBackend($db, $principal, $userManager, $config, $random); }); $container->registerService('BirthdayService', function($c) { diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 0cdfcd69571..7b8c1be51f5 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -33,6 +33,7 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\IUser; use OCP\IUserManager; +use OCP\Security\ISecureRandom; use Sabre\CalDAV\Backend\AbstractBackend; use Sabre\CalDAV\Backend\SchedulingSupport; use Sabre\CalDAV\Backend\SubscriptionSupport; @@ -124,6 +125,9 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription /** @var IConfig */ private $config; + /** @var ISecureRandom */ + private $random; + /** * CalDavBackend constructor. * @@ -131,16 +135,19 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription * @param Principal $principalBackend * @param IUserManager $userManager * @param IConfig $config + * @param ISecureRandom $random */ public function __construct(IDBConnection $db, Principal $principalBackend, IUserManager $userManager, - IConfig $config) { + IConfig $config, + ISecureRandom $random) { $this->db = $db; $this->principalBackend = $principalBackend; $this->userManager = $userManager; $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); $this->config = $config; + $this->random = $random; } /** @@ -400,10 +407,9 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription if ($row['components']) { $components = explode(',',$row['components']); } - $uri = md5($this->config->getSystemValue('secret', '') . $row['id']); $calendar = [ 'id' => $row['id'], - 'uri' => $uri, + 'uri' => $row['publicuri'], 'principaluri' => $row['principaluri'], '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', @@ -1601,24 +1607,28 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription /** * @param boolean $value * @param \OCA\DAV\CalDAV\Calendar $calendar + * @return string|null */ public function setPublishStatus($value, $calendar) { $query = $this->db->getQueryBuilder(); if ($value) { + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_DIGITS); $query->insert('dav_shares') ->values([ 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), 'type' => $query->createNamedParameter('calendar'), 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), - 'publicuri' => $query->createNamedParameter(md5($this->config->getSystemValue('secret', '') . $calendar->getResourceId())) + 'publicuri' => $query->createNamedParameter($publicUri) ]); - } else { - $query->delete('dav_shares') - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); + $query->execute(); + return $publicUri; } + $query->delete('dav_shares') + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); $query->execute(); + return null; } /** diff --git a/apps/dav/lib/CalDAV/Calendar.php b/apps/dav/lib/CalDAV/Calendar.php index d6799d1827b..5fe9be8957d 100644 --- a/apps/dav/lib/CalDAV/Calendar.php +++ b/apps/dav/lib/CalDAV/Calendar.php @@ -252,9 +252,12 @@ class Calendar extends \Sabre\CalDAV\Calendar implements IShareable { /** * @param boolean $value + * @return string|null */ function setPublishStatus($value) { - $this->caldavBackend->setPublishStatus($value, $this); + $publicUri = $this->caldavBackend->setPublishStatus($value, $this); + $this->calendarInfo['publicuri'] = $publicUri; + return $publicUri; } /** diff --git a/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php b/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php index 7434da6b62e..0e5377d30c1 100644 --- a/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php +++ b/apps/dav/lib/CalDAV/Publishing/PublishPlugin.php @@ -94,22 +94,16 @@ class PublishPlugin extends ServerPlugin { public function propFind(PropFind $propFind, INode $node) { if ($node instanceof Calendar) { - $token = md5($this->config->getSystemValue('secret', '').$node->getResourceId()); - - $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token; - - $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node, $publishUrl) { + $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) { if ($node->getPublishStatus()) { // We return the publish-url only if the calendar is published. + $token = $node->getName(); + $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token; + return new Publisher($publishUrl, true); } }); - $propFind->handle('{'.self::NS_CALENDARSERVER.'}pre-publish-url', function () use ($node, $publishUrl) { - // The pre-publish-url is always returned - return new Publisher($publishUrl, false); - }); - $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function() use ($node) { return new AllowedSharingModes(!$node->isSubscription(), !$node->isSubscription()); }); diff --git a/apps/dav/lib/Command/CreateCalendar.php b/apps/dav/lib/Command/CreateCalendar.php index 54cb06db666..da1f706a8b8 100644 --- a/apps/dav/lib/Command/CreateCalendar.php +++ b/apps/dav/lib/Command/CreateCalendar.php @@ -76,9 +76,10 @@ class CreateCalendar extends Command { $this->groupManager ); $config = \OC::$server->getConfig(); + $random = \OC::$server->getSecureRandom(); $name = $input->getArgument('name'); - $caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $config); + $caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $config, $random); $caldav->createCalendar("principals/users/$user", $name, []); } } diff --git a/apps/dav/lib/DAV/PublicAuth.php b/apps/dav/lib/DAV/PublicAuth.php index 3f5d37f1a69..33588fc0add 100644 --- a/apps/dav/lib/DAV/PublicAuth.php +++ b/apps/dav/lib/DAV/PublicAuth.php @@ -86,10 +86,6 @@ class PublicAuth implements BackendInterface { * @return bool */ private function isRequestPublic(RequestInterface $request) { - $params = $request->getQueryParameters(); - if (isset($params['sabreAction']) && $params['sabreAction'] == 'asset') { - return true; - } $url = $request->getPath(); $matchingUrls = array_filter($this->publicURLs, function ($publicUrl) use ($url) { return strpos($url, $publicUrl, 0) === 0; diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php index f99d5850212..4c76dc30c3f 100644 --- a/apps/dav/lib/RootCollection.php +++ b/apps/dav/lib/RootCollection.php @@ -39,10 +39,12 @@ class RootCollection extends SimpleCollection { public function __construct() { $config = \OC::$server->getConfig(); + $random = \OC::$server->getSecureRandom(); + $userManager = \OC::$server->getUserManager(); $db = \OC::$server->getDatabaseConnection(); $dispatcher = \OC::$server->getEventDispatcher(); $userPrincipalBackend = new Principal( - \OC::$server->getUserManager(), + $userManager, \OC::$server->getGroupManager() ); $groupPrincipalBackend = new GroupPrincipalBackend( @@ -60,7 +62,7 @@ class RootCollection extends SimpleCollection { $systemPrincipals->disableListing = $disableListing; $filesCollection = new Files\RootCollection($userPrincipalBackend, 'principals/users'); $filesCollection->disableListing = $disableListing; - $caldavBackend = new CalDavBackend($db, $userPrincipalBackend, \OC::$server->getUserManager(), $config); + $caldavBackend = new CalDavBackend($db, $userPrincipalBackend, $userManager, $config, $random); $calendarRoot = new CalendarRoot($userPrincipalBackend, $caldavBackend, 'principals/users'); $calendarRoot->disableListing = $disableListing; $publicCalendarRoot = new PublicCalendarRoot($caldavBackend); diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php index 589c00c377a..2559ecbbf89 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackendTest.php @@ -29,6 +29,7 @@ use OCA\DAV\CalDAV\Calendar; use OCA\DAV\Connector\Sabre\Principal; use OCP\IL10N; use OCP\IConfig; +use OCP\Security\ISecureRandom; use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet; use Sabre\DAV\PropPatch; use Sabre\DAV\Xml\Property\Href; @@ -56,6 +57,9 @@ abstract class AbstractCalDavBackendTest extends TestCase { /** var OCP\IConfig */ protected $config; + /** @var ISecureRandom */ + private $random; + const UNIT_TEST_USER = 'principals/users/caldav-unit-test'; const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1'; const UNIT_TEST_GROUP = 'principals/groups/caldav-unit-test-group'; @@ -80,8 +84,8 @@ abstract class AbstractCalDavBackendTest extends TestCase { $db = \OC::$server->getDatabaseConnection(); $this->config = \OC::$server->getConfig(); - $this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $this->config); - + $this->random = \OC::$server->getSecureRandom(); + $this->backend = new CalDavBackend($db, $this->principal, $this->userManager, $this->config, $this->random); $this->tearDown(); } diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 0c07ed7c292..6f846515d8e 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -350,7 +350,7 @@ EOD; $this->assertEquals(1, count($publicCalendars)); $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']); - $publicCalendarURI = md5($this->config->getSystemValue('secret', '') . $calendar->getResourceId()); + $publicCalendarURI = $publicCalendars[0]['uri']; $publicCalendar = $this->backend->getPublicCalendar($publicCalendarURI); $this->assertEquals(true, $publicCalendar['{http://owncloud.org/ns}public']); diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php index 85aad24d368..6dfec6d7e1f 100644 --- a/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarRootTest.php @@ -5,12 +5,11 @@ namespace OCA\DAV\Tests\unit\CalDAV; use OCA\DAV\CalDAV\Calendar; use OCA\DAV\Connector\Sabre\Principal; use OCP\IL10N; -use OCP\IConfig; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\PublicCalendarRoot; use OCP\IUserManager; +use OCP\Security\ISecureRandom; use Test\TestCase; -use Sabre\Uri; /** * Class PublicCalendarRootTest @@ -22,13 +21,10 @@ use Sabre\Uri; class PublicCalendarRootTest extends TestCase { const UNIT_TEST_USER = 'principals/users/caldav-unit-test'; - /** @var CalDavBackend */ private $backend; - /** @var PublicCalendarRoot */ private $publicCalendarRoot; - /** @var IL10N */ private $l10n; /** @var IUserManager */ @@ -37,6 +33,8 @@ class PublicCalendarRootTest extends TestCase { private $principal; /** var IConfig */ protected $config; + /** @var ISecureRandom */ + private $random; public function setUp() { parent::setUp(); @@ -47,12 +45,14 @@ class PublicCalendarRootTest extends TestCase { ->getMock(); $this->config = \OC::$server->getConfig(); $this->userManager = $this->getMockBuilder('\OCP\IUserManager')->getMock(); + $this->random = \OC::$server->getSecureRandom(); $this->backend = new CalDavBackend( $db, $this->principal, $this->userManager, - $this->config + $this->config, + $this->random ); $this->publicCalendarRoot = new PublicCalendarRoot($this->backend); @@ -61,6 +61,18 @@ class PublicCalendarRootTest extends TestCase { ->disableOriginalConstructor()->getMock(); } + public function tearDown() { + parent::tearDown(); + + if (is_null($this->backend)) { + return; + } + $books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER); + foreach ($books as $book) { + $this->backend->deleteCalendar($book['id']); + } + } + public function testGetName() { $name = $this->publicCalendarRoot->getName(); $this->assertEquals('public-calendars', $name); @@ -70,13 +82,18 @@ class PublicCalendarRootTest extends TestCase { $calendar = $this->createPublicCalendar(); - $publicCalendarURI = md5($this->config->getSystemValue('secret', '') . $calendar->getResourceId()); + $publicCalendars = $this->backend->getPublicCalendars(); + $this->assertEquals(1, count($publicCalendars)); + $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']); + + $publicCalendarURI = $publicCalendars[0]['uri']; $calendarResult = $this->publicCalendarRoot->getChild($publicCalendarURI); $this->assertEquals($calendar, $calendarResult); } public function testGetChildren() { + $this->createPublicCalendar(); $publicCalendars = $this->backend->getPublicCalendars(); @@ -84,7 +101,6 @@ class PublicCalendarRootTest extends TestCase { $this->assertEquals(1, count($calendarResults)); $this->assertEquals(new Calendar($this->backend, $publicCalendars[0], $this->l10n), $calendarResults[0]); - } /** @@ -94,16 +110,11 @@ class PublicCalendarRootTest extends TestCase { $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []); $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0]; + $calendar = new Calendar($this->backend, $calendarInfo, $this->l10n); + $publicUri = $calendar->setPublishStatus(true); - $calendarInfo['uri'] = md5($this->config->getSystemValue('secret', '') . $calendarInfo['id']); - list(, $name) = Uri\split($calendarInfo['principaluri']); - $calendarInfo['{DAV:}displayname'] = $calendarInfo['{DAV:}displayname'] . ' (' . $name . ')'; - $calendarInfo['{http://owncloud.org/ns}owner-principal'] = $calendarInfo['principaluri']; - $calendarInfo['{http://owncloud.org/ns}read-only'] = false; - $calendarInfo['{http://owncloud.org/ns}public'] = true; - + $calendarInfo = $this->backend->getPublicCalendar($publicUri); $calendar = new Calendar($this->backend, $calendarInfo, $this->l10n); - $calendar->setPublishStatus(true); return $calendar; } -- cgit v1.2.3 From 9c75b00850f940993fc5f6e5bb52bccf58b43023 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 15 Sep 2016 17:09:24 +0200 Subject: fix tests --- apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apps/dav/tests') diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 6f846515d8e..8349d98cd94 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -344,7 +344,7 @@ EOD; $calendar = new Calendar($this->backend, $calendarInfo, $l10n); $calendar->setPublishStatus(true); - $this->assertEquals(true, $calendar->getPublishStatus()); + $this->assertNotEquals(false, $calendar->getPublishStatus()); $publicCalendars = $this->backend->getPublicCalendars(); $this->assertEquals(1, count($publicCalendars)); -- cgit v1.2.3