From af449c38ded10146b0782fea6d7077354bab3675 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 8 Sep 2023 01:53:38 +0000 Subject: [PATCH] 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 Signed-off-by: Martin Renvoize --- t/db_dependent/Koha/Auth/TwoFactorAuth.t | 35 +++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Auth/TwoFactorAuth.t b/t/db_dependent/Koha/Auth/TwoFactorAuth.t index 6a82e427e0..eb50a503f7 100755 --- a/t/db_dependent/Koha/Auth/TwoFactorAuth.t +++ b/t/db_dependent/Koha/Auth/TwoFactorAuth.t @@ -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; +}; -- 2.41.0