Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace tool_mfa;
18
 
19
/**
20
 * Tests for MFA admin settings
21
 *
22
 * @package     tool_mfa
23
 * @author      Mikhail Golenkov <golenkovm@gmail.com>
24
 * @author      Peter Burnett <peterburnett@catalyst-au.net>
25
 * @copyright   Catalyst IT
26
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 27
 *
28
 * @covers \tool_mfa\table\admin_setting_managemfa
1 efrain 29
 */
1441 ariadna 30
final class admin_setting_managemfa_test extends \advanced_testcase {
1 efrain 31
 
1441 ariadna 32
    use \tool_mfa\tests\mfa_settings_trait;
1 efrain 33
 
34
    /**
1441 ariadna 35
     * Setup testcase.
36
     */
37
    public function setUp(): void {
38
        parent::setUp();
39
        $this->resetAfterTest();
40
        // Disable email factor (enabled by default).
41
        $this->set_factor_state('email', 0);
42
    }
43
 
44
    /**
1 efrain 45
     * Tests getting the factor combinations
46
     */
11 efrain 47
    public function test_get_factor_combinations_default(): void {
1441 ariadna 48
        $namagemfa = new \tool_mfa\table\admin_setting_managemfa();
1 efrain 49
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
50
        $combinations = $namagemfa->get_factor_combinations($factors, 0, count($factors) - 1);
51
        $this->assertEquals(0, count($factors));
52
        $this->assertEquals(0, count($combinations));
53
    }
54
 
55
    /**
56
     * Data provider for test_get_factor_combinations_with_data_provider().
57
     *
58
     * @return array
59
     */
1441 ariadna 60
    public static function get_factor_combinations_provider(): array {
1 efrain 61
        $provider = [];
62
 
63
        $factors = [];
64
        $provider[] = [$factors, 0];
65
 
66
        $factors = [];
67
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90];
68
        $provider[] = [$factors, 0];
69
 
70
        $factors = [];
71
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 100];
72
        $provider[] = [$factors, 1];
73
 
74
        $factors = [];
75
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 150];
76
        $provider[] = [$factors, 1];
77
 
78
        $factors = [];
79
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40];
80
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 40];
81
        $provider[] = [$factors, 0];
82
 
83
        $factors = [];
84
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 90];
85
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 40];
86
        $provider[] = [$factors, 1];
87
 
88
        $factors = [];
89
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 100];
90
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 100];
91
        $provider[] = [$factors, 2];
92
 
93
        $factors = [];
94
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 100];
95
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 100];
96
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 100];
97
        $provider[] = [$factors, 3];
98
 
99
        $factors = [];
100
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90];
101
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 30];
102
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40];
103
        $provider[] = [$factors, 2];
104
 
105
        $factors = [];
106
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 30];
107
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40];
108
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90];
109
        $provider[] = [$factors, 3];
110
 
111
        $factors = [];
112
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 30];
113
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40];
114
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90];
115
        $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 90];
116
        $provider[] = [$factors, 7];
117
 
118
        $factors = [];
119
        $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 50];
120
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 50];
121
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 50];
122
        $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 50];
123
        $provider[] = [$factors, 6];
124
 
125
        $factors = [];
126
        $factors[] = ['name' => 'email', 'enabled' => 0, 'weight' => 50];
127
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 50];
128
        $factors[] = ['name' => 'totp', 'enabled' => 0, 'weight' => 50];
129
        $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 50];
130
        $provider[] = [$factors, 1];
131
 
132
        $factors = [];
133
        $factors[] = ['name' => 'email', 'enabled' => 0, 'weight' => 50];
134
        $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 50];
135
        $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 50];
136
        $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 50];
137
        $provider[] = [$factors, 3];
138
 
139
        return $provider;
140
    }
141
 
142
    /**
143
     * Tests getting the factor combinations with data provider
144
     *
145
     * @dataProvider get_factor_combinations_provider
146
     * @param array $factorset configured factors
147
     * @param int $combinationscount expected count of available combinations
148
     */
1441 ariadna 149
    public function test_get_factor_combinations_with_data_provider(array $factorset, int $combinationscount): void {
1 efrain 150
        $enabledcount = 0;
151
 
152
        foreach ($factorset as $factor) {
153
            $this->set_factor_state($factor['name'], $factor['enabled'], $factor['weight']);
154
            if ($factor['enabled'] == 1) {
155
                $enabledcount++;
156
            }
157
        }
158
 
1441 ariadna 159
        $managemfa = new \tool_mfa\table\admin_setting_managemfa();
1 efrain 160
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
161
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
162
 
163
        foreach ($combinations as $combination) {
164
            $this->assertGreaterThanOrEqual(100, $combination['totalweight']);
165
            $this->assertLessThan(200, $combination['totalweight']);
166
            $this->assertGreaterThanOrEqual(1, count($combination['combination']));
167
            foreach ($combination['combination'] as $combinationfactor) {
168
                $this->assertInstanceOf('\tool_mfa\local\factor\object_factor', $combinationfactor);
169
            }
170
        }
171
 
172
        $this->assertEquals($enabledcount, count($factors));
173
        $this->assertEquals($combinationscount, count($combinations));
174
    }
175
 
176
    /**
177
     * Tests checking the factor combinations
178
     */
11 efrain 179
    public function test_factor_combination_checker(): void {
1441 ariadna 180
        $managemfa = new \tool_mfa\table\admin_setting_managemfa();
1 efrain 181
        $user = $this->getDataGenerator()->create_user();
182
        $this->setUser($user);
183
 
184
        // Test combination with 2 valid compatible factors.
185
        $this->set_factor_state('email', 1, 50);
186
        $this->set_factor_state('totp', 1, 50);
187
 
188
        // Check that there is only 1 valid combination.
189
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
190
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
191
        $this->assertEquals(1, count($combinations));
192
 
193
        // Change weights to 100 for each, and check for 2 valid.
194
        $this->set_factor_state('email', 1, 100);
195
        $this->set_factor_state('totp', 1, 100);
196
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
197
        $this->assertEquals(2, count($combinations));
198
 
199
        // Add another compatible factors, and check for 3 combinations.
200
        $this->set_factor_state('email', 1, 50);
201
        $this->set_factor_state('totp', 1, 50);
202
        $this->set_factor_state('iprange', 1, 50);
203
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
204
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
205
        $this->assertEquals(3, count($combinations));
206
 
207
        // Now same tests again, with an invalid combination set.
208
        $this->set_factor_state('email', 1, 100);
209
        $this->set_factor_state('totp', 0, 100);
210
        $this->set_factor_state('iprange', 0, 50);
211
        $this->set_factor_state('nosetup', 1, 100);
212
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
213
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
214
        $this->assertEquals(2, count($combinations));
215
 
216
        $this->set_factor_state('totp', 1, 50);
217
        $this->set_factor_state('email', 0, 50);
218
        $this->set_factor_state('nosetup', 1, 50);
219
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
220
        $this->assertEquals(0, count($combinations));
221
 
222
        $this->set_factor_state('email', 1, 50);
223
        $this->set_factor_state('nosetup', 1, 50);
224
        $this->set_factor_state('totp', 1, 50);
225
        $factors = \tool_mfa\plugininfo\factor::get_enabled_factors();
226
        $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1);
227
        $this->assertEquals(1, count($combinations));
228
    }
229
}