function testExpand() { $request = HTTP\Sapi::createFromServerArray(['REQUEST_METHOD' => 'REPORT', 'HTTP_CONTENT_TYPE' => 'application/xml', 'REQUEST_URI' => '/calendars/user1/calendar1', 'HTTP_DEPTH' => '1']); $request->setBody('<?xml version="1.0" encoding="utf-8" ?> <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> <D:prop> <C:calendar-data> <C:expand start="20120205T230000Z" end="20120212T225959Z"/> </C:calendar-data> <D:getetag/> </D:prop> <C:filter> <C:comp-filter name="VCALENDAR"> <C:comp-filter name="VEVENT"> <C:time-range start="20120205T230000Z" end="20120212T225959Z"/> </C:comp-filter> </C:comp-filter> </C:filter> </C:calendar-query>'); $response = $this->request($request); // Everts super awesome xml parser. $body = substr($response->body, $start = strpos($response->body, 'BEGIN:VCALENDAR'), strpos($response->body, 'END:VCALENDAR') - $start + 13); $body = str_replace(' ', '', $body); $vObject = VObject\Reader::read($body); // We only expect 3 events $this->assertEquals(3, count($vObject->VEVENT), 'We got 6 events instead of 3. Output: ' . $body); // TZID should be gone $this->assertFalse(isset($vObject->VEVENT->DTSTART['TZID'])); }
function testCheckSuccess() { $request = HTTP\Sapi::createFromServerArray(['PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'password']); $response = new HTTP\Response(); $backend = new AbstractBasicMock(); $this->assertEquals([true, 'principals/username'], $backend->check($request, $response)); }
function testCorrect() { $xml = '<?xml version="1.0"?> <d:principal-search-property-set xmlns:d="DAV:"/>'; $serverVars = array('REQUEST_METHOD' => 'REPORT', 'HTTP_DEPTH' => '0', 'REQUEST_URI' => '/principals'); $request = HTTP\Sapi::createFromServerArray($serverVars); $request->setBody($xml); $server = $this->getServer(); $server->httpRequest = $request; $server->exec(); $this->assertEquals(200, $server->httpResponse->status, $server->httpResponse->body); $this->assertEquals(array('X-Sabre-Version' => [DAV\Version::VERSION], 'Content-Type' => ['application/xml; charset=utf-8']), $server->httpResponse->getHeaders()); $check = array('/d:principal-search-property-set', '/d:principal-search-property-set/d:principal-search-property' => 2, '/d:principal-search-property-set/d:principal-search-property/d:prop' => 2, '/d:principal-search-property-set/d:principal-search-property/d:prop/d:displayname' => 1, '/d:principal-search-property-set/d:principal-search-property/d:prop/s:email-address' => 1, '/d:principal-search-property-set/d:principal-search-property/d:description' => 2); $xml = simplexml_load_string($server->httpResponse->body); $xml->registerXPathNamespace('d', 'DAV:'); $xml->registerXPathNamespace('s', 'http://sabredav.org/ns'); foreach ($check as $v1 => $v2) { $xpath = is_int($v1) ? $v2 : $v1; $result = $xml->xpath($xpath); $count = 1; if (!is_int($v1)) { $count = $v2; } $this->assertEquals($count, count($result), 'we expected ' . $count . ' appearances of ' . $xpath . ' . We found ' . count($result) . '. Full response body: ' . $server->httpResponse->body); } }
function testRedirectRemoteUser() { $request = HTTP\Sapi::createFromServerArray(['REDIRECT_REMOTE_USER' => 'username']); $response = new HTTP\Response(); $backend = new Apache(); $this->assertEquals([true, 'principals/username'], $backend->check($request, $response)); }
function testPatchNoRange() { $this->node->put('00000000'); $request = HTTP\Sapi::createFromServerArray(['REQUEST_METHOD' => 'PATCH', 'REQUEST_URI' => '/partial']); $response = $this->request($request); $this->assertEquals(400, $response->status, 'Full response body:' . $response->body); }
function testUnsupportedMethod() { $req = HTTP\Sapi::createFromServerArray(['REQUEST_METHOD' => 'POST', 'REQUEST_URI' => '/calendars/user1/outbox', 'HTTP_ORIGINATOR' => 'mailto:user1.sabredav@sabredav.org', 'HTTP_RECIPIENT' => 'mailto:user2@example.org', 'HTTP_CONTENT_TYPE' => 'text/calendar']); $body = ['BEGIN:VCALENDAR', 'METHOD:PUBLISH', 'BEGIN:VEVENT', 'END:VEVENT', 'END:VCALENDAR']; $req->setBody(implode("\r\n", $body)); $this->assertHTTPStatus(501, $req); }
function testIssue205() { $request = HTTP\Sapi::createFromServerArray(['REQUEST_METHOD' => 'REPORT', 'HTTP_CONTENT_TYPE' => 'application/xml', 'REQUEST_URI' => '/calendars/user1/calendar1', 'HTTP_DEPTH' => '1']); $request->setBody('<?xml version="1.0" encoding="utf-8" ?> <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> <D:prop> <C:calendar-data> <C:expand start="20120325T220000Z" end="20120401T215959Z"/> </C:calendar-data> <D:getetag/> </D:prop> <C:filter> <C:comp-filter name="VCALENDAR"> <C:comp-filter name="VEVENT"> <C:comp-filter name="VALARM"> <C:time-range start="20120325T220000Z" end="20120401T215959Z"/> </C:comp-filter> </C:comp-filter> </C:comp-filter> </C:filter> </C:calendar-query>'); $response = $this->request($request); $this->assertFalse(strpos($response->body, '<s:exception>Exception</s:exception>'), 'Exception occurred: ' . $response->body); $this->assertFalse(strpos($response->body, 'Unknown or bad format'), 'DateTime unknown format Exception: ' . $response->body); // Everts super awesome xml parser. $body = substr($response->body, $start = strpos($response->body, 'BEGIN:VCALENDAR'), strpos($response->body, 'END:VCALENDAR') - $start + 13); $body = str_replace(' ', '', $body); $vObject = VObject\Reader::read($body); $this->assertEquals(1, count($vObject->VEVENT)); }
function testCheckSuccess() { $request = HTTP\Sapi::createFromServerArray(array('HTTP_AUTHORIZATION' => 'Bearer valid')); $response = new HTTP\Response(); $backend = new AbstractBearerMock(); $this->assertEquals([true, 'principals/username'], $backend->check($request, $response)); }
function setUp() { $calendars = array(array('principaluri' => 'principals/user2', 'id' => 1, 'uri' => 'calendar1', '{' . CalDAV\Plugin::NS_CALDAV . '}calendar-timezone' => "BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nEND:VTIMEZONE\r\nEND:VCALENDAR"), array('principaluri' => 'principals/user2', 'id' => 2, 'uri' => 'calendar2', '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp(ScheduleCalendarTransp::TRANSPARENT))); $calendarobjects = array(1 => array('1.ics' => array('uri' => '1.ics', 'calendardata' => 'BEGIN:VCALENDAR BEGIN:VEVENT DTSTART:20110101T130000 DURATION:PT1H END:VEVENT END:VCALENDAR', 'calendarid' => 1)), 2 => array('2.ics' => array('uri' => '2.ics', 'calendardata' => 'BEGIN:VCALENDAR BEGIN:VEVENT DTSTART:20110101T080000 DURATION:PT1H END:VEVENT END:VCALENDAR', 'calendarid' => 2))); $principalBackend = new DAVACL\PrincipalBackend\Mock(); $this->caldavBackend = new CalDAV\Backend\MockScheduling($calendars, $calendarobjects); $tree = array(new DAVACL\PrincipalCollection($principalBackend), new CalDAV\CalendarRoot($principalBackend, $this->caldavBackend)); $this->request = HTTP\Sapi::createFromServerArray(['CONTENT_TYPE' => 'text/calendar']); $this->response = new HTTP\ResponseMock(); $this->server = new DAV\Server($tree); $this->server->httpRequest = $this->request; $this->server->httpResponse = $this->response; $this->aclPlugin = new DAVACL\Plugin(); $this->server->addPlugin($this->aclPlugin); $authBackend = new DAV\Auth\Backend\Mock(); $authBackend->setCurrentUser('user1'); $this->authPlugin = new DAV\Auth\Plugin($authBackend, 'SabreDAV'); $this->server->addPlugin($this->authPlugin); // CalDAV plugin $this->plugin = new CalDAV\Plugin(); $this->server->addPlugin($this->plugin); // Scheduling plugin $this->plugin = new Plugin(); $this->server->addPlugin($this->plugin); }
function testException() { $this->server->on('exception', [$this, 'exceptionHandler']); $req = HTTP\Sapi::createFromServerArray(['REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/not/exisitng']); $this->server->httpRequest = $req; $this->server->exec(); $this->assertInstanceOf('Sabre\\DAV\\Exception\\NotFound', $this->exception); }
function testComplexIf() { $serverVars = array('HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1> ["etag1"]) ' . '(Not <opaquelocktoken:token2>) (["etag2"]) <http://www.example.org/node2> ' . '(<opaquelocktoken:token3>) (Not <opaquelocktoken:token4>) (["etag3"])'); $request = HTTP\Sapi::createFromServerArray($serverVars); $conditions = $this->server->getIfConditions($request); $compare = array(array('uri' => 'node1', 'tokens' => array(array('negate' => false, 'token' => 'opaquelocktoken:token1', 'etag' => '"etag1"'), array('negate' => true, 'token' => 'opaquelocktoken:token2', 'etag' => ''), array('negate' => false, 'token' => '', 'etag' => '"etag2"'))), array('uri' => 'node2', 'tokens' => array(array('negate' => false, 'token' => 'opaquelocktoken:token3', 'etag' => ''), array('negate' => true, 'token' => 'opaquelocktoken:token4', 'etag' => ''), array('negate' => false, 'token' => '', 'etag' => '"etag3"')))); $this->assertEquals($compare, $conditions); }
function sendPROPFIND($body) { $serverVars = ['REQUEST_URI' => '/', 'REQUEST_METHOD' => 'PROPFIND', 'HTTP_DEPTH' => '0']; $request = HTTP\Sapi::createFromServerArray($serverVars); $request->setBody($body); $this->server->httpRequest = $request; $this->server->exec(); }
public function testPatchSuccess() { $this->node->put('00000000'); $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'PATCH', 'REQUEST_URI' => '/partial', 'HTTP_X_UPDATE_RANGE' => 'bytes=3-5', 'HTTP_CONTENT_TYPE' => 'application/x-sabredav-partialupdate', 'HTTP_CONTENT_LENGTH' => 3)); $request->setBody('111'); $response = $this->request($request); $this->assertEquals(204, $response->status, 'Full response body:' . $response->body); $this->assertEquals('00011100', $this->node->get()); }
function testCheck() { $digestHash = md5('HELLO:12345:1:1:auth:' . md5('GET:/')); $header = 'username=user, realm=myRealm, nonce=12345, uri=/, response=' . $digestHash . ', opaque=1, qop=auth, nc=1, cnonce=1'; $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'GET', 'PHP_AUTH_DIGEST' => $header, 'REQUEST_URI' => '/')); $response = new HTTP\Response(); $backend = new AbstractDigestMock(); $this->assertEquals([true, 'principals/user'], $backend->check($request, $response)); }
function testCollectionGet() { $serverVars = array('REQUEST_URI' => '/', 'REQUEST_METHOD' => 'GET'); $request = HTTP\Sapi::createFromServerArray($serverVars); $request->setBody(''); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(207, $this->response->status, 'Incorrect status response received. Full response body: ' . $this->response->body); $this->assertEquals(array('X-Sabre-Version' => [DAV\Version::VERSION], 'Content-Type' => ['application/xml; charset=utf-8'], 'DAV' => ['1, 3, extended-mkcol'], 'Vary' => ['Brief,Prefer']), $this->response->getHeaders()); }
function testRedirectRemoteUser() { $backend = new Apache(); $server = new DAV\Server(); $request = HTTP\Sapi::createFromServerArray(['REDIRECT_REMOTE_USER' => 'username']); $server->httpRequest = $request; $this->assertTrue($backend->authenticate($server, 'Realm')); $userInfo = 'username'; $this->assertEquals($userInfo, $backend->getCurrentUser()); }
function testExport() { $request = HTTP\Sapi::createFromServerArray(array('REQUEST_URI' => '/addressbooks/user1/book1?export', 'QUERY_STRING' => 'export', 'REQUEST_METHOD' => 'GET')); $response = $this->request($request); $this->assertEquals(200, $response->status, $response->body); $expected = "BEGIN:VCARD\nFN:Person1\nEND:VCARD\nBEGIN:VCARD\nFN:Person2\nEND:VCARD\nBEGIN:VCARD\nFN:Person3\nEND:VCARD\nBEGIN:VCARD\nFN:Person4\nEND:VCARD\n"; // We actually expected windows line endings $expected = str_replace("\n", "\r\n", $expected); $this->assertEquals($expected, $response->body); }
function testOptions() { $serverVars = array('REQUEST_URI' => '/', 'REQUEST_METHOD' => 'OPTIONS'); $request = HTTP\Sapi::createFromServerArray($serverVars); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(array('DAV' => '1, 3, extended-mkcol, drinking', 'MS-Author-Via' => 'DAV', 'Allow' => 'OPTIONS, GET, HEAD, DELETE, PROPFIND, PUT, PROPPATCH, COPY, MOVE, REPORT, BEER, WINE', 'Accept-Ranges' => 'bytes', 'Content-Length' => '0', 'X-Sabre-Version' => Version::VERSION), $this->response->getHeaders()); $this->assertEquals(200, $this->response->status); $this->assertEquals('', $this->response->body); $this->assertEquals('OPTIONS', $this->testPlugin->beforeMethod); }
function testUpdateFileParsableBody() { $this->cardBackend->createCard('addressbook1', 'blabla.vcf', 'foo'); $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'PUT', 'REQUEST_URI' => '/addressbooks/admin/addressbook1/blabla.vcf')); $body = "BEGIN:VCARD\r\nUID:foo\r\nEND:VCARD\r\n"; $request->setBody($body); $response = $this->request($request); $this->assertEquals(204, $response->status); $expected = array('uri' => 'blabla.vcf', 'carddata' => $body); $this->assertEquals($expected, $this->cardBackend->getCard('addressbook1', 'blabla.vcf')); }
/** * @depends testNoAdminAccess */ function testAdminAccess() { $plugin = new Plugin(); $plugin->adminPrincipals = array('principals/admin'); $this->server->addPlugin($plugin); $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'OPTIONS', 'HTTP_DEPTH' => 1, 'REQUEST_URI' => '/adminonly')); $response = new HTTP\ResponseMock(); $this->server->httpRequest = $request; $this->server->httpResponse = $response; $this->server->exec(); $this->assertEquals(200, $response->status); }
function testPostMkCol() { $serverVars = array('REQUEST_URI' => '/', 'REQUEST_METHOD' => 'POST', 'CONTENT_TYPE' => 'application/x-www-form-urlencoded'); $postVars = array('sabreAction' => 'mkcol', 'name' => 'new_collection'); $request = HTTP\Sapi::createFromServerArray($serverVars); $request->setPostData($postVars); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(302, $this->response->status); $this->assertEquals(array('X-Sabre-Version' => DAV\Version::VERSION, 'Location' => '/'), $this->response->getHeaders()); $this->assertTrue(is_dir(SABRE_TEMPDIR . '/new_collection')); }
function testCallBack() { $args = []; $callBack = function ($user, $pass) use(&$args) { $args = [$user, $pass]; return true; }; $backend = new BasicCallBack($callBack); $request = Sapi::createFromServerArray(['HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar')]); $response = new Response(); $this->assertEquals([true, 'principals/foo'], $backend->check($request, $response)); $this->assertEquals(['foo', 'bar'], $args); }
/** * @expectedException Sabre\DAV\Exception\NotImplemented */ function testFreeBusyReportWrongNode() { $request = HTTP\Sapi::createFromServerArray(array('REQUEST_URI' => '/')); $this->server->httpRequest = $request; $reportXML = <<<XML <?xml version="1.0"?> <c:free-busy-query xmlns:c="urn:ietf:params:xml:ns:caldav"> <c:time-range start="20111001T000000Z" end="20111101T000000Z" /> </c:free-busy-query> XML; $dom = DAV\XMLUtil::loadDOMDocument($reportXML); $this->plugin->report('{urn:ietf:params:xml:ns:caldav}free-busy-query', $dom); }
function testReportPassThrough() { $fakeServer = new DAV\Server(new DAV\SimpleCollection('bla')); $plugin = new Plugin(new Backend\Mock(), 'realm'); $fakeServer->addPlugin($plugin); $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'REPORT', 'HTTP_CONTENT_TYPE' => 'application/xml', 'REQUEST_URI' => '/')); $request->setBody('<?xml version="1.0"?><s:somereport xmlns:s="http://www.rooftopsolutions.nl/NS/example" />'); $fakeServer->httpRequest = $request; $fakeServer->sapi = new HTTP\SapiMock(); $fakeServer->httpResponse = new HTTP\ResponseMock(); $fakeServer->exec(); $this->assertEquals(415, $fakeServer->httpResponse->status); }
public function testAuthenticate() { $response = new HTTP\ResponseMock(); $tree = new DAV\Tree(new DAV\SimpleCollection('bla')); $server = new DAV\Server($tree); $server->httpResponse = $response; $request = HTTP\Sapi::createFromServerArray(array('PHP_AUTH_USER' => 'username', 'PHP_AUTH_PW' => 'password')); $server->httpRequest = $request; $backend = new AbstractBasicMock(); $this->assertTrue($backend->authenticate($server, 'myRealm')); $result = $backend->getCurrentUser(); $this->assertEquals('username', $result); }
function testMountResponse() { $serverVars = array('REQUEST_URI' => '/?mount', 'REQUEST_METHOD' => 'GET', 'QUERY_STRING' => 'mount', 'HTTP_HOST' => 'example.org'); $request = HTTP\Sapi::createFromServerArray($serverVars); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(200, $this->response->status); $xml = simplexml_load_string($this->response->body); $this->assertInstanceOf('SimpleXMLElement', $xml, 'Response was not a valid xml document. The list of errors:' . print_r(libxml_get_errors(), true) . '. xml body: ' . $this->response->body . '. What type we got: ' . gettype($xml) . ' class, if object: ' . get_class($xml)); $xml->registerXPathNamespace('dm', 'http://purl.org/NET/webdav/mount'); $url = $xml->xpath('//dm:url'); $this->assertEquals('http://example.org/', (string) $url[0]); }
function testCallBack() { $args = array(); $callBack = function ($user, $pass) use(&$args) { $args = [$user, $pass]; return true; }; $backend = new BasicCallBack($callBack); $server = new Server(); $server->httpRequest = Sapi::createFromServerArray(['HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar')]); $this->assertTrue($backend->authenticate($server, 'Realm')); $this->assertEquals(['foo', 'bar'], $args); }
public function testAuthenticate() { $response = new HTTP\ResponseMock(); $server = new DAV\Server(); $server->httpResponse = $response; $digestHash = md5('HELLO:12345:1:1:auth:' . md5('GET:/')); $header = 'username=user, realm=myRealm, nonce=12345, uri=/, response=' . $digestHash . ', opaque=1, qop=auth, nc=1, cnonce=1'; $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'GET', 'PHP_AUTH_DIGEST' => $header, 'REQUEST_URI' => '/')); $server->httpRequest = $request; $backend = new AbstractDigestMock(); $this->assertTrue($backend->authenticate($server, 'myRealm')); $result = $backend->getCurrentUser(); $this->assertEquals('user', $result); $this->assertEquals('HELLO', $backend->getDigestHash('myRealm', $result)); }
function testIssue203() { $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'REPORT', 'HTTP_CONTENT_TYPE' => 'application/xml', 'REQUEST_URI' => '/calendars/user1/calendar1', 'HTTP_DEPTH' => '1')); $request->setBody('<?xml version="1.0" encoding="utf-8" ?> <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> <D:prop> <C:calendar-data> <C:expand start="20120325T220000Z" end="20120401T215959Z"/> </C:calendar-data> <D:getetag/> </D:prop> <C:filter> <C:comp-filter name="VCALENDAR"> <C:comp-filter name="VEVENT"> <C:time-range start="20120325T220000Z" end="20120401T215959Z"/> </C:comp-filter> </C:comp-filter> </C:filter> </C:calendar-query>'); $response = $this->request($request); // Everts super awesome xml parser. $body = substr($response->body, $start = strpos($response->body, 'BEGIN:VCALENDAR'), strpos($response->body, 'END:VCALENDAR') - $start + 13); $body = str_replace(' ', '', $body); $vObject = VObject\Reader::read($body); $this->assertEquals(2, count($vObject->VEVENT)); $expectedEvents = array(array('DTSTART' => '20120326T135200Z', 'DTEND' => '20120326T145200Z', 'SUMMARY' => 'original summary'), array('DTSTART' => '20120328T135200Z', 'DTEND' => '20120328T145200Z', 'SUMMARY' => 'overwritten summary', 'RECURRENCE-ID' => '20120327T135200Z')); // try to match agains $expectedEvents array foreach ($expectedEvents as $expectedEvent) { $matching = false; foreach ($vObject->VEVENT as $vevent) { /** @var $vevent Sabre\VObject\Component\VEvent */ foreach ($vevent->children as $child) { /** @var $child Sabre\VObject\Property */ if (isset($expectedEvent[$child->name])) { if ($expectedEvent[$child->name] != $child->getValue()) { continue 2; } } } $matching = true; break; } $this->assertTrue($matching, 'Did not find the following event in the response: ' . var_export($expectedEvent, true)); } }
/** * @depends testNoAdminAccess */ function testAdminAccess() { $principalBackend = new PrincipalBackend\Mock(); $tree = array(new MockACLNode('adminonly', array()), new PrincipalCollection($principalBackend)); $fakeServer = new DAV\Server($tree); $fakeServer->sapi = new HTTP\SapiMock(); $plugin = new DAV\Auth\Plugin(new DAV\Auth\Backend\Mock(), 'realm'); $fakeServer->addPlugin($plugin); $plugin = new Plugin(); $plugin->adminPrincipals = array('principals/admin'); $fakeServer->addPlugin($plugin); $request = HTTP\Sapi::createFromServerArray(array('REQUEST_METHOD' => 'OPTIONS', 'HTTP_DEPTH' => 1, 'REQUEST_URI' => '/adminonly')); $response = new HTTP\ResponseMock(); $fakeServer->httpRequest = $request; $fakeServer->httpResponse = $response; $fakeServer->exec(); $this->assertEquals(200, $response->status); }