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