CollaborationContext.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. if (isset($expected['status'])) {
  67. $data['status'] = json_encode($suggestion['status']);
  68. }
  69. return $data;
  70. }, $suggestions, $formData->getHash()));
  71. }
  72. /**
  73. * @Given /^there is a contact in an addressbook$/
  74. */
  75. public function thereIsAContactInAnAddressbook() {
  76. $this->usingNewDavPath();
  77. try {
  78. $destination = '/users/admin/myaddressbook';
  79. $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>';
  80. $this->response = $this->makeDavRequest($this->currentUser, "MKCOL", $destination, ['Content-Type' => 'application/xml'], $data, "addressbooks");
  81. } catch (\GuzzleHttp\Exception\ServerException $e) {
  82. // 5xx responses cause a server exception
  83. $this->response = $e->getResponse();
  84. } catch (\GuzzleHttp\Exception\ClientException $e) {
  85. // 4xx responses cause a client exception
  86. $this->response = $e->getResponse();
  87. }
  88. try {
  89. $destination = '/users/admin/myaddressbook/contact1.vcf';
  90. $data = <<<EOF
  91. BEGIN:VCARD
  92. VERSION:4.0
  93. PRODID:-//Nextcloud Contacts v4.0.2
  94. UID:a0f4088a-4dca-4308-9b63-09a1ebcf78f3
  95. FN:A person
  96. ADR;TYPE=HOME:;;;;;;
  97. EMAIL;TYPE=HOME:user@example.com
  98. REV;VALUE=DATE-AND-OR-TIME:20211130T140111Z
  99. END:VCARD
  100. EOF;
  101. $this->response = $this->makeDavRequest($this->currentUser, "PUT", $destination, [], $data, "addressbooks");
  102. } catch (\GuzzleHttp\Exception\ServerException $e) {
  103. // 5xx responses cause a server exception
  104. $this->response = $e->getResponse();
  105. } catch (\GuzzleHttp\Exception\ClientException $e) {
  106. // 4xx responses cause a client exception
  107. $this->response = $e->getResponse();
  108. }
  109. }
  110. protected function resetAppConfigs(): void {
  111. $this->deleteServerConfig('core', 'shareapi_allow_share_dialog_user_enumeration');
  112. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_to_group');
  113. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_to_phone');
  114. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match');
  115. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_userid');
  116. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_email');
  117. $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn');
  118. $this->deleteServerConfig('core', 'shareapi_only_share_with_group_members');
  119. }
  120. /**
  121. * @Given /^user "([^"]*)" has status "([^"]*)"$/
  122. * @param string $user
  123. * @param string $status
  124. */
  125. public function assureUserHasStatus($user, $status) {
  126. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status/status";
  127. $client = new Client();
  128. $options = [
  129. 'headers' => [
  130. 'OCS-APIREQUEST' => 'true',
  131. ],
  132. ];
  133. if ($user === 'admin') {
  134. $options['auth'] = $this->adminUser;
  135. } else {
  136. $options['auth'] = [$user, $this->regularUser];
  137. }
  138. $options['form_params'] = [
  139. 'statusType' => $status
  140. ];
  141. $this->response = $client->put($fullUrl, $options);
  142. $this->theHTTPStatusCodeShouldBe(200);
  143. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status";
  144. unset($options['form_params']);
  145. $this->response = $client->get($fullUrl, $options);
  146. $this->theHTTPStatusCodeShouldBe(200);
  147. $returnedStatus = json_decode(json_encode(simplexml_load_string($this->response->getBody()->getContents())->data), true)['status'];
  148. Assert::assertEquals($status, $returnedStatus);
  149. }
  150. /**
  151. * @param string $user
  152. * @return null|array
  153. */
  154. public function getStatusList(string $user): ?array {
  155. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/statuses";
  156. $client = new Client();
  157. $options = [
  158. 'headers' => [
  159. 'OCS-APIREQUEST' => 'true',
  160. ],
  161. ];
  162. if ($user === 'admin') {
  163. $options['auth'] = $this->adminUser;
  164. } else {
  165. $options['auth'] = [$user, $this->regularUser];
  166. }
  167. $this->response = $client->get($fullUrl, $options);
  168. $this->theHTTPStatusCodeShouldBe(200);
  169. $contents = $this->response->getBody()->getContents();
  170. return json_decode(json_encode(simplexml_load_string($contents)->data), true);
  171. }
  172. /**
  173. * @Given /^user statuses for "([^"]*)" list "([^"]*)" with status "([^"]*)"$/
  174. * @param string $user
  175. * @param string $statusUser
  176. * @param string $status
  177. */
  178. public function assertStatusesList(string $user, string $statusUser, string $status): void {
  179. $statusList = $this->getStatusList($user);
  180. Assert::assertArrayHasKey('element', $statusList, 'Returned status list empty or broken');
  181. if (array_key_exists('userId', $statusList['element'])) {
  182. // If only one user has a status set, the API returns their status directly
  183. Assert::assertArrayHasKey('status', $statusList['element'], 'Returned status list empty or broken');
  184. $filteredStatusList = [ $statusList['element']['userId'] => $statusList['element']['status'] ];
  185. } else {
  186. // If more than one user have their status set, the API returns an array of their statuses
  187. $filteredStatusList = array_column($statusList['element'], 'status', 'userId');
  188. }
  189. Assert::assertArrayHasKey($statusUser, $filteredStatusList, 'User not listed in statuses: ' . $statusUser);
  190. Assert::assertEquals($status, $filteredStatusList[$statusUser]);
  191. }
  192. /**
  193. * @Given /^user statuses for "([^"]*)" are empty$/
  194. * @param string $user
  195. */
  196. public function assertStatusesEmpty(string $user): void {
  197. $statusList = $this->getStatusList($user);
  198. Assert::assertEmpty($statusList);
  199. }
  200. }