Provisioning.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. use GuzzleHttp\Client;
  8. use GuzzleHttp\Message\ResponseInterface;
  9. use PHPUnit\Framework\Assert;
  10. require __DIR__ . '/../../vendor/autoload.php';
  11. trait Provisioning {
  12. use BasicStructure;
  13. /** @var array */
  14. private $createdUsers = [];
  15. /** @var array */
  16. private $createdRemoteUsers = [];
  17. /** @var array */
  18. private $createdRemoteGroups = [];
  19. /** @var array */
  20. private $createdGroups = [];
  21. /**
  22. * @Given /^user "([^"]*)" exists$/
  23. * @param string $user
  24. */
  25. public function assureUserExists($user) {
  26. try {
  27. $this->userExists($user);
  28. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  29. $previous_user = $this->currentUser;
  30. $this->currentUser = 'admin';
  31. $this->creatingTheUser($user);
  32. $this->currentUser = $previous_user;
  33. }
  34. $this->userExists($user);
  35. Assert::assertEquals(200, $this->response->getStatusCode());
  36. }
  37. /**
  38. * @Given /^user "([^"]*)" with displayname "((?:[^"]|\\")*)" exists$/
  39. * @param string $user
  40. */
  41. public function assureUserWithDisplaynameExists($user, $displayname) {
  42. try {
  43. $this->userExists($user);
  44. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  45. $previous_user = $this->currentUser;
  46. $this->currentUser = 'admin';
  47. $this->creatingTheUser($user, $displayname);
  48. $this->currentUser = $previous_user;
  49. }
  50. $this->userExists($user);
  51. Assert::assertEquals(200, $this->response->getStatusCode());
  52. }
  53. /**
  54. * @Given /^user "([^"]*)" does not exist$/
  55. * @param string $user
  56. */
  57. public function userDoesNotExist($user) {
  58. try {
  59. $this->userExists($user);
  60. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  61. $this->response = $ex->getResponse();
  62. Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
  63. return;
  64. }
  65. $previous_user = $this->currentUser;
  66. $this->currentUser = 'admin';
  67. $this->deletingTheUser($user);
  68. $this->currentUser = $previous_user;
  69. try {
  70. $this->userExists($user);
  71. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  72. $this->response = $ex->getResponse();
  73. Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
  74. }
  75. }
  76. public function creatingTheUser($user, $displayname = '') {
  77. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users";
  78. $client = new Client();
  79. $options = [];
  80. if ($this->currentUser === 'admin') {
  81. $options['auth'] = $this->adminUser;
  82. }
  83. $options['form_params'] = [
  84. 'userid' => $user,
  85. 'password' => '123456'
  86. ];
  87. if ($displayname !== '') {
  88. $options['form_params']['displayName'] = $displayname;
  89. }
  90. $options['headers'] = [
  91. 'OCS-APIREQUEST' => 'true',
  92. ];
  93. $this->response = $client->post($fullUrl, $options);
  94. if ($this->currentServer === 'LOCAL') {
  95. $this->createdUsers[$user] = $user;
  96. } elseif ($this->currentServer === 'REMOTE') {
  97. $this->createdRemoteUsers[$user] = $user;
  98. }
  99. //Quick hack to login once with the current user
  100. $options2 = [
  101. 'auth' => [$user, '123456'],
  102. ];
  103. $options2['headers'] = [
  104. 'OCS-APIREQUEST' => 'true',
  105. ];
  106. $url = $fullUrl . '/' . $user;
  107. $client->get($url, $options2);
  108. }
  109. /**
  110. * @Then /^user "([^"]*)" has$/
  111. *
  112. * @param string $user
  113. * @param \Behat\Gherkin\Node\TableNode|null $settings
  114. */
  115. public function userHasSetting($user, $settings) {
  116. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  117. $client = new Client();
  118. $options = [];
  119. if ($this->currentUser === 'admin') {
  120. $options['auth'] = $this->adminUser;
  121. } else {
  122. $options['auth'] = [$this->currentUser, $this->regularUser];
  123. }
  124. $options['headers'] = [
  125. 'OCS-APIREQUEST' => 'true',
  126. ];
  127. $response = $client->get($fullUrl, $options);
  128. foreach ($settings->getRows() as $setting) {
  129. $value = json_decode(json_encode(simplexml_load_string($response->getBody())->data->{$setting[0]}), 1);
  130. if (isset($value['element']) && in_array($setting[0], ['additional_mail', 'additional_mailScope'], true)) {
  131. $expectedValues = explode(';', $setting[1]);
  132. foreach ($expectedValues as $expected) {
  133. Assert::assertTrue(in_array($expected, $value['element'], true));
  134. }
  135. } elseif (isset($value[0])) {
  136. Assert::assertEqualsCanonicalizing($setting[1], $value[0]);
  137. } else {
  138. Assert::assertEquals('', $setting[1]);
  139. }
  140. }
  141. }
  142. /**
  143. * @Then /^group "([^"]*)" has$/
  144. *
  145. * @param string $user
  146. * @param \Behat\Gherkin\Node\TableNode|null $settings
  147. */
  148. public function groupHasSetting($group, $settings) {
  149. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups/details?search=$group";
  150. $client = new Client();
  151. $options = [];
  152. if ($this->currentUser === 'admin') {
  153. $options['auth'] = $this->adminUser;
  154. } else {
  155. $options['auth'] = [$this->currentUser, $this->regularUser];
  156. }
  157. $options['headers'] = [
  158. 'OCS-APIREQUEST' => 'true',
  159. ];
  160. $response = $client->get($fullUrl, $options);
  161. $groupDetails = simplexml_load_string($response->getBody())->data[0]->groups[0]->element;
  162. foreach ($settings->getRows() as $setting) {
  163. $value = json_decode(json_encode($groupDetails->{$setting[0]}), 1);
  164. if (isset($value[0])) {
  165. Assert::assertEqualsCanonicalizing($setting[1], $value[0]);
  166. } else {
  167. Assert::assertEquals('', $setting[1]);
  168. }
  169. }
  170. }
  171. /**
  172. * @Then /^user "([^"]*)" has editable fields$/
  173. *
  174. * @param string $user
  175. * @param \Behat\Gherkin\Node\TableNode|null $fields
  176. */
  177. public function userHasEditableFields($user, $fields) {
  178. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/user/fields";
  179. if ($user !== 'self') {
  180. $fullUrl .= '/' . $user;
  181. }
  182. $client = new Client();
  183. $options = [];
  184. if ($this->currentUser === 'admin') {
  185. $options['auth'] = $this->adminUser;
  186. } else {
  187. $options['auth'] = [$this->currentUser, $this->regularUser];
  188. }
  189. $options['headers'] = [
  190. 'OCS-APIREQUEST' => 'true',
  191. ];
  192. $response = $client->get($fullUrl, $options);
  193. $fieldsArray = json_decode(json_encode(simplexml_load_string($response->getBody())->data->element), 1);
  194. $expectedFields = $fields->getRows();
  195. $expectedFields = $this->simplifyArray($expectedFields);
  196. Assert::assertEquals($expectedFields, $fieldsArray);
  197. }
  198. /**
  199. * @Then /^search users by phone for region "([^"]*)" with$/
  200. *
  201. * @param string $user
  202. * @param \Behat\Gherkin\Node\TableNode|null $settings
  203. */
  204. public function searchUserByPhone($region, \Behat\Gherkin\Node\TableNode $searchTable) {
  205. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/search/by-phone";
  206. $client = new Client();
  207. $options = [];
  208. $options['auth'] = $this->adminUser;
  209. $options['headers'] = [
  210. 'OCS-APIREQUEST' => 'true',
  211. ];
  212. $search = [];
  213. foreach ($searchTable->getRows() as $row) {
  214. if (!isset($search[$row[0]])) {
  215. $search[$row[0]] = [];
  216. }
  217. $search[$row[0]][] = $row[1];
  218. }
  219. $options['form_params'] = [
  220. 'location' => $region,
  221. 'search' => $search,
  222. ];
  223. $this->response = $client->post($fullUrl, $options);
  224. }
  225. public function createUser($user) {
  226. $previous_user = $this->currentUser;
  227. $this->currentUser = 'admin';
  228. $this->creatingTheUser($user);
  229. $this->userExists($user);
  230. $this->currentUser = $previous_user;
  231. }
  232. public function deleteUser($user) {
  233. $previous_user = $this->currentUser;
  234. $this->currentUser = 'admin';
  235. $this->deletingTheUser($user);
  236. $this->userDoesNotExist($user);
  237. $this->currentUser = $previous_user;
  238. }
  239. public function createGroup($group) {
  240. $previous_user = $this->currentUser;
  241. $this->currentUser = 'admin';
  242. $this->creatingTheGroup($group);
  243. $this->groupExists($group);
  244. $this->currentUser = $previous_user;
  245. }
  246. public function deleteGroup($group) {
  247. $previous_user = $this->currentUser;
  248. $this->currentUser = 'admin';
  249. $this->deletingTheGroup($group);
  250. $this->groupDoesNotExist($group);
  251. $this->currentUser = $previous_user;
  252. }
  253. public function userExists($user) {
  254. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user";
  255. $client = new Client();
  256. $options = [];
  257. $options['auth'] = $this->adminUser;
  258. $options['headers'] = [
  259. 'OCS-APIREQUEST' => 'true'
  260. ];
  261. $this->response = $client->get($fullUrl, $options);
  262. }
  263. /**
  264. * @Then /^check that user "([^"]*)" belongs to group "([^"]*)"$/
  265. * @param string $user
  266. * @param string $group
  267. */
  268. public function checkThatUserBelongsToGroup($user, $group) {
  269. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
  270. $client = new Client();
  271. $options = [];
  272. if ($this->currentUser === 'admin') {
  273. $options['auth'] = $this->adminUser;
  274. }
  275. $options['headers'] = [
  276. 'OCS-APIREQUEST' => 'true',
  277. ];
  278. $this->response = $client->get($fullUrl, $options);
  279. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  280. sort($respondedArray);
  281. Assert::assertContains($group, $respondedArray);
  282. Assert::assertEquals(200, $this->response->getStatusCode());
  283. }
  284. public function userBelongsToGroup($user, $group) {
  285. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
  286. $client = new Client();
  287. $options = [];
  288. if ($this->currentUser === 'admin') {
  289. $options['auth'] = $this->adminUser;
  290. }
  291. $options['headers'] = [
  292. 'OCS-APIREQUEST' => 'true',
  293. ];
  294. $this->response = $client->get($fullUrl, $options);
  295. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  296. if (array_key_exists($group, $respondedArray)) {
  297. return true;
  298. } else {
  299. return false;
  300. }
  301. }
  302. /**
  303. * @Given /^user "([^"]*)" belongs to group "([^"]*)"$/
  304. * @param string $user
  305. * @param string $group
  306. */
  307. public function assureUserBelongsToGroup($user, $group) {
  308. $previous_user = $this->currentUser;
  309. $this->currentUser = 'admin';
  310. if (!$this->userBelongsToGroup($user, $group)) {
  311. $this->addingUserToGroup($user, $group);
  312. }
  313. $this->checkThatUserBelongsToGroup($user, $group);
  314. $this->currentUser = $previous_user;
  315. }
  316. /**
  317. * @Given /^user "([^"]*)" does not belong to group "([^"]*)"$/
  318. * @param string $user
  319. * @param string $group
  320. */
  321. public function userDoesNotBelongToGroup($user, $group) {
  322. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
  323. $client = new Client();
  324. $options = [];
  325. if ($this->currentUser === 'admin') {
  326. $options['auth'] = $this->adminUser;
  327. }
  328. $options['headers'] = [
  329. 'OCS-APIREQUEST' => 'true',
  330. ];
  331. $this->response = $client->get($fullUrl, $options);
  332. $groups = [$group];
  333. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  334. Assert::assertNotEqualsCanonicalizing($groups, $respondedArray);
  335. Assert::assertEquals(200, $this->response->getStatusCode());
  336. }
  337. /**
  338. * @When /^creating the group "([^"]*)"$/
  339. * @param string $group
  340. */
  341. public function creatingTheGroup($group) {
  342. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups";
  343. $client = new Client();
  344. $options = [];
  345. if ($this->currentUser === 'admin') {
  346. $options['auth'] = $this->adminUser;
  347. }
  348. $options['form_params'] = [
  349. 'groupid' => $group,
  350. ];
  351. $options['headers'] = [
  352. 'OCS-APIREQUEST' => 'true',
  353. ];
  354. $this->response = $client->post($fullUrl, $options);
  355. if ($this->currentServer === 'LOCAL') {
  356. $this->createdGroups[$group] = $group;
  357. } elseif ($this->currentServer === 'REMOTE') {
  358. $this->createdRemoteGroups[$group] = $group;
  359. }
  360. }
  361. /**
  362. * @When /^assure user "([^"]*)" is disabled$/
  363. */
  364. public function assureUserIsDisabled($user) {
  365. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/disable";
  366. $client = new Client();
  367. $options = [];
  368. if ($this->currentUser === 'admin') {
  369. $options['auth'] = $this->adminUser;
  370. }
  371. $options['headers'] = [
  372. 'OCS-APIREQUEST' => 'true',
  373. ];
  374. // TODO: fix hack
  375. $options['form_params'] = [
  376. 'foo' => 'bar'
  377. ];
  378. $this->response = $client->put($fullUrl, $options);
  379. }
  380. /**
  381. * @When /^Deleting the user "([^"]*)"$/
  382. * @param string $user
  383. */
  384. public function deletingTheUser($user) {
  385. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  386. $client = new Client();
  387. $options = [];
  388. if ($this->currentUser === 'admin') {
  389. $options['auth'] = $this->adminUser;
  390. }
  391. $options['headers'] = [
  392. 'OCS-APIREQUEST' => 'true',
  393. ];
  394. $this->response = $client->delete($fullUrl, $options);
  395. }
  396. /**
  397. * @When /^Deleting the group "([^"]*)"$/
  398. * @param string $group
  399. */
  400. public function deletingTheGroup($group) {
  401. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups/$group";
  402. $client = new Client();
  403. $options = [];
  404. if ($this->currentUser === 'admin') {
  405. $options['auth'] = $this->adminUser;
  406. }
  407. $options['headers'] = [
  408. 'OCS-APIREQUEST' => 'true',
  409. ];
  410. $this->response = $client->delete($fullUrl, $options);
  411. if ($this->currentServer === 'LOCAL') {
  412. unset($this->createdGroups[$group]);
  413. } elseif ($this->currentServer === 'REMOTE') {
  414. unset($this->createdRemoteGroups[$group]);
  415. }
  416. }
  417. /**
  418. * @Given /^Add user "([^"]*)" to the group "([^"]*)"$/
  419. * @param string $user
  420. * @param string $group
  421. */
  422. public function addUserToGroup($user, $group) {
  423. $this->userExists($user);
  424. $this->groupExists($group);
  425. $this->addingUserToGroup($user, $group);
  426. }
  427. /**
  428. * @When /^User "([^"]*)" is added to the group "([^"]*)"$/
  429. * @param string $user
  430. * @param string $group
  431. */
  432. public function addingUserToGroup($user, $group) {
  433. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/groups";
  434. $client = new Client();
  435. $options = [];
  436. if ($this->currentUser === 'admin') {
  437. $options['auth'] = $this->adminUser;
  438. }
  439. $options['headers'] = [
  440. 'OCS-APIREQUEST' => 'true',
  441. ];
  442. $options['form_params'] = [
  443. 'groupid' => $group,
  444. ];
  445. $this->response = $client->post($fullUrl, $options);
  446. }
  447. public function groupExists($group) {
  448. $fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group";
  449. $client = new Client();
  450. $options = [];
  451. $options['auth'] = $this->adminUser;
  452. $options['headers'] = [
  453. 'OCS-APIREQUEST' => 'true',
  454. ];
  455. $this->response = $client->get($fullUrl, $options);
  456. }
  457. /**
  458. * @Given /^group "([^"]*)" exists$/
  459. * @param string $group
  460. */
  461. public function assureGroupExists($group) {
  462. try {
  463. $this->groupExists($group);
  464. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  465. $previous_user = $this->currentUser;
  466. $this->currentUser = 'admin';
  467. $this->creatingTheGroup($group);
  468. $this->currentUser = $previous_user;
  469. }
  470. $this->groupExists($group);
  471. Assert::assertEquals(200, $this->response->getStatusCode());
  472. }
  473. /**
  474. * @Given /^group "([^"]*)" does not exist$/
  475. * @param string $group
  476. */
  477. public function groupDoesNotExist($group) {
  478. try {
  479. $this->groupExists($group);
  480. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  481. $this->response = $ex->getResponse();
  482. Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
  483. return;
  484. }
  485. $previous_user = $this->currentUser;
  486. $this->currentUser = 'admin';
  487. $this->deletingTheGroup($group);
  488. $this->currentUser = $previous_user;
  489. try {
  490. $this->groupExists($group);
  491. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  492. $this->response = $ex->getResponse();
  493. Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
  494. }
  495. }
  496. /**
  497. * @Given /^user "([^"]*)" is subadmin of group "([^"]*)"$/
  498. * @param string $user
  499. * @param string $group
  500. */
  501. public function userIsSubadminOfGroup($user, $group) {
  502. $fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group/subadmins";
  503. $client = new Client();
  504. $options = [];
  505. if ($this->currentUser === 'admin') {
  506. $options['auth'] = $this->adminUser;
  507. }
  508. $options['headers'] = [
  509. 'OCS-APIREQUEST' => 'true',
  510. ];
  511. $this->response = $client->get($fullUrl, $options);
  512. $respondedArray = $this->getArrayOfSubadminsResponded($this->response);
  513. sort($respondedArray);
  514. Assert::assertContains($user, $respondedArray);
  515. Assert::assertEquals(200, $this->response->getStatusCode());
  516. }
  517. /**
  518. * @Given /^Assure user "([^"]*)" is subadmin of group "([^"]*)"$/
  519. * @param string $user
  520. * @param string $group
  521. */
  522. public function assureUserIsSubadminOfGroup($user, $group) {
  523. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/subadmins";
  524. $client = new Client();
  525. $options = [];
  526. if ($this->currentUser === 'admin') {
  527. $options['auth'] = $this->adminUser;
  528. }
  529. $options['form_params'] = [
  530. 'groupid' => $group
  531. ];
  532. $options['headers'] = [
  533. 'OCS-APIREQUEST' => 'true',
  534. ];
  535. $this->response = $client->post($fullUrl, $options);
  536. Assert::assertEquals(200, $this->response->getStatusCode());
  537. }
  538. /**
  539. * @Given /^user "([^"]*)" is not a subadmin of group "([^"]*)"$/
  540. * @param string $user
  541. * @param string $group
  542. */
  543. public function userIsNotSubadminOfGroup($user, $group) {
  544. $fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group/subadmins";
  545. $client = new Client();
  546. $options = [];
  547. if ($this->currentUser === 'admin') {
  548. $options['auth'] = $this->adminUser;
  549. }
  550. $options['headers'] = [
  551. 'OCS-APIREQUEST' => 'true',
  552. ];
  553. $this->response = $client->get($fullUrl, $options);
  554. $respondedArray = $this->getArrayOfSubadminsResponded($this->response);
  555. sort($respondedArray);
  556. Assert::assertNotContains($user, $respondedArray);
  557. Assert::assertEquals(200, $this->response->getStatusCode());
  558. }
  559. /**
  560. * @Then /^users returned are$/
  561. * @param \Behat\Gherkin\Node\TableNode|null $usersList
  562. */
  563. public function theUsersShouldBe($usersList) {
  564. if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
  565. $users = $usersList->getRows();
  566. $usersSimplified = $this->simplifyArray($users);
  567. $respondedArray = $this->getArrayOfUsersResponded($this->response);
  568. Assert::assertEqualsCanonicalizing($usersSimplified, $respondedArray);
  569. }
  570. }
  571. /**
  572. * @Then /^phone matches returned are$/
  573. * @param \Behat\Gherkin\Node\TableNode|null $usersList
  574. */
  575. public function thePhoneUsersShouldBe($usersList) {
  576. if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
  577. $users = $usersList->getRowsHash();
  578. $listCheckedElements = simplexml_load_string($this->response->getBody())->data;
  579. $respondedArray = json_decode(json_encode($listCheckedElements), true);
  580. Assert::assertEquals($users, $respondedArray);
  581. }
  582. }
  583. /**
  584. * @Then /^detailed users returned are$/
  585. * @param \Behat\Gherkin\Node\TableNode|null $usersList
  586. */
  587. public function theDetailedUsersShouldBe($usersList) {
  588. if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
  589. $users = $usersList->getRows();
  590. $usersSimplified = $this->simplifyArray($users);
  591. $respondedArray = $this->getArrayOfDetailedUsersResponded($this->response);
  592. $respondedArray = array_keys($respondedArray);
  593. Assert::assertEquals($usersSimplified, $respondedArray);
  594. }
  595. }
  596. /**
  597. * @Then /^groups returned are$/
  598. * @param \Behat\Gherkin\Node\TableNode|null $groupsList
  599. */
  600. public function theGroupsShouldBe($groupsList) {
  601. if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
  602. $groups = $groupsList->getRows();
  603. $groupsSimplified = $this->simplifyArray($groups);
  604. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  605. Assert::assertEqualsCanonicalizing($groupsSimplified, $respondedArray);
  606. }
  607. }
  608. /**
  609. * @Then /^subadmin groups returned are$/
  610. * @param \Behat\Gherkin\Node\TableNode|null $groupsList
  611. */
  612. public function theSubadminGroupsShouldBe($groupsList) {
  613. if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
  614. $groups = $groupsList->getRows();
  615. $groupsSimplified = $this->simplifyArray($groups);
  616. $respondedArray = $this->getArrayOfSubadminsResponded($this->response);
  617. Assert::assertEqualsCanonicalizing($groupsSimplified, $respondedArray);
  618. }
  619. }
  620. /**
  621. * @Then /^apps returned are$/
  622. * @param \Behat\Gherkin\Node\TableNode|null $appList
  623. */
  624. public function theAppsShouldBe($appList) {
  625. if ($appList instanceof \Behat\Gherkin\Node\TableNode) {
  626. $apps = $appList->getRows();
  627. $appsSimplified = $this->simplifyArray($apps);
  628. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  629. Assert::assertEqualsCanonicalizing($appsSimplified, $respondedArray);
  630. }
  631. }
  632. /**
  633. * @Then /^subadmin users returned are$/
  634. * @param \Behat\Gherkin\Node\TableNode|null $groupsList
  635. */
  636. public function theSubadminUsersShouldBe($groupsList) {
  637. $this->theSubadminGroupsShouldBe($groupsList);
  638. }
  639. /**
  640. * Parses the xml answer to get the array of users returned.
  641. *
  642. * @param ResponseInterface $resp
  643. * @return array
  644. */
  645. public function getArrayOfUsersResponded($resp) {
  646. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->users[0]->element;
  647. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  648. return $extractedElementsArray;
  649. }
  650. /**
  651. * Parses the xml answer to get the array of detailed users returned.
  652. *
  653. * @param ResponseInterface $resp
  654. * @return array
  655. */
  656. public function getArrayOfDetailedUsersResponded($resp) {
  657. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->users;
  658. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  659. return $extractedElementsArray;
  660. }
  661. /**
  662. * Parses the xml answer to get the array of groups returned.
  663. *
  664. * @param ResponseInterface $resp
  665. * @return array
  666. */
  667. public function getArrayOfGroupsResponded($resp) {
  668. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->groups[0]->element;
  669. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  670. return $extractedElementsArray;
  671. }
  672. /**
  673. * Parses the xml answer to get the array of apps returned.
  674. *
  675. * @param ResponseInterface $resp
  676. * @return array
  677. */
  678. public function getArrayOfAppsResponded($resp) {
  679. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->apps[0]->element;
  680. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  681. return $extractedElementsArray;
  682. }
  683. /**
  684. * Parses the xml answer to get the array of subadmins returned.
  685. *
  686. * @param ResponseInterface $resp
  687. * @return array
  688. */
  689. public function getArrayOfSubadminsResponded($resp) {
  690. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->element;
  691. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  692. return $extractedElementsArray;
  693. }
  694. /**
  695. * @Given /^app "([^"]*)" is disabled$/
  696. * @param string $app
  697. */
  698. public function appIsDisabled($app) {
  699. $fullUrl = $this->baseUrl . 'v2.php/cloud/apps?filter=disabled';
  700. $client = new Client();
  701. $options = [];
  702. if ($this->currentUser === 'admin') {
  703. $options['auth'] = $this->adminUser;
  704. }
  705. $options['headers'] = [
  706. 'OCS-APIREQUEST' => 'true',
  707. ];
  708. $this->response = $client->get($fullUrl, $options);
  709. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  710. Assert::assertContains($app, $respondedArray);
  711. Assert::assertEquals(200, $this->response->getStatusCode());
  712. }
  713. /**
  714. * @Given /^app "([^"]*)" is enabled$/
  715. * @param string $app
  716. */
  717. public function appIsEnabled($app) {
  718. $fullUrl = $this->baseUrl . 'v2.php/cloud/apps?filter=enabled';
  719. $client = new Client();
  720. $options = [];
  721. if ($this->currentUser === 'admin') {
  722. $options['auth'] = $this->adminUser;
  723. }
  724. $options['headers'] = [
  725. 'OCS-APIREQUEST' => 'true',
  726. ];
  727. $this->response = $client->get($fullUrl, $options);
  728. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  729. Assert::assertContains($app, $respondedArray);
  730. Assert::assertEquals(200, $this->response->getStatusCode());
  731. }
  732. /**
  733. * @Given /^app "([^"]*)" is not enabled$/
  734. *
  735. * Checks that the app is disabled or not installed.
  736. *
  737. * @param string $app
  738. */
  739. public function appIsNotEnabled($app) {
  740. $fullUrl = $this->baseUrl . 'v2.php/cloud/apps?filter=enabled';
  741. $client = new Client();
  742. $options = [];
  743. if ($this->currentUser === 'admin') {
  744. $options['auth'] = $this->adminUser;
  745. }
  746. $options['headers'] = [
  747. 'OCS-APIREQUEST' => 'true',
  748. ];
  749. $this->response = $client->get($fullUrl, $options);
  750. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  751. Assert::assertNotContains($app, $respondedArray);
  752. Assert::assertEquals(200, $this->response->getStatusCode());
  753. }
  754. /**
  755. * @Then /^user "([^"]*)" is disabled$/
  756. * @param string $user
  757. */
  758. public function userIsDisabled($user) {
  759. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  760. $client = new Client();
  761. $options = [];
  762. if ($this->currentUser === 'admin') {
  763. $options['auth'] = $this->adminUser;
  764. }
  765. $options['headers'] = [
  766. 'OCS-APIREQUEST' => 'true',
  767. ];
  768. $this->response = $client->get($fullUrl, $options);
  769. // false in xml is empty
  770. Assert::assertTrue(empty(simplexml_load_string($this->response->getBody())->data[0]->enabled));
  771. }
  772. /**
  773. * @Then /^user "([^"]*)" is enabled$/
  774. * @param string $user
  775. */
  776. public function userIsEnabled($user) {
  777. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  778. $client = new Client();
  779. $options = [];
  780. if ($this->currentUser === 'admin') {
  781. $options['auth'] = $this->adminUser;
  782. }
  783. $options['headers'] = [
  784. 'OCS-APIREQUEST' => 'true',
  785. ];
  786. $this->response = $client->get($fullUrl, $options);
  787. // boolean to string is integer
  788. Assert::assertEquals('1', simplexml_load_string($this->response->getBody())->data[0]->enabled);
  789. }
  790. /**
  791. * @Given user :user has a quota of :quota
  792. * @param string $user
  793. * @param string $quota
  794. */
  795. public function userHasAQuotaOf($user, $quota) {
  796. $body = new \Behat\Gherkin\Node\TableNode([
  797. 0 => ['key', 'quota'],
  798. 1 => ['value', $quota],
  799. ]);
  800. // method used from BasicStructure trait
  801. $this->sendingToWith('PUT', '/cloud/users/' . $user, $body);
  802. }
  803. /**
  804. * @Given user :user has unlimited quota
  805. * @param string $user
  806. */
  807. public function userHasUnlimitedQuota($user) {
  808. $this->userHasAQuotaOf($user, 'none');
  809. }
  810. /**
  811. * Returns home path of the given user
  812. *
  813. * @param string $user
  814. */
  815. public function getUserHome($user) {
  816. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  817. $client = new Client();
  818. $options = [];
  819. $options['auth'] = $this->adminUser;
  820. $this->response = $client->get($fullUrl, $options);
  821. return simplexml_load_string($this->response->getBody())->data[0]->home;
  822. }
  823. /**
  824. * @BeforeScenario
  825. * @AfterScenario
  826. */
  827. public function cleanupUsers() {
  828. $previousServer = $this->currentServer;
  829. $this->usingServer('LOCAL');
  830. foreach ($this->createdUsers as $user) {
  831. $this->deleteUser($user);
  832. }
  833. $this->usingServer('REMOTE');
  834. foreach ($this->createdRemoteUsers as $remoteUser) {
  835. $this->deleteUser($remoteUser);
  836. }
  837. $this->usingServer($previousServer);
  838. }
  839. /**
  840. * @BeforeScenario
  841. * @AfterScenario
  842. */
  843. public function cleanupGroups() {
  844. $previousServer = $this->currentServer;
  845. $this->usingServer('LOCAL');
  846. foreach ($this->createdGroups as $group) {
  847. $this->deleteGroup($group);
  848. }
  849. $this->usingServer('REMOTE');
  850. foreach ($this->createdRemoteGroups as $remoteGroup) {
  851. $this->deleteGroup($remoteGroup);
  852. }
  853. $this->usingServer($previousServer);
  854. }
  855. /**
  856. * @Then /^user "([^"]*)" has not$/
  857. */
  858. public function userHasNotSetting($user, \Behat\Gherkin\Node\TableNode $settings) {
  859. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  860. $client = new Client();
  861. $options = [];
  862. if ($this->currentUser === 'admin') {
  863. $options['auth'] = $this->adminUser;
  864. } else {
  865. $options['auth'] = [$this->currentUser, $this->regularUser];
  866. }
  867. $options['headers'] = [
  868. 'OCS-APIREQUEST' => 'true',
  869. ];
  870. $response = $client->get($fullUrl, $options);
  871. foreach ($settings->getRows() as $setting) {
  872. $value = json_decode(json_encode(simplexml_load_string($response->getBody())->data->{$setting[0]}), 1);
  873. if (isset($value[0])) {
  874. if (in_array($setting[0], ['additional_mail', 'additional_mailScope'], true)) {
  875. $expectedValues = explode(';', $setting[1]);
  876. foreach ($expectedValues as $expected) {
  877. Assert::assertFalse(in_array($expected, $value, true));
  878. }
  879. } else {
  880. Assert::assertNotEqualsCanonicalizing($setting[1], $value[0]);
  881. }
  882. } else {
  883. Assert::assertNotEquals('', $setting[1]);
  884. }
  885. }
  886. }
  887. }