CardDavContext.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Phil Davis <phil.davis@inf.org>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. require __DIR__ . '/../../vendor/autoload.php';
  27. use GuzzleHttp\Client;
  28. use GuzzleHttp\Exception\GuzzleException;
  29. use GuzzleHttp\Message\ResponseInterface;
  30. class CardDavContext implements \Behat\Behat\Context\Context {
  31. /** @var string */
  32. private $baseUrl;
  33. /** @var Client */
  34. private $client;
  35. /** @var ResponseInterface */
  36. private $response;
  37. /** @var string */
  38. private $responseXml = '';
  39. /**
  40. * @param string $baseUrl
  41. */
  42. public function __construct($baseUrl) {
  43. $this->baseUrl = $baseUrl;
  44. // in case of ci deployment we take the server url from the environment
  45. $testServerUrl = getenv('TEST_SERVER_URL');
  46. if ($testServerUrl !== false) {
  47. $this->baseUrl = substr($testServerUrl, 0, -5);
  48. }
  49. }
  50. /** @BeforeScenario */
  51. public function setUpScenario() {
  52. $this->client = new Client();
  53. $this->responseXml = '';
  54. }
  55. /** @AfterScenario */
  56. public function afterScenario() {
  57. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/admin/MyAddressbook';
  58. try {
  59. $this->client->delete(
  60. $davUrl,
  61. [
  62. 'auth' => [
  63. 'admin',
  64. 'admin',
  65. ],
  66. ]
  67. );
  68. } catch (\GuzzleHttp\Exception\ClientException $e) {
  69. }
  70. }
  71. /**
  72. * @When :user requests addressbook :addressBook with statuscode :statusCode on the endpoint :endpoint
  73. * @param string $user
  74. * @param string $addressBook
  75. * @param int $statusCode
  76. * @param string $endpoint
  77. * @throws \Exception
  78. */
  79. public function requestsAddressbookWithStatuscodeOnTheEndpoint($user, $addressBook, $statusCode, $endpoint) {
  80. $davUrl = $this->baseUrl . $endpoint . $addressBook;
  81. $password = ($user === 'admin') ? 'admin' : '123456';
  82. try {
  83. $this->response = $this->client->request(
  84. 'PROPFIND',
  85. $davUrl,
  86. [
  87. 'auth' => [
  88. $user,
  89. $password,
  90. ],
  91. ]
  92. );
  93. } catch (\GuzzleHttp\Exception\ClientException $e) {
  94. $this->response = $e->getResponse();
  95. }
  96. if ((int)$statusCode !== $this->response->getStatusCode()) {
  97. throw new \Exception(
  98. sprintf(
  99. 'Expected %s got %s',
  100. (int)$statusCode,
  101. $this->response->getStatusCode()
  102. )
  103. );
  104. }
  105. $body = $this->response->getBody()->getContents();
  106. if (substr($body, 0, 1) === '<') {
  107. $reader = new Sabre\Xml\Reader();
  108. $reader->xml($body);
  109. $this->responseXml = $reader->parse();
  110. }
  111. }
  112. /**
  113. * @Given :user creates an addressbook named :addressBook with statuscode :statusCode
  114. * @param string $user
  115. * @param string $addressBook
  116. * @param int $statusCode
  117. * @throws \Exception
  118. */
  119. public function createsAnAddressbookNamedWithStatuscode($user, $addressBook, $statusCode) {
  120. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook;
  121. $password = ($user === 'admin') ? 'admin' : '123456';
  122. $this->response = $this->client->request(
  123. 'MKCOL',
  124. $davUrl,
  125. [
  126. 'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav"
  127. xmlns:d="DAV:">
  128. <d:set>
  129. <d:prop>
  130. <d:resourcetype>
  131. <d:collection />,<card:addressbook />
  132. </d:resourcetype>,<d:displayname>'.$addressBook.'</d:displayname>
  133. </d:prop>
  134. </d:set>
  135. </d:mkcol>',
  136. 'auth' => [
  137. $user,
  138. $password,
  139. ],
  140. 'headers' => [
  141. 'Content-Type' => 'application/xml;charset=UTF-8',
  142. ],
  143. ]
  144. );
  145. if ($this->response->getStatusCode() !== (int)$statusCode) {
  146. throw new \Exception(
  147. sprintf(
  148. 'Expected %s got %s',
  149. (int)$statusCode,
  150. $this->response->getStatusCode()
  151. )
  152. );
  153. }
  154. }
  155. /**
  156. * @When The CardDAV exception is :message
  157. * @param string $message
  158. * @throws \Exception
  159. */
  160. public function theCarddavExceptionIs($message) {
  161. $result = $this->responseXml['value'][0]['value'];
  162. if ($message !== $result) {
  163. throw new \Exception(
  164. sprintf(
  165. 'Expected %s got %s',
  166. $message,
  167. $result
  168. )
  169. );
  170. }
  171. }
  172. /**
  173. * @When The CardDAV error message is :arg1
  174. * @param string $message
  175. * @throws \Exception
  176. */
  177. public function theCarddavErrorMessageIs($message) {
  178. $result = $this->responseXml['value'][1]['value'];
  179. if ($message !== $result) {
  180. throw new \Exception(
  181. sprintf(
  182. 'Expected %s got %s',
  183. $message,
  184. $result
  185. )
  186. );
  187. }
  188. }
  189. /**
  190. * @Given :user uploads the contact :fileName to the addressbook :addressbook
  191. */
  192. public function uploadsTheContactToTheAddressbook($user, $fileName, $addressBook) {
  193. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
  194. $password = ($user === 'admin') ? 'admin' : '123456';
  195. $this->response = $this->client->request(
  196. 'PUT',
  197. $davUrl,
  198. [
  199. 'body' => file_get_contents(__DIR__ . '/../../data/' . $fileName),
  200. 'auth' => [
  201. $user,
  202. $password,
  203. ],
  204. 'headers' => [
  205. 'Content-Type' => 'application/xml;charset=UTF-8',
  206. ],
  207. ]
  208. );
  209. if ($this->response->getStatusCode() !== 201) {
  210. throw new \Exception(
  211. sprintf(
  212. 'Expected %s got %s',
  213. 201,
  214. $this->response->getStatusCode()
  215. )
  216. );
  217. }
  218. }
  219. /**
  220. * @When Exporting the picture of contact :fileName from addressbook :addressBook as user :user
  221. */
  222. public function whenExportingThePictureOfContactFromAddressbookAsUser($fileName, $addressBook, $user) {
  223. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName . '?photo=true';
  224. $password = ($user === 'admin') ? 'admin' : '123456';
  225. try {
  226. $this->response = $this->client->request(
  227. 'GET',
  228. $davUrl,
  229. [
  230. 'auth' => [
  231. $user,
  232. $password,
  233. ],
  234. 'headers' => [
  235. 'Content-Type' => 'application/xml;charset=UTF-8',
  236. ],
  237. ]
  238. );
  239. } catch (\GuzzleHttp\Exception\ClientException $e) {
  240. $this->response = $e->getResponse();
  241. }
  242. }
  243. /**
  244. * @When Downloading the contact :fileName from addressbook :addressBook as user :user
  245. */
  246. public function whenDownloadingTheContactFromAddressbookAsUser($fileName, $addressBook, $user) {
  247. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
  248. $password = ($user === 'admin') ? 'admin' : '123456';
  249. try {
  250. $this->response = $this->client->request(
  251. 'GET',
  252. $davUrl,
  253. [
  254. 'auth' => [
  255. $user,
  256. $password,
  257. ],
  258. 'headers' => [
  259. 'Content-Type' => 'application/xml;charset=UTF-8',
  260. ],
  261. ]
  262. );
  263. } catch (\GuzzleHttp\Exception\ClientException $e) {
  264. $this->response = $e->getResponse();
  265. }
  266. }
  267. /**
  268. * @Then The following HTTP headers should be set
  269. * @param \Behat\Gherkin\Node\TableNode $table
  270. * @throws \Exception
  271. */
  272. public function theFollowingHttpHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) {
  273. foreach ($table->getTable() as $header) {
  274. $headerName = $header[0];
  275. $expectedHeaderValue = $header[1];
  276. $returnedHeader = $this->response->getHeader($headerName)[0];
  277. if ($returnedHeader !== $expectedHeaderValue) {
  278. throw new \Exception(
  279. sprintf(
  280. "Expected value '%s' for header '%s', got '%s'",
  281. $expectedHeaderValue,
  282. $headerName,
  283. $returnedHeader
  284. )
  285. );
  286. }
  287. }
  288. }
  289. /**
  290. * @When :user sends a create addressbook request to :addressbook on the endpoint :endpoint
  291. */
  292. public function sendsCreateAddressbookRequest(string $user, string $addressbook, string $endpoint) {
  293. $davUrl = $this->baseUrl . $endpoint . $addressbook;
  294. $password = ($user === 'admin') ? 'admin' : '123456';
  295. try {
  296. $this->response = $this->client->request(
  297. 'MKCOL',
  298. $davUrl,
  299. [
  300. 'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav"
  301. xmlns:d="DAV:">
  302. <d:set>
  303. <d:prop>
  304. <d:resourcetype>
  305. <d:collection />,<card:addressbook />
  306. </d:resourcetype>,<d:displayname>' . $addressbook . '</d:displayname>
  307. </d:prop>
  308. </d:set>
  309. </d:mkcol>',
  310. 'auth' => [
  311. $user,
  312. $password,
  313. ],
  314. 'headers' => [
  315. 'Content-Type' => 'application/xml;charset=UTF-8',
  316. ],
  317. ]
  318. );
  319. } catch (GuzzleException $e) {
  320. $this->response = $e->getResponse();
  321. }
  322. }
  323. /**
  324. * @Then The CardDAV HTTP status code should be :code
  325. * @param int $code
  326. * @throws \Exception
  327. */
  328. public function theCarddavHttpStatusCodeShouldBe($code) {
  329. if ((int)$code !== $this->response->getStatusCode()) {
  330. throw new \Exception(
  331. sprintf(
  332. 'Expected %s got %s',
  333. (int)$code,
  334. $this->response->getStatusCode()
  335. )
  336. );
  337. }
  338. $body = $this->response->getBody()->getContents();
  339. if ($body && substr($body, 0, 1) === '<') {
  340. $reader = new Sabre\Xml\Reader();
  341. $reader->xml($body);
  342. $this->responseXml = $reader->parse();
  343. }
  344. }
  345. }