LDAPContext.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. use Behat\Behat\Context\Context;
  7. use Behat\Gherkin\Node\TableNode;
  8. use PHPUnit\Framework\Assert;
  9. class LDAPContext implements Context {
  10. use AppConfiguration,
  11. CommandLine,
  12. Sharing; // Pulls in BasicStructure
  13. protected $configID;
  14. protected $apiUrl;
  15. /** @AfterScenario */
  16. public function teardown() {
  17. if ($this->configID === null) {
  18. return;
  19. }
  20. $this->disableLDAPConfiguration(); # via occ in case of big config issues
  21. $this->asAn('admin');
  22. $this->sendingTo('DELETE', $this->apiUrl . '/' . $this->configID);
  23. }
  24. /**
  25. * @Given /^the response should contain a tag "([^"]*)"$/
  26. */
  27. public function theResponseShouldContainATag($arg1) {
  28. $configID = simplexml_load_string($this->response->getBody())->data[0]->$arg1;
  29. Assert::assertInstanceOf(SimpleXMLElement::class, $configID[0]);
  30. }
  31. /**
  32. * @Given /^creating an LDAP configuration at "([^"]*)"$/
  33. */
  34. public function creatingAnLDAPConfigurationAt($apiUrl) {
  35. $this->apiUrl = $apiUrl;
  36. $this->sendingToWith('POST', $this->apiUrl, null);
  37. $configElements = simplexml_load_string($this->response->getBody())->data[0]->configID;
  38. $this->configID = $configElements[0];
  39. }
  40. /**
  41. * @When /^deleting the LDAP configuration$/
  42. */
  43. public function deletingTheLDAPConfiguration() {
  44. $this->sendingToWith('DELETE', $this->apiUrl . '/' . $this->configID, null);
  45. }
  46. /**
  47. * @Given /^the response should contain a tag "([^"]*)" with value "([^"]*)"$/
  48. */
  49. public function theResponseShouldContainATagWithValue($tagName, $expectedValue) {
  50. $data = simplexml_load_string($this->response->getBody())->data[0]->$tagName;
  51. Assert::assertEquals($expectedValue, $data[0]);
  52. }
  53. /**
  54. * @When /^getting the LDAP configuration with showPassword "([^"]*)"$/
  55. */
  56. public function gettingTheLDAPConfigurationWithShowPassword($showPassword) {
  57. $this->sendingToWith(
  58. 'GET',
  59. $this->apiUrl . '/' . $this->configID . '?showPassword=' . $showPassword,
  60. null
  61. );
  62. }
  63. /**
  64. * @Given /^setting the LDAP configuration to$/
  65. */
  66. public function settingTheLDAPConfigurationTo(TableNode $configData) {
  67. $this->sendingToWith('PUT', $this->apiUrl . '/' . $this->configID, $configData);
  68. }
  69. /**
  70. * @Given /^having a valid LDAP configuration$/
  71. */
  72. public function havingAValidLDAPConfiguration() {
  73. $this->asAn('admin');
  74. $this->creatingAnLDAPConfigurationAt('/apps/user_ldap/api/v1/config');
  75. $data = new TableNode([
  76. ['configData[ldapHost]', getenv('LDAP_HOST') ?: 'openldap'],
  77. ['configData[ldapPort]', '389'],
  78. ['configData[ldapBase]', 'dc=nextcloud,dc=ci'],
  79. ['configData[ldapAgentName]', 'cn=admin,dc=nextcloud,dc=ci'],
  80. ['configData[ldapAgentPassword]', 'admin'],
  81. ['configData[ldapUserFilter]', '(&(objectclass=inetorgperson))'],
  82. ['configData[ldapLoginFilter]', '(&(objectclass=inetorgperson)(uid=%uid))'],
  83. ['configData[ldapUserDisplayName]', 'displayname'],
  84. ['configData[ldapGroupDisplayName]', 'cn'],
  85. ['configData[ldapEmailAttribute]', 'mail'],
  86. ['configData[ldapConfigurationActive]', '1'],
  87. ]);
  88. $this->settingTheLDAPConfigurationTo($data);
  89. $this->asAn('');
  90. }
  91. /**
  92. * @Given /^looking up details for the first result matches expectations$/
  93. * @param TableNode $expectations
  94. */
  95. public function lookingUpDetailsForTheFirstResult(TableNode $expectations) {
  96. $userResultElements = simplexml_load_string($this->response->getBody())->data[0]->users[0]->element;
  97. $userResults = json_decode(json_encode($userResultElements), 1);
  98. $userId = array_shift($userResults);
  99. $this->sendingTo('GET', '/cloud/users/' . $userId);
  100. $this->theRecordFieldsShouldMatch($expectations);
  101. }
  102. /**
  103. * @Given /^modify LDAP configuration$/
  104. */
  105. public function modifyLDAPConfiguration(TableNode $table) {
  106. $originalAsAn = $this->currentUser;
  107. $this->asAn('admin');
  108. $configData = $table->getRows();
  109. foreach ($configData as &$row) {
  110. if (str_contains($row[0], 'Host') && getenv('LDAP_HOST')) {
  111. $row[1] = str_replace('openldap', getenv('LDAP_HOST'), $row[1]);
  112. }
  113. $row[0] = 'configData[' . $row[0] . ']';
  114. }
  115. $this->settingTheLDAPConfigurationTo(new TableNode($configData));
  116. $this->asAn($originalAsAn);
  117. }
  118. /**
  119. * @Given /^the "([^"]*)" result should match$/
  120. */
  121. public function theGroupResultShouldMatch(string $type, TableNode $expectations) {
  122. $listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
  123. $extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
  124. foreach ($expectations->getRows() as $expectation) {
  125. if ((int)$expectation[1] === 1) {
  126. Assert::assertContains($expectation[0], $extractedIDsArray);
  127. } else {
  128. Assert::assertNotContains($expectation[0], $extractedIDsArray);
  129. }
  130. }
  131. }
  132. /**
  133. * @Given /^Expect ServerException on failed web login as "([^"]*)"$/
  134. */
  135. public function expectServerExceptionOnFailedWebLoginAs($login) {
  136. try {
  137. $this->loggingInUsingWebAs($login);
  138. } catch (\GuzzleHttp\Exception\ServerException $e) {
  139. Assert::assertEquals(500, $e->getResponse()->getStatusCode());
  140. return;
  141. }
  142. Assert::assertTrue(false, 'expected Exception not received');
  143. }
  144. /**
  145. * @Given /^the "([^"]*)" result should contain "([^"]*)" of$/
  146. */
  147. public function theResultShouldContainOf($type, $expectedCount, TableNode $expectations) {
  148. $listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
  149. $extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
  150. $uidsFound = 0;
  151. foreach ($expectations->getRows() as $expectation) {
  152. if (in_array($expectation[0], $extractedIDsArray)) {
  153. $uidsFound++;
  154. }
  155. }
  156. Assert::assertSame((int)$expectedCount, $uidsFound);
  157. }
  158. /**
  159. * @Given /^the record's fields should match$/
  160. */
  161. public function theRecordFieldsShouldMatch(TableNode $expectations) {
  162. foreach ($expectations->getRowsHash() as $k => $v) {
  163. $value = (string)simplexml_load_string($this->response->getBody())->data[0]->$k;
  164. Assert::assertEquals($v, $value, "got $value");
  165. }
  166. $backend = (string)simplexml_load_string($this->response->getBody())->data[0]->backend;
  167. Assert::assertEquals('LDAP', $backend);
  168. }
  169. public function disableLDAPConfiguration() {
  170. $configKey = $this->configID . 'ldap_configuration_active';
  171. $this->invokingTheCommand('config:app:set user_ldap ' . $configKey . ' --value="0"');
  172. }
  173. protected function resetAppConfigs() {
  174. // not implemented
  175. }
  176. }