summaryrefslogtreecommitdiff
path: root/tests/units/Core/Ldap
diff options
context:
space:
mode:
Diffstat (limited to 'tests/units/Core/Ldap')
-rw-r--r--tests/units/Core/Ldap/ClientTest.php53
-rw-r--r--tests/units/Core/Ldap/EntriesTest.php55
-rw-r--r--tests/units/Core/Ldap/EntryTest.php71
-rw-r--r--tests/units/Core/Ldap/LdapGroupTest.php160
-rw-r--r--tests/units/Core/Ldap/LdapUserTest.php379
-rw-r--r--tests/units/Core/Ldap/QueryTest.php45
-rw-r--r--tests/units/Core/Ldap/UserTest.php95
7 files changed, 738 insertions, 120 deletions
diff --git a/tests/units/Core/Ldap/ClientTest.php b/tests/units/Core/Ldap/ClientTest.php
index 7b6e983d..d149500e 100644
--- a/tests/units/Core/Ldap/ClientTest.php
+++ b/tests/units/Core/Ldap/ClientTest.php
@@ -49,6 +49,13 @@ class ClientTest extends \Base
self::$functions = null;
}
+ public function testGetLdapServerNotConfigured()
+ {
+ $this->setExpectedException('\LogicException');
+ $ldap = new Client;
+ $ldap->getLdapServer();
+ }
+
public function testConnectSuccess()
{
self::$functions
@@ -61,7 +68,8 @@ class ClientTest extends \Base
->will($this->returnValue('my_ldap_resource'));
$ldap = new Client;
- $this->assertEquals('my_ldap_resource', $ldap->getConnection('my_ldap_server'));
+ $ldap->open('my_ldap_server');
+ $this->assertEquals('my_ldap_resource', $ldap->getConnection());
}
public function testConnectFailure()
@@ -78,7 +86,8 @@ class ClientTest extends \Base
$this->setExpectedException('\Kanboard\Core\Ldap\ClientException');
$ldap = new Client;
- $this->assertNotEquals('my_ldap_resource', $ldap->getConnection('my_ldap_server'));
+ $ldap->open('my_ldap_server');
+ $this->assertNotEquals('my_ldap_resource', $ldap->getConnection());
}
public function testConnectSuccessWithTLS()
@@ -101,7 +110,8 @@ class ClientTest extends \Base
->will($this->returnValue(true));
$ldap = new Client;
- $this->assertEquals('my_ldap_resource', $ldap->getConnection('my_ldap_server', 389, true));
+ $ldap->open('my_ldap_server', 389, true);
+ $this->assertEquals('my_ldap_resource', $ldap->getConnection());
}
public function testConnectFailureWithTLS()
@@ -126,7 +136,8 @@ class ClientTest extends \Base
$this->setExpectedException('\Kanboard\Core\Ldap\ClientException');
$ldap = new Client;
- $this->assertNotEquals('my_ldap_resource', $ldap->getConnection('my_ldap_server', 389, true));
+ $ldap->open('my_ldap_server', 389, true);
+ $this->assertNotEquals('my_ldap_resource', $ldap->getConnection());
}
public function testAnonymousAuthenticationSuccess()
@@ -134,13 +145,10 @@ class ClientTest extends \Base
self::$functions
->expects($this->once())
->method('ldap_bind')
- ->with(
- $this->equalTo('my_ldap_resource')
- )
->will($this->returnValue(true));
$ldap = new Client;
- $this->assertTrue($ldap->useAnonymousAuthentication('my_ldap_resource'));
+ $this->assertTrue($ldap->useAnonymousAuthentication());
}
public function testAnonymousAuthenticationFailure()
@@ -148,21 +156,27 @@ class ClientTest extends \Base
self::$functions
->expects($this->once())
->method('ldap_bind')
- ->with(
- $this->equalTo('my_ldap_resource')
- )
->will($this->returnValue(false));
$this->setExpectedException('\Kanboard\Core\Ldap\ClientException');
$ldap = new Client;
- $ldap->useAnonymousAuthentication('my_ldap_resource');
+ $ldap->useAnonymousAuthentication();
}
public function testUserAuthenticationSuccess()
{
self::$functions
->expects($this->once())
+ ->method('ldap_connect')
+ ->with(
+ $this->equalTo('my_ldap_server'),
+ $this->equalTo(389)
+ )
+ ->will($this->returnValue('my_ldap_resource'));
+
+ self::$functions
+ ->expects($this->once())
->method('ldap_bind')
->with(
$this->equalTo('my_ldap_resource'),
@@ -172,13 +186,23 @@ class ClientTest extends \Base
->will($this->returnValue(true));
$ldap = new Client;
- $this->assertTrue($ldap->authenticate('my_ldap_resource', 'my_ldap_user', 'my_ldap_password'));
+ $ldap->open('my_ldap_server');
+ $this->assertTrue($ldap->authenticate('my_ldap_user', 'my_ldap_password'));
}
public function testUserAuthenticationFailure()
{
self::$functions
->expects($this->once())
+ ->method('ldap_connect')
+ ->with(
+ $this->equalTo('my_ldap_server'),
+ $this->equalTo(389)
+ )
+ ->will($this->returnValue('my_ldap_resource'));
+
+ self::$functions
+ ->expects($this->once())
->method('ldap_bind')
->with(
$this->equalTo('my_ldap_resource'),
@@ -190,6 +214,7 @@ class ClientTest extends \Base
$this->setExpectedException('\Kanboard\Core\Ldap\ClientException');
$ldap = new Client;
- $ldap->authenticate('my_ldap_resource', 'my_ldap_user', 'my_ldap_password');
+ $ldap->open('my_ldap_server');
+ $ldap->authenticate('my_ldap_user', 'my_ldap_password');
}
}
diff --git a/tests/units/Core/Ldap/EntriesTest.php b/tests/units/Core/Ldap/EntriesTest.php
new file mode 100644
index 00000000..65025b6e
--- /dev/null
+++ b/tests/units/Core/Ldap/EntriesTest.php
@@ -0,0 +1,55 @@
+<?php
+
+require_once __DIR__.'/../../Base.php';
+
+use Kanboard\Core\Ldap\Entries;
+
+class EntriesTest extends Base
+{
+ private $entries = array(
+ 'count' => 2,
+ 0 => array(
+ 'cn' => array(
+ 'count' => 1,
+ 0 => 'Kanboard Other Group',
+ ),
+ 0 => 'cn',
+ 'count' => 1,
+ 'dn' => 'CN=Kanboard Other Group,CN=Users,DC=kanboard,DC=local',
+ ),
+ 1 => array(
+ 'cn' => array(
+ 'count' => 1,
+ 0 => 'Kanboard Users',
+ ),
+ 0 => 'cn',
+ 'count' => 1,
+ 'dn' => 'CN=Kanboard Users,CN=Users,DC=kanboard,DC=local',
+ ),
+ );
+
+ public function testGetAll()
+ {
+ $entries = new Entries(array());
+ $this->assertEmpty($entries->getAll());
+
+ $entries = new Entries($this->entries);
+ $result = $entries->getAll();
+ $this->assertCount(2, $result);
+ $this->assertInstanceOf('Kanboard\Core\Ldap\Entry', $result[0]);
+ $this->assertEquals('CN=Kanboard Users,CN=Users,DC=kanboard,DC=local', $result[1]->getDn());
+ $this->assertEquals('Kanboard Users', $result[1]->getFirstValue('cn'));
+ }
+
+ public function testGetFirst()
+ {
+ $entries = new Entries(array());
+ $this->assertEquals('', $entries->getFirstEntry()->getDn());
+
+ $entries = new Entries($this->entries);
+ $result = $entries->getFirstEntry();
+ $this->assertInstanceOf('Kanboard\Core\Ldap\Entry', $result);
+ $this->assertEquals('CN=Kanboard Other Group,CN=Users,DC=kanboard,DC=local', $result->getDn());
+ $this->assertEquals('Kanboard Other Group', $result->getFirstValue('cn'));
+ }
+}
diff --git a/tests/units/Core/Ldap/EntryTest.php b/tests/units/Core/Ldap/EntryTest.php
new file mode 100644
index 00000000..45585e77
--- /dev/null
+++ b/tests/units/Core/Ldap/EntryTest.php
@@ -0,0 +1,71 @@
+<?php
+
+require_once __DIR__.'/../../Base.php';
+
+use Kanboard\Core\Ldap\Entry;
+
+class EntryTest extends Base
+{
+ private $entry = array(
+ 'count' => 2,
+ 'dn' => 'uid=my_user,ou=People,dc=kanboard,dc=local',
+ 'displayname' => array(
+ 'count' => 1,
+ 0 => 'My LDAP user',
+ ),
+ 'broken' => array(
+ ),
+ 'mail' => array(
+ 'count' => 2,
+ 0 => 'user1@localhost',
+ 1 => 'user2@localhost',
+ ),
+ 'samaccountname' => array(
+ 'count' => 1,
+ 0 => 'my_ldap_user',
+ ),
+ 0 => 'displayname',
+ 1 => 'mail',
+ 2 => 'samaccountname',
+ );
+
+ public function testGetAll()
+ {
+ $expected = array(
+ 'user1@localhost',
+ 'user2@localhost',
+ );
+
+ $entry = new Entry($this->entry);
+ $this->assertEquals($expected, $entry->getAll('mail'));
+ $this->assertEmpty($entry->getAll('not found'));
+ $this->assertEmpty($entry->getAll('broken'));
+ }
+
+ public function testGetFirst()
+ {
+ $entry = new Entry($this->entry);
+ $this->assertEquals('user1@localhost', $entry->getFirstValue('mail'));
+ $this->assertEquals('', $entry->getFirstValue('not found'));
+ $this->assertEquals('default', $entry->getFirstValue('not found', 'default'));
+ $this->assertEquals('default', $entry->getFirstValue('broken', 'default'));
+ }
+
+ public function testGetDn()
+ {
+ $entry = new Entry($this->entry);
+ $this->assertEquals('uid=my_user,ou=People,dc=kanboard,dc=local', $entry->getDn());
+
+ $entry = new Entry(array());
+ $this->assertEquals('', $entry->getDn());
+ }
+
+ public function testHasValue()
+ {
+ $entry = new Entry($this->entry);
+ $this->assertTrue($entry->hasValue('mail', 'user2@localhost'));
+ $this->assertFalse($entry->hasValue('mail', 'user3@localhost'));
+ $this->assertTrue($entry->hasValue('displayname', 'My LDAP user'));
+ $this->assertFalse($entry->hasValue('displayname', 'Something else'));
+ }
+}
diff --git a/tests/units/Core/Ldap/LdapGroupTest.php b/tests/units/Core/Ldap/LdapGroupTest.php
new file mode 100644
index 00000000..3f538249
--- /dev/null
+++ b/tests/units/Core/Ldap/LdapGroupTest.php
@@ -0,0 +1,160 @@
+<?php
+
+require_once __DIR__.'/../../Base.php';
+
+use Kanboard\Core\Ldap\Group;
+use Kanboard\Core\Ldap\Entries;
+use Kanboard\Core\Security\Role;
+
+class LdapGroupTest extends Base
+{
+ private $query;
+ private $client;
+ private $group;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->client = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\Client')
+ ->setMethods(array(
+ 'getConnection',
+ ))
+ ->getMock();
+
+ $this->query = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\Query')
+ ->setConstructorArgs(array($this->client))
+ ->setMethods(array(
+ 'execute',
+ 'hasResult',
+ 'getEntries',
+ ))
+ ->getMock();
+
+ $this->group = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\Group')
+ ->setConstructorArgs(array($this->query))
+ ->setMethods(array(
+ 'getAttributeName',
+ 'getBasDn',
+ ))
+ ->getMock();
+ }
+
+ public function testGetGroups()
+ {
+ $entries = new Entries(array(
+ 'count' => 2,
+ 0 => array(
+ 'cn' => array(
+ 'count' => 1,
+ 0 => 'Kanboard Other Group',
+ ),
+ 0 => 'cn',
+ 'count' => 1,
+ 'dn' => 'CN=Kanboard Other Group,CN=Users,DC=kanboard,DC=local',
+ ),
+ 1 => array(
+ 'cn' => array(
+ 'count' => 1,
+ 0 => 'Kanboard Users',
+ ),
+ 0 => 'cn',
+ 'count' => 1,
+ 'dn' => 'CN=Kanboard Users,CN=Users,DC=kanboard,DC=local',
+ ),
+ ));
+
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
+ $this->query
+ ->expects($this->once())
+ ->method('execute')
+ ->with(
+ $this->equalTo('CN=Users,DC=kanboard,DC=local'),
+ $this->equalTo('(&(objectClass=group)(sAMAccountName=Kanboard*))')
+ );
+
+ $this->query
+ ->expects($this->once())
+ ->method('hasResult')
+ ->will($this->returnValue(true));
+
+ $this->query
+ ->expects($this->once())
+ ->method('getEntries')
+ ->will($this->returnValue($entries));
+
+ $this->group
+ ->expects($this->any())
+ ->method('getAttributeName')
+ ->will($this->returnValue('cn'));
+
+ $this->group
+ ->expects($this->any())
+ ->method('getBasDn')
+ ->will($this->returnValue('CN=Users,DC=kanboard,DC=local'));
+
+ $groups = $this->group->find('(&(objectClass=group)(sAMAccountName=Kanboard*))');
+ $this->assertCount(2, $groups);
+ $this->assertInstanceOf('Kanboard\Group\LdapGroupProvider', $groups[0]);
+ $this->assertInstanceOf('Kanboard\Group\LdapGroupProvider', $groups[1]);
+ $this->assertEquals('Kanboard Other Group', $groups[0]->getName());
+ $this->assertEquals('Kanboard Users', $groups[1]->getName());
+ $this->assertEquals('CN=Kanboard Other Group,CN=Users,DC=kanboard,DC=local', $groups[0]->getExternalId());
+ $this->assertEquals('CN=Kanboard Users,CN=Users,DC=kanboard,DC=local', $groups[1]->getExternalId());
+ }
+
+ public function testGetGroupsWithNoResult()
+ {
+ $entries = new Entries(array());
+
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
+ $this->query
+ ->expects($this->once())
+ ->method('execute')
+ ->with(
+ $this->equalTo('CN=Users,DC=kanboard,DC=local'),
+ $this->equalTo('(&(objectClass=group)(sAMAccountName=Kanboard*))')
+ );
+
+ $this->query
+ ->expects($this->once())
+ ->method('hasResult')
+ ->will($this->returnValue(false));
+
+ $this->query
+ ->expects($this->never())
+ ->method('getEntries');
+
+ $this->group
+ ->expects($this->any())
+ ->method('getAttributeName')
+ ->will($this->returnValue('cn'));
+
+ $this->group
+ ->expects($this->any())
+ ->method('getBasDn')
+ ->will($this->returnValue('CN=Users,DC=kanboard,DC=local'));
+
+ $groups = $this->group->find('(&(objectClass=group)(sAMAccountName=Kanboard*))');
+ $this->assertCount(0, $groups);
+ }
+
+ public function testGetBaseDnNotConfigured()
+ {
+ $this->setExpectedException('\LogicException');
+
+ $group = new Group($this->query);
+ $group->getBasDn();
+ }
+}
diff --git a/tests/units/Core/Ldap/LdapUserTest.php b/tests/units/Core/Ldap/LdapUserTest.php
new file mode 100644
index 00000000..2b3db1e5
--- /dev/null
+++ b/tests/units/Core/Ldap/LdapUserTest.php
@@ -0,0 +1,379 @@
+<?php
+
+require_once __DIR__.'/../../Base.php';
+
+use Kanboard\Core\Ldap\User;
+use Kanboard\Core\Ldap\Entries;
+use Kanboard\Core\Security\Role;
+
+class LdapUserTest extends Base
+{
+ private $query;
+ private $client;
+ private $user;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->client = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\Client')
+ ->setMethods(array(
+ 'getConnection',
+ ))
+ ->getMock();
+
+ $this->query = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\Query')
+ ->setConstructorArgs(array($this->client))
+ ->setMethods(array(
+ 'execute',
+ 'hasResult',
+ 'getEntries',
+ ))
+ ->getMock();
+
+ $this->user = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\User')
+ ->setConstructorArgs(array($this->query))
+ ->setMethods(array(
+ 'getAttributeUsername',
+ 'getAttributeEmail',
+ 'getAttributeName',
+ 'getAttributeGroup',
+ 'getGroupAdminDn',
+ 'getGroupManagerDn',
+ 'getBasDn',
+ ))
+ ->getMock();
+ }
+
+ public function testGetUser()
+ {
+ $entries = new Entries(array(
+ 'count' => 1,
+ 0 => array(
+ 'count' => 2,
+ 'dn' => 'uid=my_ldap_user,ou=People,dc=kanboard,dc=local',
+ 'displayname' => array(
+ 'count' => 1,
+ 0 => 'My LDAP user',
+ ),
+ 'mail' => array(
+ 'count' => 2,
+ 0 => 'user1@localhost',
+ 1 => 'user2@localhost',
+ ),
+ 'samaccountname' => array(
+ 'count' => 1,
+ 0 => 'my_ldap_user',
+ ),
+ 0 => 'displayname',
+ 1 => 'mail',
+ 2 => 'samaccountname',
+ )
+ ));
+
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
+ $this->query
+ ->expects($this->once())
+ ->method('execute')
+ ->with(
+ $this->equalTo('ou=People,dc=kanboard,dc=local'),
+ $this->equalTo('(uid=my_ldap_user)')
+ );
+
+ $this->query
+ ->expects($this->once())
+ ->method('hasResult')
+ ->will($this->returnValue(true));
+
+ $this->query
+ ->expects($this->once())
+ ->method('getEntries')
+ ->will($this->returnValue($entries));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeUsername')
+ ->will($this->returnValue('samaccountname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeName')
+ ->will($this->returnValue('displayname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeEmail')
+ ->will($this->returnValue('mail'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getBasDn')
+ ->will($this->returnValue('ou=People,dc=kanboard,dc=local'));
+
+ $user = $this->user->find('(uid=my_ldap_user)');
+ $this->assertInstanceOf('Kanboard\User\LdapUserProvider', $user);
+ $this->assertEquals('uid=my_ldap_user,ou=People,dc=kanboard,dc=local', $user->getDn());
+ $this->assertEquals('my_ldap_user', $user->getUsername());
+ $this->assertEquals('My LDAP user', $user->getName());
+ $this->assertEquals('user1@localhost', $user->getEmail());
+ $this->assertEquals(Role::APP_USER, $user->getRole());
+ $this->assertEquals(array(), $user->getExternalGroupIds());
+ $this->assertEquals(array('is_ldap_user' => 1), $user->getExtraAttributes());
+ }
+
+ public function testGetUserWithAdminRole()
+ {
+ $entries = new Entries(array(
+ 'count' => 1,
+ 0 => array(
+ 'count' => 2,
+ 'dn' => 'uid=my_ldap_user,ou=People,dc=kanboard,dc=local',
+ 'displayname' => array(
+ 'count' => 1,
+ 0 => 'My LDAP user',
+ ),
+ 'mail' => array(
+ 'count' => 2,
+ 0 => 'user1@localhost',
+ 1 => 'user2@localhost',
+ ),
+ 'samaccountname' => array(
+ 'count' => 1,
+ 0 => 'my_ldap_user',
+ ),
+ 'memberof' => array(
+ 'count' => 1,
+ 0 => 'CN=Kanboard-Admins,CN=Users,DC=kanboard,DC=local',
+ ),
+ 0 => 'displayname',
+ 1 => 'mail',
+ 2 => 'samaccountname',
+ 3 => 'memberof',
+ )
+ ));
+
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
+ $this->query
+ ->expects($this->once())
+ ->method('execute')
+ ->with(
+ $this->equalTo('ou=People,dc=kanboard,dc=local'),
+ $this->equalTo('(uid=my_ldap_user)')
+ );
+
+ $this->query
+ ->expects($this->once())
+ ->method('hasResult')
+ ->will($this->returnValue(true));
+
+ $this->query
+ ->expects($this->once())
+ ->method('getEntries')
+ ->will($this->returnValue($entries));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeUsername')
+ ->will($this->returnValue('samaccountname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeName')
+ ->will($this->returnValue('displayname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeEmail')
+ ->will($this->returnValue('mail'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeGroup')
+ ->will($this->returnValue('memberof'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getGroupAdminDn')
+ ->will($this->returnValue('CN=Kanboard-Admins,CN=Users,DC=kanboard,DC=local'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getBasDn')
+ ->will($this->returnValue('ou=People,dc=kanboard,dc=local'));
+
+ $user = $this->user->find('(uid=my_ldap_user)');
+ $this->assertInstanceOf('Kanboard\User\LdapUserProvider', $user);
+ $this->assertEquals('uid=my_ldap_user,ou=People,dc=kanboard,dc=local', $user->getDn());
+ $this->assertEquals('my_ldap_user', $user->getUsername());
+ $this->assertEquals('My LDAP user', $user->getName());
+ $this->assertEquals('user1@localhost', $user->getEmail());
+ $this->assertEquals(Role::APP_ADMIN, $user->getRole());
+ $this->assertEquals(array('CN=Kanboard-Admins,CN=Users,DC=kanboard,DC=local'), $user->getExternalGroupIds());
+ $this->assertEquals(array('is_ldap_user' => 1), $user->getExtraAttributes());
+ }
+
+ public function testGetUserWithManagerRole()
+ {
+ $entries = new Entries(array(
+ 'count' => 1,
+ 0 => array(
+ 'count' => 2,
+ 'dn' => 'uid=my_ldap_user,ou=People,dc=kanboard,dc=local',
+ 'displayname' => array(
+ 'count' => 1,
+ 0 => 'My LDAP user',
+ ),
+ 'mail' => array(
+ 'count' => 2,
+ 0 => 'user1@localhost',
+ 1 => 'user2@localhost',
+ ),
+ 'samaccountname' => array(
+ 'count' => 1,
+ 0 => 'my_ldap_user',
+ ),
+ 'memberof' => array(
+ 'count' => 2,
+ 0 => 'CN=Kanboard-Users,CN=Users,DC=kanboard,DC=local',
+ 1 => 'CN=Kanboard-Managers,CN=Users,DC=kanboard,DC=local',
+ ),
+ 0 => 'displayname',
+ 1 => 'mail',
+ 2 => 'samaccountname',
+ 3 => 'memberof',
+ )
+ ));
+
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
+ $this->query
+ ->expects($this->once())
+ ->method('execute')
+ ->with(
+ $this->equalTo('ou=People,dc=kanboard,dc=local'),
+ $this->equalTo('(uid=my_ldap_user)')
+ );
+
+ $this->query
+ ->expects($this->once())
+ ->method('hasResult')
+ ->will($this->returnValue(true));
+
+ $this->query
+ ->expects($this->once())
+ ->method('getEntries')
+ ->will($this->returnValue($entries));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeUsername')
+ ->will($this->returnValue('samaccountname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeName')
+ ->will($this->returnValue('displayname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeEmail')
+ ->will($this->returnValue('mail'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeGroup')
+ ->will($this->returnValue('memberof'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getGroupManagerDn')
+ ->will($this->returnValue('CN=Kanboard-Managers,CN=Users,DC=kanboard,DC=local'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getBasDn')
+ ->will($this->returnValue('ou=People,dc=kanboard,dc=local'));
+
+ $user = $this->user->find('(uid=my_ldap_user)');
+ $this->assertInstanceOf('Kanboard\User\LdapUserProvider', $user);
+ $this->assertEquals('uid=my_ldap_user,ou=People,dc=kanboard,dc=local', $user->getDn());
+ $this->assertEquals('my_ldap_user', $user->getUsername());
+ $this->assertEquals('My LDAP user', $user->getName());
+ $this->assertEquals('user1@localhost', $user->getEmail());
+ $this->assertEquals(Role::APP_MANAGER, $user->getRole());
+ $this->assertEquals(array('CN=Kanboard-Users,CN=Users,DC=kanboard,DC=local', 'CN=Kanboard-Managers,CN=Users,DC=kanboard,DC=local'), $user->getExternalGroupIds());
+ $this->assertEquals(array('is_ldap_user' => 1), $user->getExtraAttributes());
+ }
+
+ public function testGetUserNotFound()
+ {
+ $entries = new Entries(array());
+
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
+ $this->query
+ ->expects($this->once())
+ ->method('execute')
+ ->with(
+ $this->equalTo('ou=People,dc=kanboard,dc=local'),
+ $this->equalTo('(uid=my_ldap_user)')
+ );
+
+ $this->query
+ ->expects($this->once())
+ ->method('hasResult')
+ ->will($this->returnValue(false));
+
+ $this->query
+ ->expects($this->never())
+ ->method('getEntries');
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeUsername')
+ ->will($this->returnValue('samaccountname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeName')
+ ->will($this->returnValue('displayname'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getAttributeEmail')
+ ->will($this->returnValue('mail'));
+
+ $this->user
+ ->expects($this->any())
+ ->method('getBasDn')
+ ->will($this->returnValue('ou=People,dc=kanboard,dc=local'));
+
+ $user = $this->user->find('(uid=my_ldap_user)');
+ $this->assertEquals(null, $user);
+ }
+
+ public function testGetBaseDnNotConfigured()
+ {
+ $this->setExpectedException('\LogicException');
+
+ $user = new User($this->query);
+ $user->getBasDn();
+ }
+}
diff --git a/tests/units/Core/Ldap/QueryTest.php b/tests/units/Core/Ldap/QueryTest.php
index 2eb3940f..b3987df0 100644
--- a/tests/units/Core/Ldap/QueryTest.php
+++ b/tests/units/Core/Ldap/QueryTest.php
@@ -17,6 +17,7 @@ function ldap_get_entries($link_identifier, $result_identifier)
class QueryTest extends \Base
{
public static $functions;
+ private $client;
public function setUp()
{
@@ -29,6 +30,13 @@ class QueryTest extends \Base
'ldap_get_entries',
))
->getMock();
+
+ $this->client = $this
+ ->getMockBuilder('\Kanboard\Core\Ldap\Client')
+ ->setMethods(array(
+ 'getConnection',
+ ))
+ ->getMock();
}
public function tearDown()
@@ -58,6 +66,11 @@ class QueryTest extends \Base
)
);
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
self::$functions
->expects($this->once())
->method('ldap_search')
@@ -78,20 +91,25 @@ class QueryTest extends \Base
)
->will($this->returnValue($entries));
- $query = new Query;
- $query->execute('my_ldap_resource', 'ou=People,dc=kanboard,dc=local', 'uid=my_user', array('displayname'));
+ $query = new Query($this->client);
+ $query->execute('ou=People,dc=kanboard,dc=local', 'uid=my_user', array('displayname'));
$this->assertTrue($query->hasResult());
- $this->assertEquals('My user', $query->getAttributeValue('displayname'));
- $this->assertEquals('user1@localhost', $query->getAttributeValue('mail'));
- $this->assertEquals('', $query->getAttributeValue('not_found'));
+ $this->assertEquals('My user', $query->getEntries()->getFirstEntry()->getFirstValue('displayname'));
+ $this->assertEquals('user1@localhost', $query->getEntries()->getFirstEntry()->getFirstValue('mail'));
+ $this->assertEquals('', $query->getEntries()->getFirstEntry()->getFirstValue('not_found'));
- $this->assertEquals('uid=my_user,ou=People,dc=kanboard,dc=local', $query->getAttribute('dn'));
- $this->assertEquals(null, $query->getAttribute('missing'));
+ $this->assertEquals('uid=my_user,ou=People,dc=kanboard,dc=local', $query->getEntries()->getFirstEntry()->getDn());
+ $this->assertEquals('', $query->getEntries()->getFirstEntry()->getFirstValue('missing'));
}
public function testExecuteQueryNotFound()
{
+ $this->client
+ ->expects($this->any())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
self::$functions
->expects($this->once())
->method('ldap_search')
@@ -112,13 +130,18 @@ class QueryTest extends \Base
)
->will($this->returnValue(array()));
- $query = new Query;
- $query->execute('my_ldap_resource', 'ou=People,dc=kanboard,dc=local', 'uid=my_user', array('displayname'));
+ $query = new Query($this->client);
+ $query->execute('ou=People,dc=kanboard,dc=local', 'uid=my_user', array('displayname'));
$this->assertFalse($query->hasResult());
}
public function testExecuteQueryFailed()
{
+ $this->client
+ ->expects($this->once())
+ ->method('getConnection')
+ ->will($this->returnValue('my_ldap_resource'));
+
self::$functions
->expects($this->once())
->method('ldap_search')
@@ -130,8 +153,8 @@ class QueryTest extends \Base
)
->will($this->returnValue(false));
- $query = new Query;
- $query->execute('my_ldap_resource', 'ou=People,dc=kanboard,dc=local', 'uid=my_user', array('displayname'));
+ $query = new Query($this->client);
+ $query->execute('ou=People,dc=kanboard,dc=local', 'uid=my_user', array('displayname'));
$this->assertFalse($query->hasResult());
}
}
diff --git a/tests/units/Core/Ldap/UserTest.php b/tests/units/Core/Ldap/UserTest.php
deleted file mode 100644
index 56cc588c..00000000
--- a/tests/units/Core/Ldap/UserTest.php
+++ /dev/null
@@ -1,95 +0,0 @@
-<?php
-
-namespace Kanboard\Core\Ldap;
-
-require_once __DIR__.'/../../Base.php';
-
-class UserTest extends \Base
-{
- public function testGetProfile()
- {
- $entries = array(
- 'count' => 1,
- 0 => array(
- 'count' => 2,
- 'dn' => 'uid=my_user,ou=People,dc=kanboard,dc=local',
- 'displayname' => array(
- 'count' => 1,
- 0 => 'My LDAP user',
- ),
- 'mail' => array(
- 'count' => 2,
- 0 => 'user1@localhost',
- 1 => 'user2@localhost',
- ),
- 'samaccountname' => array(
- 'count' => 1,
- 0 => 'my_ldap_user',
- ),
- 0 => 'displayname',
- 1 => 'mail',
- 2 => 'samaccountname',
- )
- );
-
- $expected = array(
- 'ldap_id' => 'uid=my_user,ou=People,dc=kanboard,dc=local',
- 'username' => 'my_ldap_user',
- 'name' => 'My LDAP user',
- 'email' => 'user1@localhost',
- 'is_admin' => 0,
- 'is_project_admin' => 0,
- 'is_ldap_user' => 1,
- );
-
- $query = $this
- ->getMockBuilder('\Kanboard\Core\Ldap\Query')
- ->setConstructorArgs(array($entries))
- ->setMethods(array(
- 'execute',
- 'hasResult',
- ))
- ->getMock();
-
- $query
- ->expects($this->once())
- ->method('execute')
- ->with(
- $this->equalTo('my_ldap_resource'),
- $this->equalTo('ou=People,dc=kanboard,dc=local'),
- $this->equalTo('(uid=my_user)')
- );
-
- $query
- ->expects($this->once())
- ->method('hasResult')
- ->will($this->returnValue(true));
-
- $user = $this
- ->getMockBuilder('\Kanboard\Core\Ldap\User')
- ->setConstructorArgs(array($query))
- ->setMethods(array(
- 'getAttributeUsername',
- 'getAttributeEmail',
- 'getAttributeName',
- ))
- ->getMock();
-
- $user
- ->expects($this->any())
- ->method('getAttributeUsername')
- ->will($this->returnValue('samaccountname'));
-
- $user
- ->expects($this->any())
- ->method('getAttributeName')
- ->will($this->returnValue('displayname'));
-
- $user
- ->expects($this->any())
- ->method('getAttributeEmail')
- ->will($this->returnValue('mail'));
-
- $this->assertEquals($expected, $user->getProfile('my_ldap_resource', 'ou=People,dc=kanboard,dc=local', '(uid=my_user)'));
- }
-}