Skip to content

Commit

Permalink
Tests for resetAtNextConnection and some fixes in code
Browse files Browse the repository at this point in the history
  • Loading branch information
coudot committed Sep 17, 2024
1 parent 9f64895 commit 4ea8b11
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Ltb/Directory/ActiveDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getLockDate($ldap, $dn) : ?DateTime {
# Get lockoutTime
$lockoutTime = $entry[0]['lockouttime'][0];

if ( !$lockoutTime or $lockoutTime == 0) {
if ( !$lockoutTime or $lockoutTime === 0) {
return $lockDate;
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public function getPasswordExpirationDate($ldap, $dn, $config) : ?DateTime {
# Get pwdLastSet
$pwdLastSet = $entry[0]['pwdlastset'][0];

if ( !$pwdLastSet or $pwdLastSet == 0) {
if ( !$pwdLastSet or $pwdLastSet === 0) {
return $expirationDate;
}

Expand Down Expand Up @@ -201,12 +201,12 @@ public function resetAtNextConnection($ldap, $dn) : bool {

if ( $errno ) {
error_log("LDAP - Search error $errno (".ldap_error($ldap).")");
return $expirationDate;
return false;
} else {
$entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search);
}

if ($entry[0]['pwdlastset'] and $entry[0]['pwdlastset'][0] == 0) {
if ($entry[0]['pwdlastset'] and $entry[0]['pwdlastset'][0] === 0) {
return true;
} else {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Ltb/Directory/OpenLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function resetAtNextConnection($ldap, $dn) : bool {

if ( $errno ) {
error_log("LDAP - Search error $errno (".ldap_error($ldap).")");
return $expirationDate;
return false;
} else {
$entry = \Ltb\PhpLDAP::ldap_get_entries($ldap, $search);
}
Expand Down
128 changes: 128 additions & 0 deletions tests/Ltb/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,69 @@ public function test_openldap_getpasswordexpirationdate_notempty(): void
$this->assertEquals($dt->modify("+1 day")->format("Y/m/d - h:i:s"), $passwordExpirationDate->format("Y/m/d - h:i:s"), "Password expiration date is correct");
}

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

$reset = (new Ltb\Directory\OpenLDAP)->resetAtNextConnection(null, null);
$this->assertTrue($reset, "Reset should be true");
}

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

$reset = (new Ltb\Directory\OpenLDAP)->resetAtNextConnection(null, null);
$this->assertFalse($reset, "Reset should be false");
}

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

$reset = (new Ltb\Directory\OpenLDAP)->resetAtNextConnection(null, null);
$this->assertFalse($reset, "Reset should be false");
}

public function test_activedirectory_islocked_locked_forever(): void
{
$ad_date = ((int)time() + 11644473600) * 10000000;
Expand Down Expand Up @@ -557,4 +620,69 @@ public function test_activedirectory_getpasswordexpirationdate_notempty(): void
$this->assertEquals($dt->modify("+1 day")->format("Y/m/d - h:i:s"), $passwordExpirationDate->format("Y/m/d - h:i:s"), "Password expiration date is correct");
}

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

$reset = (new Ltb\Directory\ActiveDirectory)->resetAtNextConnection(null, null);
$this->assertTrue($reset, "Reset should be true");
}

public function test_activedirectory_reset_false(): void
{
$dt = new DateTime;
$ad_date = ((int)$dt->getTimestamp() + 11644473600) * 10000000;
$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
'ldap_read' => null,
'ldap_errno' => 0,
'ldap_get_entries' => [
'count' => 1,
0 => [
'pwdlastset' => [
'count' => 1,
0 => $ad_date,
]
]
]
]);

$reset = (new Ltb\Directory\ActiveDirectory)->resetAtNextConnection(null, null);
$this->assertFalse($reset, "Reset should be false");
}

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

$reset = (new Ltb\Directory\ActiveDirectory)->resetAtNextConnection(null, null);
$this->assertFalse($reset, "Reset should be false");
}

}

0 comments on commit 4ea8b11

Please sign in to comment.