1
0

CalDavContext.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. require __DIR__ . '/../../vendor/autoload.php';
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use Psr\Http\Message\ResponseInterface;
  11. class CalDavContext implements \Behat\Behat\Context\Context {
  12. /** @var string */
  13. private $baseUrl;
  14. /** @var Client */
  15. private $client;
  16. /** @var ResponseInterface */
  17. private $response;
  18. /** @var string */
  19. private $responseXml = '';
  20. /**
  21. * @param string $baseUrl
  22. */
  23. public function __construct($baseUrl) {
  24. $this->baseUrl = $baseUrl;
  25. // in case of ci deployment we take the server url from the environment
  26. $testServerUrl = getenv('TEST_SERVER_URL');
  27. if ($testServerUrl !== false) {
  28. $this->baseUrl = substr($testServerUrl, 0, -5);
  29. }
  30. }
  31. /** @BeforeScenario */
  32. public function setUpScenario() {
  33. $this->client = new Client();
  34. $this->responseXml = '';
  35. }
  36. /** @AfterScenario */
  37. public function afterScenario() {
  38. $davUrl = $this->baseUrl. '/remote.php/dav/calendars/admin/MyCalendar';
  39. try {
  40. $this->client->delete(
  41. $davUrl,
  42. [
  43. 'auth' => [
  44. 'admin',
  45. 'admin',
  46. ],
  47. 'headers' => [
  48. 'X-NC-CalDAV-No-Trashbin' => '1',
  49. ]
  50. ]
  51. );
  52. } catch (\GuzzleHttp\Exception\ClientException $e) {
  53. }
  54. }
  55. /**
  56. * @When :user requests calendar :calendar on the endpoint :endpoint
  57. * @param string $user
  58. * @param string $calendar
  59. * @param string $endpoint
  60. */
  61. public function requestsCalendar($user, $calendar, $endpoint) {
  62. $davUrl = $this->baseUrl . $endpoint . $calendar;
  63. $password = ($user === 'admin') ? 'admin' : '123456';
  64. try {
  65. $this->response = $this->client->request(
  66. 'PROPFIND',
  67. $davUrl,
  68. [
  69. 'auth' => [
  70. $user,
  71. $password,
  72. ],
  73. ]
  74. );
  75. } catch (\GuzzleHttp\Exception\ClientException $e) {
  76. $this->response = $e->getResponse();
  77. }
  78. }
  79. /**
  80. * @When :user requests principal :principal on the endpoint :endpoint
  81. */
  82. public function requestsPrincipal(string $user, string $principal, string $endpoint): void {
  83. $davUrl = $this->baseUrl . $endpoint . $principal;
  84. $password = ($user === 'admin') ? 'admin' : '123456';
  85. try {
  86. $this->response = $this->client->request(
  87. 'PROPFIND',
  88. $davUrl,
  89. [
  90. 'headers' => [
  91. 'Content-Type' => 'application/xml; charset=UTF-8',
  92. 'Depth' => 0,
  93. ],
  94. 'body' => '<x0:propfind xmlns:x0="DAV:"><x0:prop><x0:displayname/><x1:calendar-user-type xmlns:x1="urn:ietf:params:xml:ns:caldav"/><x1:calendar-user-address-set xmlns:x1="urn:ietf:params:xml:ns:caldav"/><x0:principal-URL/><x0:alternate-URI-set/><x2:email-address xmlns:x2="http://sabredav.org/ns"/><x3:language xmlns:x3="http://nextcloud.com/ns"/><x1:calendar-home-set xmlns:x1="urn:ietf:params:xml:ns:caldav"/><x1:schedule-inbox-URL xmlns:x1="urn:ietf:params:xml:ns:caldav"/><x1:schedule-outbox-URL xmlns:x1="urn:ietf:params:xml:ns:caldav"/><x1:schedule-default-calendar-URL xmlns:x1="urn:ietf:params:xml:ns:caldav"/><x3:resource-type xmlns:x3="http://nextcloud.com/ns"/><x3:resource-vehicle-type xmlns:x3="http://nextcloud.com/ns"/><x3:resource-vehicle-make xmlns:x3="http://nextcloud.com/ns"/><x3:resource-vehicle-model xmlns:x3="http://nextcloud.com/ns"/><x3:resource-vehicle-is-electric xmlns:x3="http://nextcloud.com/ns"/><x3:resource-vehicle-range xmlns:x3="http://nextcloud.com/ns"/><x3:resource-vehicle-seating-capacity xmlns:x3="http://nextcloud.com/ns"/><x3:resource-contact-person xmlns:x3="http://nextcloud.com/ns"/><x3:resource-contact-person-vcard xmlns:x3="http://nextcloud.com/ns"/><x3:room-type xmlns:x3="http://nextcloud.com/ns"/><x3:room-seating-capacity xmlns:x3="http://nextcloud.com/ns"/><x3:room-building-address xmlns:x3="http://nextcloud.com/ns"/><x3:room-building-story xmlns:x3="http://nextcloud.com/ns"/><x3:room-building-room-number xmlns:x3="http://nextcloud.com/ns"/><x3:room-features xmlns:x3="http://nextcloud.com/ns"/><x0:principal-collection-set/><x0:supported-report-set/></x0:prop></x0:propfind>',
  95. 'auth' => [
  96. $user,
  97. $password,
  98. ],
  99. ]
  100. );
  101. } catch (\GuzzleHttp\Exception\ClientException $e) {
  102. $this->response = $e->getResponse();
  103. }
  104. }
  105. /**
  106. * @Then The CalDAV response should contain a property :key
  107. * @throws \Exception
  108. */
  109. public function theCaldavResponseShouldContainAProperty(string $key): void {
  110. /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */
  111. $multiStatus = $this->responseXml['value'];
  112. $responses = $multiStatus->getResponses()[0]->getResponseProperties();
  113. if (!isset($responses[200])) {
  114. throw new \Exception(
  115. sprintf(
  116. 'Expected code 200 got [%s]',
  117. implode(',', array_keys($responses)),
  118. )
  119. );
  120. }
  121. $props = $responses[200];
  122. if (!array_key_exists($key, $props)) {
  123. throw new \Exception(
  124. sprintf(
  125. 'Expected property %s in %s',
  126. $key,
  127. json_encode($props, JSON_PRETTY_PRINT),
  128. )
  129. );
  130. }
  131. }
  132. /**
  133. * @Then The CalDAV response should contain a property :key with a href value :value
  134. * @throws \Exception
  135. */
  136. public function theCaldavResponseShouldContainAPropertyWithHrefValue(
  137. string $key,
  138. string $value,
  139. ): void {
  140. /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */
  141. $multiStatus = $this->responseXml['value'];
  142. $responses = $multiStatus->getResponses()[0]->getResponseProperties();
  143. if (!isset($responses[200])) {
  144. throw new \Exception(
  145. sprintf(
  146. 'Expected code 200 got [%s]',
  147. implode(',', array_keys($responses)),
  148. )
  149. );
  150. }
  151. $props = $responses[200];
  152. if (!array_key_exists($key, $props)) {
  153. throw new \Exception("Cannot find property \"$key\"");
  154. }
  155. $actualValue = $props[$key]->getHref();
  156. if ($actualValue !== $value) {
  157. throw new \Exception("Property \"$key\" found with value \"$actualValue\", expected \"$value\"");
  158. }
  159. }
  160. /**
  161. * @Then The CalDAV response should be multi status
  162. * @throws \Exception
  163. */
  164. public function theCaldavResponseShouldBeMultiStatus(): void {
  165. if ($this->response->getStatusCode() !== 207) {
  166. throw new \Exception(
  167. sprintf(
  168. 'Expected code 207 got %s',
  169. $this->response->getStatusCode()
  170. )
  171. );
  172. }
  173. $body = $this->response->getBody()->getContents();
  174. if ($body && substr($body, 0, 1) === '<') {
  175. $reader = new Sabre\Xml\Reader();
  176. $reader->xml($body);
  177. $reader->elementMap['{DAV:}multistatus'] = \Sabre\DAV\Xml\Response\MultiStatus::class;
  178. $reader->elementMap['{DAV:}response'] = \Sabre\DAV\Xml\Element\Response::class;
  179. $reader->elementMap['{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL'] = \Sabre\DAV\Xml\Property\Href::class;
  180. $this->responseXml = $reader->parse();
  181. }
  182. }
  183. /**
  184. * @Then The CalDAV HTTP status code should be :code
  185. * @param int $code
  186. * @throws \Exception
  187. */
  188. public function theCaldavHttpStatusCodeShouldBe($code) {
  189. if ((int)$code !== $this->response->getStatusCode()) {
  190. throw new \Exception(
  191. sprintf(
  192. 'Expected %s got %s',
  193. (int)$code,
  194. $this->response->getStatusCode()
  195. )
  196. );
  197. }
  198. $body = $this->response->getBody()->getContents();
  199. if ($body && substr($body, 0, 1) === '<') {
  200. $reader = new Sabre\Xml\Reader();
  201. $reader->xml($body);
  202. $this->responseXml = $reader->parse();
  203. }
  204. }
  205. /**
  206. * @Then The exception is :message
  207. * @param string $message
  208. * @throws \Exception
  209. */
  210. public function theExceptionIs($message) {
  211. $result = $this->responseXml['value'][0]['value'];
  212. if ($message !== $result) {
  213. throw new \Exception(
  214. sprintf(
  215. 'Expected %s got %s',
  216. $message,
  217. $result
  218. )
  219. );
  220. }
  221. }
  222. /**
  223. * @Then The error message is :message
  224. * @param string $message
  225. * @throws \Exception
  226. */
  227. public function theErrorMessageIs($message) {
  228. $result = $this->responseXml['value'][1]['value'];
  229. if ($message !== $result) {
  230. throw new \Exception(
  231. sprintf(
  232. 'Expected %s got %s',
  233. $message,
  234. $result
  235. )
  236. );
  237. }
  238. }
  239. /**
  240. * @Given :user creates a calendar named :name
  241. * @param string $user
  242. * @param string $name
  243. */
  244. public function createsACalendarNamed($user, $name) {
  245. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  246. $password = ($user === 'admin') ? 'admin' : '123456';
  247. $this->response = $this->client->request(
  248. 'MKCALENDAR',
  249. $davUrl,
  250. [
  251. 'body' => '<c:mkcalendar xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:" xmlns:a="http://apple.com/ns/ical/" xmlns:o="http://owncloud.org/ns"><d:set><d:prop><d:displayname>test</d:displayname><o:calendar-enabled>1</o:calendar-enabled><a:calendar-color>#21213D</a:calendar-color><c:supported-calendar-component-set><c:comp name="VEVENT"/></c:supported-calendar-component-set></d:prop></d:set></c:mkcalendar>',
  252. 'auth' => [
  253. $user,
  254. $password,
  255. ],
  256. ]
  257. );
  258. }
  259. /**
  260. * @Then :user publicly shares the calendar named :name
  261. *
  262. * @param string $user
  263. * @param string $name
  264. */
  265. public function publiclySharesTheCalendarNamed($user, $name) {
  266. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  267. $password = ($user === 'admin') ? 'admin' : '123456';
  268. $this->response = $this->client->request(
  269. 'POST',
  270. $davUrl,
  271. [
  272. 'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
  273. 'auth' => [
  274. $user,
  275. $password,
  276. ],
  277. 'headers' => [
  278. 'Content-Type' => 'application/xml; charset=UTF-8',
  279. ],
  280. ]
  281. );
  282. }
  283. /**
  284. * @Then There should be :amount calendars in the response body
  285. *
  286. * @param string $amount
  287. */
  288. public function t($amount) {
  289. $jsonEncoded = json_encode($this->responseXml);
  290. $arrayElement = json_decode($jsonEncoded, true);
  291. $actual = count($arrayElement['value']) - 1;
  292. if ($actual !== (int)$amount) {
  293. throw new InvalidArgumentException(
  294. sprintf(
  295. 'Expected %s got %s',
  296. $amount,
  297. $actual
  298. )
  299. );
  300. }
  301. }
  302. /**
  303. * @When :user sends a create calendar request to :calendar on the endpoint :endpoint
  304. */
  305. public function sendsCreateCalendarRequest(string $user, string $calendar, string $endpoint) {
  306. $davUrl = $this->baseUrl . $endpoint . $calendar;
  307. $password = ($user === 'admin') ? 'admin' : '123456';
  308. try {
  309. $this->response = $this->client->request(
  310. 'MKCALENDAR',
  311. $davUrl,
  312. [
  313. 'body' => '<c:mkcalendar xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:" xmlns:a="http://apple.com/ns/ical/" xmlns:o="http://owncloud.org/ns"><d:set><d:prop><d:displayname>test</d:displayname><o:calendar-enabled>1</o:calendar-enabled><a:calendar-color>#21213D</a:calendar-color><c:supported-calendar-component-set><c:comp name="VEVENT"/></c:supported-calendar-component-set></d:prop></d:set></c:mkcalendar>',
  314. 'auth' => [
  315. $user,
  316. $password,
  317. ],
  318. ]
  319. );
  320. } catch (GuzzleException $e) {
  321. $this->response = $e->getResponse();
  322. }
  323. }
  324. /**
  325. * @Given :user updates property :key to href :value of principal :principal on the endpoint :endpoint
  326. */
  327. public function updatesHrefPropertyOfPrincipal(
  328. string $user,
  329. string $key,
  330. string $value,
  331. string $principal,
  332. string $endpoint,
  333. ): void {
  334. $davUrl = $this->baseUrl . $endpoint . $principal;
  335. $password = ($user === 'admin') ? 'admin' : '123456';
  336. $propPatch = new \Sabre\DAV\Xml\Request\PropPatch();
  337. $propPatch->properties = [$key => new \Sabre\DAV\Xml\Property\Href($value)];
  338. $xml = new \Sabre\Xml\Service();
  339. $body = $xml->write('{DAV:}propertyupdate', $propPatch, '/');
  340. $this->response = $this->client->request(
  341. 'PROPPATCH',
  342. $davUrl,
  343. [
  344. 'headers' => [
  345. 'Content-Type' => 'application/xml; charset=UTF-8',
  346. ],
  347. 'body' => $body,
  348. 'auth' => [
  349. $user,
  350. $password,
  351. ],
  352. ]
  353. );
  354. }
  355. }