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\Resource; | ||||
use App\SharedFolder; | |||||
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'); | $this->deleteTestResource('test-resource@kolab.org'); | ||||
$this->deleteTestSharedFolder('test-folder@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'); | $this->deleteTestResource('test-resource@kolab.org'); | ||||
$this->deleteTestSharedFolder('test-folder@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 218 Lines • ▼ Show 20 Lines | public function testResource(): void | ||||
// Delete the resource | // Delete the resource | ||||
LDAP::deleteResource($resource); | LDAP::deleteResource($resource); | ||||
$this->assertSame(null, LDAP::getResource($resource->email)); | $this->assertSame(null, LDAP::getResource($resource->email)); | ||||
} | } | ||||
/** | /** | ||||
* Test creating/updating/deleting a shared folder record | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testSharedFolder(): void | |||||
{ | |||||
Queue::fake(); | |||||
$root_dn = \config('ldap.hosted.root_dn'); | |||||
$folder = $this->getTestSharedFolder('test-folder@kolab.org', ['type' => 'event']); | |||||
$folder->setSetting('acl', null); | |||||
// Make sure the shared folder does not exist | |||||
// LDAP::deleteSharedFolder($folder); | |||||
// Create the shared folder | |||||
LDAP::createSharedFolder($folder); | |||||
$ldap_folder = LDAP::getSharedFolder($folder->email); | |||||
$expected = [ | |||||
'cn' => 'test-folder', | |||||
'dn' => 'cn=test-folder,ou=Shared Folders,ou=kolab.org,' . $root_dn, | |||||
'mail' => $folder->email, | |||||
'objectclass' => [ | |||||
'top', | |||||
'kolabsharedfolder', | |||||
'mailrecipient', | |||||
], | |||||
'kolabfoldertype' => 'event', | |||||
'kolabtargetfolder' => 'shared/test-folder@kolab.org', | |||||
'acl' => null, | |||||
]; | |||||
foreach ($expected as $attr => $value) { | |||||
$ldap_value = isset($ldap_folder[$attr]) ? $ldap_folder[$attr] : null; | |||||
$this->assertEquals($value, $ldap_value, "Shared folder $attr attribute"); | |||||
} | |||||
// Update folder name and acl | |||||
$folder->name = 'Te(=ść)1'; | |||||
$folder->save(); | |||||
$folder->setSetting('acl', '["john@kolab.org, read-write","anyone, read-only"]'); | |||||
LDAP::updateSharedFolder($folder); | |||||
$expected['kolabtargetfolder'] = 'shared/Te(=ść)1@kolab.org'; | |||||
$expected['acl'] = ['john@kolab.org, read-write', 'anyone, read-only']; | |||||
$expected['dn'] = 'cn=Te(\\3dść)1,ou=Shared Folders,ou=kolab.org,' . $root_dn; | |||||
$expected['cn'] = 'Te(=ść)1'; | |||||
$ldap_folder = LDAP::getSharedFolder($folder->email); | |||||
foreach ($expected as $attr => $value) { | |||||
$ldap_value = isset($ldap_folder[$attr]) ? $ldap_folder[$attr] : null; | |||||
$this->assertEquals($value, $ldap_value, "Shared folder $attr attribute"); | |||||
} | |||||
// Delete the resource | |||||
LDAP::deleteSharedFolder($folder); | |||||
$this->assertSame(null, LDAP::getSharedFolder($folder->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 108 Lines • ▼ Show 20 Lines | class LDAPTest extends TestCase | ||||
public function testCreateResourceException(): void | public function testCreateResourceException(): void | ||||
{ | { | ||||
$this->expectException(\Exception::class); | $this->expectException(\Exception::class); | ||||
$this->expectExceptionMessageMatches('/Failed to create resource/'); | $this->expectExceptionMessageMatches('/Failed to create resource/'); | ||||
$resource = new Resource([ | $resource = new Resource([ | ||||
'email' => 'test-non-existing-ldap@non-existing.org', | 'email' => 'test-non-existing-ldap@non-existing.org', | ||||
'name' => 'Test', | 'name' => 'Test', | ||||
'status' => User::STATUS_ACTIVE, | 'status' => Resource::STATUS_ACTIVE, | ||||
]); | ]); | ||||
LDAP::createResource($resource); | LDAP::createResource($resource); | ||||
} | } | ||||
/** | /** | ||||
* Test handling errors on a group creation | * Test handling errors on a group creation | ||||
* | * | ||||
Show All 9 Lines | public function testCreateGroupException(): void | ||||
'email' => 'test@testldap.com', | 'email' => 'test@testldap.com', | ||||
'status' => Group::STATUS_NEW | Group::STATUS_ACTIVE, | 'status' => Group::STATUS_NEW | Group::STATUS_ACTIVE, | ||||
]); | ]); | ||||
LDAP::createGroup($group); | LDAP::createGroup($group); | ||||
} | } | ||||
/** | /** | ||||
* Test handling errors on a shared folder creation | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testCreateSharedFolderException(): void | |||||
{ | |||||
$this->expectException(\Exception::class); | |||||
$this->expectExceptionMessageMatches('/Failed to create shared folder/'); | |||||
$folder = new SharedFolder([ | |||||
'email' => 'test-non-existing-ldap@non-existing.org', | |||||
'name' => 'Test', | |||||
'status' => SharedFolder::STATUS_ACTIVE, | |||||
]); | |||||
LDAP::createSharedFolder($folder); | |||||
} | |||||
/** | |||||
* Test handling errors on user creation | * Test handling errors on user creation | ||||
* | * | ||||
* @group ldap | * @group ldap | ||||
*/ | */ | ||||
public function testCreateUserException(): void | public function testCreateUserException(): void | ||||
{ | { | ||||
$this->expectException(\Exception::class); | $this->expectException(\Exception::class); | ||||
$this->expectExceptionMessageMatches('/Failed to create user/'); | $this->expectExceptionMessageMatches('/Failed to create user/'); | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | public function testUpdateResourceException(): void | ||||
$resource = new Resource([ | $resource = new Resource([ | ||||
'email' => 'test-resource@kolab.org', | 'email' => 'test-resource@kolab.org', | ||||
]); | ]); | ||||
LDAP::updateResource($resource); | LDAP::updateResource($resource); | ||||
} | } | ||||
/** | /** | ||||
* Test handling update of a non-existing shared folder | |||||
* | |||||
* @group ldap | |||||
*/ | |||||
public function testUpdateSharedFolderException(): void | |||||
{ | |||||
$this->expectException(\Exception::class); | |||||
$this->expectExceptionMessageMatches('/folder not found/'); | |||||
$folder = new SharedFolder([ | |||||
'email' => 'test-folder-unknown@kolab.org', | |||||
]); | |||||
LDAP::updateSharedFolder($folder); | |||||
} | |||||
/** | |||||
* 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 |