1
0

CollaborationContext.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. use Behat\Behat\Context\Context;
  25. use Behat\Gherkin\Node\TableNode;
  26. use GuzzleHttp\Client;
  27. use PHPUnit\Framework\Assert;
  28. require __DIR__ . '/../../vendor/autoload.php';
  29. class CollaborationContext implements Context {
  30. use Provisioning;
  31. use AppConfiguration;
  32. use WebDav;
  33. /**
  34. * @Then /^get autocomplete for "([^"]*)"$/
  35. * @param TableNode|null $formData
  36. */
  37. public function getAutocompleteForUser(string $search, TableNode $formData): void {
  38. $this->getAutocompleteWithType(0, $search, $formData);
  39. }
  40. /**
  41. * @Then /^get email autocomplete for "([^"]*)"$/
  42. * @param TableNode|null $formData
  43. */
  44. public function getAutocompleteForEmail(string $search, TableNode $formData): void {
  45. $this->getAutocompleteWithType(4, $search, $formData);
  46. }
  47. private function getAutocompleteWithType(int $type, string $search, TableNode $formData): void {
  48. $query = $search === 'null' ? null : $search;
  49. $this->sendRequestForJSON('GET', '/core/autocomplete/get?itemType=files&itemId=123&shareTypes[]=' . $type . '&search=' . $query, [
  50. 'itemType' => 'files',
  51. 'itemId' => '123',
  52. 'search' => $query,
  53. ]);
  54. $this->theHTTPStatusCodeShouldBe(200);
  55. $data = json_decode($this->response->getBody()->getContents(), true);
  56. $suggestions = $data['ocs']['data'];
  57. Assert::assertCount(count($formData->getHash()), $suggestions, 'Suggestion count does not match');
  58. Assert::assertEquals($formData->getHash(), array_map(static function ($suggestion, $expected) {
  59. $data = [];
  60. if (isset($expected['id'])) {
  61. $data['id'] = $suggestion['id'];
  62. }
  63. if (isset($expected['source'])) {
  64. $data['source'] = $suggestion['source'];
  65. }
  66. return $data;
  67. }, $suggestions, $formData->getHash()));
  68. }
  69. /**
  70. * @Given /^there is a contact in an addressbook$/
  71. */
  72. public function thereIsAContactInAnAddressbook() {
  73. $this->usingNewDavPath();
  74. try {
  75. $destination = '/users/admin/myaddressbook';
  76. $data = '<x0:mkcol xmlns:x0="DAV:"><x0:set><x0:prop><x0:resourcetype><x0:collection/><x4:addressbook xmlns:x4="urn:ietf:params:xml:ns:carddav"/></x0:resourcetype><x0:displayname>myaddressbook</x0:displayname></x0:prop></x0:set></x0:mkcol>';
  77. $this->response = $this->makeDavRequest($this->currentUser, "MKCOL", $destination, ['Content-Type' => 'application/xml'], $data, "addressbooks");
  78. } catch (\GuzzleHttp\Exception\ServerException $e) {
  79. // 5xx responses cause a server exception
  80. $this->response = $e->getResponse();
  81. } catch (\GuzzleHttp\Exception\ClientException $e) {
  82. // 4xx responses cause a client exception
  83. $this->response = $e->getResponse();
  84. }
  85. try {
  86. $destination = '/users/admin/myaddressbook/contact1.vcf';
  87. $data = <<<EOF
  88. BEGIN:VCARD
  89. VERSION:4.0
  90. PRODID:-//Nextcloud Contacts v4.0.2
  91. UID:a0f4088a-4dca-4308-9b63-09a1ebcf78f3
  92. FN:A person
  93. ADR;TYPE=HOME:;;;;;;
  94. EMAIL;TYPE=HOME:user@example.com
  95. REV;VALUE=DATE-AND-OR-TIME:20211130T140111Z
  96. END:VCARD
  97. EOF;
  98. $this->response = $this->makeDavRequest($this->currentUser, "PUT", $destination, [], $data, "addressbooks");
  99. } catch (\GuzzleHttp\Exception\ServerException $e) {
  100. // 5xx responses cause a server exception
  101. $this->response = $e->getResponse();
  102. } catch (\GuzzleHttp\Exception\ClientException $e) {
  103. // 4xx responses cause a client exception
  104. $this->response = $e->getResponse();
  105. }
  106. }
  107. protected function resetAppConfigs(): void {
  108. $this->deleteServerConfig('core', 'shareapi_allow_share_dialog_user_enumeration');
  109. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_to_group');
  110. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_to_phone');
  111. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match');
  112. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_userid');
  113. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_email');
  114. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn');
  115. $this->deleteServerConfig('core', 'shareapi_only_share_with_group_members');
  116. }
  117. /**
  118. * @Given /^user "([^"]*)" has status "([^"]*)"$/
  119. * @param string $user
  120. * @param string $status
  121. */
  122. public function assureUserHasStatus($user, $status) {
  123. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status/status";
  124. $client = new Client();
  125. $options = [
  126. 'headers' => [
  127. 'OCS-APIREQUEST' => 'true',
  128. ],
  129. ];
  130. if ($user === 'admin') {
  131. $options['auth'] = $this->adminUser;
  132. } else {
  133. $options['auth'] = [$user, $this->regularUser];
  134. }
  135. $options['form_params'] = [
  136. 'statusType' => $status
  137. ];
  138. $this->response = $client->put($fullUrl, $options);
  139. $this->theHTTPStatusCodeShouldBe(200);
  140. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status";
  141. unset($options['form_params']);
  142. $this->response = $client->get($fullUrl, $options);
  143. $this->theHTTPStatusCodeShouldBe(200);
  144. $returnedStatus = json_decode(json_encode(simplexml_load_string($this->response->getBody()->getContents())->data), true)['status'];
  145. Assert::assertEquals($status, $returnedStatus);
  146. }
  147. /**
  148. * @param string $user
  149. * @return null|array
  150. */
  151. public function getStatusList(string $user): ?array {
  152. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/statuses";
  153. $client = new Client();
  154. $options = [
  155. 'headers' => [
  156. 'OCS-APIREQUEST' => 'true',
  157. ],
  158. ];
  159. if ($user === 'admin') {
  160. $options['auth'] = $this->adminUser;
  161. } else {
  162. $options['auth'] = [$user, $this->regularUser];
  163. }
  164. $this->response = $client->get($fullUrl, $options);
  165. $this->theHTTPStatusCodeShouldBe(200);
  166. $contents = $this->response->getBody()->getContents();
  167. return json_decode(json_encode(simplexml_load_string($contents)->data), true);
  168. }
  169. /**
  170. * @Given /^user statuses for "([^"]*)" list "([^"]*)" with status "([^"]*)"$/
  171. * @param string $user
  172. * @param string $statusUser
  173. * @param string $status
  174. */
  175. public function assertStatusesList(string $user, string $statusUser, string $status): void {
  176. $statusList = $this->getStatusList($user);
  177. Assert::assertArrayHasKey('element', $statusList, 'Returned status list empty or broken');
  178. if (array_key_exists('userId', $statusList['element'])) {
  179. // If only one user has a status set, the API returns their status directly
  180. Assert::assertArrayHasKey('status', $statusList['element'], 'Returned status list empty or broken');
  181. $filteredStatusList = [ $statusList['element']['userId'] => $statusList['element']['status'] ];
  182. } else {
  183. // If more than one user have their status set, the API returns an array of their statuses
  184. $filteredStatusList = array_column($statusList['element'], 'status', 'userId');
  185. }
  186. Assert::assertArrayHasKey($statusUser, $filteredStatusList, 'User not listed in statuses: ' . $statusUser);
  187. Assert::assertEquals($status, $filteredStatusList[$statusUser]);
  188. }
  189. /**
  190. * @Given /^user statuses for "([^"]*)" are empty$/
  191. * @param string $user
  192. */
  193. public function assertStatusesEmpty(string $user): void {
  194. $statusList = $this->getStatusList($user);
  195. Assert::assertEmpty($statusList);
  196. }
  197. }