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\Group; | use App\Group; | ||||
use App\Entitlement; | use App\Entitlement; | ||||
use App\Resource; | |||||
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 = []; | private $ldap_config = []; | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->ldap_config = [ | $this->ldap_config = [ | ||||
'ldap.hosts' => \config('ldap.hosts'), | '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'); | $this->deleteTestDomain('testldap.com'); | ||||
$this->deleteTestGroup('group@kolab.org'); | $this->deleteTestGroup('group@kolab.org'); | ||||
$this->deleteTestResource('test-resource@kolab.org'); | |||||
// TODO: Remove group members | // TODO: Remove group members | ||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
\config($this->ldap_config); | \config($this->ldap_config); | ||||
$this->deleteTestUser('user-ldap-test@' . \config('app.domain')); | $this->deleteTestUser('user-ldap-test@' . \config('app.domain')); | ||||
$this->deleteTestDomain('testldap.com'); | $this->deleteTestDomain('testldap.com'); | ||||
$this->deleteTestGroup('group@kolab.org'); | $this->deleteTestGroup('group@kolab.org'); | ||||
$this->deleteTestResource('test-resource@kolab.org'); | |||||
// TODO: Remove group members | // TODO: Remove group members | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test handling connection errors | * Test handling connection errors | ||||
* | * | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | public function testGroup(): void | ||||
} | } | ||||
$this->assertSame(['member3@testldap.com'], $group->fresh()->members); | $this->assertSame(['member3@testldap.com'], $group->fresh()->members); | ||||
// We called save() twice, and setSettings() three times, | // We called save() twice, and setSettings() three times, | ||||
// this is making sure that there's no job executed by the LDAP backend | // this is making sure that there's no job executed by the LDAP backend | ||||
Queue::assertPushed(\App\Jobs\Group\UpdateJob::class, 5); | Queue::assertPushed(\App\Jobs\Group\UpdateJob::class, 5); | ||||
// Delete the domain | // Delete the group | ||||
LDAP::deleteGroup($group); | LDAP::deleteGroup($group); | ||||
$this->assertSame(null, LDAP::getGroup($group->email)); | $this->assertSame(null, LDAP::getGroup($group->email)); | ||||
} | } | ||||
/** | /** | ||||
* Test creating/updating/deleting a resource record | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testResource(): void | |||||
{ | |||||
Queue::fake(); | |||||
$root_dn = \config('ldap.hosted.root_dn'); | |||||
$resource = $this->getTestResource('test-resource@kolab.org', ['name' => 'Test1']); | |||||
$resource->setSetting('invitation_policy', null); | |||||
// Make sure the resource does not exist | |||||
// LDAP::deleteResource($resource); | |||||
// Create the resource | |||||
LDAP::createResource($resource); | |||||
$ldap_resource = LDAP::getResource($resource->email); | |||||
$expected = [ | |||||
'cn' => 'Test1', | |||||
'dn' => 'cn=Test1,ou=Resources,ou=kolab.org,' . $root_dn, | |||||
'mail' => $resource->email, | |||||
'objectclass' => [ | |||||
'top', | |||||
'kolabresource', | |||||
'kolabsharedfolder', | |||||
'mailrecipient', | |||||
], | |||||
'kolabfoldertype' => 'event', | |||||
'kolabtargetfolder' => 'shared/Resources/Test1@kolab.org', | |||||
'kolabinvitationpolicy' => null, | |||||
'owner' => null, | |||||
]; | |||||
foreach ($expected as $attr => $value) { | |||||
$ldap_value = isset($ldap_resource[$attr]) ? $ldap_resource[$attr] : null; | |||||
$this->assertEquals($value, $ldap_value, "Resource $attr attribute"); | |||||
} | |||||
// Update resource name and invitation_policy | |||||
$resource->name = 'Te(=ść)1'; | |||||
$resource->save(); | |||||
$resource->setSetting('invitation_policy', 'manual:john@kolab.org'); | |||||
LDAP::updateResource($resource); | |||||
$expected['kolabtargetfolder'] = 'shared/Resources/Te(=ść)1@kolab.org'; | |||||
$expected['kolabinvitationpolicy'] = 'ACT_MANUAL'; | |||||
$expected['owner'] = 'uid=john@kolab.org,ou=People,ou=kolab.org,' . $root_dn; | |||||
$expected['dn'] = 'cn=Te(\\3dść)1,ou=Resources,ou=kolab.org,' . $root_dn; | |||||
$expected['cn'] = 'Te(=ść)1'; | |||||
$ldap_resource = LDAP::getResource($resource->email); | |||||
foreach ($expected as $attr => $value) { | |||||
$ldap_value = isset($ldap_resource[$attr]) ? $ldap_resource[$attr] : null; | |||||
$this->assertEquals($value, $ldap_value, "Resource $attr attribute"); | |||||
} | |||||
// Delete the resource | |||||
LDAP::deleteResource($resource); | |||||
$this->assertSame(null, LDAP::getResource($resource->email)); | |||||
} | |||||
/** | |||||
* 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 | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | public function testUser(): void | ||||
// 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 errors on a resource creation | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testCreateResourceException(): void | |||||
{ | |||||
$this->expectException(\Exception::class); | |||||
$this->expectExceptionMessageMatches('/Failed to create resource/'); | |||||
$resource = new Resource([ | |||||
'email' => 'test-non-existing-ldap@non-existing.org', | |||||
'name' => 'Test', | |||||
'status' => User::STATUS_ACTIVE, | |||||
]); | |||||
LDAP::createResource($resource); | |||||
} | |||||
/** | |||||
* Test handling errors on a group creation | * Test handling errors on a group creation | ||||
* | * | ||||
* @group ldap | * @group ldap | ||||
*/ | */ | ||||
public function testCreateGroupException(): void | public function testCreateGroupException(): void | ||||
{ | { | ||||
$this->expectException(\Exception::class); | $this->expectException(\Exception::class); | ||||
$this->expectExceptionMessageMatches('/Failed to create group/'); | $this->expectExceptionMessageMatches('/Failed to create group/'); | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | public function testUpdateGroupException(): void | ||||
'email' => 'test@testldap.com', | 'email' => 'test@testldap.com', | ||||
'status' => Group::STATUS_NEW | Group::STATUS_ACTIVE, | 'status' => Group::STATUS_NEW | Group::STATUS_ACTIVE, | ||||
]); | ]); | ||||
LDAP::updateGroup($group); | LDAP::updateGroup($group); | ||||
} | } | ||||
/** | /** | ||||
* Test handling update of a non-existing resource | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testUpdateResourceException(): void | |||||
{ | |||||
$this->expectException(\Exception::class); | |||||
$this->expectExceptionMessageMatches('/resource not found/'); | |||||
$resource = new Resource([ | |||||
'email' => 'test-resource@kolab.org', | |||||
]); | |||||
LDAP::updateResource($resource); | |||||
} | |||||
/** | |||||
* Test handling update of a non-existing user | * Test handling update of a non-existing user | ||||
* | * | ||||
* @group ldap | * @group ldap | ||||
*/ | */ | ||||
public function testUpdateUserException(): void | public function testUpdateUserException(): void | ||||
{ | { | ||||
$this->expectException(\Exception::class); | $this->expectException(\Exception::class); | ||||
$this->expectExceptionMessageMatches('/user not found/'); | $this->expectExceptionMessageMatches('/user not found/'); | ||||
Show All 9 Lines |