CardDavContext.php 7.7 KB

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