Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Backends/LDAPTest.php
<?php | <?php | ||||
namespace Tests\Feature\Backends; | namespace Tests\Feature\Backends; | ||||
use App\Backends\LDAP; | use App\Backends\LDAP; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Entitlement; | use App\Entitlement; | ||||
use App\User; | use App\User; | ||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class LDAPTest extends TestCase | class LDAPTest extends TestCase | ||||
{ | { | ||||
private $ldap_config = []; | |||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->ldap_config = [ | |||||
'ldap.hosts' => \config('ldap.hosts'), | |||||
]; | |||||
$this->deleteTestUser('user-ldap-test@' . \config('app.domain')); | $this->deleteTestUser('user-ldap-test@' . \config('app.domain')); | ||||
$this->deleteTestDomain('testldap.com'); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
\config($this->ldap_config); | |||||
$this->deleteTestUser('user-ldap-test@' . \config('app.domain')); | $this->deleteTestUser('user-ldap-test@' . \config('app.domain')); | ||||
$this->deleteTestDomain('testldap.com'); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test handling connection errors | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testConnectException(): void | |||||
{ | |||||
\config(['ldap.hosts' => 'non-existing.host']); | |||||
$this->expectException(\Exception::class); | |||||
LDAP::connect(); | |||||
} | |||||
/** | |||||
* Test creating/updating/deleting a domain record | * Test creating/updating/deleting a domain record | ||||
* | * | ||||
* @group ldap | * @group ldap | ||||
*/ | */ | ||||
public function testDomain(): void | public function testDomain(): void | ||||
{ | { | ||||
$this->markTestIncomplete(); | Queue::fake(); | ||||
$domain = $this->getTestDomain('testldap.com', [ | |||||
'type' => Domain::TYPE_EXTERNAL, | |||||
'status' => Domain::STATUS_NEW | Domain::STATUS_ACTIVE, | |||||
]); | |||||
// Create the domain | |||||
LDAP::createDomain($domain); | |||||
$ldap_domain = LDAP::getDomain($domain->namespace); | |||||
$expected = [ | |||||
'associateddomain' => $domain->namespace, | |||||
'inetdomainstatus' => $domain->status, | |||||
'objectclass' => [ | |||||
'top', | |||||
'domainrelatedobject', | |||||
'inetdomain' | |||||
], | |||||
]; | |||||
foreach ($expected as $attr => $value) { | |||||
$this->assertEquals($value, isset($ldap_domain[$attr]) ? $ldap_domain[$attr] : null); | |||||
} | |||||
// TODO: Test other attributes, aci, roles/ous | |||||
// Update the domain | |||||
$domain->status |= User::STATUS_LDAP_READY; | |||||
LDAP::updateDomain($domain); | |||||
$expected['inetdomainstatus'] = $domain->status; | |||||
$ldap_domain = LDAP::getDomain($domain->namespace); | |||||
foreach ($expected as $attr => $value) { | |||||
$this->assertEquals($value, isset($ldap_domain[$attr]) ? $ldap_domain[$attr] : null); | |||||
} | |||||
// Delete the domain | |||||
LDAP::deleteDomain($domain); | |||||
$this->assertSame(null, LDAP::getDomain($domain->namespace)); | |||||
} | } | ||||
/** | /** | ||||
* Test creating/editing/deleting a user record | * Test creating/editing/deleting a user record | ||||
* | * | ||||
* @group ldap | * @group ldap | ||||
*/ | */ | ||||
public function testUser(): void | public function testUser(): void | ||||
▲ Show 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | public function testUser(): void | ||||
$this->assertSame($expected_roles, $ldap_roles); | $this->assertSame($expected_roles, $ldap_roles); | ||||
// Delete the user | // Delete the user | ||||
LDAP::deleteUser($user); | LDAP::deleteUser($user); | ||||
$this->assertSame(null, LDAP::getUser($user->email)); | $this->assertSame(null, LDAP::getUser($user->email)); | ||||
} | } | ||||
/** | |||||
* Test handling update of a non-existing domain | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testUpdateDomainException(): void | |||||
{ | |||||
$this->expectException(\Exception::class); | |||||
$this->expectExceptionMessageMatches('/domain not found/'); | |||||
$domain = new Domain([ | |||||
'namespace' => 'testldap.com', | |||||
'type' => Domain::TYPE_EXTERNAL, | |||||
'status' => Domain::STATUS_NEW | Domain::STATUS_ACTIVE, | |||||
]); | |||||
LDAP::updateDomain($domain); | |||||
} | |||||
/** | |||||
* Test handling update of a non-existing user | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testUpdateUserException(): void | |||||
{ | |||||
$this->expectException(\Exception::class); | |||||
$this->expectExceptionMessageMatches('/user not found/'); | |||||
$user = new User([ | |||||
'email' => 'test-non-existing-ldap@kolab.org', | |||||
'status' => User::STATUS_ACTIVE, | |||||
]); | |||||
LDAP::updateUser($user); | |||||
} | |||||
} | } |