|
Line 0
Link Here
|
| 0 |
- |
1 |
use Modern::Perl; |
|
|
2 |
use Test::More tests => 1; |
| 3 |
|
| 4 |
use t::lib::Mocks; |
| 5 |
use t::lib::TestBuilder; |
| 6 |
|
| 7 |
use Koha::Database; |
| 8 |
use Koha::Auth::TwoFactorAuth; |
| 9 |
|
| 10 |
our $schema = Koha::Database->new->schema; |
| 11 |
our $builder = t::lib::TestBuilder->new; |
| 12 |
|
| 13 |
subtest 'qr_code' => sub { |
| 14 |
plan tests => 9; |
| 15 |
|
| 16 |
$schema->storage->txn_begin; |
| 17 |
|
| 18 |
t::lib::Mocks::mock_preference('TwoFactorAuthentication', 1); |
| 19 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 20 |
|
| 21 |
# Testing without secret (might change later on) |
| 22 |
is( $patron->secret, undef, 'Secret still undefined' ); |
| 23 |
my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron }); |
| 24 |
is( $auth->secret, undef, 'Still no secret yet as expected' ); |
| 25 |
# Auth::GoogleAuth will generate a secret when calling qr_code |
| 26 |
my $img_data = $auth->qr_code; |
| 27 |
is( length($auth->secret32), 16, 'Secret of 16 base32 chars expected' ); |
| 28 |
is( length($img_data) > 22, 1, 'Dataurl not empty too' ); # prefix is 22 |
| 29 |
$auth->clear; |
| 30 |
|
| 31 |
# Update patron data |
| 32 |
$patron->secret('you2wont2guess2it'); # this is base32 btw |
| 33 |
$patron->auth_method('two-factor'); |
| 34 |
$patron->store; |
| 35 |
|
| 36 |
$auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron }); |
| 37 |
$img_data = $auth->qr_code; |
| 38 |
is( substr($img_data, 0, 22), 'data:image/png;base64,', 'Checking prefix of dataurl' ); |
| 39 |
like( substr($img_data, 22), qr/^[a-zA-Z0-9\/=+]+$/, 'Contains base64 chars' ); |
| 40 |
is( $auth->qr_code, $img_data, 'Repeated call' ); |
| 41 |
$auth->clear; |
| 42 |
|
| 43 |
# Changing the secret should generate different data, right? |
| 44 |
$patron->secret('no3really3not3cracked'); # base32 |
| 45 |
$patron->store; |
| 46 |
$auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron }); |
| 47 |
my $img_data02 = $auth->qr_code; |
| 48 |
is( length($img_data02) > 22, 1, 'More characters than prefix' ); |
| 49 |
isnt( $img_data02, $img_data, 'Another secret, another image' ); |
| 50 |
|
| 51 |
$schema->storage->txn_rollback; |
| 52 |
}; |