CalDavContext.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Phil Davis <phil.davis@inf.org>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Richard Steinmetz <richard@steinmetz.cloud>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. require __DIR__ . '/../../vendor/autoload.php';
  29. use GuzzleHttp\Client;
  30. use GuzzleHttp\Exception\GuzzleException;
  31. use Psr\Http\Message\ResponseInterface;
  32. class CalDavContext implements \Behat\Behat\Context\Context {
  33. /** @var string */
  34. private $baseUrl;
  35. /** @var Client */
  36. private $client;
  37. /** @var ResponseInterface */
  38. private $response;
  39. /** @var string */
  40. private $responseXml = '';
  41. /**
  42. * @param string $baseUrl
  43. */
  44. public function __construct($baseUrl) {
  45. $this->baseUrl = $baseUrl;
  46. // in case of ci deployment we take the server url from the environment
  47. $testServerUrl = getenv('TEST_SERVER_URL');
  48. if ($testServerUrl !== false) {
  49. $this->baseUrl = substr($testServerUrl, 0, -5);
  50. }
  51. }
  52. /** @BeforeScenario */
  53. public function setUpScenario() {
  54. $this->client = new Client();
  55. $this->responseXml = '';
  56. }
  57. /** @AfterScenario */
  58. public function afterScenario() {
  59. $davUrl = $this->baseUrl. '/remote.php/dav/calendars/admin/MyCalendar';
  60. try {
  61. $this->client->delete(
  62. $davUrl,
  63. [
  64. 'auth' => [
  65. 'admin',
  66. 'admin',
  67. ],
  68. 'headers' => [
  69. 'X-NC-CalDAV-No-Trashbin' => '1',
  70. ]
  71. ]
  72. );
  73. } catch (\GuzzleHttp\Exception\ClientException $e) {
  74. }
  75. }
  76. /**
  77. * @When :user requests calendar :calendar on the endpoint :endpoint
  78. * @param string $user
  79. * @param string $calendar
  80. * @param string $endpoint
  81. */
  82. public function requestsCalendar($user, $calendar, $endpoint) {
  83. $davUrl = $this->baseUrl . $endpoint . $calendar;
  84. $password = ($user === 'admin') ? 'admin' : '123456';
  85. try {
  86. $this->response = $this->client->request(
  87. 'PROPFIND',
  88. $davUrl,
  89. [
  90. 'auth' => [
  91. $user,
  92. $password,
  93. ],
  94. ]
  95. );
  96. } catch (\GuzzleHttp\Exception\ClientException $e) {
  97. $this->response = $e->getResponse();
  98. }
  99. }
  100. /**
  101. * @When :user requests principal :principal on the endpoint :endpoint
  102. */
  103. public function requestsPrincipal(string $user, string $principal, string $endpoint): void {
  104. $davUrl = $this->baseUrl . $endpoint . $principal;
  105. $password = ($user === 'admin') ? 'admin' : '123456';
  106. try {
  107. $this->response = $this->client->request(
  108. 'PROPFIND',
  109. $davUrl,
  110. [
  111. 'headers' => [
  112. 'Content-Type' => 'application/xml; charset=UTF-8',
  113. 'Depth' => 0,
  114. ],
  115. '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>',
  116. 'auth' => [
  117. $user,
  118. $password,
  119. ],
  120. ]
  121. );
  122. } catch (\GuzzleHttp\Exception\ClientException $e) {
  123. $this->response = $e->getResponse();
  124. }
  125. }
  126. /**
  127. * @Then The CalDAV response should contain a property :key
  128. * @throws \Exception
  129. */
  130. public function theCaldavResponseShouldContainAProperty(string $key): void {
  131. /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */
  132. $multiStatus = $this->responseXml['value'];
  133. $responses = $multiStatus->getResponses()[0]->getResponseProperties();
  134. if (!isset($responses[200])) {
  135. throw new \Exception(
  136. sprintf(
  137. 'Expected code 200 got [%s]',
  138. implode(',', array_keys($responses)),
  139. )
  140. );
  141. }
  142. $props = $responses[200];
  143. if (!array_key_exists($key, $props)) {
  144. throw new \Exception(
  145. sprintf(
  146. 'Expected property %s in %s',
  147. $key,
  148. json_encode($props, JSON_PRETTY_PRINT),
  149. )
  150. );
  151. }
  152. }
  153. /**
  154. * @Then The CalDAV response should contain a property :key with a href value :value
  155. * @throws \Exception
  156. */
  157. public function theCaldavResponseShouldContainAPropertyWithHrefValue(
  158. string $key,
  159. string $value,
  160. ): void {
  161. /** @var \Sabre\DAV\Xml\Response\MultiStatus $multiStatus */
  162. $multiStatus = $this->responseXml['value'];
  163. $responses = $multiStatus->getResponses()[0]->getResponseProperties();
  164. if (!isset($responses[200])) {
  165. throw new \Exception(
  166. sprintf(
  167. 'Expected code 200 got [%s]',
  168. implode(',', array_keys($responses)),
  169. )
  170. );
  171. }
  172. $props = $responses[200];
  173. if (!array_key_exists($key, $props)) {
  174. throw new \Exception("Cannot find property \"$key\"");
  175. }
  176. $actualValue = $props[$key]->getHref();
  177. if ($actualValue !== $value) {
  178. throw new \Exception("Property \"$key\" found with value \"$actualValue\", expected \"$value\"");
  179. }
  180. }
  181. /**
  182. * @Then The CalDAV response should be multi status
  183. * @throws \Exception
  184. */
  185. public function theCaldavResponseShouldBeMultiStatus(): void {
  186. if ($this->response->getStatusCode() !== 207) {
  187. throw new \Exception(
  188. sprintf(
  189. 'Expected code 207 got %s',
  190. $this->response->getStatusCode()
  191. )
  192. );
  193. }
  194. $body = $this->response->getBody()->getContents();
  195. if ($body && substr($body, 0, 1) === '<') {
  196. $reader = new Sabre\Xml\Reader();
  197. $reader->xml($body);
  198. $reader->elementMap['{DAV:}multistatus'] = \Sabre\DAV\Xml\Response\MultiStatus::class;
  199. $reader->elementMap['{DAV:}response'] = \Sabre\DAV\Xml\Element\Response::class;
  200. $reader->elementMap['{urn:ietf:params:xml:ns:caldav}schedule-default-calendar-URL'] = \Sabre\DAV\Xml\Property\Href::class;
  201. $this->responseXml = $reader->parse();
  202. }
  203. }
  204. /**
  205. * @Then The CalDAV HTTP status code should be :code
  206. * @param int $code
  207. * @throws \Exception
  208. */
  209. public function theCaldavHttpStatusCodeShouldBe($code) {
  210. if ((int)$code !== $this->response->getStatusCode()) {
  211. throw new \Exception(
  212. sprintf(
  213. 'Expected %s got %s',
  214. (int)$code,
  215. $this->response->getStatusCode()
  216. )
  217. );
  218. }
  219. $body = $this->response->getBody()->getContents();
  220. if ($body && substr($body, 0, 1) === '<') {
  221. $reader = new Sabre\Xml\Reader();
  222. $reader->xml($body);
  223. $this->responseXml = $reader->parse();
  224. }
  225. }
  226. /**
  227. * @Then The exception is :message
  228. * @param string $message
  229. * @throws \Exception
  230. */
  231. public function theExceptionIs($message) {
  232. $result = $this->responseXml['value'][0]['value'];
  233. if ($message !== $result) {
  234. throw new \Exception(
  235. sprintf(
  236. 'Expected %s got %s',
  237. $message,
  238. $result
  239. )
  240. );
  241. }
  242. }
  243. /**
  244. * @Then The error message is :message
  245. * @param string $message
  246. * @throws \Exception
  247. */
  248. public function theErrorMessageIs($message) {
  249. $result = $this->responseXml['value'][1]['value'];
  250. if ($message !== $result) {
  251. throw new \Exception(
  252. sprintf(
  253. 'Expected %s got %s',
  254. $message,
  255. $result
  256. )
  257. );
  258. }
  259. }
  260. /**
  261. * @Given :user creates a calendar named :name
  262. * @param string $user
  263. * @param string $name
  264. */
  265. public function createsACalendarNamed($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. 'MKCALENDAR',
  270. $davUrl,
  271. [
  272. '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>',
  273. 'auth' => [
  274. $user,
  275. $password,
  276. ],
  277. ]
  278. );
  279. }
  280. /**
  281. * @Then :user publicly shares the calendar named :name
  282. *
  283. * @param string $user
  284. * @param string $name
  285. */
  286. public function publiclySharesTheCalendarNamed($user, $name) {
  287. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  288. $password = ($user === 'admin') ? 'admin' : '123456';
  289. $this->response = $this->client->request(
  290. 'POST',
  291. $davUrl,
  292. [
  293. 'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
  294. 'auth' => [
  295. $user,
  296. $password,
  297. ],
  298. 'headers' => [
  299. 'Content-Type' => 'application/xml; charset=UTF-8',
  300. ],
  301. ]
  302. );
  303. }
  304. /**
  305. * @Then There should be :amount calendars in the response body
  306. *
  307. * @param string $amount
  308. */
  309. public function t($amount) {
  310. $jsonEncoded = json_encode($this->responseXml);
  311. $arrayElement = json_decode($jsonEncoded, true);
  312. $actual = count($arrayElement['value']) - 1;
  313. if ($actual !== (int)$amount) {
  314. throw new InvalidArgumentException(
  315. sprintf(
  316. 'Expected %s got %s',
  317. $amount,
  318. $actual
  319. )
  320. );
  321. }
  322. }
  323. /**
  324. * @When :user sends a create calendar request to :calendar on the endpoint :endpoint
  325. */
  326. public function sendsCreateCalendarRequest(string $user, string $calendar, string $endpoint) {
  327. $davUrl = $this->baseUrl . $endpoint . $calendar;
  328. $password = ($user === 'admin') ? 'admin' : '123456';
  329. try {
  330. $this->response = $this->client->request(
  331. 'MKCALENDAR',
  332. $davUrl,
  333. [
  334. '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>',
  335. 'auth' => [
  336. $user,
  337. $password,
  338. ],
  339. ]
  340. );
  341. } catch (GuzzleException $e) {
  342. $this->response = $e->getResponse();
  343. }
  344. }
  345. /**
  346. * @Given :user updates property :key to href :value of principal :principal on the endpoint :endpoint
  347. */
  348. public function updatesHrefPropertyOfPrincipal(
  349. string $user,
  350. string $key,
  351. string $value,
  352. string $principal,
  353. string $endpoint,
  354. ): void {
  355. $davUrl = $this->baseUrl . $endpoint . $principal;
  356. $password = ($user === 'admin') ? 'admin' : '123456';
  357. $propPatch = new \Sabre\DAV\Xml\Request\PropPatch();
  358. $propPatch->properties = [$key => new \Sabre\DAV\Xml\Property\Href($value)];
  359. $xml = new \Sabre\Xml\Service();
  360. $body = $xml->write('{DAV:}propertyupdate', $propPatch, '/');
  361. $this->response = $this->client->request(
  362. 'PROPPATCH',
  363. $davUrl,
  364. [
  365. 'headers' => [
  366. 'Content-Type' => 'application/xml; charset=UTF-8',
  367. ],
  368. 'body' => $body,
  369. 'auth' => [
  370. $user,
  371. $password,
  372. ],
  373. ]
  374. );
  375. }
  376. }