Skip to content

Commit

Permalink
Tests for OpenLDAP
Browse files Browse the repository at this point in the history
  • Loading branch information
coudot committed Nov 19, 2024
1 parent 4523948 commit 42f74e1
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Ltb/Directory/OpenLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,15 @@ public function isAccountValid($ldap, $dn) : bool {

$time = time();

if ( $entry[0]['pwdstarttime'] ) {
$startdate = \Ltb\Date::adDate2phpDate($entry[0]['pwdstarttime'][0]);
if ( isset($entry[0]['pwdstarttime']) ) {
$startdate = \Ltb\Date::ldapDate2phpDate($entry[0]['pwdstarttime'][0]);
if ( $time <= $startdate->getTimestamp() ) {
return false;
}
}

if ( $entry[0]['pwdstarttime'] ) {
$enddate = \Ltb\Date::adDate2phpDate($entry[0]['pwdendtime'][0]);
if ( isset($entry[0]['pwdendtime']) ) {
$enddate = \Ltb\Date::ldapDate2phpDate($entry[0]['pwdendtime'][0]);
if ( $time >= $enddate->getTimestamp() ) {
return false;
}
Expand Down
129 changes: 129 additions & 0 deletions tests/Ltb/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,135 @@ public function test_openldap_reset_false_empty(): void
$this->assertFalse($reset, "Reset should be false");
}

public function test_openldap_isvalid_nodate(): void
{
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 0,
]
]);

$isAccountValid = (new Ltb\Directory\OpenLDAP)->isAccountValid(null, null);
$this->assertTrue($isAccountValid, "Account should be valid");
}

public function test_openldap_isvalid_startdate_before(): void
{
$dt = new DateTime;
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 1,
0 => [
'pwdstarttime' => [
'count' => 1,
0 => $dt->modify("-1 week")->format("Ymdhis\Z"),
]
]
]
]);

$isAccountValid = (new Ltb\Directory\OpenLDAP)->isAccountValid(null, null);
$this->assertTrue($isAccountValid, "Account should be valid");
}

public function test_openldap_isvalid_startdate_after(): void
{
$dt = new DateTime;
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 1,
0 => [
'pwdstarttime' => [
'count' => 1,
0 => $dt->modify("+1 week")->format("Ymdhis\Z"),
]
]
]
]);

$isAccountValid = (new Ltb\Directory\OpenLDAP)->isAccountValid(null, null);
$this->assertFalse($isAccountValid, "Account should not be valid");
}

public function test_openldap_isvalid_enddate_before(): void
{
$dt = new DateTime;
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 1,
0 => [
'pwdendtime' => [
'count' => 1,
0 => $dt->modify("-1 week")->format("Ymdhis\Z"),
]
]
]
]);

$isAccountValid = (new Ltb\Directory\OpenLDAP)->isAccountValid(null, null);
$this->assertFalse($isAccountValid, "Account should not be valid");
}

public function test_openldap_isvalid_enddate_after(): void
{
$dt = new DateTime;
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 1,
0 => [
'pwdendtime' => [
'count' => 1,
0 => $dt->modify("+1 week")->format("Ymdhis\Z"),
]
]
]
]);

$isAccountValid = (new Ltb\Directory\OpenLDAP)->isAccountValid(null, null);
$this->assertTrue($isAccountValid, "Account should be valid");
}

public function test_openldap_isvalid_bothdate(): void
{
$dt = new DateTime;
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 1,
0 => [
'pwdstarttime' => [
'count' => 1,
0 => $dt->modify("-1 week")->format("Ymdhis\Z"),
],
'pwdendtime' => [
'count' => 1,
0 => $dt->modify("+2 week")->format("Ymdhis\Z"),
]
]
]
]);

$isAccountValid = (new Ltb\Directory\OpenLDAP)->isAccountValid(null, null);
$this->assertTrue($isAccountValid, "Account should be valid");
}

public function test_activedirectory_islocked_locked_forever(): void
{
$ad_date = ((int)time() + 11644473600) * 10000000;
Expand Down

0 comments on commit 42f74e1

Please sign in to comment.