0
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2024-09-20 03:36:20 +02:00
postfixadmin/tests/LoginTest.php

118 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2021-03-22 10:28:28 +01:00
class LoginTest extends \PHPUnit\Framework\TestCase
{
2020-09-25 22:33:26 +02:00
public function setUp(): void {
global $CONF;
$this->cleanUp();
$CONF['pacrypt'] = 'md5'; // crap
2021-01-19 17:50:56 +01:00
db_execute("INSERT INTO domain(domain, description, transport) values ('example.com', 'test', 'foo')", [], true);
db_execute(
2021-01-19 17:50:56 +01:00
"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',
]);
2021-01-19 17:50:56 +01:00
parent::setUp();
}
2020-09-25 22:33:26 +02:00
public function tearDown(): void {
$this->cleanUp();
parent::tearDown(); // TODO: Change the autogenerated stub
}
2020-09-25 22:33:26 +02:00
private function cleanUp() {
2021-01-19 17:50:56 +01:00
db_query('DELETE FROM alias');
db_query('DELETE FROM alias_domain');
db_query('DELETE FROM mailbox');
2021-01-19 17:50:56 +01:00
db_query('DELETE FROM domain_admins');
db_query('DELETE FROM domain');
}
public function testPasswordchange() {
$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'));
}
2020-09-25 22:33:26 +02:00
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'));
}
2020-09-25 22:33:26 +02:00
public function testValidLogin() {
$login = new Login('mailbox');
$this->assertFalse($login->login('test', 'password'));
$this->assertFalse($login->login('test', 'foobar'));
$this->assertFalse($login->login('', ''));
}
2020-09-25 22:33:26 +02:00
public function testPasswordRecovery() {
$login = new Login('mailbox');
$this->assertFalse($login->generatePasswordRecoveryCode(''));
$this->assertFalse($login->generatePasswordRecoveryCode('doesnotexist'));
$this->assertNotEmpty($login->generatePasswordRecoveryCode('test@example.com'));
}
}