Provisioning.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. <?php
  2. /**
  3. *
  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) <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. $options['auth'] = $this->adminUser;
  146. $options['headers'] = [
  147. 'OCS-APIREQUEST' => 'true',
  148. ];
  149. $response = $client->get($fullUrl, $options);
  150. foreach ($settings->getRows() as $setting) {
  151. $value = json_decode(json_encode(simplexml_load_string($response->getBody())->data->{$setting[0]}), 1);
  152. if (isset($value[0])) {
  153. Assert::assertEquals($setting[1], $value[0], "", 0.0, 10, true);
  154. } else {
  155. Assert::assertEquals('', $setting[1]);
  156. }
  157. }
  158. }
  159. /**
  160. * @Then /^search users by phone for region "([^"]*)" with$/
  161. *
  162. * @param string $user
  163. * @param \Behat\Gherkin\Node\TableNode|null $settings
  164. */
  165. public function searchUserByPhone($region, \Behat\Gherkin\Node\TableNode $searchTable) {
  166. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/search/by-phone";
  167. $client = new Client();
  168. $options = [];
  169. $options['auth'] = $this->adminUser;
  170. $options['headers'] = [
  171. 'OCS-APIREQUEST' => 'true',
  172. ];
  173. $search = [];
  174. foreach ($searchTable->getRows() as $row) {
  175. if (!isset($search[$row[0]])) {
  176. $search[$row[0]] = [];
  177. }
  178. $search[$row[0]][] = $row[1];
  179. }
  180. $options['form_params'] = [
  181. 'location' => $region,
  182. 'search' => $search,
  183. ];
  184. $this->response = $client->post($fullUrl, $options);
  185. }
  186. public function createUser($user) {
  187. $previous_user = $this->currentUser;
  188. $this->currentUser = "admin";
  189. $this->creatingTheUser($user);
  190. $this->userExists($user);
  191. $this->currentUser = $previous_user;
  192. }
  193. public function deleteUser($user) {
  194. $previous_user = $this->currentUser;
  195. $this->currentUser = "admin";
  196. $this->deletingTheUser($user);
  197. $this->userDoesNotExist($user);
  198. $this->currentUser = $previous_user;
  199. }
  200. public function createGroup($group) {
  201. $previous_user = $this->currentUser;
  202. $this->currentUser = "admin";
  203. $this->creatingTheGroup($group);
  204. $this->groupExists($group);
  205. $this->currentUser = $previous_user;
  206. }
  207. public function deleteGroup($group) {
  208. $previous_user = $this->currentUser;
  209. $this->currentUser = "admin";
  210. $this->deletingTheGroup($group);
  211. $this->groupDoesNotExist($group);
  212. $this->currentUser = $previous_user;
  213. }
  214. public function userExists($user) {
  215. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user";
  216. $client = new Client();
  217. $options = [];
  218. $options['auth'] = $this->adminUser;
  219. $options['headers'] = [
  220. 'OCS-APIREQUEST' => 'true'
  221. ];
  222. $this->response = $client->get($fullUrl, $options);
  223. }
  224. /**
  225. * @Then /^check that user "([^"]*)" belongs to group "([^"]*)"$/
  226. * @param string $user
  227. * @param string $group
  228. */
  229. public function checkThatUserBelongsToGroup($user, $group) {
  230. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
  231. $client = new Client();
  232. $options = [];
  233. if ($this->currentUser === 'admin') {
  234. $options['auth'] = $this->adminUser;
  235. }
  236. $options['headers'] = [
  237. 'OCS-APIREQUEST' => 'true',
  238. ];
  239. $this->response = $client->get($fullUrl, $options);
  240. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  241. sort($respondedArray);
  242. Assert::assertContains($group, $respondedArray);
  243. Assert::assertEquals(200, $this->response->getStatusCode());
  244. }
  245. public function userBelongsToGroup($user, $group) {
  246. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
  247. $client = new Client();
  248. $options = [];
  249. if ($this->currentUser === 'admin') {
  250. $options['auth'] = $this->adminUser;
  251. }
  252. $options['headers'] = [
  253. 'OCS-APIREQUEST' => 'true',
  254. ];
  255. $this->response = $client->get($fullUrl, $options);
  256. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  257. if (array_key_exists($group, $respondedArray)) {
  258. return true;
  259. } else {
  260. return false;
  261. }
  262. }
  263. /**
  264. * @Given /^user "([^"]*)" belongs to group "([^"]*)"$/
  265. * @param string $user
  266. * @param string $group
  267. */
  268. public function assureUserBelongsToGroup($user, $group) {
  269. $previous_user = $this->currentUser;
  270. $this->currentUser = "admin";
  271. if (!$this->userBelongsToGroup($user, $group)) {
  272. $this->addingUserToGroup($user, $group);
  273. }
  274. $this->checkThatUserBelongsToGroup($user, $group);
  275. $this->currentUser = $previous_user;
  276. }
  277. /**
  278. * @Given /^user "([^"]*)" does not belong to group "([^"]*)"$/
  279. * @param string $user
  280. * @param string $group
  281. */
  282. public function userDoesNotBelongToGroup($user, $group) {
  283. $fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
  284. $client = new Client();
  285. $options = [];
  286. if ($this->currentUser === 'admin') {
  287. $options['auth'] = $this->adminUser;
  288. }
  289. $options['headers'] = [
  290. 'OCS-APIREQUEST' => 'true',
  291. ];
  292. $this->response = $client->get($fullUrl, $options);
  293. $groups = [$group];
  294. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  295. Assert::assertNotEquals($groups, $respondedArray, "", 0.0, 10, true);
  296. Assert::assertEquals(200, $this->response->getStatusCode());
  297. }
  298. /**
  299. * @When /^creating the group "([^"]*)"$/
  300. * @param string $group
  301. */
  302. public function creatingTheGroup($group) {
  303. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups";
  304. $client = new Client();
  305. $options = [];
  306. if ($this->currentUser === 'admin') {
  307. $options['auth'] = $this->adminUser;
  308. }
  309. $options['form_params'] = [
  310. 'groupid' => $group,
  311. ];
  312. $options['headers'] = [
  313. 'OCS-APIREQUEST' => 'true',
  314. ];
  315. $this->response = $client->post($fullUrl, $options);
  316. if ($this->currentServer === 'LOCAL') {
  317. $this->createdGroups[$group] = $group;
  318. } elseif ($this->currentServer === 'REMOTE') {
  319. $this->createdRemoteGroups[$group] = $group;
  320. }
  321. }
  322. /**
  323. * @When /^assure user "([^"]*)" is disabled$/
  324. */
  325. public function assureUserIsDisabled($user) {
  326. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/disable";
  327. $client = new Client();
  328. $options = [];
  329. if ($this->currentUser === 'admin') {
  330. $options['auth'] = $this->adminUser;
  331. }
  332. $options['headers'] = [
  333. 'OCS-APIREQUEST' => 'true',
  334. ];
  335. // TODO: fix hack
  336. $options['form_params'] = [
  337. 'foo' => 'bar'
  338. ];
  339. $this->response = $client->put($fullUrl, $options);
  340. }
  341. /**
  342. * @When /^Deleting the user "([^"]*)"$/
  343. * @param string $user
  344. */
  345. public function deletingTheUser($user) {
  346. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  347. $client = new Client();
  348. $options = [];
  349. if ($this->currentUser === 'admin') {
  350. $options['auth'] = $this->adminUser;
  351. }
  352. $options['headers'] = [
  353. 'OCS-APIREQUEST' => 'true',
  354. ];
  355. $this->response = $client->delete($fullUrl, $options);
  356. }
  357. /**
  358. * @When /^Deleting the group "([^"]*)"$/
  359. * @param string $group
  360. */
  361. public function deletingTheGroup($group) {
  362. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups/$group";
  363. $client = new Client();
  364. $options = [];
  365. if ($this->currentUser === 'admin') {
  366. $options['auth'] = $this->adminUser;
  367. }
  368. $options['headers'] = [
  369. 'OCS-APIREQUEST' => 'true',
  370. ];
  371. $this->response = $client->delete($fullUrl, $options);
  372. if ($this->currentServer === 'LOCAL') {
  373. unset($this->createdGroups[$group]);
  374. } elseif ($this->currentServer === 'REMOTE') {
  375. unset($this->createdRemoteGroups[$group]);
  376. }
  377. }
  378. /**
  379. * @Given /^Add user "([^"]*)" to the group "([^"]*)"$/
  380. * @param string $user
  381. * @param string $group
  382. */
  383. public function addUserToGroup($user, $group) {
  384. $this->userExists($user);
  385. $this->groupExists($group);
  386. $this->addingUserToGroup($user, $group);
  387. }
  388. /**
  389. * @When /^User "([^"]*)" is added to the group "([^"]*)"$/
  390. * @param string $user
  391. * @param string $group
  392. */
  393. public function addingUserToGroup($user, $group) {
  394. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/groups";
  395. $client = new Client();
  396. $options = [];
  397. if ($this->currentUser === 'admin') {
  398. $options['auth'] = $this->adminUser;
  399. }
  400. $options['headers'] = [
  401. 'OCS-APIREQUEST' => 'true',
  402. ];
  403. $options['form_params'] = [
  404. 'groupid' => $group,
  405. ];
  406. $this->response = $client->post($fullUrl, $options);
  407. }
  408. public function groupExists($group) {
  409. $fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group";
  410. $client = new Client();
  411. $options = [];
  412. $options['auth'] = $this->adminUser;
  413. $options['headers'] = [
  414. 'OCS-APIREQUEST' => 'true',
  415. ];
  416. $this->response = $client->get($fullUrl, $options);
  417. }
  418. /**
  419. * @Given /^group "([^"]*)" exists$/
  420. * @param string $group
  421. */
  422. public function assureGroupExists($group) {
  423. try {
  424. $this->groupExists($group);
  425. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  426. $previous_user = $this->currentUser;
  427. $this->currentUser = "admin";
  428. $this->creatingTheGroup($group);
  429. $this->currentUser = $previous_user;
  430. }
  431. $this->groupExists($group);
  432. Assert::assertEquals(200, $this->response->getStatusCode());
  433. }
  434. /**
  435. * @Given /^group "([^"]*)" does not exist$/
  436. * @param string $group
  437. */
  438. public function groupDoesNotExist($group) {
  439. try {
  440. $this->groupExists($group);
  441. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  442. $this->response = $ex->getResponse();
  443. Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
  444. return;
  445. }
  446. $previous_user = $this->currentUser;
  447. $this->currentUser = "admin";
  448. $this->deletingTheGroup($group);
  449. $this->currentUser = $previous_user;
  450. try {
  451. $this->groupExists($group);
  452. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  453. $this->response = $ex->getResponse();
  454. Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
  455. }
  456. }
  457. /**
  458. * @Given /^user "([^"]*)" is subadmin of group "([^"]*)"$/
  459. * @param string $user
  460. * @param string $group
  461. */
  462. public function userIsSubadminOfGroup($user, $group) {
  463. $fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group/subadmins";
  464. $client = new Client();
  465. $options = [];
  466. if ($this->currentUser === 'admin') {
  467. $options['auth'] = $this->adminUser;
  468. }
  469. $options['headers'] = [
  470. 'OCS-APIREQUEST' => 'true',
  471. ];
  472. $this->response = $client->get($fullUrl, $options);
  473. $respondedArray = $this->getArrayOfSubadminsResponded($this->response);
  474. sort($respondedArray);
  475. Assert::assertContains($user, $respondedArray);
  476. Assert::assertEquals(200, $this->response->getStatusCode());
  477. }
  478. /**
  479. * @Given /^Assure user "([^"]*)" is subadmin of group "([^"]*)"$/
  480. * @param string $user
  481. * @param string $group
  482. */
  483. public function assureUserIsSubadminOfGroup($user, $group) {
  484. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/subadmins";
  485. $client = new Client();
  486. $options = [];
  487. if ($this->currentUser === 'admin') {
  488. $options['auth'] = $this->adminUser;
  489. }
  490. $options['form_params'] = [
  491. 'groupid' => $group
  492. ];
  493. $options['headers'] = [
  494. 'OCS-APIREQUEST' => 'true',
  495. ];
  496. $this->response = $client->post($fullUrl, $options);
  497. Assert::assertEquals(200, $this->response->getStatusCode());
  498. }
  499. /**
  500. * @Given /^user "([^"]*)" is not a subadmin of group "([^"]*)"$/
  501. * @param string $user
  502. * @param string $group
  503. */
  504. public function userIsNotSubadminOfGroup($user, $group) {
  505. $fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group/subadmins";
  506. $client = new Client();
  507. $options = [];
  508. if ($this->currentUser === 'admin') {
  509. $options['auth'] = $this->adminUser;
  510. }
  511. $options['headers'] = [
  512. 'OCS-APIREQUEST' => 'true',
  513. ];
  514. $this->response = $client->get($fullUrl, $options);
  515. $respondedArray = $this->getArrayOfSubadminsResponded($this->response);
  516. sort($respondedArray);
  517. Assert::assertNotContains($user, $respondedArray);
  518. Assert::assertEquals(200, $this->response->getStatusCode());
  519. }
  520. /**
  521. * @Then /^users returned are$/
  522. * @param \Behat\Gherkin\Node\TableNode|null $usersList
  523. */
  524. public function theUsersShouldBe($usersList) {
  525. if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
  526. $users = $usersList->getRows();
  527. $usersSimplified = $this->simplifyArray($users);
  528. $respondedArray = $this->getArrayOfUsersResponded($this->response);
  529. Assert::assertEquals($usersSimplified, $respondedArray, "", 0.0, 10, true);
  530. }
  531. }
  532. /**
  533. * @Then /^phone matches returned are$/
  534. * @param \Behat\Gherkin\Node\TableNode|null $usersList
  535. */
  536. public function thePhoneUsersShouldBe($usersList) {
  537. if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
  538. $users = $usersList->getRowsHash();
  539. $listCheckedElements = simplexml_load_string($this->response->getBody())->data;
  540. $respondedArray = json_decode(json_encode($listCheckedElements), true);
  541. Assert::assertEquals($users, $respondedArray);
  542. }
  543. }
  544. /**
  545. * @Then /^detailed users returned are$/
  546. * @param \Behat\Gherkin\Node\TableNode|null $usersList
  547. */
  548. public function theDetailedUsersShouldBe($usersList) {
  549. if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
  550. $users = $usersList->getRows();
  551. $usersSimplified = $this->simplifyArray($users);
  552. $respondedArray = $this->getArrayOfDetailedUsersResponded($this->response);
  553. $respondedArray = array_keys($respondedArray);
  554. Assert::assertEquals($usersSimplified, $respondedArray);
  555. }
  556. }
  557. /**
  558. * @Then /^groups returned are$/
  559. * @param \Behat\Gherkin\Node\TableNode|null $groupsList
  560. */
  561. public function theGroupsShouldBe($groupsList) {
  562. if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
  563. $groups = $groupsList->getRows();
  564. $groupsSimplified = $this->simplifyArray($groups);
  565. $respondedArray = $this->getArrayOfGroupsResponded($this->response);
  566. Assert::assertEquals($groupsSimplified, $respondedArray, "", 0.0, 10, true);
  567. }
  568. }
  569. /**
  570. * @Then /^subadmin groups returned are$/
  571. * @param \Behat\Gherkin\Node\TableNode|null $groupsList
  572. */
  573. public function theSubadminGroupsShouldBe($groupsList) {
  574. if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
  575. $groups = $groupsList->getRows();
  576. $groupsSimplified = $this->simplifyArray($groups);
  577. $respondedArray = $this->getArrayOfSubadminsResponded($this->response);
  578. Assert::assertEquals($groupsSimplified, $respondedArray, "", 0.0, 10, true);
  579. }
  580. }
  581. /**
  582. * @Then /^apps returned are$/
  583. * @param \Behat\Gherkin\Node\TableNode|null $appList
  584. */
  585. public function theAppsShouldBe($appList) {
  586. if ($appList instanceof \Behat\Gherkin\Node\TableNode) {
  587. $apps = $appList->getRows();
  588. $appsSimplified = $this->simplifyArray($apps);
  589. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  590. Assert::assertEquals($appsSimplified, $respondedArray, "", 0.0, 10, true);
  591. }
  592. }
  593. /**
  594. * @Then /^subadmin users returned are$/
  595. * @param \Behat\Gherkin\Node\TableNode|null $groupsList
  596. */
  597. public function theSubadminUsersShouldBe($groupsList) {
  598. $this->theSubadminGroupsShouldBe($groupsList);
  599. }
  600. /**
  601. * Parses the xml answer to get the array of users returned.
  602. *
  603. * @param ResponseInterface $resp
  604. * @return array
  605. */
  606. public function getArrayOfUsersResponded($resp) {
  607. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->users[0]->element;
  608. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  609. return $extractedElementsArray;
  610. }
  611. /**
  612. * Parses the xml answer to get the array of detailed users returned.
  613. *
  614. * @param ResponseInterface $resp
  615. * @return array
  616. */
  617. public function getArrayOfDetailedUsersResponded($resp) {
  618. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->users;
  619. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  620. return $extractedElementsArray;
  621. }
  622. /**
  623. * Parses the xml answer to get the array of groups returned.
  624. *
  625. * @param ResponseInterface $resp
  626. * @return array
  627. */
  628. public function getArrayOfGroupsResponded($resp) {
  629. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->groups[0]->element;
  630. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  631. return $extractedElementsArray;
  632. }
  633. /**
  634. * Parses the xml answer to get the array of apps returned.
  635. *
  636. * @param ResponseInterface $resp
  637. * @return array
  638. */
  639. public function getArrayOfAppsResponded($resp) {
  640. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->apps[0]->element;
  641. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  642. return $extractedElementsArray;
  643. }
  644. /**
  645. * Parses the xml answer to get the array of subadmins returned.
  646. *
  647. * @param ResponseInterface $resp
  648. * @return array
  649. */
  650. public function getArrayOfSubadminsResponded($resp) {
  651. $listCheckedElements = simplexml_load_string($resp->getBody())->data[0]->element;
  652. $extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
  653. return $extractedElementsArray;
  654. }
  655. /**
  656. * @Given /^app "([^"]*)" is disabled$/
  657. * @param string $app
  658. */
  659. public function appIsDisabled($app) {
  660. $fullUrl = $this->baseUrl . "v2.php/cloud/apps?filter=disabled";
  661. $client = new Client();
  662. $options = [];
  663. if ($this->currentUser === 'admin') {
  664. $options['auth'] = $this->adminUser;
  665. }
  666. $options['headers'] = [
  667. 'OCS-APIREQUEST' => 'true',
  668. ];
  669. $this->response = $client->get($fullUrl, $options);
  670. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  671. Assert::assertContains($app, $respondedArray);
  672. Assert::assertEquals(200, $this->response->getStatusCode());
  673. }
  674. /**
  675. * @Given /^app "([^"]*)" is enabled$/
  676. * @param string $app
  677. */
  678. public function appIsEnabled($app) {
  679. $fullUrl = $this->baseUrl . "v2.php/cloud/apps?filter=enabled";
  680. $client = new Client();
  681. $options = [];
  682. if ($this->currentUser === 'admin') {
  683. $options['auth'] = $this->adminUser;
  684. }
  685. $options['headers'] = [
  686. 'OCS-APIREQUEST' => 'true',
  687. ];
  688. $this->response = $client->get($fullUrl, $options);
  689. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  690. Assert::assertContains($app, $respondedArray);
  691. Assert::assertEquals(200, $this->response->getStatusCode());
  692. }
  693. /**
  694. * @Given /^app "([^"]*)" is not enabled$/
  695. *
  696. * Checks that the app is disabled or not installed.
  697. *
  698. * @param string $app
  699. */
  700. public function appIsNotEnabled($app) {
  701. $fullUrl = $this->baseUrl . "v2.php/cloud/apps?filter=enabled";
  702. $client = new Client();
  703. $options = [];
  704. if ($this->currentUser === 'admin') {
  705. $options['auth'] = $this->adminUser;
  706. }
  707. $options['headers'] = [
  708. 'OCS-APIREQUEST' => 'true',
  709. ];
  710. $this->response = $client->get($fullUrl, $options);
  711. $respondedArray = $this->getArrayOfAppsResponded($this->response);
  712. Assert::assertNotContains($app, $respondedArray);
  713. Assert::assertEquals(200, $this->response->getStatusCode());
  714. }
  715. /**
  716. * @Then /^user "([^"]*)" is disabled$/
  717. * @param string $user
  718. */
  719. public function userIsDisabled($user) {
  720. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  721. $client = new Client();
  722. $options = [];
  723. if ($this->currentUser === 'admin') {
  724. $options['auth'] = $this->adminUser;
  725. }
  726. $options['headers'] = [
  727. 'OCS-APIREQUEST' => 'true',
  728. ];
  729. $this->response = $client->get($fullUrl, $options);
  730. // false in xml is empty
  731. Assert::assertTrue(empty(simplexml_load_string($this->response->getBody())->data[0]->enabled));
  732. }
  733. /**
  734. * @Then /^user "([^"]*)" is enabled$/
  735. * @param string $user
  736. */
  737. public function userIsEnabled($user) {
  738. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  739. $client = new Client();
  740. $options = [];
  741. if ($this->currentUser === 'admin') {
  742. $options['auth'] = $this->adminUser;
  743. }
  744. $options['headers'] = [
  745. 'OCS-APIREQUEST' => 'true',
  746. ];
  747. $this->response = $client->get($fullUrl, $options);
  748. // boolean to string is integer
  749. Assert::assertEquals("1", simplexml_load_string($this->response->getBody())->data[0]->enabled);
  750. }
  751. /**
  752. * @Given user :user has a quota of :quota
  753. * @param string $user
  754. * @param string $quota
  755. */
  756. public function userHasAQuotaOf($user, $quota) {
  757. $body = new \Behat\Gherkin\Node\TableNode([
  758. 0 => ['key', 'quota'],
  759. 1 => ['value', $quota],
  760. ]);
  761. // method used from BasicStructure trait
  762. $this->sendingToWith("PUT", "/cloud/users/" . $user, $body);
  763. }
  764. /**
  765. * @Given user :user has unlimited quota
  766. * @param string $user
  767. */
  768. public function userHasUnlimitedQuota($user) {
  769. $this->userHasAQuotaOf($user, 'none');
  770. }
  771. /**
  772. * Returns home path of the given user
  773. *
  774. * @param string $user
  775. */
  776. public function getUserHome($user) {
  777. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
  778. $client = new Client();
  779. $options = [];
  780. $options['auth'] = $this->adminUser;
  781. $this->response = $client->get($fullUrl, $options);
  782. return simplexml_load_string($this->response->getBody())->data[0]->home;
  783. }
  784. /**
  785. * @BeforeScenario
  786. * @AfterScenario
  787. */
  788. public function cleanupUsers() {
  789. $previousServer = $this->currentServer;
  790. $this->usingServer('LOCAL');
  791. foreach ($this->createdUsers as $user) {
  792. $this->deleteUser($user);
  793. }
  794. $this->usingServer('REMOTE');
  795. foreach ($this->createdRemoteUsers as $remoteUser) {
  796. $this->deleteUser($remoteUser);
  797. }
  798. $this->usingServer($previousServer);
  799. }
  800. /**
  801. * @BeforeScenario
  802. * @AfterScenario
  803. */
  804. public function cleanupGroups() {
  805. $previousServer = $this->currentServer;
  806. $this->usingServer('LOCAL');
  807. foreach ($this->createdGroups as $group) {
  808. $this->deleteGroup($group);
  809. }
  810. $this->usingServer('REMOTE');
  811. foreach ($this->createdRemoteGroups as $remoteGroup) {
  812. $this->deleteGroup($remoteGroup);
  813. }
  814. $this->usingServer($previousServer);
  815. }
  816. }