LDAPContext.php 6.5 KB

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