|
23 | 23 | * @author Peter Burnett <[email protected]>
|
24 | 24 | * @copyright Catalyst IT
|
25 | 25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 26 | + * @covers \factor_grace |
26 | 27 | */
|
27 | 28 | class factor_test extends \advanced_testcase {
|
28 | 29 |
|
@@ -125,4 +126,59 @@ public function test_gracemode_expires_noredirect() {
|
125 | 126 |
|
126 | 127 | $this->assertFalse($redirected, 'The function cause a redirect, where none was expected.');
|
127 | 128 | }
|
| 129 | + |
| 130 | + /** |
| 131 | + * Tests that gracemode correctly calculates passing weight even when factors can return UNKNOWN |
| 132 | + */ |
| 133 | + public function test_gracemode_unknown_factor_handling(): void { |
| 134 | + $this->resetAfterTest(true); |
| 135 | + $user1 = $this->getDataGenerator()->create_user(); |
| 136 | + $user2 = $this->getDataGenerator()->create_user(); |
| 137 | + $grace = \tool_mfa\plugininfo\factor::get_factor('grace'); |
| 138 | + |
| 139 | + set_config('enabled', 1, 'factor_totp'); |
| 140 | + set_config('enabled', 1, 'factor_exemption'); |
| 141 | + set_config('enabled', 1, 'factor_grace'); |
| 142 | + set_config('enabled', 1, 'factor_loginbanner'); |
| 143 | + set_config('forcesetup', 1, 'factor_grace'); |
| 144 | + set_config('graceperiod', -1, 'factor_grace'); // Grace period expired. |
| 145 | + |
| 146 | + // Setup the order so that the loginbanner is at the top. |
| 147 | + set_config('factor_order', 'loginbanner,exemption,totp,grace'); |
| 148 | + |
| 149 | + // Login banner should give no points. |
| 150 | + set_config('weight', 0, 'factor_loginbanner'); |
| 151 | + |
| 152 | + // Add exemption for user 1, but NOT user 2. |
| 153 | + \factor_exemption\factor::add_exemption($user1); |
| 154 | + |
| 155 | + // Get state for user 1. This should return a neutral and not redirect. |
| 156 | + $this->setUser($user1); |
| 157 | + $this->assertEquals(\tool_mfa\plugininfo\factor::STATE_NEUTRAL, $grace->get_state(true)); |
| 158 | + $redirected = null; |
| 159 | + try { |
| 160 | + $grace->get_state(true); |
| 161 | + $redirected = false; |
| 162 | + } catch (\Throwable $e) { |
| 163 | + $expected = get_string('redirecterrordetected', 'error'); |
| 164 | + if ($expected === $e->getMessage()) { |
| 165 | + $redirected = true; |
| 166 | + } |
| 167 | + } |
| 168 | + $this->assertFalse($redirected); |
| 169 | + |
| 170 | + // Get state for user 2. This should redirect them to the totp setup since they do not pass the exemption. |
| 171 | + $this->setUser($user2); |
| 172 | + $redirected = null; |
| 173 | + try { |
| 174 | + $grace->get_state(true); |
| 175 | + $redirected = false; |
| 176 | + } catch (\Throwable $e) { |
| 177 | + $expected = get_string('redirecterrordetected', 'error'); |
| 178 | + if ($expected === $e->getMessage()) { |
| 179 | + $redirected = true; |
| 180 | + } |
| 181 | + } |
| 182 | + $this->assertTrue($redirected); |
| 183 | + } |
128 | 184 | }
|
0 commit comments