Files
postfixadmin/tests/LoginTest.php
T
Rodrigo Cortés Cano 6e93d57766 Fix password expiration handling and visibility on 4.x (#1110)
- port the password-expiration correction to `postfixadmin_4.0`
- make password expiration optional and disabled by default
- make 0 consistently mean that domain passwords do not expire
- validate whole-day policies from 0 through 36500
- align password-change paths and preserve the existing Login fallback
- show mailbox expiration states in lists and CSV output
- add the new labels to every 4.x language file
- document how to enable the feature in `config.local.php`

thanks @TrapoSAMA
2026-08-11 09:28:19 +01:00

178 lines
6.0 KiB
PHP

<?php
class LoginTest extends \PHPUnit\Framework\TestCase
{
private $passwordExpirationConfig;
public function setUp(): void
{
global $CONF;
$this->passwordExpirationConfig = (string)Config::read('password_expiration');
$this->cleanUp();
$CONF['pacrypt'] = 'md5'; // crap
db_execute("INSERT INTO domain(domain, description, transport) values ('example.com', 'test', 'foo')", [], true);
db_execute(
"INSERT INTO mailbox(username, password, name, maildir, local_part, domain) VALUES(:username, :password, :name, :maildir, :local_part, :domain)",
[
'username' => 'test@example.com',
'password' => pacrypt('foobar'),
'name' => 'test user',
'maildir' => '/foo/bar',
'local_part' => 'test',
'domain' => 'example.com',
]);
parent::setUp();
}
public function tearDown(): void
{
Config::write('password_expiration', $this->passwordExpirationConfig);
$this->cleanUp();
parent::tearDown(); // TODO: Change the autogenerated stub
}
private function cleanUp()
{
db_query('DELETE FROM alias');
db_query('DELETE FROM alias_domain');
db_query('DELETE FROM mailbox');
db_query('DELETE FROM domain_admins');
db_query('DELETE FROM domain');
}
public function testChangePassword()
{
$login = new Login('mailbox');
$this->assertTrue($login->login('test@example.com', 'foobar'));
// Can't change - current password wrong.
try {
$login->changePassword('test@example.com', 'foobar2', 'foobar2');
$this->fail("Exception should have been thrown");
} catch (\Exception $e) {
$this->assertEquals("You didn't supply your current password!", $e->getMessage());
}
// Should change, current password correct.
$this->assertTrue($login->changePassword('test@example.com', 'foobar2', 'foobar'));
// Can't now login with the old password
$this->assertFalse($login->login('test@example.com', 'foobar'));
// Can login with the new one...
$this->assertTrue($login->login('test@example.com', 'foobar2'));
$mailbox = db_query_one(
'SELECT password_expiry FROM mailbox WHERE username = :username',
['username' => 'test@example.com']
);
$this->assertSame(strtotime(PASSWORD_EXPIRATION_NEVER), strtotime($mailbox['password_expiry']));
Config::write('password_expiration', 'YES');
db_execute(
'UPDATE domain SET password_expiry = :days WHERE domain = :domain',
['days' => 30, 'domain' => 'example.com']
);
$this->assertTrue($login->changePassword('test@example.com', 'foobar3', 'foobar2'));
$mailbox = db_query_one(
'SELECT password_expiry FROM mailbox WHERE username = :username',
['username' => 'test@example.com']
);
$expiry = strtotime($mailbox['password_expiry']);
$this->assertGreaterThanOrEqual(strtotime('+29 days'), $expiry);
$this->assertLessThanOrEqual(strtotime('+31 days'), $expiry);
}
public function testInvalidUsers()
{
$login = new Login('mailbox');
$this->assertFalse($login->login('test', 'password'));
$this->assertFalse($login->login('test', ''));
$this->assertFalse($login->login('', ''));
}
public function testEmptyStringWithDovecot()
{
global $CONF;
if (!file_exists('/usr/bin/doveadm')) {
$this->markTestSkipped("/usr/bin/doveadm doesn't exist.");
}
$CONF['encrypt'] = 'dovecot:sha512';
db_execute(
"UPDATE mailbox SET password = :password WHERE username = :username",
[
'username' => 'test@example.com',
'password' => '{SHA512}ClAmHr0aOQ/tK/Mm8mc8FFWCpjQtUjIElz0CGTN/gWFqgGmwElh89WNfaSXxtWw2AjDBmyc1AO4BPgMGAb8kJQ==', // pacrypt('foobar'),
]
);
$l = new Login('mailbox');
$this->assertFalse($l->login('test@example.com', ''));
$this->assertTrue($l->login('test@example.com', 'foobar'));
$this->assertFalse($l->login('test@fails.com', 'foobar'));
}
public function testInvalidLogin()
{
$login = new Login('mailbox');
$this->assertFalse($login->login('test', 'password'));
$this->assertFalse($login->login('test', 'foobar'));
$this->assertFalse($login->login('', ''));
}
public function testPasswordRecovery()
{
$login = new Login('mailbox');
$this->assertFalse($login->generatePasswordRecoveryCode(''));
$this->assertFalse($login->generatePasswordRecoveryCode('doesnotexist'));
$this->assertNotEmpty($login->generatePasswordRecoveryCode('test@example.com'));
}
public function testAddAppPasswordIncorrectPassword()
{
$login = new Login('mailbox');
$this->assertTrue($login->login('test@example.com', 'foobar'));
$this->expectExceptionMessage("You didn't supply your current password!");
$this->assertTrue($login->addAppPassword('test@example.com', 'fish', '1st-app-password', 'something'));
}
public function testAddAppPassword()
{
$login = new Login('mailbox');
$this->assertTrue($login->login('test@example.com', 'foobar'));
$this->assertTrue($login->addAppPassword('test@example.com', 'foobar', '1st-app-password', 'something'));
$this->assertTrue($login->addAppPassword('test@example.com', 'foobar', '1st-app-password', 'something'));
$rows = db_query_all('SELECT * FROM mailbox_app_password');
$this->assertEquals(2, count($rows));
foreach ($rows as $r) {
$this->assertEquals('1st-app-password', $r['description']);
$this->assertNotEmpty($r['password_hash']);
$this->assertEquals('test@example.com', $r['username']);
}
}
}