Skip to content

Commit

Permalink
Fix inconsistencies, increase test coverage, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
amsprost committed Oct 4, 2019
1 parent 4bd287b commit 887c1dc
Show file tree
Hide file tree
Showing 11 changed files with 536 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
List of adapters:
* [GitHub](https://github.com/) (WIP)
* [Camunda](https://docs.camunda.org/manual/7.9/)
* LDAP (Planned)
* [LDAP](https://ldap.com/)
* [MatterMost](https://mattermost.com/) (Planned)

## Installation
Expand Down
10 changes: 7 additions & 3 deletions src/Services/Ldap/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,22 @@ public function remove(string $dn): bool

public function generateDn(array $rdn = []): string
{
$dc = '';
$dn = '';

foreach (array_merge($rdn, $this->target->getDomain()) as $key => $domainComponent) {
$dnKey = is_string($key) ? $key : 'dc';

$domainComponent = is_array($domainComponent) ? $domainComponent : [$domainComponent];

foreach ($domainComponent as $domainComponentElement) {
$dc .= sprintf('%s=%s,', $dnKey, $domainComponentElement);
$dn .= sprintf('%s=%s,', $dnKey, $domainComponentElement);
}
}

return substr($dc, 0, -1);
if (empty($dn)) {
return $dn;
}

return substr($dn, 0, -1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use LinkORB\OrgSync\Services\Ldap\LdapAssertionAwareTrait;
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;

final class LdapSetPasswordAdapter implements SetPasswordInterface
class LdapSetPasswordAdapter implements SetPasswordInterface
{
use LdapAssertionAwareTrait;

Expand All @@ -30,7 +30,7 @@ public function setPassword(User $user): SetPasswordInterface
$userSearchFirstDn = $this->client->getDn(
$this->client->first(
$this->client->search(
sprintf('(cn=%s)', $user->getUsername()),
sprintf('(uid=%s)', $user->getUsername()),
['ou' => LdapUserPushAdapter::USERS_ORG_UNIT]
)
)
Expand All @@ -46,7 +46,7 @@ public function setPassword(User $user): SetPasswordInterface
return $this;
}

private function encodePassword(string $password): string
protected function encodePassword(string $password): string
{
$salt = substr(str_shuffle(
str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 4)
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/DTO/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ public function getDtoClassName(): string
{
return User::class;
}

public function testSetPassword()
{
$somePass = '0000000';

$dto = (new User('user1', 'differentPass'))->setPassword($somePass);

$this->assertSame($dto->getPassword(), $somePass);
}

public function testSetPreviousPassword()
{
$somePass = '0000000';

$dto = (new User('user1', 'differentPass'))->setPreviousPassword($somePass);

$this->assertSame($dto->getProperties()[User::PREVIOUS_PASSWORD], $somePass);
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Services/Ldap/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);

namespace LinkORB\OrgSync\Tests\Unit\Services\Ldap;

use LinkORB\OrgSync\DTO\Target\Ldap;
use LinkORB\OrgSync\Services\Ldap\Client;
use PHPUnit\Framework\TestCase;

class ClientTest extends TestCase
{
/**
* @dataProvider getGenerateDnData
*/
public function testGenerateDn(array $domain, array $rdn, string $expectedDn)
{
$ldap = new Ldap('', '', '', '', $domain);

$client = $this->createPartialMock(Client::class, ['__destruct']);
$client->__construct($ldap);

$this->assertEquals($expectedDn, $client->generateDn($rdn));
}

/**
* @return array
*/
public function getGenerateDnData(): array
{
return [
[
['internal', 'com'],
['cn' => ['test', 'somewhere'], 'ou' => 'org'],
'cn=test,cn=somewhere,ou=org,dc=internal,dc=com'
],
[
['com', 'internal'],
['cn' => ['somewhere', 'test']],
'cn=somewhere,cn=test,dc=com,dc=internal'
],
[
[],
['uid' => 111222,'ou' => 'org'],
'uid=111222,ou=org'
],
[
['abc', 'xyz'],
[],
'dc=abc,dc=xyz'
],
[
[],
[],
''
],
];
}
}
68 changes: 68 additions & 0 deletions tests/Unit/Services/Ldap/LdapParentHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare(strict_types=1);

namespace LinkORB\OrgSync\Tests\Unit\Services\Ldap;

use LinkORB\OrgSync\DTO\Group;
use LinkORB\OrgSync\Services\Ldap\LdapParentHelper;
use PHPUnit\Framework\TestCase;

class LdapParentHelperTest extends TestCase
{
/** @var LdapParentHelper */
private $parentHelper;

protected function setUp(): void
{
$this->parentHelper = new LdapParentHelper();
}

/**
* @dataProvider getParentData
*/
public function testGetParentGroups(Group $group, array $expectedParents, array $initialParents = [])
{
$this->assertEquals($expectedParents, $this->parentHelper->getParentGroups($initialParents, $group));
}

public function testGetParentsCircularReference()
{
$this->expectExceptionMessage('Circular reference detected');
$this->expectException(\UnexpectedValueException::class);

$group1 = new Group('test1', '');
$group2 = new Group('test2', '');
$group1->setParent($group2);
$group2->setParent($group1);

$this->parentHelper->getParentGroups([], $group1);
}

public function getParentData(): array
{
return [
[
new Group('hello', '', '', new Group('to', '', '', new Group('all', '', '', new Group('world', '')))),
['to', 'all', 'world'],
],
[
new Group('empty', ''),
[],
],
[
new Group('empty', ''),
['initial', 'set'],
['initial', 'set']
],
[
new Group('Earth', '', '', new Group('Eurasia', '', '', new Group('Europe', '',))),
['space', 'MilkyWay', 'Eurasia', 'Europe'],
['space', 'MilkyWay']
],
[
new Group('Earth', ''),
[],
[]
],
];
}
}
63 changes: 63 additions & 0 deletions tests/Unit/Services/Ldap/UserDataMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

namespace LinkORB\OrgSync\Tests\Unit\Services\Ldap;

use LinkORB\OrgSync\DTO\User;
use LinkORB\OrgSync\Services\Ldap\UserDataMapper;
use PHPUnit\Framework\TestCase;

class UserDataMapperTest extends TestCase
{
/** @var UserDataMapper */
private $mapper;

protected function setUp(): void
{
$this->mapper = new UserDataMapper();
}

/**
* @dataProvider getMapData
*/
public function testMap(User $user, array $expectedUserData)
{
$this->assertEquals($expectedUserData, $this->mapper->map($user));
}

public function getMapData(): array
{
return [
[
new User(''),
[
'cn' => '',
'uid' => '',
'sn' => '',
'objectClass' => ['inetOrgPerson', 'organizationalPerson', 'person', 'top']
],
],
[
new User('uadmin', 'p@ss', '[email protected]', 'superadm', '1.jpg', ['lastName' => 'Jong']),
[
'cn' => 'uadmin',
'uid' => 'uadmin',
'sn' => 'Jong',
'objectClass' => ['inetOrgPerson', 'organizationalPerson', 'person', 'top'],
'mail' => '[email protected]',
'displayName' => 'superadm',
'Photo' => '1.jpg',
],
],
[
new User('uadmin', 'p@ss', '', 'superadm'),
[
'cn' => 'uadmin',
'uid' => 'uadmin',
'sn' => 'superadm',
'objectClass' => ['inetOrgPerson', 'organizationalPerson', 'person', 'top'],
'displayName' => 'superadm',
],
],
];
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Services/SyncRemover/LdapSyncRemoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;

class LdapSyncRemoverTest extends TestCase
{
Expand Down Expand Up @@ -124,6 +125,28 @@ public function testRemoveNonExistsGroups(array $orgGroups, array $existingGroup
$this->assertNull($this->syncRemover->removeNonExists($organization));
}

public function testRemoveException()
{
$this->client
->expects($this->exactly(2))
->method('search')
->withConsecutive(
[sprintf('(ou=%s)', LdapUserPushAdapter::USERS_ORG_UNIT)],
[sprintf('(ou=%s)', LdapGroupPushAdapter::GROUPS_ORG_UNIT)]
)
->willReturnOnConsecutiveCalls(null, null);
$this->client
->expects($this->exactly(2))
->method('count')
->withConsecutive([null], [null])
->willReturnOnConsecutiveCalls(null, null);

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Error during search!');

$this->syncRemover->removeNonExists(new Organization('test'));
}

public function getRemoveUsersData(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use LinkORB\OrgSync\Services\Ldap\Client;
use LinkORB\OrgSync\Services\SyncRemover\LdapSyncRemover;
use LinkORB\OrgSync\SynchronizationAdapter\AdapterFactory\LdapAdapterFactory;
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\LdapGroupPushAdapter;
use LinkORB\OrgSync\SynchronizationAdapter\SetPassword\LdapSetPasswordAdapter;
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -35,6 +37,16 @@ public function testCreateUserPushAdapter()
$this->assertInstanceOf(LdapUserPushAdapter::class, $this->factory->createUserPushAdapter());
}

public function testCreateGroupPushAdapter()
{
$this->assertInstanceOf(LdapGroupPushAdapter::class, $this->factory->createGroupPushAdapter());
}

public function testCreateSetPasswordAdapter()
{
$this->assertInstanceOf(LdapSetPasswordAdapter::class, $this->factory->createSetPasswordAdapter());
}

/**
* @dataProvider getSupportsData
*/
Expand Down
Loading

0 comments on commit 887c1dc

Please sign in to comment.