2017-01-19 12:04:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-01-19 12:04:15 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-19 12:04:15 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Behat\Behat\Context\Context;
|
2017-01-20 23:57:12 +03:00
|
|
|
use Behat\Gherkin\Node\TableNode;
|
2018-06-13 08:15:42 +03:00
|
|
|
use PHPUnit\Framework\Assert;
|
2017-01-19 12:04:15 +03:00
|
|
|
|
|
|
|
class LDAPContext implements Context {
|
2019-06-03 16:24:26 +03:00
|
|
|
use AppConfiguration,
|
|
|
|
CommandLine,
|
|
|
|
Sharing; // Pulls in BasicStructure
|
2017-01-19 12:04:15 +03:00
|
|
|
|
|
|
|
protected $configID;
|
|
|
|
|
|
|
|
protected $apiUrl;
|
|
|
|
|
2018-08-13 00:12:42 +03:00
|
|
|
/** @AfterScenario */
|
|
|
|
public function teardown() {
|
|
|
|
if($this->configID === null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-05 14:53:13 +03:00
|
|
|
$this->disableLDAPConfiguration(); # via occ in case of big config issues
|
|
|
|
$this->asAn('admin');
|
2018-08-13 00:12:42 +03:00
|
|
|
$this->sendingTo('DELETE', $this->apiUrl . '/' . $this->configID);
|
|
|
|
}
|
|
|
|
|
2017-01-19 12:04:15 +03:00
|
|
|
/**
|
|
|
|
* @Given /^the response should contain a tag "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function theResponseShouldContainATag($arg1) {
|
2018-02-09 15:19:39 +03:00
|
|
|
$configID = simplexml_load_string($this->response->getBody())->data[0]->$arg1;
|
2018-06-13 08:15:42 +03:00
|
|
|
Assert::assertInstanceOf(SimpleXMLElement::class, $configID[0]);
|
2017-01-19 12:04:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-19 13:09:04 +03:00
|
|
|
* @Given /^creating an LDAP configuration at "([^"]*)"$/
|
2017-01-19 12:04:15 +03:00
|
|
|
*/
|
2017-01-19 13:09:04 +03:00
|
|
|
public function creatingAnLDAPConfigurationAt($apiUrl) {
|
2017-01-19 12:04:15 +03:00
|
|
|
$this->apiUrl = $apiUrl;
|
|
|
|
$this->sendingToWith('POST', $this->apiUrl, null);
|
2018-02-09 15:19:39 +03:00
|
|
|
$configElements = simplexml_load_string($this->response->getBody())->data[0]->configID;
|
2017-01-19 12:04:15 +03:00
|
|
|
$this->configID = $configElements[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-19 13:09:04 +03:00
|
|
|
* @When /^deleting the LDAP configuration$/
|
2017-01-19 12:04:15 +03:00
|
|
|
*/
|
2017-01-19 13:09:04 +03:00
|
|
|
public function deletingTheLDAPConfiguration() {
|
2017-01-19 12:04:15 +03:00
|
|
|
$this->sendingToWith('DELETE', $this->apiUrl . '/' . $this->configID, null);
|
|
|
|
}
|
2017-01-19 13:09:04 +03:00
|
|
|
|
2017-01-19 17:19:20 +03:00
|
|
|
/**
|
|
|
|
* @Given /^the response should contain a tag "([^"]*)" with value "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function theResponseShouldContainATagWithValue($tagName, $expectedValue) {
|
2018-02-09 15:19:39 +03:00
|
|
|
$data = simplexml_load_string($this->response->getBody())->data[0]->$tagName;
|
2018-06-13 08:15:42 +03:00
|
|
|
Assert::assertEquals($expectedValue, $data[0]);
|
2017-01-19 17:19:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When /^getting the LDAP configuration with showPassword "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function gettingTheLDAPConfigurationWithShowPassword($showPassword) {
|
|
|
|
$this->sendingToWith(
|
|
|
|
'GET',
|
|
|
|
$this->apiUrl . '/' . $this->configID . '?showPassword=' . $showPassword,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2017-01-20 23:57:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^setting the LDAP configuration to$/
|
|
|
|
*/
|
|
|
|
public function settingTheLDAPConfigurationTo(TableNode $configData) {
|
|
|
|
$this->sendingToWith('PUT', $this->apiUrl . '/' . $this->configID, $configData);
|
|
|
|
}
|
2018-03-15 20:39:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^having a valid LDAP configuration$/
|
|
|
|
*/
|
|
|
|
public function havingAValidLDAPConfiguration() {
|
|
|
|
$this->asAn('admin');
|
|
|
|
$this->creatingAnLDAPConfigurationAt('/apps/user_ldap/api/v1/config');
|
|
|
|
$data = new TableNode([
|
|
|
|
['configData[ldapHost]', 'openldap'],
|
|
|
|
['configData[ldapPort]', '389'],
|
|
|
|
['configData[ldapBase]', 'dc=nextcloud,dc=ci'],
|
|
|
|
['configData[ldapAgentName]', 'cn=admin,dc=nextcloud,dc=ci'],
|
|
|
|
['configData[ldapAgentPassword]', 'admin'],
|
|
|
|
['configData[ldapUserFilter]', '(&(objectclass=inetorgperson))'],
|
|
|
|
['configData[ldapLoginFilter]', '(&(objectclass=inetorgperson)(uid=%uid))'],
|
|
|
|
['configData[ldapUserDisplayName]', 'displayname'],
|
2018-04-11 01:32:49 +03:00
|
|
|
['configData[ldapGroupDisplayName]', 'cn'],
|
2018-03-15 20:39:27 +03:00
|
|
|
['configData[ldapEmailAttribute]', 'mail'],
|
|
|
|
['configData[ldapConfigurationActive]', '1'],
|
|
|
|
]);
|
|
|
|
$this->settingTheLDAPConfigurationTo($data);
|
|
|
|
$this->asAn('');
|
|
|
|
}
|
2018-04-09 13:27:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^looking up details for the first result matches expectations$/
|
|
|
|
* @param TableNode $expectations
|
|
|
|
*/
|
|
|
|
public function lookingUpDetailsForTheFirstResult(TableNode $expectations) {
|
|
|
|
$userResultElements = simplexml_load_string($this->response->getBody())->data[0]->users[0]->element;
|
|
|
|
$userResults = json_decode(json_encode($userResultElements), 1);
|
|
|
|
$userId = array_shift($userResults);
|
|
|
|
|
|
|
|
$this->sendingTo('GET', '/cloud/users/' . $userId);
|
2018-05-22 19:07:25 +03:00
|
|
|
$this->theRecordFieldsShouldMatch($expectations);
|
2018-04-09 13:27:36 +03:00
|
|
|
}
|
2018-04-11 01:32:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^modify LDAP configuration$/
|
|
|
|
*/
|
|
|
|
public function modifyLDAPConfiguration(TableNode $table) {
|
|
|
|
$originalAsAn = $this->currentUser;
|
|
|
|
$this->asAn('admin');
|
|
|
|
$configData = $table->getRows();
|
|
|
|
foreach($configData as &$row) {
|
|
|
|
$row[0] = 'configData[' . $row[0] . ']';
|
|
|
|
}
|
|
|
|
$this->settingTheLDAPConfigurationTo(new TableNode($configData));
|
|
|
|
$this->asAn($originalAsAn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-12 11:57:42 +03:00
|
|
|
* @Given /^the "([^"]*)" result should match$/
|
2018-04-11 01:32:49 +03:00
|
|
|
*/
|
2018-04-12 11:57:42 +03:00
|
|
|
public function theGroupResultShouldMatch(string $type, TableNode $expectations) {
|
|
|
|
$listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
|
|
|
|
$extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
|
|
|
|
foreach($expectations->getRows() as $expectation) {
|
|
|
|
if((int)$expectation[1] === 1) {
|
2018-08-12 15:46:22 +03:00
|
|
|
Assert::assertContains($expectation[0], $extractedIDsArray);
|
2018-04-11 01:32:49 +03:00
|
|
|
} else {
|
2018-08-12 15:46:22 +03:00
|
|
|
Assert::assertNotContains($expectation[0], $extractedIDsArray);
|
2018-04-11 01:32:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 19:14:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^Expect ServerException on failed web login as "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function expectServerExceptionOnFailedWebLoginAs($login) {
|
|
|
|
try {
|
|
|
|
$this->loggingInUsingWebAs($login);
|
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) {
|
2018-08-12 15:46:22 +03:00
|
|
|
Assert::assertEquals(500, $e->getResponse()->getStatusCode());
|
2018-04-11 19:14:28 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-08-12 15:46:22 +03:00
|
|
|
Assert::assertTrue(false, 'expected Exception not received');
|
2018-04-11 19:14:28 +03:00
|
|
|
}
|
2018-04-24 13:04:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^the "([^"]*)" result should contain "([^"]*)" of$/
|
|
|
|
*/
|
|
|
|
public function theResultShouldContainOf($type, $expectedCount, TableNode $expectations) {
|
|
|
|
$listReturnedElements = simplexml_load_string($this->response->getBody())->data[0]->$type[0]->element;
|
|
|
|
$extractedIDsArray = json_decode(json_encode($listReturnedElements), 1);
|
|
|
|
$uidsFound = 0;
|
|
|
|
foreach($expectations->getRows() as $expectation) {
|
|
|
|
if(in_array($expectation[0], $extractedIDsArray)) {
|
|
|
|
$uidsFound++;
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 15:46:22 +03:00
|
|
|
Assert::assertSame((int)$expectedCount, $uidsFound);
|
2018-04-24 13:04:29 +03:00
|
|
|
}
|
2018-05-22 19:07:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^the record's fields should match$/
|
|
|
|
*/
|
|
|
|
public function theRecordFieldsShouldMatch(TableNode $expectations) {
|
|
|
|
foreach($expectations->getRowsHash() as $k => $v) {
|
|
|
|
$value = (string)simplexml_load_string($this->response->getBody())->data[0]->$k;
|
2018-08-12 23:45:22 +03:00
|
|
|
Assert::assertEquals($v, $value, "got $value");
|
2018-05-22 19:07:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$backend = (string)simplexml_load_string($this->response->getBody())->data[0]->backend;
|
2018-08-12 15:46:22 +03:00
|
|
|
Assert::assertEquals('LDAP', $backend);
|
2018-05-22 19:07:25 +03:00
|
|
|
}
|
2019-03-05 14:53:13 +03:00
|
|
|
|
|
|
|
public function disableLDAPConfiguration() {
|
|
|
|
$configKey = $this->configID . 'ldap_configuration_active';
|
|
|
|
$this->invokingTheCommand('config:app:set user_ldap ' . $configKey . ' --value="0"');
|
|
|
|
}
|
2019-06-03 16:24:26 +03:00
|
|
|
|
|
|
|
protected function resetAppConfigs() {
|
|
|
|
// not implemented
|
|
|
|
}
|
2017-01-19 12:04:15 +03:00
|
|
|
}
|