Bug 30843: Add unit test for Koha::Auth::TwoFactorAuth::verify

This change adds a unit test to test Koha::Auth::TwoFactorAuth::verify

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
David Cook 2023-09-08 01:53:38 +00:00 committed by Tomas Cohen Arazi
parent 6a0955946e
commit 7ac91b24cf
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
1 changed files with 34 additions and 1 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 2;
use Test::More tests => 3;
use Test::Exception;
use Test::MockModule;
@ -89,3 +89,36 @@ subtest 'qr_code' => sub {
$schema->storage->txn_rollback;
};
subtest 'verify' => sub {
plan tests => 4;
$schema->storage->txn_begin;
t::lib::Mocks::mock_preference( 'TwoFactorAuthentication', 'enabled' );
t::lib::Mocks::mock_config( 'encryption_key', 'bad_example' );
t::lib::Mocks::mock_config( 'mfa_range', undef );
my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
$patron->encode_secret('6557s35ui2gyhkmuuvei6rpk44'); # this is base32 btw
$patron->auth_method('two-factor');
$patron->store;
my $auth = Koha::Auth::TwoFactorAuth->new( { patron => $patron } );
my $code = 394342;
my $timestamp = 1694137671;
my $verified;
$verified = $auth->verify( $code, undef, undef, $timestamp, undef );
is( $verified, 1, "code valid within 1 minute" );
$verified = $auth->verify( $code, undef, undef, ( $timestamp + 60 ), undef );
is( $verified, 0, "code invalid within 2 minutes" );
t::lib::Mocks::mock_config( 'mfa_range', 10 );
$verified = $auth->verify( $code, undef, undef, ( $timestamp + 300 ), undef );
is( $verified, 1, "code valid within 5 minutes" );
$verified = $auth->verify( $code, undef, undef, ( $timestamp + 330 ), undef );
is( $verified, 0, "code valid within 5.5 minutes" );
$schema->storage->txn_rollback;
};