LDAPContext.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. use Behat\Behat\Context\Context;
  27. use Behat\Gherkin\Node\TableNode;
  28. use PHPUnit\Framework\Assert;
  29. class LDAPContext implements Context {
  30. use AppConfiguration,
  31. CommandLine,
  32. Sharing; // Pulls in BasicStructure
  33. protected $configID;
  34. protected $apiUrl;
  35. /** @AfterScenario */
  36. public function teardown() {
  37. if ($this->configID === null) {
  38. return;
  39. }
  40. $this->disableLDAPConfiguration(); # via occ in case of big config issues
  41. $this->asAn('admin');
  42. $this->sendingTo('DELETE', $this->apiUrl . '/' . $this->configID);
  43. }
  44. /**
  45. * @Given /^the response should contain a tag "([^"]*)"$/
  46. */
  47. public function theResponseShouldContainATag($arg1) {
  48. $configID = simplexml_load_string($this->response->getBody())->data[0]->$arg1;
  49. Assert::assertInstanceOf(SimpleXMLElement::class, $configID[0]);
  50. }
  51. /**
  52. * @Given /^creating an LDAP configuration at "([^"]*)"$/
  53. */
  54. public function creatingAnLDAPConfigurationAt($apiUrl) {
  55. $this->apiUrl = $apiUrl;
  56. $this->sendingToWith('POST', $this->apiUrl, null);
  57. $configElements = simplexml_load_string($this->response->getBody())->data[0]->configID;
  58. $this->configID = $configElements[0];
  59. }
  60. /**
  61. * @When /^deleting the LDAP configuration$/
  62. */
  63. public function deletingTheLDAPConfiguration() {
  64. $this->sendingToWith('DELETE', $this->apiUrl . '/' . $this->configID, null);
  65. }
  66. /**
  67. * @Given /^the response should contain a tag "([^"]*)" with value "([^"]*)"$/
  68. */
  69. public function theResponseShouldContainATagWithValue($tagName, $expectedValue) {
  70. $data = simplexml_load_string($this->response->getBody())->data[0]->$tagName;
  71. Assert::assertEquals($expectedValue, $data[0]);
  72. }
  73. /**
  74. * @When /^getting the LDAP configuration with showPassword "([^"]*)"$/
  75. */
  76. public function gettingTheLDAPConfigurationWithShowPassword($showPassword) {
  77. $this->sendingToWith(
  78. 'GET',
  79. $this->apiUrl . '/' . $this->configID . '?showPassword=' . $showPassword,
  80. null
  81. );
  82. }
  83. /**
  84. * @Given /^setting the LDAP configuration to$/
  85. */
  86. public function settingTheLDAPConfigurationTo(TableNode $configData) {
  87. $this->sendingToWith('PUT', $this->apiUrl . '/' . $this->configID, $configData);
  88. }
  89. /**
  90. * @Given /^having a valid LDAP configuration$/
  91. */
  92. public function havingAValidLDAPConfiguration() {
  93. $this->asAn('admin');
  94. $this->creatingAnLDAPConfigurationAt('/apps/user_ldap/api/v1/config');
  95. $data = new TableNode([
  96. ['configData[ldapHost]', 'openldap'],
  97. ['configData[ldapPort]', '389'],
  98. ['configData[ldapBase]', 'dc=nextcloud,dc=ci'],
  99. ['configData[ldapAgentName]', 'cn=admin,dc=nextcloud,dc=ci'],
  100. ['configData[ldapAgentPassword]', 'admin'],
  101. ['configData[ldapUserFilter]', '(&(objectclass=inetorgperson))'],
  102. ['configData[ldapLoginFilter]', '(&(objectclass=inetorgperson)(uid=%uid))'],
  103. ['configData[ldapUserDisplayName]', 'displayname'],
  104. ['configData[ldapGroupDisplayName]', 'cn'],
  105. ['configData[ldapEmailAttribute]', 'mail'],
  106. ['configData[ldapConfigurationActive]', '1'],
  107. ]);
  108. $this->settingTheLDAPConfigurationTo($data);
  109. $this->asAn('');
  110. }
  111. /**
  112. * @Given /^looking up details for the first result matches expectations$/
  113. * @param TableNode $expectations
  114. */
  115. public function lookingUpDetailsForTheFirstResult(TableNode $expectations) {
  116. $userResultElements = simplexml_load_string($this->response->getBody())->data[0]->users[0]->element;
  117. $userResults = json_decode(json_encode($userResultElements), 1);
  118. $userId = array_shift($userResults);
  119. $this->sendingTo('GET', '/cloud/users/' . $userId);
  120. $this->theRecordFieldsShouldMatch($expectations);
  121. }
  122. /**
  123. * @Given /^modify LDAP configuration$/
  124. */
  125. public function modifyLDAPConfiguration(TableNode $table) {
  126. $originalAsAn = $this->currentUser;
  127. $this->asAn('admin');
  128. $configData = $table->getRows();
  129. foreach ($configData as &$row) {
  130. $row[0] = 'configData[' . $row[0] . ']';
  131. }
  132. $this->settingTheLDAPConfigurationTo(new TableNode($configData));
  133. $this->asAn($originalAsAn);
  134. }
  135. /**
  136. * @Given /^the "([^"]*)" result should match$/
  137. */
  138. public function theGroupResultShouldMatch(string $type, TableNode $expectations) {
  139. $listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
  140. $extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
  141. foreach ($expectations->getRows() as $expectation) {
  142. if ((int)$expectation[1] === 1) {
  143. Assert::assertContains($expectation[0], $extractedIDsArray);
  144. } else {
  145. Assert::assertNotContains($expectation[0], $extractedIDsArray);
  146. }
  147. }
  148. }
  149. /**
  150. * @Given /^Expect ServerException on failed web login as "([^"]*)"$/
  151. */
  152. public function expectServerExceptionOnFailedWebLoginAs($login) {
  153. try {
  154. $this->loggingInUsingWebAs($login);
  155. } catch (\GuzzleHttp\Exception\ServerException $e) {
  156. Assert::assertEquals(500, $e->getResponse()->getStatusCode());
  157. return;
  158. }
  159. Assert::assertTrue(false, 'expected Exception not received');
  160. }
  161. /**
  162. * @Given /^the "([^"]*)" result should contain "([^"]*)" of$/
  163. */
  164. public function theResultShouldContainOf($type, $expectedCount, TableNode $expectations) {
  165. $listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
  166. $extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
  167. $uidsFound = 0;
  168. foreach ($expectations->getRows() as $expectation) {
  169. if (in_array($expectation[0], $extractedIDsArray)) {
  170. $uidsFound++;
  171. }
  172. }
  173. Assert::assertSame((int)$expectedCount, $uidsFound);
  174. }
  175. /**
  176. * @Given /^the record's fields should match$/
  177. */
  178. public function theRecordFieldsShouldMatch(TableNode $expectations) {
  179. foreach ($expectations->getRowsHash() as $k => $v) {
  180. $value = (string)simplexml_load_string($this->response->getBody())->data[0]->$k;
  181. Assert::assertEquals($v, $value, "got $value");
  182. }
  183. $backend = (string)simplexml_load_string($this->response->getBody())->data[0]->backend;
  184. Assert::assertEquals('LDAP', $backend);
  185. }
  186. public function disableLDAPConfiguration() {
  187. $configKey = $this->configID . 'ldap_configuration_active';
  188. $this->invokingTheCommand('config:app:set user_ldap ' . $configKey . ' --value="0"');
  189. }
  190. protected function resetAppConfigs() {
  191. // not implemented
  192. }
  193. }