Provisioning.php 29 KB

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