LDAPContext.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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]', getenv('LDAP_HOST') ?: '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. if (str_contains($row[0], 'Host') && getenv('LDAP_HOST')) {
  131. $row[1] = str_replace('openldap', getenv('LDAP_HOST'), $row[1]);
  132. }
  133. $row[0] = 'configData[' . $row[0] . ']';
  134. }
  135. $this->settingTheLDAPConfigurationTo(new TableNode($configData));
  136. $this->asAn($originalAsAn);
  137. }
  138. /**
  139. * @Given /^the "([^"]*)" result should match$/
  140. */
  141. public function theGroupResultShouldMatch(string $type, TableNode $expectations) {
  142. $listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
  143. $extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
  144. foreach ($expectations->getRows() as $expectation) {
  145. if ((int)$expectation[1] === 1) {
  146. Assert::assertContains($expectation[0], $extractedIDsArray);
  147. } else {
  148. Assert::assertNotContains($expectation[0], $extractedIDsArray);
  149. }
  150. }
  151. }
  152. /**
  153. * @Given /^Expect ServerException on failed web login as "([^"]*)"$/
  154. */
  155. public function expectServerExceptionOnFailedWebLoginAs($login) {
  156. try {
  157. $this->loggingInUsingWebAs($login);
  158. } catch (\GuzzleHttp\Exception\ServerException $e) {
  159. Assert::assertEquals(500, $e->getResponse()->getStatusCode());
  160. return;
  161. }
  162. Assert::assertTrue(false, 'expected Exception not received');
  163. }
  164. /**
  165. * @Given /^the "([^"]*)" result should contain "([^"]*)" of$/
  166. */
  167. public function theResultShouldContainOf($type, $expectedCount, TableNode $expectations) {
  168. $listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
  169. $extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
  170. $uidsFound = 0;
  171. foreach ($expectations->getRows() as $expectation) {
  172. if (in_array($expectation[0], $extractedIDsArray)) {
  173. $uidsFound++;
  174. }
  175. }
  176. Assert::assertSame((int)$expectedCount, $uidsFound);
  177. }
  178. /**
  179. * @Given /^the record's fields should match$/
  180. */
  181. public function theRecordFieldsShouldMatch(TableNode $expectations) {
  182. foreach ($expectations->getRowsHash() as $k => $v) {
  183. $value = (string)simplexml_load_string($this->response->getBody())->data[0]->$k;
  184. Assert::assertEquals($v, $value, "got $value");
  185. }
  186. $backend = (string)simplexml_load_string($this->response->getBody())->data[0]->backend;
  187. Assert::assertEquals('LDAP', $backend);
  188. }
  189. public function disableLDAPConfiguration() {
  190. $configKey = $this->configID . 'ldap_configuration_active';
  191. $this->invokingTheCommand('config:app:set user_ldap ' . $configKey . ' --value="0"');
  192. }
  193. protected function resetAppConfigs() {
  194. // not implemented
  195. }
  196. }