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 |
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 base factor implementation methods.
|
|
|
25 |
*
|
|
|
26 |
* @package tool_mfa
|
|
|
27 |
* @author Peter Burnett <peterburnett@catalyst-au.net>
|
|
|
28 |
* @copyright 2023 Catalyst IT
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class object_factor_base_test extends \advanced_testcase {
|
|
|
32 |
|
|
|
33 |
use tool_mfa_trait;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Test deleting user's configured factors
|
|
|
37 |
*
|
|
|
38 |
* @covers ::setup_user_factor
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
11 |
efrain |
41 |
public function test_revoke_user_factor(): void {
|
1 |
efrain |
42 |
$this->resetAfterTest();
|
|
|
43 |
$user = $this->getDataGenerator()->create_user();
|
|
|
44 |
$this->setUser($user);
|
|
|
45 |
|
|
|
46 |
$this->set_factor_state('totp', 1, 100);
|
|
|
47 |
$totpfactor = \tool_mfa\plugininfo\factor::get_factor('totp');
|
|
|
48 |
$totpdata = [
|
|
|
49 |
'secret' => 'fakekey',
|
|
|
50 |
'devicename' => 'fakedevice',
|
|
|
51 |
];
|
|
|
52 |
$factorinstance = $totpfactor->setup_user_factor((object) $totpdata);
|
|
|
53 |
$totpdata2 = [
|
|
|
54 |
'secret' => 'fakekey2',
|
|
|
55 |
'devicename' => 'fakedevice2',
|
|
|
56 |
];
|
|
|
57 |
$totpfactor->setup_user_factor((object) $totpdata2);
|
|
|
58 |
|
|
|
59 |
$this->assertFalse((bool) $factorinstance->revoked);
|
|
|
60 |
$this->assertEquals(2, count($totpfactor->get_active_user_factors($user)));
|
|
|
61 |
|
|
|
62 |
// Test that calling the revoke on the generic type revokes all.
|
|
|
63 |
$totpfactor->revoke_user_factor();
|
|
|
64 |
$this->assertEquals(0, count($totpfactor->get_active_user_factors($user)));
|
|
|
65 |
|
|
|
66 |
// Add another factor for testing.
|
|
|
67 |
$totpdata3 = [
|
|
|
68 |
'secret' => 'fakekey3',
|
|
|
69 |
'devicename' => 'fakedevice3',
|
|
|
70 |
];
|
|
|
71 |
$factorinstance2 = $totpfactor->setup_user_factor((object) $totpdata3);
|
|
|
72 |
|
|
|
73 |
// Now test you can't revoke another users factor.
|
|
|
74 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
75 |
$this->setUser($user2);
|
|
|
76 |
|
|
|
77 |
$this->assertFalse($totpfactor->revoke_user_factor($factorinstance2->id));
|
|
|
78 |
$this->assertEquals(1, count($totpfactor->get_active_user_factors($user)));
|
|
|
79 |
|
|
|
80 |
// Now revoke as ourselves.
|
|
|
81 |
$this->setUser($user);
|
|
|
82 |
$this->assertTrue($totpfactor->revoke_user_factor($factorinstance2->id));
|
|
|
83 |
$this->assertEquals(0, count($totpfactor->get_active_user_factors($user)));
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Tests the replacement of a factor.
|
|
|
88 |
*
|
|
|
89 |
* @covers ::setup_user_factor
|
|
|
90 |
* @covers ::replace_user_factor
|
|
|
91 |
*/
|
|
|
92 |
public function test_replace_user_factor(): void {
|
|
|
93 |
$this->resetAfterTest();
|
|
|
94 |
$user = $this->getDataGenerator()->create_user();
|
|
|
95 |
$this->setUser($user);
|
|
|
96 |
|
|
|
97 |
$factor = \tool_mfa\plugininfo\factor::get_factor('totp');
|
|
|
98 |
|
|
|
99 |
// Set up the factor.
|
|
|
100 |
$data1 = new \stdClass();
|
|
|
101 |
$data1->secret = 'fakesecret1';
|
|
|
102 |
$data1->devicename = 'fakedevice1';
|
|
|
103 |
$factor1 = $factor->setup_user_factor($data1);
|
|
|
104 |
|
|
|
105 |
// Prepare some replacement data.
|
|
|
106 |
$data2 = new \stdClass();
|
|
|
107 |
$data2->secret = 'fakesecret2';
|
|
|
108 |
$data2->devicename = 'fakedevice2';
|
|
|
109 |
|
|
|
110 |
// Replace the active factor with the replacement data.
|
|
|
111 |
$factor2 = $factor->replace_user_factor($data2, $factor1->id);
|
|
|
112 |
|
|
|
113 |
// Check the active factor is the newer one.
|
|
|
114 |
$activefactors = $factor->get_active_user_factors($user);
|
|
|
115 |
$this->assertEquals(1, count($activefactors));
|
|
|
116 |
$this->assertEquals($factor2->id, $activefactors[0]->id);
|
|
|
117 |
}
|
|
|
118 |
}
|