| 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 plugininfo.
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @package     tool_mfa
  | 
        
        
            | 
            | 
           23 | 
            * @author      Peter Burnett <peterburnett@catalyst-au.net>
  | 
        
        
            | 
            | 
           24 | 
            * @copyright   Catalyst IT
  | 
        
        
            | 
            | 
           25 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           27 | 
           final class plugininfo_factor_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Tests getting next user factor
  | 
        
        
            | 
            | 
           31 | 
                *
  | 
        
        
            | 
            | 
           32 | 
                * @covers ::get_next_user_login_factor
  | 
        
        
            | 
            | 
           33 | 
                * @covers ::setup_user_factor
  | 
        
        
            | 
            | 
           34 | 
                * @covers ::get_enabled_factors
  | 
        
        
            | 
            | 
           35 | 
                * @covers ::is_enabled
  | 
        
        
            | 
            | 
           36 | 
                * @covers ::has_setup
  | 
        
        
            | 
            | 
           37 | 
                * @covers ::get_active_user_factor_types
  | 
        
        
            | 
            | 
           38 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           39 | 
               public function test_get_next_user_login_factor(): void {
  | 
        
        
           | 1 | 
           efrain | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   // Create and login a user.
  | 
        
        
            | 
            | 
           44 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           45 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           47 | 
                   // Disable the email factor (enabled by default).
  | 
        
        
            | 
            | 
           48 | 
                   set_config('enabled', 0, 'factor_email');
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
           | 1 | 
           efrain | 
           50 | 
                   // Test that with no enabled factors, fallback is returned.
  | 
        
        
            | 
            | 
           51 | 
                   $this->assertEquals('fallback', \tool_mfa\plugininfo\factor::get_next_user_login_factor()->name);
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
                   // Setup enabled totp factor for user.
  | 
        
        
            | 
            | 
           54 | 
                   set_config('enabled', 1, 'factor_totp');
  | 
        
        
            | 
            | 
           55 | 
                   $totpfactor = \tool_mfa\plugininfo\factor::get_factor('totp');
  | 
        
        
            | 
            | 
           56 | 
                   $totpdata = [
  | 
        
        
            | 
            | 
           57 | 
                       'secret' => 'fakekey',
  | 
        
        
            | 
            | 
           58 | 
                       'devicename' => 'fakedevice',
  | 
        
        
            | 
            | 
           59 | 
                   ];
  | 
        
        
            | 
            | 
           60 | 
                   $this->assertNotEmpty($totpfactor->setup_user_factor((object) $totpdata));
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
                   // Test that factor now appears (from STATE_UNKNOWN).
  | 
        
        
            | 
            | 
           63 | 
                   $this->assertEquals('totp', \tool_mfa\plugininfo\factor::get_next_user_login_factor()->name);
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   // Now pass this factor, check for fallback.
  | 
        
        
            | 
            | 
           66 | 
                   $totpfactor->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
  | 
        
        
            | 
            | 
           67 | 
                   $this->assertEquals('fallback', \tool_mfa\plugininfo\factor::get_next_user_login_factor()->name);
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   // Add in a no-input factor.
  | 
        
        
            | 
            | 
           70 | 
                   set_config('enabled', 1, 'factor_auth');
  | 
        
        
            | 
            | 
           71 | 
                   $this->assertEquals(2, count(\tool_mfa\plugininfo\factor::get_enabled_factors()));
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   $authfactor = \tool_mfa\plugininfo\factor::get_factor('auth');
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertTrue($authfactor->is_enabled());
  | 
        
        
            | 
            | 
           75 | 
                   $this->assertFalse($authfactor->has_setup());
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   // Check that the next factor is still the fallback factor.
  | 
        
        
            | 
            | 
           78 | 
                   $this->assertEquals(2, count(\tool_mfa\plugininfo\factor::get_active_user_factor_types()));
  | 
        
        
            | 
            | 
           79 | 
                   $this->assertEquals('fallback', \tool_mfa\plugininfo\factor::get_next_user_login_factor()->name);
  | 
        
        
            | 
            | 
           80 | 
               }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
               /**
  | 
        
        
            | 
            | 
           83 | 
                * Tests if a user has more than one active factor.
  | 
        
        
            | 
            | 
           84 | 
                *
  | 
        
        
            | 
            | 
           85 | 
                * @covers ::user_has_more_than_one_active_factors
  | 
        
        
            | 
            | 
           86 | 
                */
  | 
        
        
            | 
            | 
           87 | 
               public function test_user_has_more_than_one_active_factors(): void {
  | 
        
        
            | 
            | 
           88 | 
                   global $DB;
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
                   // Create a user.
  | 
        
        
            | 
            | 
           93 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           94 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           96 | 
                   // Add another factor (email factor is enabled by default).
  | 
        
        
           | 1 | 
           efrain | 
           97 | 
                   set_config('enabled', 1, 'factor_totp');
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $data = new \stdClass();
  | 
        
        
            | 
            | 
           100 | 
                   $data->userid = $user->id;
  | 
        
        
            | 
            | 
           101 | 
                   $data->factor = 'totp';
  | 
        
        
            | 
            | 
           102 | 
                   $data->label = 'testtotp';
  | 
        
        
            | 
            | 
           103 | 
                   $data->revoked = 0;
  | 
        
        
            | 
            | 
           104 | 
                   $factorid = $DB->insert_record('tool_mfa', $data);
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   // Test there is more than one active factor.
  | 
        
        
            | 
            | 
           107 | 
                   $hasmorethanonefactor = \tool_mfa\plugininfo\factor::user_has_more_than_one_active_factors();
  | 
        
        
            | 
            | 
           108 | 
                   $this->assertTrue($hasmorethanonefactor);
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
                   // Revoke a factor.
  | 
        
        
            | 
            | 
           111 | 
                   $DB->set_field('tool_mfa', 'revoked', 1, ['id' => $factorid]);
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                   // There should no longer be more than one active factor.
  | 
        
        
            | 
            | 
           114 | 
                   $hasmorethanonefactor = \tool_mfa\plugininfo\factor::user_has_more_than_one_active_factors();
  | 
        
        
            | 
            | 
           115 | 
                   $this->assertFalse($hasmorethanonefactor);
  | 
        
        
            | 
            | 
           116 | 
               }
  | 
        
        
            | 
            | 
           117 | 
           }
  |