Provisioning.php 29 KB

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