File: RecoveryCodeDaoTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (166 lines) | stat: -rw-r--r-- 6,772 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\TwoFactorAuth\tests\Integration\Dao;

use Piwik\Container\StaticContainer;
use Piwik\DbHelper;
use Piwik\Plugins\TwoFactorAuth\Dao\RecoveryCodeDao;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group TwoFactorAuth
 * @group RecoveryCodeDaoTest
 * @group Plugins
 */
class RecoveryCodeDaoTest extends IntegrationTestCase
{
    /**
     * @var RecoveryCodeDao
     */
    private $dao;

    public function setUp(): void
    {
        parent::setUp();

        $this->dao = StaticContainer::get(RecoveryCodeDao::class);
    }

    public function testShouldInstallTable()
    {
        $columns = DbHelper::getTableColumns($this->dao->getPrefixedTableName());
        $columns = array_keys($columns);

        $this->assertEquals(['idrecoverycode', 'login', 'recovery_code'], $columns);
    }

    public function testGetAllRecoveryCodesForLoginEmptyByDefault()
    {
        $this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login1'));
    }

    public function testInsertRecoveryCodeGetAllRecoveryCodesForLogin()
    {
        $this->dao->insertRecoveryCode('login1', '123456');
        $this->dao->insertRecoveryCode('login1', '654321');
        $this->dao->insertRecoveryCode('login2', '333111');
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['333111'], $this->dao->getAllRecoveryCodesForLogin('login2'));
    }

    public function testDeleteRecoveryCode()
    {
        $this->insertManyCodesDifferentLogins();
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->assertEquals(1, $this->dao->deleteRecoveryCode('login2', '654321')); // this one should be deleted
        $this->assertEquals(0, $this->dao->deleteRecoveryCode('login2', 'xya123')); // cannot be found
        $this->assertEquals(0, $this->dao->deleteRecoveryCode('login999', '123456')); // cannot be found

        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['123456'], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->dao->deleteRecoveryCode('login2', '123456'); // delete last code for this login
        $this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->assertEquals(0, $this->dao->deleteRecoveryCode('login2', '654321')); // cannot be deleted again
    }

    public function testDeleteAllRecoveryCodesForLogin()
    {
        $this->insertManyCodesDifferentLogins();
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->dao->deleteAllRecoveryCodesForLogin('login2'); // this one should be deleted
        $this->dao->deleteAllRecoveryCodesForLogin('login999'); // login cannot be found

        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login2'));
    }

    public function testUseRecoveryCode()
    {
        $this->insertManyCodesDifferentLogins();
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->assertTrue($this->dao->useRecoveryCode('login2', '654321')); // this one should be used and deleted

        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['123456'], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->assertFalse($this->dao->useRecoveryCode('login2', '654321')); // cannot be used again
        $this->assertFalse($this->dao->useRecoveryCode('login2', 'xya123')); // cannot be found
        $this->assertFalse($this->dao->useRecoveryCode('login999', '123456')); // cannot be found

        $this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->assertEquals(['123456'], $this->dao->getAllRecoveryCodesForLogin('login2'));

        $this->assertTrue($this->dao->useRecoveryCode('login2', '123456')); // cannot be used again
        $this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login2'));
    }

    public function testCreateRecoveryCodesForLogin()
    {
        $this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login1'));
        $this->dao->createRecoveryCodesForLogin('login1');

        $codes1 = $this->dao->getAllRecoveryCodesForLogin('login1');
        $this->assertCount(10, $codes1);

        // generating new codes will remove the old codes
        $this->dao->createRecoveryCodesForLogin('login1');

        $codes2 = $this->dao->getAllRecoveryCodesForLogin('login1');
        $this->assertCount(10, $codes2);

        // not the same
        $this->assertCount(10, array_diff($codes1, $codes2));
        foreach ($codes1 as $code) {
            // none of the old codes can be used
            $this->assertFalse($this->dao->useRecoveryCode('login1', $code));
        }
        foreach ($codes2 as $code) {
            // all new codes can be used
            $this->assertTrue($this->dao->useRecoveryCode('login1', $code));
        }
    }

    public function testCreateRecoveryCodesForLoginDifferentPerLogin()
    {
        $this->dao->createRecoveryCodesForLogin('login1');
        $this->dao->createRecoveryCodesForLogin('login2');

        $codes1 = $this->dao->getAllRecoveryCodesForLogin('login1');
        $codes2 = $this->dao->getAllRecoveryCodesForLogin('login2');

        // not the same
        $this->assertCount(10, array_diff($codes1, $codes2));

        foreach ($codes1 as $code) {
            // all new codes can be used
            $this->assertTrue($this->dao->useRecoveryCode('login1', $code));
        }
        foreach ($codes2 as $code) {
            // all new codes can be used
            $this->assertTrue($this->dao->useRecoveryCode('login2', $code));
        }
    }

    private function insertManyCodesDifferentLogins()
    {
        $this->dao->insertRecoveryCode('login1', '123456');
        $this->dao->insertRecoveryCode('login1', '654321');
        $this->dao->insertRecoveryCode('login2', '123456');
        $this->dao->insertRecoveryCode('login2', '654321');
    }
}