1
0

CardDavContext.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Message\ResponseInterface;
  29. class CardDavContext implements \Behat\Behat\Context\Context {
  30. /** @var string */
  31. private $baseUrl;
  32. /** @var Client */
  33. private $client;
  34. /** @var ResponseInterface */
  35. private $response;
  36. /** @var string */
  37. private $responseXml = '';
  38. /**
  39. * @param string $baseUrl
  40. */
  41. public function __construct($baseUrl) {
  42. $this->baseUrl = $baseUrl;
  43. // in case of ci deployment we take the server url from the environment
  44. $testServerUrl = getenv('TEST_SERVER_URL');
  45. if ($testServerUrl !== false) {
  46. $this->baseUrl = substr($testServerUrl, 0, -5);
  47. }
  48. }
  49. /** @BeforeScenario */
  50. public function setUpScenario() {
  51. $this->client = new Client();
  52. $this->responseXml = '';
  53. }
  54. /** @AfterScenario */
  55. public function afterScenario() {
  56. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/admin/MyAddressbook';
  57. try {
  58. $this->client->delete(
  59. $davUrl,
  60. [
  61. 'auth' => [
  62. 'admin',
  63. 'admin',
  64. ],
  65. ]
  66. );
  67. } catch (\GuzzleHttp\Exception\ClientException $e) {
  68. }
  69. }
  70. /**
  71. * @When :user requests addressbook :addressBook with statuscode :statusCode on the endpoint :endpoint
  72. * @param string $user
  73. * @param string $addressBook
  74. * @param int $statusCode
  75. * @param string $endpoint
  76. * @throws \Exception
  77. */
  78. public function requestsAddressbookWithStatuscodeOnTheEndpoint($user, $addressBook, $statusCode, $endpoint) {
  79. $davUrl = $this->baseUrl . $endpoint . $addressBook;
  80. $password = ($user === 'admin') ? 'admin' : '123456';
  81. try {
  82. $this->response = $this->client->request(
  83. 'PROPFIND',
  84. $davUrl,
  85. [
  86. 'auth' => [
  87. $user,
  88. $password,
  89. ],
  90. ]
  91. );
  92. } catch (\GuzzleHttp\Exception\ClientException $e) {
  93. $this->response = $e->getResponse();
  94. }
  95. if ((int)$statusCode !== $this->response->getStatusCode()) {
  96. throw new \Exception(
  97. sprintf(
  98. 'Expected %s got %s',
  99. (int)$statusCode,
  100. $this->response->getStatusCode()
  101. )
  102. );
  103. }
  104. $body = $this->response->getBody()->getContents();
  105. if (substr($body, 0, 1) === '<') {
  106. $reader = new Sabre\Xml\Reader();
  107. $reader->xml($body);
  108. $this->responseXml = $reader->parse();
  109. }
  110. }
  111. /**
  112. * @Given :user creates an addressbook named :addressBook with statuscode :statusCode
  113. * @param string $user
  114. * @param string $addressBook
  115. * @param int $statusCode
  116. * @throws \Exception
  117. */
  118. public function createsAnAddressbookNamedWithStatuscode($user, $addressBook, $statusCode) {
  119. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook;
  120. $password = ($user === 'admin') ? 'admin' : '123456';
  121. $this->response = $this->client->request(
  122. 'MKCOL',
  123. $davUrl,
  124. [
  125. 'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav"
  126. xmlns:d="DAV:">
  127. <d:set>
  128. <d:prop>
  129. <d:resourcetype>
  130. <d:collection />,<card:addressbook />
  131. </d:resourcetype>,<d:displayname>'.$addressBook.'</d:displayname>
  132. </d:prop>
  133. </d:set>
  134. </d:mkcol>',
  135. 'auth' => [
  136. $user,
  137. $password,
  138. ],
  139. 'headers' => [
  140. 'Content-Type' => 'application/xml;charset=UTF-8',
  141. ],
  142. ]
  143. );
  144. if ($this->response->getStatusCode() !== (int)$statusCode) {
  145. throw new \Exception(
  146. sprintf(
  147. 'Expected %s got %s',
  148. (int)$statusCode,
  149. $this->response->getStatusCode()
  150. )
  151. );
  152. }
  153. }
  154. /**
  155. * @When The CardDAV exception is :message
  156. * @param string $message
  157. * @throws \Exception
  158. */
  159. public function theCarddavExceptionIs($message) {
  160. $result = $this->responseXml['value'][0]['value'];
  161. if ($message !== $result) {
  162. throw new \Exception(
  163. sprintf(
  164. 'Expected %s got %s',
  165. $message,
  166. $result
  167. )
  168. );
  169. }
  170. }
  171. /**
  172. * @When The CardDAV error message is :arg1
  173. * @param string $message
  174. * @throws \Exception
  175. */
  176. public function theCarddavErrorMessageIs($message) {
  177. $result = $this->responseXml['value'][1]['value'];
  178. if ($message !== $result) {
  179. throw new \Exception(
  180. sprintf(
  181. 'Expected %s got %s',
  182. $message,
  183. $result
  184. )
  185. );
  186. }
  187. }
  188. /**
  189. * @Given :user uploads the contact :fileName to the addressbook :addressbook
  190. */
  191. public function uploadsTheContactToTheAddressbook($user, $fileName, $addressBook) {
  192. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
  193. $password = ($user === 'admin') ? 'admin' : '123456';
  194. $this->response = $this->client->request(
  195. 'PUT',
  196. $davUrl,
  197. [
  198. 'body' => file_get_contents(__DIR__ . '/../../data/' . $fileName),
  199. 'auth' => [
  200. $user,
  201. $password,
  202. ],
  203. 'headers' => [
  204. 'Content-Type' => 'application/xml;charset=UTF-8',
  205. ],
  206. ]
  207. );
  208. if ($this->response->getStatusCode() !== 201) {
  209. throw new \Exception(
  210. sprintf(
  211. 'Expected %s got %s',
  212. 201,
  213. $this->response->getStatusCode()
  214. )
  215. );
  216. }
  217. }
  218. /**
  219. * @When Exporting the picture of contact :fileName from addressbook :addressBook as user :user
  220. */
  221. public function whenExportingThePictureOfContactFromAddressbookAsUser($fileName, $addressBook, $user) {
  222. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName . '?photo=true';
  223. $password = ($user === 'admin') ? 'admin' : '123456';
  224. try {
  225. $this->response = $this->client->request(
  226. 'GET',
  227. $davUrl,
  228. [
  229. 'auth' => [
  230. $user,
  231. $password,
  232. ],
  233. 'headers' => [
  234. 'Content-Type' => 'application/xml;charset=UTF-8',
  235. ],
  236. ]
  237. );
  238. } catch (\GuzzleHttp\Exception\ClientException $e) {
  239. $this->response = $e->getResponse();
  240. }
  241. }
  242. /**
  243. * @When Downloading the contact :fileName from addressbook :addressBook as user :user
  244. */
  245. public function whenDownloadingTheContactFromAddressbookAsUser($fileName, $addressBook, $user) {
  246. $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
  247. $password = ($user === 'admin') ? 'admin' : '123456';
  248. try {
  249. $this->response = $this->client->request(
  250. 'GET',
  251. $davUrl,
  252. [
  253. 'auth' => [
  254. $user,
  255. $password,
  256. ],
  257. 'headers' => [
  258. 'Content-Type' => 'application/xml;charset=UTF-8',
  259. ],
  260. ]
  261. );
  262. } catch (\GuzzleHttp\Exception\ClientException $e) {
  263. $this->response = $e->getResponse();
  264. }
  265. }
  266. /**
  267. * @Then The following HTTP headers should be set
  268. * @param \Behat\Gherkin\Node\TableNode $table
  269. * @throws \Exception
  270. */
  271. public function theFollowingHttpHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) {
  272. foreach ($table->getTable() as $header) {
  273. $headerName = $header[0];
  274. $expectedHeaderValue = $header[1];
  275. $returnedHeader = $this->response->getHeader($headerName)[0];
  276. if ($returnedHeader !== $expectedHeaderValue) {
  277. throw new \Exception(
  278. sprintf(
  279. "Expected value '%s' for header '%s', got '%s'",
  280. $expectedHeaderValue,
  281. $headerName,
  282. $returnedHeader
  283. )
  284. );
  285. }
  286. }
  287. }
  288. }