1
0

LDAPContext.php 6.9 KB

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