| 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 factor_email;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * Tests for email factor.
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @covers      \factor_email\factor
  | 
        
        
            | 
            | 
           23 | 
            * @package     factor_email
  | 
        
        
            | 
            | 
           24 | 
            * @copyright   2023 Stevani Andolo <stevani@hotmail.com.au>
  | 
        
        
            | 
            | 
           25 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           26 | 
            */
  | 
        
        
            | 
            | 
           27 | 
           class factor_test extends \advanced_testcase {
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Tests checking verification code
  | 
        
        
            | 
            | 
           31 | 
                *
  | 
        
        
            | 
            | 
           32 | 
                * @covers ::check_verification_code
  | 
        
        
            | 
            | 
           33 | 
                * @covers ::post_pass_state
  | 
        
        
            | 
            | 
           34 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           35 | 
               public function test_check_verification_code(): void {
  | 
        
        
           | 1 | 
           efrain | 
           36 | 
                   global $DB, $USER;
  | 
        
        
            | 
            | 
           37 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
                   $emailfactorclass = new \factor_email\factor('email');
  | 
        
        
            | 
            | 
           40 | 
                   $rc = new \ReflectionClass($emailfactorclass::class);
  | 
        
        
            | 
            | 
           41 | 
                   $rcm = $rc->getMethod('check_verification_code');
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   // Assigned email to be used in getting the email factor.
  | 
        
        
            | 
            | 
           44 | 
                   $USER->email = 'user@mail.com';
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
                   set_config('enabled', 1, 'factor_email');
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   // Testing with current timecreated.
  | 
        
        
            | 
            | 
           49 | 
                   $newcode = random_int(100000, 999999);
  | 
        
        
            | 
            | 
           50 | 
                   $instanceid = $DB->insert_record('tool_mfa', [
  | 
        
        
            | 
            | 
           51 | 
                       'userid' => $USER->id,
  | 
        
        
            | 
            | 
           52 | 
                       'factor' => 'email',
  | 
        
        
            | 
            | 
           53 | 
                       'secret' => $newcode,
  | 
        
        
            | 
            | 
           54 | 
                       'label' => 'unittest',
  | 
        
        
            | 
            | 
           55 | 
                       'timecreated' => time(),
  | 
        
        
            | 
            | 
           56 | 
                       'timemodified' => time(),
  | 
        
        
            | 
            | 
           57 | 
                       'lastverified' => time(),
  | 
        
        
            | 
            | 
           58 | 
                       'revoked' => 0,
  | 
        
        
            | 
            | 
           59 | 
                   ]);
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   $data = $DB->get_record('tool_mfa', ['id' => $instanceid]);
  | 
        
        
            | 
            | 
           62 | 
                   $this->assertTrue($rcm->invoke($emailfactorclass, $data->secret));
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   // Update the data to test with really old timecreated.
  | 
        
        
            | 
            | 
           65 | 
                   $DB->update_record('tool_mfa', [
  | 
        
        
            | 
            | 
           66 | 
                       'id' => $instanceid,
  | 
        
        
            | 
            | 
           67 | 
                       'timecreated' => time() - 1689657581,
  | 
        
        
            | 
            | 
           68 | 
                       'timemodified' => time() - 1689657581,
  | 
        
        
            | 
            | 
           69 | 
                       'lastverified' => time() - 1689657581,
  | 
        
        
            | 
            | 
           70 | 
                       'revoked' => 0,
  | 
        
        
            | 
            | 
           71 | 
                   ]);
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   $data = $DB->get_record('tool_mfa', ['id' => $instanceid]);
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertFalse($rcm->invoke($emailfactorclass, $data->secret));
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
                   // Cleans up email records once MFA passed.
  | 
        
        
            | 
            | 
           77 | 
                   $rcm = $rc->getMethod('post_pass_state');
  | 
        
        
            | 
            | 
           78 | 
                   $rcm->invoke($emailfactorclass);
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   // Check if the email records have been deleted.
  | 
        
        
            | 
            | 
           81 | 
                   $data = $DB->count_records('tool_mfa', ['factor' => 'email']);
  | 
        
        
            | 
            | 
           82 | 
                   $this->assertEquals(0, $data);
  | 
        
        
            | 
            | 
           83 | 
               }
  | 
        
        
            | 
            | 
           84 | 
           }
  |