File: LoginTest.php

package info (click to toggle)
postfixadmin 3.3.15%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,816 kB
  • sloc: php: 10,120; perl: 1,069; sh: 643; python: 169; xml: 62; sql: 3; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 3,790 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

class LoginTest extends \PHPUnit\Framework\TestCase {
    public function setUp(): void {
        global $CONF;

        $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 {
        $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 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'));
    }


    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 testValidLogin() {
        $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'));
    }
}