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 use tool_mfa\tool_mfa_trait; 19 20 defined('MOODLE_INTERNAL') || die(); 21 require_once (__DIR__ . '/tool_mfa_trait.php'); 22 23 /** 24 * Tests for MFA admin settings 25 * 26 * @package tool_mfa 27 * @author Mikhail Golenkov <golenkovm@gmail.com> 28 * @author Peter Burnett <peterburnett@catalyst-au.net> 29 * @copyright Catalyst IT 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 */ 32 class admin_setting_managemfa_test extends \advanced_testcase { 33 34 use tool_mfa_trait; 35 36 /** 37 * Tests getting the factor combinations 38 * 39 * @covers ::get_factor_combinations 40 */ 41 public function test_get_factor_combinations_default() { 42 $namagemfa = new \tool_mfa\local\admin_setting_managemfa(); 43 $factors = \tool_mfa\plugininfo\factor::get_enabled_factors(); 44 $combinations = $namagemfa->get_factor_combinations($factors, 0, count($factors) - 1); 45 $this->assertEquals(0, count($factors)); 46 $this->assertEquals(0, count($combinations)); 47 } 48 49 /** 50 * Data provider for test_get_factor_combinations_with_data_provider(). 51 * 52 * @return array 53 */ 54 public function get_factor_combinations_provider() { 55 $provider = []; 56 57 $factors = []; 58 $provider[] = [$factors, 0]; 59 60 $factors = []; 61 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90]; 62 $provider[] = [$factors, 0]; 63 64 $factors = []; 65 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 100]; 66 $provider[] = [$factors, 1]; 67 68 $factors = []; 69 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 150]; 70 $provider[] = [$factors, 1]; 71 72 $factors = []; 73 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40]; 74 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 40]; 75 $provider[] = [$factors, 0]; 76 77 $factors = []; 78 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 90]; 79 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 40]; 80 $provider[] = [$factors, 1]; 81 82 $factors = []; 83 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 100]; 84 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 100]; 85 $provider[] = [$factors, 2]; 86 87 $factors = []; 88 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 100]; 89 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 100]; 90 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 100]; 91 $provider[] = [$factors, 3]; 92 93 $factors = []; 94 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90]; 95 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 30]; 96 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40]; 97 $provider[] = [$factors, 2]; 98 99 $factors = []; 100 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 30]; 101 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 40]; 102 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 90]; 103 $provider[] = [$factors, 3]; 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 $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 90]; 110 $provider[] = [$factors, 7]; 111 112 $factors = []; 113 $factors[] = ['name' => 'email', 'enabled' => 1, 'weight' => 50]; 114 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 50]; 115 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 50]; 116 $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 50]; 117 $provider[] = [$factors, 6]; 118 119 $factors = []; 120 $factors[] = ['name' => 'email', 'enabled' => 0, 'weight' => 50]; 121 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 50]; 122 $factors[] = ['name' => 'totp', 'enabled' => 0, 'weight' => 50]; 123 $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 50]; 124 $provider[] = [$factors, 1]; 125 126 $factors = []; 127 $factors[] = ['name' => 'email', 'enabled' => 0, 'weight' => 50]; 128 $factors[] = ['name' => 'iprange', 'enabled' => 1, 'weight' => 50]; 129 $factors[] = ['name' => 'totp', 'enabled' => 1, 'weight' => 50]; 130 $factors[] = ['name' => 'auth', 'enabled' => 1, 'weight' => 50]; 131 $provider[] = [$factors, 3]; 132 133 return $provider; 134 } 135 136 /** 137 * Tests getting the factor combinations with data provider 138 * 139 * @covers ::get_factor_combinations 140 * @dataProvider get_factor_combinations_provider 141 * @param array $factorset configured factors 142 * @param int $combinationscount expected count of available combinations 143 */ 144 public function test_get_factor_combinations_with_data_provider($factorset, $combinationscount) { 145 $this->resetAfterTest(); 146 $enabledcount = 0; 147 148 foreach ($factorset as $factor) { 149 $this->set_factor_state($factor['name'], $factor['enabled'], $factor['weight']); 150 if ($factor['enabled'] == 1) { 151 $enabledcount++; 152 } 153 } 154 155 $managemfa = new \tool_mfa\local\admin_setting_managemfa(); 156 $factors = \tool_mfa\plugininfo\factor::get_enabled_factors(); 157 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 158 159 foreach ($combinations as $combination) { 160 $this->assertGreaterThanOrEqual(100, $combination['totalweight']); 161 $this->assertLessThan(200, $combination['totalweight']); 162 $this->assertGreaterThanOrEqual(1, count($combination['combination'])); 163 foreach ($combination['combination'] as $combinationfactor) { 164 $this->assertInstanceOf('\tool_mfa\local\factor\object_factor', $combinationfactor); 165 } 166 } 167 168 $this->assertEquals($enabledcount, count($factors)); 169 $this->assertEquals($combinationscount, count($combinations)); 170 } 171 172 /** 173 * Tests checking the factor combinations 174 * 175 * @covers ::get_factor_combinations 176 */ 177 public function test_factor_combination_checker() { 178 $this->resetAfterTest(); 179 $managemfa = new \tool_mfa\local\admin_setting_managemfa(); 180 $user = $this->getDataGenerator()->create_user(); 181 $this->setUser($user); 182 183 // Test combination with 2 valid compatible factors. 184 $this->set_factor_state('email', 1, 50); 185 $this->set_factor_state('totp', 1, 50); 186 187 // Check that there is only 1 valid combination. 188 $factors = \tool_mfa\plugininfo\factor::get_enabled_factors(); 189 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 190 $this->assertEquals(1, count($combinations)); 191 192 // Change weights to 100 for each, and check for 2 valid. 193 $this->set_factor_state('email', 1, 100); 194 $this->set_factor_state('totp', 1, 100); 195 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 196 $this->assertEquals(2, count($combinations)); 197 198 // Add another compatible factors, and check for 3 combinations. 199 $this->set_factor_state('email', 1, 50); 200 $this->set_factor_state('totp', 1, 50); 201 $this->set_factor_state('iprange', 1, 50); 202 $factors = \tool_mfa\plugininfo\factor::get_enabled_factors(); 203 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 204 $this->assertEquals(3, count($combinations)); 205 206 // Now same tests again, with an invalid combination set. 207 $this->set_factor_state('email', 1, 100); 208 $this->set_factor_state('totp', 0, 100); 209 $this->set_factor_state('iprange', 0, 50); 210 $this->set_factor_state('nosetup', 1, 100); 211 $factors = \tool_mfa\plugininfo\factor::get_enabled_factors(); 212 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 213 $this->assertEquals(2, count($combinations)); 214 215 $this->set_factor_state('totp', 1, 50); 216 $this->set_factor_state('email', 0, 50); 217 $this->set_factor_state('nosetup', 1, 50); 218 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 219 $this->assertEquals(0, count($combinations)); 220 221 $this->set_factor_state('email', 1, 50); 222 $this->set_factor_state('nosetup', 1, 50); 223 $this->set_factor_state('totp', 1, 50); 224 $factors = \tool_mfa\plugininfo\factor::get_enabled_factors(); 225 $combinations = $managemfa->get_factor_combinations($factors, 0, count($factors) - 1); 226 $this->assertEquals(1, count($combinations)); 227 } 228 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body