From dce180e379974c6d739e86b64390f2545f7e6242 Mon Sep 17 00:00:00 2001 From: Hamza Date: Fri, 17 Apr 2026 15:29:05 +0200 Subject: [PATCH] test(integration): add tests for calendar delegation Signed-off-by: Hamza --- .../features/bootstrap/CalDavContext.php | 133 ++++++++++++++++++ .../features/caldav-delegation.feature | 21 +++ 2 files changed, 154 insertions(+) create mode 100644 build/integration/features/caldav-delegation.feature diff --git a/build/integration/features/bootstrap/CalDavContext.php b/build/integration/features/bootstrap/CalDavContext.php index 936463b579e..54355a38776 100644 --- a/build/integration/features/bootstrap/CalDavContext.php +++ b/build/integration/features/bootstrap/CalDavContext.php @@ -105,6 +105,139 @@ class CalDavContext implements \Behat\Behat\Context\Context { } } + /** + * @When :user requests principal :principal on the endpoint :endpoint + */ + public function requestsPrincipal(string $user, string $principal, string $endpoint): void { + $davUrl = $this->baseUrl . $endpoint . $principal; + + $password = ($user === 'admin') ? 'admin' : '123456'; + try { + $this->response = $this->client->request( + 'PROPFIND', + $davUrl, + [ + 'headers' => [ + 'Content-Type' => 'application/xml; charset=UTF-8', + 'Depth' => 0, + ], + 'body' => '', + 'auth' => [ + $user, + $password, + ], + ] + ); + } catch (\GuzzleHttp\Exception\ClientException $e) { + $this->response = $e->getResponse(); + } + } + + /** + * @Then The CalDAV response should contain a property :key + * @throws \Exception + */ + public function theCaldavResponseShouldContainAProperty(string $key): void { + /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */ + $multiStatus = $this->responseXml['value']; + $responses = $multiStatus->getResponses()[0]->getResponseProperties(); + if (!isset($responses[200])) { + throw new \Exception( + sprintf( + 'Expected code 200 got [%s]', + implode(',', array_keys($responses)), + ) + ); + } + + $props = $responses[200]; + if (!array_key_exists($key, $props)) { + throw new \Exception( + sprintf( + 'Expected property %s in %s', + $key, + json_encode($props, JSON_PRETTY_PRINT), + ) + ); + } + } + + /** + * @Then The CalDAV response should contain a property :key with a href value :value + * @throws \Exception + */ + public function theCaldavResponseShouldContainAPropertyWithHrefValue( + string $key, + string $value, + ): void { + /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */ + $multiStatus = $this->responseXml['value']; + $responses = $multiStatus->getResponses()[0]->getResponseProperties(); + if (!isset($responses[200])) { + throw new \Exception( + sprintf( + 'Expected code 200 got [%s]', + implode(',', array_keys($responses)), + ) + ); + } + + $props = $responses[200]; + if (!array_key_exists($key, $props)) { + throw new \Exception("Cannot find property \"$key\""); + } + + $actualValue = $props[$key]->getHref(); + if ($actualValue !== $value) { + throw new \Exception("Property \"$key\" found with value \"$actualValue\", expected \"$value\""); + } + } + + /** + * @Then The CalDAV response should contain an href :href + * @throws \Exception + */ + public function theCaldavResponseShouldContainAnHref(string $href): void { + /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */ + $multiStatus = $this->responseXml['value']; + foreach ($multiStatus->getResponses() as $response) { + if ($response->getHref() === $href) { + return; + } + } + throw new \Exception( + sprintf( + 'Expected href %s not found in response', + $href, + ) + ); + } + + /** + * @Then The CalDAV response should be multi status + * @throws \Exception + */ + public function theCaldavResponseShouldBeMultiStatus(): void { + if ($this->response->getStatusCode() !== 207) { + throw new \Exception( + sprintf( + 'Expected code 207 got %s', + $this->response->getStatusCode() + ) + ); + } + + $body = $this->response->getBody()->getContents(); + if ($body && substr($body, 0, 1) === '<') { + $reader = new Sabre\Xml\Reader(); + $reader->xml($body); + $reader->elementMap['{DAV:}multistatus'] = \Sabre\DAV\Xml\Response\MultiStatus::class; + $reader->elementMap['{DAV:}response'] = \Sabre\DAV\Xml\Element\Response::class; + $reader->elementMap['{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL'] = \Sabre\DAV\Xml\Property\Href::class; + $this->responseXml = $reader->parse(); + } + } + /** * @Then The CalDAV HTTP status code should be :code * @param int $code diff --git a/build/integration/features/caldav-delegation.feature b/build/integration/features/caldav-delegation.feature new file mode 100644 index 00000000000..ce52bca6ab8 --- /dev/null +++ b/build/integration/features/caldav-delegation.feature @@ -0,0 +1,21 @@ +# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors +# SPDX-License-Identifier: AGPL-3.0-or-later +Feature: calendar delegation + Calendar delegation grants another user/principal control of a calendar account, + including all calendars the delegator can access. + + Scenario: admin grants user0 read access to her calendar account + Given user "admin" exists + And user "user0" exists + When "admin" updates property "{DAV:}group-member-set" to href "/remote.php/dav/principals/users/user0" of principal "users/admin/calendar-proxy-read" on the endpoint "/remote.php/dav/principals/" + Then The CalDAV response should be multi status + And The CalDAV response should contain an href "/remote.php/dav/principals/users/admin/calendar-proxy-read" + And The CalDAV response should contain a property "{DAV:}group-member-set" + + Scenario: admin grants write access to her calendar account + Given user "admin" exists + And user "user0" exists + When "admin" updates property "{DAV:}group-member-set" to href "/remote.php/dav/principals/users/user0" of principal "users/admin/calendar-proxy-write" on the endpoint "/remote.php/dav/principals/" + Then The CalDAV response should be multi status + And The CalDAV response should contain an href "/remote.php/dav/principals/users/admin/calendar-proxy-write" + And The CalDAV response should contain a property "{DAV:}group-member-set" \ No newline at end of file