diff options
Diffstat (limited to 'apps/dav/tests')
17 files changed, 1194 insertions, 297 deletions
diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 5adda30c19d..dc531b5a64a 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -1,10 +1,12 @@ <?php /** * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2017 Georg Ehrke * * @author Joas Schilling <coding@schilljs.com> * @author Thomas Citharel <tcit@tcit.fr> * @author Thomas Müller <thomas.mueller@tmit.eu> + * @author Georg Ehrke <oc.list@georgehrke.com> * * @license AGPL-3.0 * @@ -489,4 +491,134 @@ EOD; 'unknown class -> private' => [CalDavBackend::CLASSIFICATION_PRIVATE, 'classification', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//dmfs.org//mimedir.icalendar//EN\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTART;TZID=Europe/Berlin:20160419T130000\r\nSUMMARY:Test\r\nCLASS:VERTRAULICH\r\nTRANSP:OPAQUE\r\nSTATUS:CONFIRMED\r\nDTEND;TZID=Europe/Berlin:20160419T140000\r\nLAST-MODIFIED:20160419T074202Z\r\nDTSTAMP:20160419T074202Z\r\nCREATED:20160419T074202Z\r\nUID:2e468c48-7860-492e-bc52-92fa0daeeccf.1461051722310\r\nEND:VEVENT\r\nEND:VCALENDAR"], ]; } + + public function testCalendarSearch() { + $calendarId = $this->createTestCalendar(); + + $uri = static::getUniqueID('calobj'); + $calData = <<<EOD +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:ownCloud Calendar +BEGIN:VEVENT +CREATED;VALUE=DATE-TIME:20130910T125139Z +UID:47d15e3ec8 +LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z +DTSTAMP;VALUE=DATE-TIME:20130910T125139Z +SUMMARY:Test Event +DTSTART;VALUE=DATE-TIME:20130912T130000Z +DTEND;VALUE=DATE-TIME:20130912T140000Z +CLASS:PUBLIC +END:VEVENT +END:VCALENDAR +EOD; + + $this->backend->createCalendarObject($calendarId, $uri, $calData); + + $search1 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION' + ], + 'search-term' => 'Test', + ]); + $this->assertEquals(count($search1), 1); + + + // update the card + $calData = <<<'EOD' +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:ownCloud Calendar +BEGIN:VEVENT +CREATED;VALUE=DATE-TIME:20130910T125139Z +UID:47d15e3ec8 +LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z +DTSTAMP;VALUE=DATE-TIME:20130910T125139Z +SUMMARY:123 Event +DTSTART;VALUE=DATE-TIME:20130912T130000Z +DTEND;VALUE=DATE-TIME:20130912T140000Z +ATTENDEE;CN=test:mailto:foo@bar.com +END:VEVENT +END:VCALENDAR +EOD; + $this->backend->updateCalendarObject($calendarId, $uri, $calData); + + $search2 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION' + ], + 'search-term' => 'Test', + ]); + $this->assertEquals(count($search2), 0); + + $search3 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION' + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 'Test', + ]); + $this->assertEquals(count($search3), 1); + + // t matches both summary and attendee's CN, but we want unique results + $search4 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION' + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 't', + ]); + $this->assertEquals(count($search4), 1); + + $this->backend->deleteCalendarObject($calendarId, $uri); + + $search5 = $this->backend->calendarSearch(self::UNIT_TEST_USER, [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION' + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 't', + ]); + $this->assertEquals(count($search5), 0); + } } diff --git a/apps/dav/tests/unit/CalDAV/CalendarTest.php b/apps/dav/tests/unit/CalDAV/CalendarTest.php index 4ede886d31e..cf295f01065 100644 --- a/apps/dav/tests/unit/CalDAV/CalendarTest.php +++ b/apps/dav/tests/unit/CalDAV/CalendarTest.php @@ -246,6 +246,7 @@ class CalendarTest extends TestCase { ]); $backend->expects($this->any())->method('getCalendarObject') ->willReturn($calObject2)->with(666, 'event-2'); + $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1); $calendarInfo = [ 'principaluri' => 'user2', @@ -333,6 +334,7 @@ EOD; ]); $backend->expects($this->any())->method('getCalendarObject') ->willReturn($calObject1)->with(666, 'event-1'); + $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1); $calendarInfo = [ '{http://owncloud.org/ns}owner-principal' => $isShared ? 'user1' : 'user2', diff --git a/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php b/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php index 6b2bf58d392..03cbf71d6ca 100644 --- a/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php +++ b/apps/dav/tests/unit/CalDAV/PublicCalendarTest.php @@ -51,6 +51,7 @@ class PublicCalendarTest extends CalendarTest { ]); $backend->expects($this->any())->method('getCalendarObject') ->willReturn($calObject2)->with(666, 'event-2'); + $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1); $calendarInfo = [ '{http://owncloud.org/ns}owner-principal' => 'user2', @@ -135,6 +136,7 @@ EOD; ]); $backend->expects($this->any())->method('getCalendarObject') ->willReturn($calObject1)->with(666, 'event-1'); + $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1); $calendarInfo = [ '{http://owncloud.org/ns}owner-principal' => 'user1', diff --git a/apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php b/apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php new file mode 100644 index 00000000000..20bac8aa9f5 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Search/Request/CalendarSearchReportTest.php @@ -0,0 +1,339 @@ +<?php +/** + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com> + * @license GNU AGPL version 3 or any later version + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\Search\Xml\Request; + +use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport; +use Sabre\Xml\Reader; +use Test\TestCase; + +class CalendarSearchReportTest extends TestCase { + + private $elementMap = [ + '{http://nextcloud.com/ns}calendar-search' => + 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport', + ]; + + public function testFoo() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:comp-filter name="VEVENT" /> + <nc:comp-filter name="VTODO" /> + <nc:prop-filter name="SUMMARY" /> + <nc:prop-filter name="LOCATION" /> + <nc:prop-filter name="ATTENDEE" /> + <nc:param-filter property="ATTENDEE" name="CN" /> + <nc:search-term>foo</nc:search-term> + </nc:filter> + <nc:limit>10</nc:limit> + <nc:offset>5</nc:offset> +</nc:calendar-search> +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'props' => [ + 'SUMMARY', + 'LOCATION', + 'ATTENDEE' + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = 10; + $calendarSearchReport->offset = 5; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + public function testNoLimitOffset() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:comp-filter name="VEVENT" /> + <nc:prop-filter name="SUMMARY" /> + <nc:search-term>foo</nc:search-term> + </nc:filter> +</nc:calendar-search> +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + ], + 'props' => [ + 'SUMMARY', + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage {http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter given without any {http://nextcloud.com/ns}comp-filter + */ + public function testRequiresCompFilter() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:prop-filter name="SUMMARY" /> + <nc:prop-filter name="LOCATION" /> + <nc:prop-filter name="ATTENDEE" /> + <nc:param-filter property="ATTENDEE" name="CN" /> + <nc:search-term>foo</nc:search-term> + </nc:filter> + <nc:limit>10</nc:limit> + <nc:offset>5</nc:offset> +</nc:calendar-search> +XML; + + $this->parse($xml); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage The {http://nextcloud.com/ns}filter element is required for this request + */ + public function testRequiresFilter() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> +</nc:calendar-search> +XML; + + $this->parse($xml); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage {http://nextcloud.com/ns}search-term is required for this request + */ + public function testNoSearchTerm() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:comp-filter name="VEVENT" /> + <nc:comp-filter name="VTODO" /> + <nc:prop-filter name="SUMMARY" /> + <nc:prop-filter name="LOCATION" /> + <nc:prop-filter name="ATTENDEE" /> + <nc:param-filter property="ATTENDEE" name="CN" /> + </nc:filter> + <nc:limit>10</nc:limit> + <nc:offset>5</nc:offset> +</nc:calendar-search> +XML; + + $result = $this->parse($xml); + } + + /** + * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedExceptionMessage At least one{http://nextcloud.com/ns}prop-filter or {http://nextcloud.com/ns}param-filter is required for this request + */ + public function testCompOnly() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:comp-filter name="VEVENT" /> + <nc:comp-filter name="VTODO" /> + <nc:search-term>foo</nc:search-term> + </nc:filter> +</nc:calendar-search> +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + 'VTODO' + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + public function testPropOnly() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:comp-filter name="VEVENT" /> + <nc:prop-filter name="SUMMARY" /> + <nc:search-term>foo</nc:search-term> + </nc:filter> +</nc:calendar-search> +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + ], + 'props' => [ + 'SUMMARY', + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + public function testParamOnly() { + $xml = <<<XML +<?xml version="1.0" encoding="UTF-8"?> +<nc:calendar-search xmlns:nc="http://nextcloud.com/ns" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"> + <d:prop> + <d:getetag /> + <c:calendar-data /> + </d:prop> + <nc:filter> + <nc:comp-filter name="VEVENT" /> + <nc:param-filter property="ATTENDEE" name="CN" /> + <nc:search-term>foo</nc:search-term> + </nc:filter> +</nc:calendar-search> +XML; + + $result = $this->parse($xml); + + $calendarSearchReport = new CalendarSearchReport(); + $calendarSearchReport->properties = [ + '{DAV:}getetag', + '{urn:ietf:params:xml:ns:caldav}calendar-data', + ]; + $calendarSearchReport->filters = [ + 'comps' => [ + 'VEVENT', + ], + 'params' => [ + [ + 'property' => 'ATTENDEE', + 'parameter' => 'CN' + ] + ], + 'search-term' => 'foo' + ]; + $calendarSearchReport->limit = null; + $calendarSearchReport->offset = null; + + $this->assertEquals( + $calendarSearchReport, + $result['value'] + ); + } + + private function parse($xml, array $elementMap = []) { + $reader = new Reader(); + $reader->elementMap = array_merge($this->elementMap, $elementMap); + $reader->xml($xml); + return $reader->parse(); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php b/apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php new file mode 100644 index 00000000000..fc647bb5daf --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Search/SearchPluginTest.php @@ -0,0 +1,124 @@ +<?php +/** + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com> + * @license GNU AGPL version 3 or any later version + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\Search; + +use OCA\DAV\CalDAV\CalendarHome; +use OCA\DAV\CalDAV\Search\SearchPlugin; +use OCA\DAV\CalDAV\Search\Xml\Request\CalendarSearchReport; +use Test\TestCase; + +class SearchPluginTest extends TestCase { + + protected $server; + + /** @var \OCA\DAV\CalDAV\Search\SearchPlugin $plugin */ + protected $plugin; + + public function setUp() { + parent::setUp(); + + $this->server = $this->createMock(\Sabre\DAV\Server::class); + $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class); + $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class); + + $this->plugin = new SearchPlugin(); + $this->plugin->initialize($this->server); + } + + public function testGetFeatures() { + $this->assertEquals(['nc-calendar-search'], $this->plugin->getFeatures()); + } + + public function testGetName() { + $this->assertEquals('nc-calendar-search', $this->plugin->getPluginName()); + } + + public function testInitialize() { + $server = $this->createMock(\Sabre\DAV\Server::class); + + $plugin = new SearchPlugin(); + + $server->expects($this->at(0)) + ->method('on') + ->with('report', [$plugin, 'report']); + + $plugin->initialize($server); + + $this->assertEquals( + $server->xml->elementMap['{http://nextcloud.com/ns}calendar-search'], + 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport' + ); + } + + public function testReportUnknown() { + $result = $this->plugin->report('{urn:ietf:params:xml:ns:caldav}calendar-query', 'REPORT', null); + $this->assertEquals($result, null); + $this->assertNotEquals($this->server->transactionType, 'report-nc-calendar-search'); + } + + public function testReport() { + $report = $this->createMock(CalendarSearchReport::class); + $report->filters = []; + $calendarHome = $this->createMock(CalendarHome::class); + $this->server->expects($this->at(0)) + ->method('getRequestUri') + ->with() + ->will($this->returnValue('/re/quest/u/r/i')); + $this->server->tree->expects($this->at(0)) + ->method('getNodeForPath') + ->with('/re/quest/u/r/i') + ->will($this->returnValue($calendarHome)); + $this->server->expects($this->at(1)) + ->method('getHTTPDepth') + ->with(2) + ->will($this->returnValue(2)); + $calendarHome->expects($this->at(0)) + ->method('calendarSearch') + ->will($this->returnValue([])); + + $this->plugin->report('{http://nextcloud.com/ns}calendar-search', $report, ''); + } + + public function testSupportedReportSetNoCalendarHome() { + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/foo/bar') + ->will($this->returnValue(null)); + + $reports = $this->plugin->getSupportedReportSet('/foo/bar'); + $this->assertEquals([], $reports); + } + + public function testSupportedReportSet() { + $calendarHome = $this->createMock(CalendarHome::class); + + $this->server->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/bar/foo') + ->will($this->returnValue($calendarHome)); + + $reports = $this->plugin->getSupportedReportSet('/bar/foo'); + $this->assertEquals([ + '{http://nextcloud.com/ns}calendar-search' + ], $reports); + } +} diff --git a/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php b/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php index c108432d65b..f3a271a2db2 100644 --- a/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php +++ b/apps/dav/tests/unit/CardDAV/CardDavBackendTest.php @@ -34,9 +34,12 @@ use OCA\DAV\Connector\Sabre\Principal; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\IL10N; +use OCP\IUserManager; use Sabre\DAV\PropPatch; use Sabre\VObject\Component\VCard; use Sabre\VObject\Property\Text; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\GenericEvent; use Test\TestCase; /** @@ -54,9 +57,12 @@ class CardDavBackendTest extends TestCase { /** @var Principal | \PHPUnit_Framework_MockObject_MockObject */ private $principal; - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ private $userManager; + /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $dispatcher; + /** @var IDBConnection */ private $db; @@ -73,9 +79,7 @@ class CardDavBackendTest extends TestCase { public function setUp() { parent::setUp(); - $this->userManager = $this->getMockBuilder('OCP\IUserManager') - ->disableOriginalConstructor() - ->getMock(); + $this->userManager = $this->createMock(IUserManager::class); $this->principal = $this->getMockBuilder('OCA\DAV\Connector\Sabre\Principal') ->disableOriginalConstructor() ->setMethods(['getPrincipalByPath', 'getGroupMembership']) @@ -88,11 +92,11 @@ class CardDavBackendTest extends TestCase { $this->principal->method('getGroupMembership') ->withAnyParameters() ->willReturn([self::UNIT_TEST_GROUP]); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->db = \OC::$server->getDatabaseConnection(); - $this->backend = new CardDavBackend($this->db, $this->principal, $this->userManager, null); - + $this->backend = new CardDavBackend($this->db, $this->principal, $this->userManager, $this->dispatcher); // start every test with a empty cards_properties and cards table $query = $this->db->getQueryBuilder(); $query->delete('cards_properties')->execute(); @@ -172,7 +176,7 @@ class CardDavBackendTest extends TestCase { /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backend */ $backend = $this->getMockBuilder(CardDavBackend::class) - ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null]) + ->setConstructorArgs([$this->db, $this->principal, $this->userManager, $this->dispatcher]) ->setMethods(['updateProperties', 'purgeProperties'])->getMock(); // create a new address book @@ -185,6 +189,16 @@ class CardDavBackendTest extends TestCase { // updateProperties is expected twice, once for createCard and once for updateCard $backend->expects($this->at(0))->method('updateProperties')->with($bookId, $uri, ''); $backend->expects($this->at(1))->method('updateProperties')->with($bookId, $uri, '***'); + + // Expect event + $this->dispatcher->expects($this->at(0)) + ->method('dispatch') + ->with('\OCA\DAV\CardDAV\CardDavBackend::createCard', $this->callback(function(GenericEvent $e) use ($bookId, $uri) { + return $e->getArgument('addressBookId') === $bookId && + $e->getArgument('cardUri') === $uri && + $e->getArgument('cardData') === ''; + })); + // create a card $backend->createCard($bookId, $uri, ''); @@ -203,11 +217,28 @@ class CardDavBackendTest extends TestCase { $this->assertArrayHasKey('size', $card); $this->assertEquals('', $card['carddata']); + // Expect event + $this->dispatcher->expects($this->at(0)) + ->method('dispatch') + ->with('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $this->callback(function(GenericEvent $e) use ($bookId, $uri) { + return $e->getArgument('addressBookId') === $bookId && + $e->getArgument('cardUri') === $uri && + $e->getArgument('cardData') === '***'; + })); + // update the card $backend->updateCard($bookId, $uri, '***'); $card = $backend->getCard($bookId, $uri); $this->assertEquals('***', $card['carddata']); + // Expect event + $this->dispatcher->expects($this->at(0)) + ->method('dispatch') + ->with('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', $this->callback(function(GenericEvent $e) use ($bookId, $uri) { + return $e->getArgument('addressBookId') === $bookId && + $e->getArgument('cardUri') === $uri; + })); + // delete the card $backend->expects($this->once())->method('purgeProperties')->with($bookId, $card['id']); $backend->deleteCard($bookId, $uri); @@ -218,7 +249,7 @@ class CardDavBackendTest extends TestCase { public function testMultiCard() { $this->backend = $this->getMockBuilder(CardDavBackend::class) - ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null]) + ->setConstructorArgs([$this->db, $this->principal, $this->userManager, $this->dispatcher]) ->setMethods(['updateProperties'])->getMock(); // create a new address book @@ -264,7 +295,7 @@ class CardDavBackendTest extends TestCase { public function testDeleteWithoutCard() { $this->backend = $this->getMockBuilder(CardDavBackend::class) - ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null]) + ->setConstructorArgs([$this->db, $this->principal, $this->userManager, $this->dispatcher]) ->setMethods([ 'getCardId', 'addChange', @@ -304,7 +335,7 @@ class CardDavBackendTest extends TestCase { public function testSyncSupport() { $this->backend = $this->getMockBuilder(CardDavBackend::class) - ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null]) + ->setConstructorArgs([$this->db, $this->principal, $this->userManager, $this->dispatcher]) ->setMethods(['updateProperties'])->getMock(); // create a new address book @@ -363,7 +394,7 @@ class CardDavBackendTest extends TestCase { $cardId = 2; $backend = $this->getMockBuilder(CardDavBackend::class) - ->setConstructorArgs([$this->db, $this->principal, $this->userManager, null]) + ->setConstructorArgs([$this->db, $this->principal, $this->userManager, $this->dispatcher]) ->setMethods(['getCardId'])->getMock(); $backend->expects($this->any())->method('getCardId')->willReturn($cardId); diff --git a/apps/dav/tests/unit/CardDAV/ImageExportPluginTest.php b/apps/dav/tests/unit/CardDAV/ImageExportPluginTest.php index fc4be1433fe..e773e41ba5e 100644 --- a/apps/dav/tests/unit/CardDAV/ImageExportPluginTest.php +++ b/apps/dav/tests/unit/CardDAV/ImageExportPluginTest.php @@ -25,9 +25,13 @@ namespace OCA\DAV\Tests\unit\CardDAV; +use OCA\DAV\CardDAV\AddressBook; use OCA\DAV\CardDAV\ImageExportPlugin; -use OCP\ILogger; +use OCA\DAV\CardDAV\PhotoCache; +use OCP\Files\NotFoundException; +use OCP\Files\SimpleFS\ISimpleFile; use Sabre\CardDAV\Card; +use Sabre\DAV\Node; use Sabre\DAV\Server; use Sabre\DAV\Tree; use Sabre\HTTP\RequestInterface; @@ -36,32 +40,32 @@ use Test\TestCase; class ImageExportPluginTest extends TestCase { - /** @var ResponseInterface | \PHPUnit_Framework_MockObject_MockObject */ + /** @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */ private $response; - /** @var RequestInterface | \PHPUnit_Framework_MockObject_MockObject */ + /** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */ private $request; - /** @var ImageExportPlugin | \PHPUnit_Framework_MockObject_MockObject */ + /** @var ImageExportPlugin|\PHPUnit_Framework_MockObject_MockObject */ private $plugin; /** @var Server */ private $server; - /** @var Tree | \PHPUnit_Framework_MockObject_MockObject */ + /** @var Tree|\PHPUnit_Framework_MockObject_MockObject */ private $tree; - /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */ - private $logger; + /** @var PhotoCache|\PHPUnit_Framework_MockObject_MockObject */ + private $cache; function setUp() { parent::setUp(); - $this->request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')->getMock(); - $this->response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')->getMock(); - $this->server = $this->getMockBuilder('Sabre\DAV\Server')->getMock(); - $this->tree = $this->getMockBuilder('Sabre\DAV\Tree')->disableOriginalConstructor()->getMock(); + $this->request = $this->createMock(RequestInterface::class); + $this->response = $this->createMock(ResponseInterface::class); + $this->server = $this->createMock(Server::class); + $this->tree = $this->createMock(Tree::class); $this->server->tree = $this->tree; - $this->logger = $this->getMockBuilder('\OCP\ILogger')->getMock(); + $this->cache = $this->createMock(PhotoCache::class); - $this->plugin = $this->getMockBuilder('OCA\DAV\CardDAV\ImageExportPlugin') + $this->plugin = $this->getMockBuilder(ImageExportPlugin::class) ->setMethods(['getPhoto']) - ->setConstructorArgs([$this->logger]) + ->setConstructorArgs([$this->cache]) ->getMock(); $this->plugin->initialize($this->server); } @@ -84,112 +88,115 @@ class ImageExportPluginTest extends TestCase { ]; } - public function testNotACard() { - $this->request->expects($this->once())->method('getQueryParameters')->willReturn(['photo' => true]); - $this->request->expects($this->once())->method('getPath')->willReturn('/files/welcome.txt'); - $this->tree->expects($this->once())->method('getNodeForPath')->with('/files/welcome.txt')->willReturn(null); - $result = $this->plugin->httpGet($this->request, $this->response); - $this->assertTrue($result); - } - - /** - * @dataProvider providesCardWithOrWithoutPhoto - * @param bool $expected - * @param array $getPhotoResult - */ - public function testCardWithOrWithoutPhoto($expected, $getPhotoResult) { - $this->request->expects($this->once())->method('getQueryParameters')->willReturn(['photo' => true]); - $this->request->expects($this->once())->method('getPath')->willReturn('/files/welcome.txt'); + public function testNoCard() { + $this->request->method('getQueryParameters') + ->willReturn([ + 'photo' + ]); + $this->request->method('getPath') + ->willReturn('user/book/card'); - $card = $this->getMockBuilder('Sabre\CardDAV\Card')->disableOriginalConstructor()->getMock(); - $this->tree->expects($this->once())->method('getNodeForPath')->with('/files/welcome.txt')->willReturn($card); - - $this->plugin->expects($this->once())->method('getPhoto')->willReturn($getPhotoResult); - - if (!$expected) { - $this->response - ->expects($this->at(0)) - ->method('setHeader') - ->with('Content-Type', $getPhotoResult['Content-Type']); - $this->response - ->expects($this->at(1)) - ->method('setHeader') - ->with('Content-Disposition', 'attachment'); - $this->response - ->expects($this->once()) - ->method('setStatus'); - $this->response - ->expects($this->once()) - ->method('setBody'); - } + $node = $this->createMock(Node::class); + $this->tree->method('getNodeForPath') + ->with('user/book/card') + ->willReturn($node); $result = $this->plugin->httpGet($this->request, $this->response); - $this->assertEquals($expected, $result); + $this->assertTrue($result); } - public function providesCardWithOrWithoutPhoto() { + public function dataTestCard() { return [ - [true, null], - [false, ['Content-Type' => 'image/jpeg', 'body' => '1234']], + [null, false], + [null, true], + [32, false], + [32, true], ]; } /** - * @dataProvider providesPhotoData - * @param $expected - * @param $cardData + * @dataProvider dataTestCard + * + * @param $size + * @param bool $photo */ - public function testGetPhoto($expected, $cardData) { - /** @var Card | \PHPUnit_Framework_MockObject_MockObject $card */ - $card = $this->getMockBuilder('Sabre\CardDAV\Card')->disableOriginalConstructor()->getMock(); - $card->expects($this->once())->method('get')->willReturn($cardData); + public function testCard($size, $photo) { + $query = ['photo' => null]; + if ($size !== null) { + $query['size'] = $size; + } - $this->plugin = new ImageExportPlugin($this->logger); - $this->plugin->initialize($this->server); + $this->request->method('getQueryParameters') + ->willReturn($query); + $this->request->method('getPath') + ->willReturn('user/book/card'); + + $card = $this->createMock(Card::class); + $card->method('getETag') + ->willReturn('"myEtag"'); + $card->method('getName') + ->willReturn('card'); + $book = $this->createMock(AddressBook::class); + $book->method('getResourceId') + ->willReturn(1); + + $this->tree->method('getNodeForPath') + ->willReturnCallback(function($path) use ($card, $book) { + if ($path === 'user/book/card') { + return $card; + } else if ($path === 'user/book') { + return $book; + } + $this->fail(); + }); + + $this->response->expects($this->at(0)) + ->method('setHeader') + ->with('Cache-Control', 'private, max-age=3600, must-revalidate'); + $this->response->expects($this->at(1)) + ->method('setHeader') + ->with('Etag', '"myEtag"'); + $this->response->expects($this->at(2)) + ->method('setHeader') + ->with('Pragma', 'public'); + + $size = $size === null ? -1 : $size; + + if ($photo) { + $file = $this->createMock(ISimpleFile::class); + $file->method('getMimeType') + ->willReturn('imgtype'); + $file->method('getContent') + ->willReturn('imgdata'); + + $this->cache->method('get') + ->with(1, 'card', $size, $card) + ->willReturn($file); + + $this->response->expects($this->at(3)) + ->method('setHeader') + ->with('Content-Type', 'imgtype'); + $this->response->expects($this->at(4)) + ->method('setHeader') + ->with('Content-Disposition', 'attachment'); - $result = $this->plugin->getPhoto($card); - $this->assertEquals($expected, $result); - } + $this->response->expects($this->once()) + ->method('setStatus') + ->with(200); + $this->response->expects($this->once()) + ->method('setBody') + ->with('imgdata'); + + } else { + $this->cache->method('get') + ->with(1, 'card', $size, $card) + ->willThrowException(new NotFoundException()); + $this->response->expects($this->once()) + ->method('setStatus') + ->with(404); + } - public function providesPhotoData() { - return [ - 'empty vcard' => [ - false, - '' - ], - 'vcard without PHOTO' => [ - false, - "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n" - ], - 'vcard 3 with PHOTO' => [ - [ - 'Content-Type' => 'image/jpeg', - 'body' => '12345' - ], - "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU=\r\nEND:VCARD\r\n" - ], - 'vcard 3 with PHOTO URL' => [ - false, - "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;TYPE=JPEG;VALUE=URI:http://example.com/photo.jpg\r\nEND:VCARD\r\n" - ], - 'vcard 4 with PHOTO' => [ - [ - 'Content-Type' => 'image/jpeg', - 'body' => '12345' - ], - "BEGIN:VCARD\r\nVERSION:4.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO:data:image/jpeg;base64,MTIzNDU=\r\nEND:VCARD\r\n" - ], - 'vcard 4 with PHOTO URL' => [ - false, - "BEGIN:VCARD\r\nVERSION:4.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;MEDIATYPE=image/jpeg:http://example.org/photo.jpg\r\nEND:VCARD\r\n" - ], - 'vcard 4 with PHOTO AND INVALID MIMEtYPE' => [ - [ - 'Content-Type' => 'application/octet-stream', - 'body' => '12345' - ], - "BEGIN:VCARD\r\nVERSION:4.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO:data:image/svg;base64,MTIzNDU=\r\nEND:VCARD\r\n" - ], - ]; + $result = $this->plugin->httpGet($this->request, $this->response); + $this->assertFalse($result); } } diff --git a/apps/dav/tests/unit/Connector/PublicAuthTest.php b/apps/dav/tests/unit/Connector/PublicAuthTest.php index 47e1a5be7b8..41cfc0f8ceb 100644 --- a/apps/dav/tests/unit/Connector/PublicAuthTest.php +++ b/apps/dav/tests/unit/Connector/PublicAuthTest.php @@ -33,7 +33,7 @@ use OCP\Share\IManager; * Class PublicAuthTest * * @group DB - * + * * @package OCA\DAV\Tests\unit\Connector */ class PublicAuthTest extends \Test\TestCase { @@ -163,6 +163,28 @@ class PublicAuthTest extends \Test\TestCase { $this->assertTrue($result); } + public function testSharePasswordMailValidPassword() { + $share = $this->getMockBuilder('OCP\Share\IShare') + ->disableOriginalConstructor() + ->getMock(); + $share->method('getPassword')->willReturn('password'); + $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_EMAIL); + + $this->shareManager->expects($this->once()) + ->method('getShareByToken') + ->willReturn($share); + + $this->shareManager->expects($this->once()) + ->method('checkPassword')->with( + $this->equalTo($share), + $this->equalTo('password') + )->willReturn(true); + + $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']); + + $this->assertTrue($result); + } + public function testSharePasswordLinkValidSession() { $share = $this->getMockBuilder('OCP\Share\IShare') ->disableOriginalConstructor() @@ -214,4 +236,32 @@ class PublicAuthTest extends \Test\TestCase { $this->assertFalse($result); } + + + public function testSharePasswordMailInvalidSession() { + $share = $this->getMockBuilder('OCP\Share\IShare') + ->disableOriginalConstructor() + ->getMock(); + $share->method('getPassword')->willReturn('password'); + $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_EMAIL); + $share->method('getId')->willReturn('42'); + + $this->shareManager->expects($this->once()) + ->method('getShareByToken') + ->willReturn($share); + + $this->shareManager->method('checkPassword') + ->with( + $this->equalTo($share), + $this->equalTo('password') + )->willReturn(false); + + $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); + $this->session->method('get')->with('public_link_authenticated')->willReturn('43'); + + $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']); + + $this->assertFalse($result); + } + } diff --git a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php index 18f91bbd8c9..f27f67b0aae 100644 --- a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php @@ -27,6 +27,42 @@ namespace OCA\DAV\Tests\Unit\Connector\Sabre; use OCP\Files\ForbiddenException; +use OC\Files\FileInfo; +use OCA\DAV\Connector\Sabre\Directory; + +class TestViewDirectory extends \OC\Files\View { + + private $updatables; + private $deletables; + private $canRename; + + public function __construct($updatables, $deletables, $canRename = true) { + $this->updatables = $updatables; + $this->deletables = $deletables; + $this->canRename = $canRename; + } + + public function isUpdatable($path) { + return $this->updatables[$path]; + } + + public function isCreatable($path) { + return $this->updatables[$path]; + } + + public function isDeletable($path) { + return $this->deletables[$path]; + } + + public function rename($path1, $path2) { + return $this->canRename; + } + + public function getRelativePath($path) { + return $path; + } +} + /** * @group DB @@ -41,12 +77,11 @@ class DirectoryTest extends \Test\TestCase { protected function setUp() { parent::setUp(); - $this->view = $this->getMockBuilder('OC\Files\View') - ->disableOriginalConstructor() - ->getMock(); - $this->info = $this->getMockBuilder('OC\Files\FileInfo') - ->disableOriginalConstructor() - ->getMock(); + $this->view = $this->createMock('OC\Files\View'); + $this->info = $this->createMock('OC\Files\FileInfo'); + $this->info->expects($this->any()) + ->method('isReadable') + ->willReturn(true); } private function getDir($path = '/') { @@ -58,7 +93,7 @@ class DirectoryTest extends \Test\TestCase { ->method('getPath') ->will($this->returnValue($path)); - return new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info); + return new Directory($this->view, $this->info); } /** @@ -172,7 +207,7 @@ class DirectoryTest extends \Test\TestCase { ->method('getRelativePath') ->will($this->returnValue('')); - $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info); + $dir = new Directory($this->view, $this->info); $nodes = $dir->getChildren(); $this->assertEquals(2, count($nodes)); @@ -183,6 +218,31 @@ class DirectoryTest extends \Test\TestCase { } /** + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testGetChildrenNoPermission() { + $info = $this->createMock(FileInfo::class); + $info->expects($this->any()) + ->method('isReadable') + ->will($this->returnValue(false)); + + $dir = new Directory($this->view, $info); + $dir->getChildren(); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetChildNoPermission() { + $this->info->expects($this->any()) + ->method('isReadable') + ->will($this->returnValue(false)); + + $dir = new Directory($this->view, $this->info); + $dir->getChild('test'); + } + + /** * @expectedException \Sabre\DAV\Exception\ServiceUnavailable */ public function testGetChildThrowStorageNotAvailableException() { @@ -190,7 +250,7 @@ class DirectoryTest extends \Test\TestCase { ->method('getFileInfo') ->willThrowException(new \OCP\Files\StorageNotAvailableException()); - $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info); + $dir = new Directory($this->view, $this->info); $dir->getChild('.'); } @@ -204,7 +264,7 @@ class DirectoryTest extends \Test\TestCase { $this->view->expects($this->never()) ->method('getFileInfo'); - $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info); + $dir = new Directory($this->view, $this->info); $dir->getChild('.'); } @@ -235,7 +295,7 @@ class DirectoryTest extends \Test\TestCase { ->method('getStorage') ->will($this->returnValue($storage)); - $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info); + $dir = new Directory($this->view, $this->info); $this->assertEquals([200, -3], $dir->getQuotaInfo()); //200 used, unlimited } @@ -267,7 +327,105 @@ class DirectoryTest extends \Test\TestCase { ->method('getStorage') ->will($this->returnValue($storage)); - $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info); + $dir = new Directory($this->view, $this->info); $this->assertEquals([200, 800], $dir->getQuotaInfo()); //200 used, 800 free } + + /** + * @dataProvider moveFailedProvider + * @expectedException \Sabre\DAV\Exception\Forbidden + */ + public function testMoveFailed($source, $destination, $updatables, $deletables) { + $this->moveTest($source, $destination, $updatables, $deletables); + } + + /** + * @dataProvider moveSuccessProvider + */ + public function testMoveSuccess($source, $destination, $updatables, $deletables) { + $this->moveTest($source, $destination, $updatables, $deletables); + $this->assertTrue(true); + } + + /** + * @dataProvider moveFailedInvalidCharsProvider + * @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath + */ + public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) { + $this->moveTest($source, $destination, $updatables, $deletables); + } + + public function moveFailedInvalidCharsProvider() { + return [ + ['a/b', 'a/*', ['a' => true, 'a/b' => true, 'a/c*' => false], []], + ]; + } + + public function moveFailedProvider() { + return [ + ['a/b', 'a/c', ['a' => false, 'a/b' => false, 'a/c' => false], []], + ['a/b', 'b/b', ['a' => false, 'a/b' => false, 'b' => false, 'b/b' => false], []], + ['a/b', 'b/b', ['a' => false, 'a/b' => true, 'b' => false, 'b/b' => false], []], + ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => false, 'b/b' => false], []], + ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => false]], + ['a/b', 'a/c', ['a' => false, 'a/b' => true, 'a/c' => false], []], + ]; + } + + public function moveSuccessProvider() { + return [ + ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => true]], + // older files with special chars can still be renamed to valid names + ['a/b*', 'b/b', ['a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false], ['a/b*' => true]], + ]; + } + + /** + * @param $source + * @param $destination + * @param $updatables + */ + private function moveTest($source, $destination, $updatables, $deletables) { + $view = new TestViewDirectory($updatables, $deletables); + + $sourceInfo = new FileInfo($source, null, null, [], null); + $targetInfo = new FileInfo(dirname($destination), null, null, [], null); + + $sourceNode = new Directory($view, $sourceInfo); + $targetNode = $this->getMockBuilder(Directory::class) + ->setMethods(['childExists']) + ->setConstructorArgs([$view, $targetInfo]) + ->getMock(); + $targetNode->expects($this->any())->method('childExists') + ->with(basename($destination)) + ->willReturn(false); + $this->assertTrue($targetNode->moveInto(basename($destination), $source, $sourceNode)); + } + + /** + * @expectedException \Sabre\DAV\Exception\Forbidden + * @expectedExceptionMessage Could not copy directory b, target exists + */ + public function testFailingMove() { + $source = 'a/b'; + $destination = 'c/b'; + $updatables = ['a' => true, 'a/b' => true, 'b' => true, 'c/b' => false]; + $deletables = ['a/b' => true]; + + $view = new TestViewDirectory($updatables, $deletables); + + $sourceInfo = new FileInfo($source, null, null, [], null); + $targetInfo = new FileInfo(dirname($destination), null, null, [], null); + + $sourceNode = new Directory($view, $sourceInfo); + $targetNode = $this->getMockBuilder(Directory::class) + ->setMethods(['childExists']) + ->setConstructorArgs([$view, $targetInfo]) + ->getMock(); + $targetNode->expects($this->once())->method('childExists') + ->with(basename($destination)) + ->willReturn(true); + + $targetNode->moveInto(basename($destination), $source, $sourceNode); + } } diff --git a/apps/dav/tests/unit/Connector/Sabre/FileTest.php b/apps/dav/tests/unit/Connector/Sabre/FileTest.php index 31344b36463..e2666d0de27 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FileTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FileTest.php @@ -1003,4 +1003,23 @@ class FileTest extends \Test\TestCase { $file->get(); } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetThrowsIfNoPermission() { + $view = $this->getMockBuilder('\OC\Files\View') + ->setMethods(['fopen']) + ->getMock(); + $view->expects($this->never()) + ->method('fopen'); + + $info = new \OC\Files\FileInfo('/test.txt', $this->getMockStorage(), null, [ + 'permissions' => \OCP\Constants::PERMISSION_CREATE // no read perm + ], null); + + $file = new \OCA\DAV\Connector\Sabre\File($view, $info); + + $file->get(); + } } diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php index 1c9ebdd09b6..739c8f62540 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesPluginTest.php @@ -32,6 +32,9 @@ use Sabre\DAV\PropPatch; use Sabre\HTTP\RequestInterface; use Sabre\HTTP\ResponseInterface; use Test\TestCase; +use OCA\DAV\Upload\FutureFile; +use OCA\DAV\Connector\Sabre\Directory; +use OCP\Files\FileInfo; /** * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> @@ -107,6 +110,12 @@ class FilesPluginTest extends TestCase { $this->request, $this->previewManager ); + + $response = $this->getMockBuilder(ResponseInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->server->httpResponse = $response; + $this->plugin->initialize($this->server); } @@ -140,13 +149,15 @@ class FilesPluginTest extends TestCase { $node->expects($this->any()) ->method('getDavPermissions') ->will($this->returnValue('DWCKMSR')); + + $fileInfo = $this->createMock(FileInfo::class); + $fileInfo->expects($this->any()) + ->method('isReadable') + ->willReturn(true); + $node->expects($this->any()) ->method('getFileInfo') - ->will($this->returnValue( - $this->getMockBuilder('\OCP\Files\FileInfo') - ->disableOriginalConstructor() - ->getMock() - )); + ->willReturn($fileInfo); return $node; } @@ -305,6 +316,15 @@ class FilesPluginTest extends TestCase { ->getMock(); $node->expects($this->any())->method('getPath')->willReturn('/'); + $fileInfo = $this->createMock(FileInfo::class); + $fileInfo->expects($this->any()) + ->method('isReadable') + ->willReturn(true); + + $node->expects($this->any()) + ->method('getFileInfo') + ->willReturn($fileInfo); + $propFind = new PropFind( '/', [ @@ -321,6 +341,39 @@ class FilesPluginTest extends TestCase { $this->assertEquals('my_fingerprint', $propFind->get(self::DATA_FINGERPRINT_PROPERTYNAME)); } + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetPropertiesWhenNoPermission() { + /** @var \OCA\DAV\Connector\Sabre\Directory | \PHPUnit_Framework_MockObject_MockObject $node */ + $node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->any())->method('getPath')->willReturn('/'); + + $fileInfo = $this->createMock(FileInfo::class); + $fileInfo->expects($this->any()) + ->method('isReadable') + ->willReturn(false); + + $node->expects($this->any()) + ->method('getFileInfo') + ->willReturn($fileInfo); + + $propFind = new PropFind( + '/test', + [ + self::DATA_FINGERPRINT_PROPERTYNAME, + ], + 0 + ); + + $this->plugin->handleGetProperties( + $propFind, + $node + ); + } + public function testUpdateProps() { $node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File'); @@ -535,4 +588,59 @@ class FilesPluginTest extends TestCase { $this->assertEquals("false", $propFind->get(self::HAS_PREVIEW_PROPERTYNAME)); } + + public function testBeforeMoveFutureFileSkip() { + $node = $this->createMock(Directory::class); + + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->with('source') + ->will($this->returnValue($node)); + $this->server->httpResponse->expects($this->never()) + ->method('setStatus'); + + $this->assertNull($this->plugin->beforeMoveFutureFile('source', 'target')); + } + + public function testBeforeMoveFutureFileSkipNonExisting() { + $sourceNode = $this->createMock(FutureFile::class); + + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->with('source') + ->will($this->returnValue($sourceNode)); + $this->tree->expects($this->any()) + ->method('nodeExists') + ->with('target') + ->will($this->returnValue(false)); + $this->server->httpResponse->expects($this->never()) + ->method('setStatus'); + + $this->assertNull($this->plugin->beforeMoveFutureFile('source', 'target')); + } + + public function testBeforeMoveFutureFileMoveIt() { + $sourceNode = $this->createMock(FutureFile::class); + + $this->tree->expects($this->any()) + ->method('getNodeForPath') + ->with('source') + ->will($this->returnValue($sourceNode)); + $this->tree->expects($this->any()) + ->method('nodeExists') + ->with('target') + ->will($this->returnValue(true)); + $this->tree->expects($this->once()) + ->method('move') + ->with('source', 'target'); + + $this->server->httpResponse->expects($this->once()) + ->method('setHeader') + ->with('Content-Length', '0'); + $this->server->httpResponse->expects($this->once()) + ->method('setStatus') + ->with(204); + + $this->assertFalse($this->plugin->beforeMoveFutureFile('source', 'target')); + } } diff --git a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php index 4d8a87b093f..3ca131dbf6f 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FilesReportPluginTest.php @@ -34,6 +34,7 @@ use OCP\Files\Folder; use OCP\IGroupManager; use OCP\SystemTag\ISystemTagManager; use OCP\ITags; +use OCP\Files\FileInfo; class FilesReportPluginTest extends \Test\TestCase { /** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */ @@ -349,6 +350,9 @@ class FilesReportPluginTest extends \Test\TestCase { public function testPrepareResponses() { $requestedProps = ['{DAV:}getcontentlength', '{http://owncloud.org/ns}fileid', '{DAV:}resourcetype']; + $fileInfo = $this->createMock(FileInfo::class); + $fileInfo->method('isReadable')->willReturn(true); + $node1 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory') ->disableOriginalConstructor() ->getMock(); @@ -362,6 +366,7 @@ class FilesReportPluginTest extends \Test\TestCase { $node1->expects($this->any()) ->method('getPath') ->will($this->returnValue('/node1')); + $node1->method('getFileInfo')->willReturn($fileInfo); $node2->expects($this->once()) ->method('getInternalFileId') ->will($this->returnValue('222')); @@ -371,6 +376,7 @@ class FilesReportPluginTest extends \Test\TestCase { $node2->expects($this->any()) ->method('getPath') ->will($this->returnValue('/sub/node2')); + $node2->method('getFileInfo')->willReturn($fileInfo); $config = $this->getMockBuilder('\OCP\IConfig') ->disableOriginalConstructor() diff --git a/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php b/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php index 17cb598bf6e..53f60bd0f1c 100644 --- a/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php @@ -30,47 +30,11 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre; use OC\Files\FileInfo; +use OC\Files\Filesystem; use OC\Files\Storage\Temporary; - -class TestDoubleFileView extends \OC\Files\View { - - public function __construct($creatables, $updatables, $deletables, $canRename = true) { - $this->creatables = $creatables; - $this->updatables = $updatables; - $this->deletables = $deletables; - $this->canRename = $canRename; - $this->lockingProvider = \OC::$server->getLockingProvider(); - } - - public function isUpdatable($path) { - return !empty($this->updatables[$path]); - } - - public function isCreatable($path) { - return !empty($this->creatables[$path]); - } - - public function isDeletable($path) { - return !empty($this->deletables[$path]); - } - - public function rename($path1, $path2) { - return $this->canRename; - } - - public function getRelativePath($path) { - return $path; - } - - public function getFileInfo($path, $includeMountPoints = true) { - $objectTreeTest = new ObjectTreeTest(); - return $objectTreeTest->getFileInfoMock( - $this->isCreatable($path), - $this->isUpdatable($path), - $this->isDeletable($path) - ); - } -} +use OC\Files\View; +use OCA\DAV\Connector\Sabre\Directory; +use OCA\DAV\Connector\Sabre\ObjectTree; /** * Class ObjectTreeTest @@ -81,103 +45,100 @@ class TestDoubleFileView extends \OC\Files\View { */ class ObjectTreeTest extends \Test\TestCase { - public function getFileInfoMock($create = true, $update = true, $delete = true) { - $mock = $this->getMockBuilder('\OCP\Files\FileInfo') - ->disableOriginalConstructor() - ->getMock(); - $mock - ->expects($this->any()) - ->method('isCreatable') - ->willReturn($create); - $mock - ->expects($this->any()) - ->method('isUpdateable') - ->willReturn($update); - $mock - ->expects($this->any()) - ->method('isDeletable') - ->willReturn($delete); - - return $mock; + public function copyDataProvider() { + return [ + // copy into same dir + ['a', 'b', ''], + // copy into same dir + ['a/a', 'a/b', 'a'], + // copy into another dir + ['a', 'sub/a', 'sub'], + ]; } /** - * @dataProvider moveFailedProvider - * @expectedException \Sabre\DAV\Exception\Forbidden + * @dataProvider copyDataProvider */ - public function testMoveFailed($source, $destination, $updatables, $deletables) { - $this->moveTest($source, $destination, $updatables, $updatables, $deletables, true); - } + public function testCopy($sourcePath, $targetPath, $targetParent) { + $view = $this->createMock(View::class); + $view->expects($this->once()) + ->method('verifyPath') + ->with($targetParent) + ->will($this->returnValue(true)); + $view->expects($this->once()) + ->method('file_exists') + ->with($targetPath) + ->willReturn(false); + $view->expects($this->once()) + ->method('copy') + ->with($sourcePath, $targetPath) + ->will($this->returnValue(true)); - /** - * @dataProvider moveSuccessProvider - */ - public function testMoveSuccess($source, $destination, $updatables, $deletables) { - $this->moveTest($source, $destination, $updatables, $updatables, $deletables); - $this->assertTrue(true); - } + $info = $this->createMock(FileInfo::class); + $info->expects($this->once()) + ->method('isCreatable') + ->willReturn(true); - /** - * @dataProvider moveFailedInvalidCharsProvider - * @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath - */ - public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) { - $this->moveTest($source, $destination, $updatables, $updatables, $deletables); - } + $view->expects($this->once()) + ->method('getFileInfo') + ->with($targetParent === '' ? '.' : $targetParent) + ->willReturn($info); - function moveFailedInvalidCharsProvider() { - return array( - array('a/b', 'a/*', array('a' => true, 'a/b' => true, 'a/c*' => false), array()), - ); - } + $rootDir = new Directory($view, $info); + $objectTree = $this->getMockBuilder(ObjectTree::class) + ->setMethods(['nodeExists', 'getNodeForPath']) + ->setConstructorArgs([$rootDir, $view]) + ->getMock(); - function moveFailedProvider() { - return array( - array('a/b', 'a/c', array('a' => false, 'a/b' => false, 'a/c' => false), array()), - array('a/b', 'b/b', array('a' => false, 'a/b' => false, 'b' => false, 'b/b' => false), array()), - array('a/b', 'b/b', array('a' => false, 'a/b' => true, 'b' => false, 'b/b' => false), array()), - array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => false, 'b/b' => false), array()), - array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => false)), - array('a/b', 'a/c', array('a' => false, 'a/b' => true, 'a/c' => false), array()), - ); - } + $objectTree->expects($this->once()) + ->method('getNodeForPath') + ->with($this->identicalTo($sourcePath)) + ->will($this->returnValue(false)); - function moveSuccessProvider() { - return array( - array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => true)), - // older files with special chars can still be renamed to valid names - array('a/b*', 'b/b', array('a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false), array('a/b*' => true)), - ); + /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */ + $mountManager = Filesystem::getMountManager(); + $objectTree->init($rootDir, $view, $mountManager); + $objectTree->copy($sourcePath, $targetPath); } /** - * @param $source - * @param $destination - * @param $creatables - * @param $updatables - * @param $deletables - * @param $throwsBeforeGetNode + * @dataProvider copyDataProvider + * @expectedException \Sabre\DAV\Exception\Forbidden */ - private function moveTest($source, $destination, $creatables, $updatables, $deletables, $throwsBeforeGetNode = false) { - $view = new TestDoubleFileView($creatables, $updatables, $deletables); + public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) { + $view = $this->createMock(View::class); + $view->expects($this->never()) + ->method('verifyPath'); + $view->expects($this->once()) + ->method('file_exists') + ->with($targetPath) + ->willReturn(false); + $view->expects($this->never()) + ->method('copy'); + + $info = $this->createMock(FileInfo::class); + $info->expects($this->once()) + ->method('isCreatable') + ->willReturn(false); - $info = new FileInfo('', null, null, array(), null); + $view->expects($this->once()) + ->method('getFileInfo') + ->with($targetParent === '' ? '.' : $targetParent) + ->willReturn($info); - $rootDir = new \OCA\DAV\Connector\Sabre\Directory($view, $info); - $objectTree = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\ObjectTree') + $rootDir = new Directory($view, $info); + $objectTree = $this->getMockBuilder(ObjectTree::class) ->setMethods(['nodeExists', 'getNodeForPath']) ->setConstructorArgs([$rootDir, $view]) ->getMock(); - $objectTree->expects($throwsBeforeGetNode ? $this->never() : $this->once()) - ->method('getNodeForPath') - ->with($this->identicalTo($source)) - ->will($this->returnValue(false)); + $objectTree->expects($this->never()) + ->method('getNodeForPath'); /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */ - $mountManager = \OC\Files\Filesystem::getMountManager(); + $mountManager = Filesystem::getMountManager(); $objectTree->init($rootDir, $view, $mountManager); - $objectTree->move($source, $destination); + $objectTree->copy($sourcePath, $targetPath); } /** @@ -361,46 +322,4 @@ class ObjectTreeTest extends \Test\TestCase { $this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path)); } - - /** - * @expectedException \Sabre\DAV\Exception\Forbidden - * @expectedExceptionMessage Could not copy directory nameOfSourceNode, target exists - */ - public function testFailingMove() { - $source = 'a/b'; - $destination = 'b/b'; - $updatables = array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false); - $deletables = array('a/b' => true); - - $view = new TestDoubleFileView($updatables, $updatables, $deletables); - - $info = new FileInfo('', null, null, array(), null); - - $rootDir = new \OCA\DAV\Connector\Sabre\Directory($view, $info); - $objectTree = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\ObjectTree') - ->setMethods(['nodeExists', 'getNodeForPath']) - ->setConstructorArgs([$rootDir, $view]) - ->getMock(); - - $sourceNode = $this->getMockBuilder('\Sabre\DAV\ICollection') - ->disableOriginalConstructor() - ->getMock(); - $sourceNode->expects($this->once()) - ->method('getName') - ->will($this->returnValue('nameOfSourceNode')); - - $objectTree->expects($this->once()) - ->method('nodeExists') - ->with($this->identicalTo($destination)) - ->will($this->returnValue(true)); - $objectTree->expects($this->once()) - ->method('getNodeForPath') - ->with($this->identicalTo($source)) - ->will($this->returnValue($sourceNode)); - - /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */ - $mountManager = \OC\Files\Filesystem::getMountManager(); - $objectTree->init($rootDir, $view, $mountManager); - $objectTree->move($source, $destination); - } } diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/DeleteTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/DeleteTest.php index 7468e981020..35fd83f1fe6 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/DeleteTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/DeleteTest.php @@ -31,7 +31,7 @@ use OCP\AppFramework\Http; * * @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest */ -class DeleteTest extends RequestTest { +class DeleteTest extends RequestTestCase { public function testBasicUpload() { $user = $this->getUniqueID(); $view = $this->setupUser($user, 'pass'); diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/DownloadTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/DownloadTest.php index 8aac99e8c54..2cb08420f8d 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/DownloadTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/DownloadTest.php @@ -34,7 +34,7 @@ use OCP\Lock\ILockingProvider; * * @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest */ -class DownloadTest extends RequestTest { +class DownloadTest extends RequestTestCase { public function testDownload() { $user = $this->getUniqueID(); $view = $this->setupUser($user, 'pass'); diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTestCase.php index 63bd3cf19cc..50e228b7e84 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTestCase.php @@ -34,7 +34,7 @@ use Test\TestCase; use Test\Traits\MountProviderTrait; use Test\Traits\UserTrait; -abstract class RequestTest extends TestCase { +abstract class RequestTestCase extends TestCase { use UserTrait; use MountProviderTrait; diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php index 1db85b1bcaf..5376241c61d 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php @@ -35,7 +35,7 @@ use OCP\Lock\ILockingProvider; * * @package OCA\DAV\Tests\unit\Connector\Sabre\RequestTest */ -class UploadTest extends RequestTest { +class UploadTest extends RequestTestCase { public function testBasicUpload() { $user = $this->getUniqueID(); $view = $this->setupUser($user, 'pass'); |